* @author Nathan Sweet\r
*/\r
public class OpenALAudio implements Audio {\r
- private IntArray idleStreams, allStreams;\r
+ private IntArray idleSources, allSources;\r
private ObjectMap<String, Class<? extends OpenALSound>> extensionToSoundClass = new ObjectMap();\r
private ObjectMap<String, Class<? extends OpenALMusic>> extensionToMusicClass = new ObjectMap();\r
\r
this(16);\r
}\r
\r
- public OpenALAudio (int simultaneousStreams) {\r
+ public OpenALAudio (int simultaneousSources) {\r
registerSound("ogg", Ogg.Sound.class);\r
registerMusic("ogg", Ogg.Music.class);\r
registerSound("wav", Wav.Sound.class);\r
throw new GdxRuntimeException("Error initializing OpenAL.", ex);\r
}\r
\r
- allStreams = new IntArray(false, simultaneousStreams);\r
- for (int i = 0; i < simultaneousStreams; i++) {\r
- int streamID = alGenSources();\r
+ allSources = new IntArray(false, simultaneousSources);\r
+ for (int i = 0; i < simultaneousSources; i++) {\r
+ int sourceID = alGenSources();\r
if (alGetError() != AL_NO_ERROR) break;\r
- allStreams.add(streamID);\r
+ allSources.add(sourceID);\r
}\r
- idleStreams = new IntArray(allStreams);\r
+ idleSources = new IntArray(allSources);\r
\r
FloatBuffer orientation = (FloatBuffer)BufferUtils.createFloatBuffer(6)\r
.put(new float[] {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip();\r
}\r
}\r
\r
- int obtainStream (boolean isMusic) {\r
- for (int i = 0, n = idleStreams.size; i < n; i++) {\r
- int streamID = idleStreams.get(i);\r
- int state = alGetSourcei(streamID, AL_SOURCE_STATE);\r
+ int obtainSource (boolean isMusic) {\r
+ for (int i = 0, n = idleSources.size; i < n; i++) {\r
+ int sourceID = idleSources.get(i);\r
+ int state = alGetSourcei(sourceID, AL_SOURCE_STATE);\r
if (state != AL_PLAYING && state != AL_PAUSED) {\r
- if (isMusic) idleStreams.removeIndex(i);\r
- alSourceStop(streamID);\r
- alSourcei(streamID, AL_BUFFER, 0);\r
- return streamID;\r
+ if (isMusic) idleSources.removeIndex(i);\r
+ alSourceStop(sourceID);\r
+ alSourcei(sourceID, AL_BUFFER, 0);\r
+ return sourceID;\r
}\r
}\r
return -1;\r
}\r
\r
- void freeStream (int streamID) {\r
- alSourceStop(streamID);\r
- alSourcei(streamID, AL_BUFFER, 0);\r
- idleStreams.add(streamID);\r
+ void freeSource (int sourceID) {\r
+ alSourceStop(sourceID);\r
+ alSourcei(sourceID, AL_BUFFER, 0);\r
+ idleSources.add(sourceID);\r
}\r
\r
void freeBuffer (int bufferID) {\r
- for (int i = 0, n = idleStreams.size; i < n; i++) {\r
- int streamID = idleStreams.get(i);\r
- if (alGetSourcei(streamID, AL_BUFFER) == bufferID) {\r
- alSourceStop(streamID);\r
- alSourcei(streamID, AL_BUFFER, 0);\r
+ for (int i = 0, n = idleSources.size; i < n; i++) {\r
+ int sourceID = idleSources.get(i);\r
+ if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) {\r
+ alSourceStop(sourceID);\r
+ alSourcei(sourceID, AL_BUFFER, 0);\r
}\r
}\r
}\r
\r
- void stopStreamsWithBuffer (int bufferID) {\r
- for (int i = 0, n = idleStreams.size; i < n; i++) {\r
- int streamID = idleStreams.get(i);\r
- if (alGetSourcei(streamID, AL_BUFFER) == bufferID) alSourceStop(streamID);\r
+ void stopSourcesWithBuffer (int bufferID) {\r
+ for (int i = 0, n = idleSources.size; i < n; i++) {\r
+ int sourceID = idleSources.get(i);\r
+ if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) alSourceStop(sourceID);\r
}\r
}\r
\r
}\r
\r
public void dispose () {\r
- for (int i = 0, n = allStreams.size; i < n; i++) {\r
- int streamID = allStreams.get(i);\r
- int state = alGetSourcei(streamID, AL_SOURCE_STATE);\r
- if (state != AL_STOPPED) alSourceStop(streamID);\r
- alDeleteSources(streamID);\r
+ for (int i = 0, n = allSources.size; i < n; i++) {\r
+ int sourceID = allSources.get(i);\r
+ int state = alGetSourcei(sourceID, AL_SOURCE_STATE);\r
+ if (state != AL_STOPPED) alSourceStop(sourceID);\r
+ alDeleteSources(sourceID);\r
}\r
\r
AL.destroy();\r
\r
private final OpenALAudio audio;\r
private IntBuffer buffers;\r
- private int streamID = -1;\r
+ private int sourceID = -1;\r
private int format, sampleRate;\r
private boolean isLooping, isPlaying;\r
private float volume = 1;\r
}\r
\r
public void play () {\r
- if (streamID == -1) {\r
- streamID = audio.obtainStream(true);\r
- if (streamID == -1) return;\r
- alSourcei(streamID, AL_LOOPING, AL_FALSE);\r
- alSourcef(streamID, AL_GAIN, volume);\r
+ if (sourceID == -1) {\r
+ sourceID = audio.obtainSource(true);\r
+ if (sourceID == -1) return;\r
+ alSourcei(sourceID, AL_LOOPING, AL_FALSE);\r
+ alSourcef(sourceID, AL_GAIN, volume);\r
for (int i = 0; i < bufferCount; i++)\r
fill(buffers.get(i));\r
- alSourceQueueBuffers(streamID, buffers);\r
+ alSourceQueueBuffers(sourceID, buffers);\r
if (alGetError() != AL_NO_ERROR) {\r
stop();\r
return;\r
}\r
}\r
- alSourcePlay(streamID);\r
+ alSourcePlay(sourceID);\r
isPlaying = true;\r
}\r
\r
public void stop () {\r
- if (streamID == -1) return;\r
+ if (sourceID == -1) return;\r
reset();\r
- alSourceStop(streamID);\r
- alSourcei(streamID, AL_BUFFER, 0);\r
- audio.freeStream(streamID);\r
- streamID = -1;\r
+ audio.freeSource(sourceID);\r
+ sourceID = -1;\r
renderedSeconds = 0;\r
isPlaying = false;\r
}\r
\r
public void pause () {\r
- if (streamID != -1) alSourcePause(streamID);\r
+ if (sourceID != -1) alSourcePause(sourceID);\r
isPlaying = false;\r
}\r
\r
public boolean isPlaying () {\r
- if (streamID == -1) return false;\r
+ if (sourceID == -1) return false;\r
return isPlaying;\r
}\r
\r
\r
public void setVolume (float volume) {\r
this.volume = volume;\r
- if (streamID != -1) alSourcef(streamID, AL_GAIN, volume);\r
+ if (sourceID != -1) alSourcef(sourceID, AL_GAIN, volume);\r
}\r
\r
public float getPosition () {\r
- if (streamID == -1) return 0;\r
- return renderedSeconds + alGetSourcef(streamID, AL11.AL_SEC_OFFSET);\r
+ if (sourceID == -1) return 0;\r
+ return renderedSeconds + alGetSourcef(sourceID, AL11.AL_SEC_OFFSET);\r
}\r
\r
/**\r
abstract protected void reset ();\r
\r
public void update () {\r
- if (streamID == -1) return;\r
+ if (sourceID == -1) return;\r
+\r
// A buffer underflow will cause the source to stop.\r
- if (isPlaying && alGetSourcei(streamID, AL_SOURCE_STATE) != AL_PLAYING) alSourcePlay(streamID);\r
- int buffers = alGetSourcei(streamID, AL_BUFFERS_PROCESSED);\r
+ if (isPlaying && alGetSourcei(sourceID, AL_SOURCE_STATE) != AL_PLAYING) alSourcePlay(sourceID);\r
+\r
+ int buffers = alGetSourcei(sourceID, AL_BUFFERS_PROCESSED);\r
while (buffers-- > 0) {\r
- int bufferID = alSourceUnqueueBuffers(streamID);\r
+ int bufferID = alSourceUnqueueBuffers(sourceID);\r
if (bufferID == AL_INVALID_VALUE) break;\r
renderedSeconds += secondsPerBuffer;\r
if (!fill(bufferID)) return;\r
- alSourceQueueBuffers(streamID, bufferID);\r
+ alSourceQueueBuffers(sourceID, bufferID);\r
}\r
}\r
\r
\r
public void dispose () {\r
if (buffers == null) return;\r
- if (streamID != -1) {\r
+ if (sourceID != -1) {\r
reset();\r
audio.music.removeValue(this, true);\r
- alSourceStop(streamID);\r
- alSourcei(streamID, AL_BUFFER, 0);\r
- audio.freeStream(streamID);\r
- streamID = -1;\r
+ audio.freeSource(sourceID);\r
+ sourceID = -1;\r
}\r
alDeleteBuffers(buffers);\r
buffers = null;\r
}\r
\r
public void play (float volume) {\r
- int streamID = audio.obtainStream(false);\r
- if (streamID == -1) return;\r
- alSourcei(streamID, AL_BUFFER, bufferID);\r
- alSourcei(streamID, AL_LOOPING, AL_FALSE);\r
- alSourcef(streamID, AL_GAIN, volume);\r
- alSourcePlay(streamID);\r
+ int sourceID = audio.obtainSource(false);\r
+ if (sourceID == -1) return;\r
+ alSourcei(sourceID, AL_BUFFER, bufferID);\r
+ alSourcei(sourceID, AL_LOOPING, AL_FALSE);\r
+ alSourcef(sourceID, AL_GAIN, volume);\r
+ alSourcePlay(sourceID);\r
}\r
\r
public void loop () {\r
- int streamID = audio.obtainStream(false);\r
- if (streamID == -1) return;\r
- alSourcei(streamID, AL_BUFFER, bufferID);\r
- alSourcei(streamID, AL_LOOPING, AL_TRUE);\r
- alSourcePlay(streamID);\r
+ int sourceID = audio.obtainSource(false);\r
+ if (sourceID == -1) return;\r
+ alSourcei(sourceID, AL_BUFFER, bufferID);\r
+ alSourcei(sourceID, AL_LOOPING, AL_TRUE);\r
+ alSourcePlay(sourceID);\r
}\r
\r
public void stop () {\r
- audio.stopStreamsWithBuffer(bufferID);\r
+ audio.stopSourcesWithBuffer(bufferID);\r
}\r
\r
public void dispose () {\r