Skip to content

Commit

Permalink
Clean up streamed and processed buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Feb 4, 2024
1 parent c307221 commit 6fa8946
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ import java.nio.ByteBuffer
*/
interface AudioRenderer {

/**
* Return the number of buffers that have been processed for the given source,
* which is the number of buffers that can be safely unqueued from the source.
*
* @param sourceId
* @return Number of processed buffers.
*/
int buffersProcessed(int sourceId)

/**
* Creates and fills a sound buffer with the given data.
*
Expand Down Expand Up @@ -71,22 +62,6 @@ interface AudioRenderer {
*/
void deleteSource(Source source)

/**
* Queue some buffers to an existing source.
*
* @param sourceId
* @param bufferIds
*/
void queueBuffers(int sourceId, int ... bufferId)

/**
* Unqueue some buffers from an existing source.
*
* @param sourceId
* @param bufferIds
*/
void unqueueBuffers(int sourceId, int ... bufferId)

/**
* Update details about the listener.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ abstract class Source implements AudioResource {
*/
abstract void attachBuffer(Buffer buffer)

/**
* Return the number of queued buffers that have been processed for this
* source.
*
* @return Number of processed buffers.
*/
abstract int buffersProcessed()

/**
* Rether whether this source is currently paused.
*
Expand Down Expand Up @@ -79,4 +87,9 @@ abstract class Source implements AudioResource {
* Stop playing the sound through this source.
*/
abstract void stop()

/**
* Unqueue buffers from this source.
*/
abstract void unqueueBuffers(Buffer... buffers)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ class OpenALRenderer implements AudioRenderer, EventTarget {
return result
}

@Override
int buffersProcessed(int sourceId) {

return checkForError { -> alGetSourcei(sourceId, AL_BUFFERS_PROCESSED) }
}

@Override
Buffer createBuffer(int bits, int channels, int frequency, ByteBuffer data) {

Expand Down Expand Up @@ -109,12 +103,6 @@ class OpenALRenderer implements AudioRenderer, EventTarget {
trigger(new SourceDeletedEvent(source))
}

@Override
void queueBuffers(int sourceId, int ... bufferIds) {

checkForError { -> alSourceQueueBuffers(sourceId, bufferIds) }
}

/**
* Emit some information about the OpenAL rendering device.
*
Expand All @@ -131,12 +119,6 @@ class OpenALRenderer implements AudioRenderer, EventTarget {
""".stripIndent()
}

@Override
void unqueueBuffers(int sourceId, int ... bufferIds) {

checkForError { -> alSourceUnqueueBuffers(sourceId, bufferIds) }
}

@Override
void updateListener(Vector3f position, Vector3f velocity, Orientation orientation) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ class OpenALSource extends Source {
}
}

@Override
int buffersProcessed() {

return alGetSourcei(sourceId, AL_BUFFERS_PROCESSED)
}

@Override
void close() {

Expand Down Expand Up @@ -108,7 +114,7 @@ class OpenALSource extends Source {
@Override
void queueBuffers(Buffer... buffers) {

alSourceQueueBuffers(sourceId, ((OpenALBuffer[])buffers)*.bufferId as int[])
alSourceQueueBuffers(sourceId, *((OpenALBuffer[])buffers)*.bufferId)
}

@Override
Expand All @@ -124,4 +130,10 @@ class OpenALSource extends Source {
alSourceStop(sourceId)
}
}

@Override
void unqueueBuffers(Buffer... buffers) {

alSourceUnqueueBuffers(sourceId, *((OpenALBuffer[])buffers)*.bufferId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import groovy.transform.TupleConstructor
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.BlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue

/**
* An emitter of sound in the scene.
Expand All @@ -52,7 +53,7 @@ class Sound implements Node<Sound>, AudioElement, Playable {
private Source source
private Buffer staticBuffer
private BlockingQueue<Buffer> streamingBuffers = new ArrayBlockingQueue<>(10)
private final List<Buffer> streamedBuffers = []
private final BlockingQueue<Buffer> streamedBuffers = new LinkedBlockingQueue<>()

@Override
void delete(AudioRenderer renderer) {
Expand Down Expand Up @@ -99,7 +100,7 @@ class Sound implements Node<Sound>, AudioElement, Playable {
if (staticBuffer) {
scene.requestDelete(staticBuffer)
}
if (streamingBuffers) {
if (streamedBuffers) {
scene.requestDelete(*streamedBuffers)
}
}
Expand All @@ -111,6 +112,7 @@ class Sound implements Node<Sound>, AudioElement, Playable {
return
}

// Add static or streaming buffers to the source
if (staticBuffer) {
source.attachBuffer(staticBuffer)
}
Expand All @@ -120,6 +122,7 @@ class Sound implements Node<Sound>, AudioElement, Playable {
streamedBuffers.addAll(newBuffers)
}

// Control playback
if (playing) {
if (source.stopped) {
logger.debug("Buffer exhausted, stopping")
Expand All @@ -143,5 +146,15 @@ class Sound implements Node<Sound>, AudioElement, Playable {
source.stop()
}
}

// Clean up used buffers for a streaming source
if (soundFile.forStreaming) {
var buffersProcessed = source.buffersProcessed()
if (buffersProcessed) {
var processedBuffers = streamedBuffers.drain(buffersProcessed)
source.unqueueBuffers(*processedBuffers)
renderer.deleteBuffers(*processedBuffers)
}
}
}
}

0 comments on commit 6fa8946

Please sign in to comment.