Permalink
Browse files

Base stream code and redesign with Music, Sound, MusicReference, and …

…SoundReference broken out as interfaces.
  • Loading branch information...
finnkuusisto committed Aug 6, 2012
1 parent d172f26 commit 56d57d7e9078a554629cc3364ad8ae812d85f411
@@ -25,187 +25,112 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
package kuusisto.tinysound;
-import kuusisto.tinysound.internal.Mixer;
-import kuusisto.tinysound.internal.MusicReference;
/**
- * The Music class is an abstraction for music. Music objects should only be
- * loaded via the TinySound <code>loadMusic()</code> functions. Music can be
+ * The Music interface is an abstraction for music. Music objects should only
+ * be loaded via the TinySound <code>loadMusic()</code> functions. Music can be
* played, paused, resumed, stopped and looped from specified positions.
*
* @author Finn Kuusisto
*/
-public class Music {
-
- private byte[] left;
- private byte[] right;
- private Mixer mixer;
- private MusicReference reference;
-
- /**
- * Construct a new Music with the given music data and the Mixer with which
- * to register this Music.
- * @param left left channel of music data
- * @param right right channel of music data
- * @param mixer Mixer with which this Music is registered
- */
- public Music(byte[] left, byte[] right, Mixer mixer) {
- this.left = left;
- this.right = right;
- this.mixer = mixer;
- this.reference = new MusicReference(this.left, this.right, false, false,
- 0, 0, 1.0);
- this.mixer.registerMusicReference(this.reference);
- }
-
+public interface Music {
+
/**
* Play this Music and loop if specified.
* @param loop if this Music should loop
*/
- public void play(boolean loop) {
- this.reference.setPlaying(true);
- this.reference.setLoop(loop);
- }
+ public void play(boolean loop);
/**
* Play this Music at the specified volume and loop if specified.
* @param loop if this Music should loop
* @param volume the volume to play the this Music
*/
- public void play(boolean loop, double volume) {
- this.reference.setPlaying(true);
- this.setLoop(loop);
- this.setVolume(volume);
- }
+ public void play(boolean loop, double volume);
/**
* Stop playing this Music and set its position to the beginning.
*/
- public void stop() {
- this.reference.setPlaying(false);
- this.rewind();
- }
+ public void stop();
/**
* Stop playing this Music and keep its current position.
*/
- public void pause() {
- this.reference.setPlaying(false);
- }
+ public void pause();
/**
* Play this Music from its current position.
*/
- public void resume() {
- this.reference.setPlaying(true);
- }
+ public void resume();
/**
* Set this Music's position to the beginning.
*/
- public void rewind() {
- this.reference.setPosition(0);
- }
+ public void rewind();
/**
* Set this Music's position to the loop position.
*/
- public void rewindToLoopPosition() {
- int byteIndex = this.reference.getLoopPosition();
- this.reference.setPosition(byteIndex);
- }
+ public void rewindToLoopPosition();
/**
* Determine if this Music is playing.
* @return true if this Music is playing
*/
- public boolean playing() {
- return this.reference.getPlaying();
- }
+ public boolean playing();
/**
* Determine if this Music will loop.
* @return true if this Music will loop
*/
- public boolean loop() {
- return this.reference.getLoop();
- }
+ public boolean loop();
/**
* Set whether this Music will loop.
* @param loop whether this Music will loop
*/
- public void setLoop(boolean loop) {
- this.reference.setLoop(loop);
- }
+ public void setLoop(boolean loop);
/**
* Get the loop position of this Music by sample frame.
* @return loop position by sample frame
*/
- public int getLoopPositionByFrame() {
- int byteIndex = this.reference.getLoopPosition();
- return (byteIndex / TinySound.FORMAT.getFrameSize());
- }
+ public int getLoopPositionByFrame();
/**
* Get the loop position of this Music by seconds.
* @return loop position by seconds
*/
- public double getLoopPositionBySeconds() {
- int byteIndex = this.reference.getLoopPosition();
- return (int)(byteIndex / (TinySound.FORMAT.getFrameRate() *
- TinySound.FORMAT.getFrameSize()));
- }
+ public double getLoopPositionBySeconds();
/**
* Set the loop position of this Music by sample frame.
* @param frameIndex sample frame loop position to set
*/
- public void setLoopPositionByFrame(int frameIndex) {
- int byteIndex = frameIndex * TinySound.FORMAT.getFrameSize();
- this.reference.setLoopPosition(byteIndex);
- }
+ public void setLoopPositionByFrame(int frameIndex);
/**
* Set the loop position of this Music by seconds.
* @param seconds loop position to set by seconds
*/
- public void setLoopPositionBySeconds(double seconds) {
- int byteIndex = (int)(seconds * TinySound.FORMAT.getFrameRate() *
- TinySound.FORMAT.getFrameSize());
- this.reference.setLoopPosition(byteIndex);
- }
+ public void setLoopPositionBySeconds(double seconds);
/**
* Get the volume of this Music.
* @return volume of this Music
*/
- public double getVolume() {
- return this.reference.getVolume();
- }
+ public double getVolume();
/**
* Set the volume of this Music.
* @param volume the desired volume of this Music
*/
- public void setVolume(double volume) {
- if (volume >= 0.0) {
- this.reference.setVolume(volume);
- }
- }
+ public void setVolume(double volume);
/**
* Unload this Music from the system. Attempts to use this Music after
* unloading will result in error.
*/
- public void unload() {
- //unregister the reference
- this.mixer.unRegisterMusicReference(this.reference);
- this.mixer = null;
- this.left = null;
- this.right = null;
- this.reference = null;
- }
-
+ public void unload();
+
}
@@ -25,77 +25,38 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
package kuusisto.tinysound;
-import kuusisto.tinysound.internal.Mixer;
-import kuusisto.tinysound.internal.SoundReference;
/**
- * The Sound class is an abstraction for sound effects. Sound objects should
- * only be loaded via the TinySound <code>loadSound()</code> functions. Sound
- * can be played repeatedly in an overlapping fashion.
+ * The Sound interface is an abstraction for sound effects. Sound objects
+ * should only be loaded via the TinySound <code>loadSound()</code> functions.
+ * Sounds can be played repeatedly in an overlapping fashion.
*
* @author Finn Kuusisto
*/
-public class Sound {
-
- private static int soundCount = 0;
-
- private byte[] left;
- private byte[] right;
- private Mixer mixer;
- private final int ID; //unique ID to match references
-
- /**
- * Construct a new Sound with the given data and Mixer which will handle
- * handle this Sound.
- * @param left left channel of sound data
- * @param right right channel of sound data
- * @param mixer Mixer that will handle this Sound
- */
- public Sound(byte[] left, byte[] right, Mixer mixer) {
- this.left = left;
- this.right = right;
- this.mixer = mixer;
- //get the next ID
- this.ID = Sound.soundCount;
- Sound.soundCount++;
- }
-
+public interface Sound {
+
/**
* Plays this Sound.
*/
- public void play() {
- this.play(1.0);
- }
+ public void play();
/**
* Plays this Sound with a specified volume.
* @param volume the volume at which to play this Sound
*/
- public void play(double volume) {
- //dispatch a SoundReference to the mixer
- SoundReference ref = new SoundReference(this.left, this.right, volume,
- this.ID);
- this.mixer.registerSoundReference(ref);
- }
+ public void play(double volume);
/**
* Stops this Sound from playing. Note that if this Sound was played
* repeatedly in an overlapping fashion, all instances of this Sound still
* playing will be stopped.
*/
- public void stop() {
- this.mixer.unRegisterSoundReference(this.ID);
- }
+ public void stop();
/**
* Unloads this Sound from the system. Attempts to use this Sound after
* unloading will result in error.
*/
- public void unload() {
- this.mixer.unRegisterSoundReference(this.ID);
- this.mixer = null;
- this.left = null;
- this.right = null;
- }
-
+ public void unload();
+
}
@@ -41,6 +41,8 @@
import javax.sound.sampled.UnsupportedAudioFileException;
import kuusisto.tinysound.internal.ByteList;
+import kuusisto.tinysound.internal.MemMusic;
+import kuusisto.tinysound.internal.MemSound;
import kuusisto.tinysound.internal.Mixer;
import kuusisto.tinysound.internal.UpdateRunner;
@@ -75,6 +77,8 @@
private static boolean inited = false;
//auto-updater for the system
private static UpdateRunner autoUpdater;
+ //counter for unique sound IDs
+ private static int soundCount = 0;
/**
* Initialize Tinysound. This must be called before loading audio.
@@ -257,7 +261,7 @@ public static Music loadMusic(URL url) {
return null;
}
//construct the Music object and register it with the mixer
- return new Music(data[0], data[1], TinySound.mixer);
+ return new MemMusic(data[0], data[1], TinySound.mixer);
}
/**
@@ -342,7 +346,9 @@ public static Sound loadSound(URL url) {
return null;
}
//construct the Sound object
- return new Sound(data[0], data[1], TinySound.mixer);
+ TinySound.soundCount++;
+ return new MemSound(data[0], data[1], TinySound.mixer,
+ TinySound.soundCount);
}
/**
Oops, something went wrong.

0 comments on commit 56d57d7

Please sign in to comment.