public long play(float volume) { if (audio.noDevice) return 0; int sourceID = audio.obtainSource(false); if (sourceID == -1) { // Attempt to recover by stopping the least recently played sound audio.retain(this, true); sourceID = audio.obtainSource(false); } else audio.retain(this, false); // In case it still didn't work if (sourceID == -1) return -1; long soundId = audio.getSoundId(sourceID); alSourcei(sourceID, AL_BUFFER, bufferID); alSourcei(sourceID, AL_LOOPING, AL_FALSE); alSourcef(sourceID, AL_GAIN, volume); alSourcePlay(sourceID); return soundId; }
@Override public long loop(float volume) { if (audio.noDevice) return 0; int sourceID = audio.obtainSource(false); if (sourceID == -1) return -1; long soundId = audio.getSoundId(sourceID); alSourcei(sourceID, AL_BUFFER, bufferID); alSourcei(sourceID, AL_LOOPING, AL_TRUE); alSourcef(sourceID, AL_GAIN, volume); alSourcePlay(sourceID); return soundId; }
public void writeSamples(byte[] data, int offset, int length) { if (length < 0) throw new IllegalArgumentException("length cannot be < 0."); if (sourceID == -1) { sourceID = audio.obtainSource(true); if (sourceID == -1) return; if (buffers == null) { buffers = Buffers.newDirectIntBuffer(bufferCount); audio.getAL().alGenBuffers(buffers.limit(), buffers); if (audio.getAL().alGetError() != ALConstants.AL_NO_ERROR) throw new GdxRuntimeException("Unabe to allocate audio buffers."); } audio.getAL().alSourcei(sourceID, ALConstants.AL_LOOPING, ALConstants.AL_FALSE); audio.getAL().alSourcef(sourceID, ALConstants.AL_GAIN, volume); // Fill initial buffers. int queuedBuffers = 0; for (int i = 0; i < bufferCount; i++) { int bufferID = buffers.get(i); int written = Math.min(bufferSize, length); tempBuffer.clear(); tempBuffer.put(data, offset, written).flip(); audio .getAL() .alBufferData(bufferID, format, tempBuffer, tempBuffer.remaining(), sampleRate); ib.put(0, bufferID).rewind(); audio.getAL().alSourceQueueBuffers(sourceID, ib.limit(), ib); length -= written; offset += written; queuedBuffers++; } // Queue rest of buffers, empty. tempBuffer.clear().flip(); for (int i = queuedBuffers; i < bufferCount; i++) { int bufferID = buffers.get(i); audio .getAL() .alBufferData(bufferID, format, tempBuffer, tempBuffer.remaining(), sampleRate); audio.getAL().alSourceQueueBuffers(sourceID, ib.limit(), ib); } audio.getAL().alSourcePlay(sourceID); isPlaying = true; } while (length > 0) { int written = fillBuffer(data, offset, length); length -= written; offset += written; } }