[common] ringbuffer: add unbounded mode

In unbounded mode, the read and write pointers are free to move
independently of one another. This is useful where the input and output
streams are progressing at the same rate on average, and we want to keep
the latency stable in the event than an underrun or overrun occurs.

If an underrun occurs (i.e., there is not enough data in the buffer to
satisfy a read request), the missing values with be filled with zeros. When
the writer catches up, the same number of values will be skipped from the
input.

If an overrun occurs (i.e., there is not enough free space in the buffer to
satisfy a write request), excess values will be discarded. When the reader
catches up, the same number of values will be zeroed in the output.

Unbounded mode is currently unused since our audio input and output
streams are not synchronised. This will be implemented in a later commit.

Also reimplemented as a lock-free queue which is safer for use in audio
device callbacks.
This commit is contained in:
Chris Spencer
2022-01-23 16:44:00 +00:00
committed by Geoffrey McRae
parent b34b253814
commit 599fdd6ffd
3 changed files with 226 additions and 148 deletions

View File

@@ -143,18 +143,7 @@ static void playbackStopNL(void)
static int playbackPullFrames(uint8_t * dst, int frames)
{
if (audio.playback.buffer)
{
frames = min(frames, ringbuffer_getCount(audio.playback.buffer));
for(int fetched = 0; fetched < frames; )
{
int copy = frames - fetched;
uint8_t * src = ringbuffer_consume(audio.playback.buffer, &copy);
memcpy(dst, src, copy * audio.playback.stride);
dst += copy * audio.playback.stride;
fetched += copy;
}
}
frames = ringbuffer_consume(audio.playback.buffer, dst, frames);
else
frames = 0;
@@ -183,6 +172,10 @@ void audio_playbackStart(int channels, int sampleRate, PSAudioFormat format,
playbackStopNL();
}
// Using a bounded ring buffer for now. We are not currently doing anything to
// keep the input and output in sync, so if we were using an unbounded buffer,
// it would eventually end up in a state of permanent underrun or overrun and
// the user would only hear silence
const int bufferFrames = sampleRate;
audio.playback.buffer = ringbuffer_new(bufferFrames,
channels * sizeof(uint16_t));