Path: blob/master/SLICK_HOME/src/org/newdawn/slick/openal/StreamSound.java
1461 views
package org.newdawn.slick.openal;12import java.io.IOException;3import java.nio.IntBuffer;45import org.lwjgl.BufferUtils;6import org.lwjgl.openal.AL10;7import org.newdawn.slick.util.Log;89/**10* A sound implementation wrapped round a player which reads (and potentially) rereads11* a stream. This supplies streaming audio12*13* @author kevin14* @author Nathan Sweet <[email protected]>15* @author Rockstar playAsMusic cleanup16*/17public class StreamSound extends AudioImpl {18/** The player we're going to ask to stream data */19private OpenALStreamPlayer player;2021/**22* Create a new sound wrapped round a stream23*24* @param player The stream player we'll use to access the stream25*/26public StreamSound(OpenALStreamPlayer player) {27this.player = player;28}2930/**31* @see org.newdawn.slick.openal.AudioImpl#isPlaying()32*/33public boolean isPlaying() {34return SoundStore.get().isPlaying(player);35}3637/**38* @see org.newdawn.slick.openal.AudioImpl#playAsMusic(float, float, boolean)39*/40public int playAsMusic(float pitch, float gain, boolean loop) {41try {42cleanUpSource();4344player.setup(pitch);45player.play(loop);46SoundStore.get().setStream(player);47} catch (IOException e) {48Log.error("Failed to read OGG source: "+player.getSource());49}5051return SoundStore.get().getSource(0);52}5354/**55* Clean up the buffers applied to the sound source56*/57private void cleanUpSource() {58SoundStore store = SoundStore.get();5960AL10.alSourceStop(store.getSource(0));61IntBuffer buffer = BufferUtils.createIntBuffer(1);62int queued = AL10.alGetSourcei(store.getSource(0), AL10.AL_BUFFERS_QUEUED);6364while (queued > 0)65{66AL10.alSourceUnqueueBuffers(store.getSource(0), buffer);67queued--;68}6970AL10.alSourcei(store.getSource(0), AL10.AL_BUFFER, 0);71}7273/**74* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean, float, float, float)75*/76public int playAsSoundEffect(float pitch, float gain, boolean loop, float x, float y, float z) {77return playAsMusic(pitch, gain, loop);78}7980/**81* @see org.newdawn.slick.openal.AudioImpl#playAsSoundEffect(float, float, boolean)82*/83public int playAsSoundEffect(float pitch, float gain, boolean loop) {84return playAsMusic(pitch, gain, loop);85}8687/**88* @see org.newdawn.slick.openal.AudioImpl#stop()89*/90public void stop() {91SoundStore.get().setStream(null);92}9394/**95* @see org.newdawn.slick.openal.AudioImpl#setPosition(float)96*/97public boolean setPosition(float position) {98return player.setPosition(position);99}100101/**102* @see org.newdawn.slick.openal.AudioImpl#getPosition()103*/104public float getPosition() {105return player.getPosition();106}107}108109110