Commits

davedes  committed a0ad745

adding release() and isPaused() to Sound/Music

  • Participants
  • Parent commits ea359cc
  • Branches development

Comments (0)

Files changed (10)

File trunk/Slick/src/org/newdawn/slick/Music.java

 		return (currentMusic == this) && (playing);
 	}
 	
+	public boolean paused() {
+		return sound.isPaused();
+	}
+	
+	
 	/**
 	 * Set the volume of the music as a factor of the global volume setting
 	 * 
 	public float getPosition () {
 		return sound.getPosition();
 	}
+	
+
+	/**
+	 * Stops the sound, removes any queued buffers,
+	 * and closes the stream. 
+	 */
+	public void release() {
+		sound.release();
+	}
 }

File trunk/Slick/src/org/newdawn/slick/Sound.java

 	public void stop() {
 		sound.stop();
 	}
+
+	/**
+	 * Releases this buffer from memory. The music should be 
+	 * stopped before calling this. Using sounds after
+	 * they have been released has undefined effects.
+	 */
+	public void release() {
+		sound.release();
+	}
 }

File trunk/Slick/src/org/newdawn/slick/openal/Audio.java

 	 * @return True if the sound is playing
 	 */
 	public boolean isPlaying();
+	
+
+	/**
+	 * Check if the sound is paused.
+	 * 
+	 * @return true if sound is paused
+	 */
+	public boolean isPaused();
 
 	/**
 	 * Play this sound as a sound effect
 	 * @return The current position in seconds.
 	 */
 	public float getPosition();
+	
+
+	/**
+	 * Calls stop() and releases this buffer from memory. For music, this will
+	 * stop the source, remove any queued buffers, and close the stream.
+	 * For sound, this will stop the source and release the buffer contained by
+	 * the Sound. 
+	 */
+	public void release();
 }

File trunk/Slick/src/org/newdawn/slick/openal/AudioImpl.java

  */
 public class AudioImpl implements Audio {
 	/** The store from which this sound was loaded */
-	private SoundStore store;
+	protected SoundStore store;
 	/** The buffer containing the sound */
-	private int buffer;
+	protected int buffer;
 	/** The index of the source being used to play this sound */
-	private int index = -1;
+	protected int index = -1;
 	
 	/** The length of the audio */
-	private float length;
+	protected float length;
 	
 	/**
 	 * Create a new sound
 		int samples = bytes / (bits / 8);
 		length = (samples / (float) freq) / channels;
 	}
+
+	/**
+	 * Calls stop() and releases this buffer from memory. For music, this will
+	 * stop the source, remove any queued buffers, and close the stream.
+	 * For sound, this will stop the source and release the buffer contained by
+	 * the Sound.
+	 */
+	public void release() {
+		stop();
+		if (index!=-1) {
+			//detach buffer from source
+			AL10.alSourcei(SoundStore.get().getSource(index), AL10.AL_BUFFER, 0);
+		}
+		//delete buffer
+		if (buffer!=0)
+			AL10.alDeleteBuffers(buffer);
+		index = -1;
+		buffer = 0;
+	}
 	
 	/**
 	 * Get the ID of the OpenAL buffer holding this data (if any). This method
 	 * is not valid with streaming resources.
 	 * 
+	 * If the source has been released, this will return zero.
+	 * 
 	 * @return The ID of the OpenAL buffer holding this data 
 	 */
 	public int getBufferID() {
 	}
 	
 	/**
+	 * Returns the index of the source found in the SoundStore;
+	 * the source ID can then be retrieved with SoundStore.getSource().
+	 * This may be -1 if the sound is not attached to a source.
+	 * @return the last attached source
+	 */
+	protected int getSourceIndex() {
+		return index;
+	}
+	
+	/**
 	 *
 	 */
 	protected AudioImpl() {
 	/**
 	 * @see org.newdawn.slick.openal.Audio#stop()
 	 */
-	public void stop() {
+	public void stop() {			
 		if (index != -1) {
 			store.stopSource(index);
 			index = -1;
 	 */
 	public boolean isPlaying() {
 		if (index != -1) {
-			return store.isPlaying(index);
+			return SoundStore.get().isPlaying(index);
 		}
 		
 		return false;
 	}
 	
 	/**
+	 * Returns true if this audio has a source attached and that source
+	 * is currently paused.
+	 * @return true if paused
+	 */
+	public boolean isPaused() {
+		if (index != -1)
+			return SoundStore.get().isPaused(index);
+		return false;
+	}
+	
+	/**
 	 * @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean)
 	 */
 	public int playAsSoundEffect(float pitch, float gain, boolean loop) {
+		if (buffer==0)
+			return 0;
 		index = store.playAsSound(buffer, pitch, gain, loop);
 		return store.getSource(index);
 	}
 	 * @see org.newdawn.slick.openal.Audio#playAsSoundEffect(float, float, boolean, float, float, float)
 	 */
 	public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {
+		if (buffer==0)
+			return 0;
 		index = store.playAsSoundAt(buffer, pitch, gain, loop, x, y, z);
 		return store.getSource(index);
 	}
 	 * @see org.newdawn.slick.openal.Audio#playAsMusic(float, float, boolean)
 	 */
 	public int playAsMusic(float pitch, float gain, boolean loop) {
+		if (buffer==0)
+			return 0;
 		store.playAsMusic(buffer, pitch, gain, loop);
 		index = 0;
 		return store.getSource(0);

File trunk/Slick/src/org/newdawn/slick/openal/MODSound.java

 	 */
 	public MODSound(SoundStore store, InputStream in) throws IOException {
 		this.store = store;
+		index = 0;
 		module = OpenALMODPlayer.loadModule(in);
 	}
 	
 	/**
+	 * Stops the music, removes any queued buffers, and closes the stream.
+	 */
+	public void release() {
+		cleanUpSource();
+		super.release();
+		index = 0;
+	}
+	
+	/**
 	 * @see org.newdawn.slick.openal.AudioImpl#playAsMusic(float, float, boolean)
 	 */
 	public int playAsMusic(float pitch, float gain, boolean loop) {

File trunk/Slick/src/org/newdawn/slick/openal/NullAudio.java

 	 */
 	public void stop() {
 	}
+	
+	public void release() {
+		
+	}
 
 }

File trunk/Slick/src/org/newdawn/slick/openal/OpenALStreamPlayer.java

 		return (url == null) ? ref : url.toString();
 	}
 	
+	
+	public void close() {
+		removeBuffers();
+		if (audio!=null) {
+			try {audio.close();}
+			catch (IOException e) {}
+		}
+		done = true;
+	}
+	
 	/**
 	 * Clean up the buffers applied to the sound source
 	 */

File trunk/Slick/src/org/newdawn/slick/openal/SoundStore.java

 	}
 	
 	/**
+	 * Check if a particle source is paused 
+	 * @param index the index of the source
+	 * @return true if the source is playing
+	 */
+	boolean isPaused(int index) {
+		int state = AL10.alGetSourcei(sources.get(index), AL10.AL_SOURCE_STATE);
+		return (state == AL10.AL_PAUSED);
+	}
+	
+	/**
 	 * Find a free sound source
 	 * 
 	 * @return The index of the free sound source

File trunk/Slick/src/org/newdawn/slick/openal/StreamSound.java

 	 */
 	public StreamSound(OpenALStreamPlayer player) {
 		this.player = player;
+		this.index = 0;
+	}
+	
+	/**
+	 * Stops the music, removes any queued buffers, and closes the stream. 
+	 */
+	public void release() {
+		cleanUpSource();
+		super.release();
+		index = 0;
+		player.close();
 	}
 	
 	/**

File trunk/Slick/src/org/newdawn/slick/tests/SoundTest.java

 		g.drawString("The OGG loop is now streaming from the file, woot.",100,60);
 		g.drawString("Press space for sound effect (OGG)",100,100);
 		g.drawString("Press P to pause/resume music (XM)",100,130);
-		g.drawString("Press E to pause/resume engine sound (WAV)",100,190);
+		g.drawString("Press 1-3 to play engine sound (WAV)",100,190);
 		g.drawString("Press enter for charlie (WAV)",100,160);
 		g.drawString("Press C to change music",100,210);
 		g.drawString("Press B to burp (AIF)",100,240);
 			if (music.playing()) {
 				music.pause();
 			} else {
-				music.resume();
+				if (music.paused()) {
+					music.resume();
+				} else {
+					music.loop();
+				}
 			}
 		}
 		if (key == Input.KEY_C) {