SDL_QueueAudio underrun

Hi,

I’m attempting to decode an opus audio stream and play it back using SDL2 but I’m having issues with a constantly growing audio delay.

It seems like the audio isn’t getting moved to the audio device quick enough or is playing back for slightly too long causing a gradual increase in delay over time.

I’ve tried to limit the audio queue size as seen in the code below but this ends up causing small jitters in the audio. Any help on this would be greatly appreciated

if (SDL_GetQueuedAudioSize(dev) < 38400)
{
int output_samples = opus_decode(dec, (unsigned char*)entry->data, entry->length, decodedBuffer, FRAME_SIZE, 0);
if (output_samples > 0)
{
if (SDL_QueueAudio(dev, decodedBuffer, output_samples * CHANNEL_COUNT * sizeof(opus_int16)) < 0)
LOG_ERROR(“error queuing audio: %s”, SDL_GetError());
}
}
else {
SDL_ClearQueuedAudio(dev);
}

Regards,

Tim.

[…]

else {
SDL_ClearQueuedAudio(dev);
}

As I understand it, this will discard anything that hasn’t already
been sent to the underlying audio API, which is probably what’s
causing glitches. When there’s enough in the queue, you’re not
supposed to do anything at all. :-)On Thu, Oct 20, 2016 at 12:42 AM, Timothy Macintyre wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

But I still then end up with gaps as this is a live stream I have to discard several of the opus packets whilst the buffer goes down. It doesn’t make sense that the buffer is filling up that much, it should be playing just as fast as I fill it up.From: David Olofson
Sent: Thursday, 20 October, 00:53
Subject: Re: [SDL] SDL_QueueAudio underrun
To: SDL Development List

On Thu, Oct 20, 2016 at 12:42 AM, Timothy Macintyre wrote: […] > else { > SDL_ClearQueuedAudio(dev); > } As I understand it, this will discard anything that hasn’t already been sent to the underlying audio API, which is probably what’s causing glitches. When there’s enough in the queue, you’re not supposed to do anything at all. :slight_smile: – //David Olofson - Consultant, Developer, Artist, Open Source Advocate .— Games, examples, libraries, scripting, sound, music, graphics —. | http://consulting.olofson.net http://olofsonarcade.com | ‘---------------------------------------------------------------------’ _______________________________________________ SDL mailing list SDL at lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Ah…! But then the sample rate of the stream isn’t matching the audio
interface. Even if you set them up to exactly the same value, you
can’t rely on them matching exactly, because even the finest crystal
oscillators aren’t infinitely accurate. The only common situation I
can think of where you do NOT have this problem is in studios, between
local audio interfaces synchronized via wordclock or similar.

However, one can usually get away with just inserting or dropping a
single sample frame every once in a while, so I’m kind of wondering if
you even have the right nominal sample rates… For example, 44 kHz vs
the standard 44.1 kHz would be a problem, and would probably require
proper resampling to avoid constant audible artifacts.On Thu, Oct 20, 2016 at 9:34 AM, Timothy Macintyre wrote:

But I still then end up with gaps as this is a live stream I have to discard
several of the opus packets whilst the buffer goes down. It doesn’t make
sense that the buffer is filling up that much, it should be playing just as
fast as I fill it up.


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Ok, so I took another look at the sample rates. Although every example I’ve seen has set the rate to 48000 for Opus I increased it slightly to 48100:

SDL_zero(w_wav_spec);
w_wav_spec.freq = 48100;

This appears to have resolved the underrun for the most part although it does occasionally fully empty the buffer causing a slight pause in audio it’s a lot better than filling the whole thing up then having to clear to maintain a reasonable latency. I believe the official demo app implements a double buffer which sounds like it may help mitigate this somewhat although the additional latency may be an issue.

Anyway, thanks for the help!

Regards,

Tim.________________________________
From: sdl-bounces@lists.libsdl.org> on behalf of david olofson <david at olofson.net (SDL)
Sent: 20 October 2016 08:12:21
To: SDL Development List
Subject: Re: [SDL] SDL_QueueAudio underrun

On Thu, Oct 20, 2016 at 9:34 AM, Timothy Macintyre <@Timothy_Macintyre> wrote:

But I still then end up with gaps as this is a live stream I have to discard
several of the opus packets whilst the buffer goes down. It doesn’t make
sense that the buffer is filling up that much, it should be playing just as
fast as I fill it up.

Ah…! But then the sample rate of the stream isn’t matching the audio
interface. Even if you set them up to exactly the same value, you
can’t rely on them matching exactly, because even the finest crystal
oscillators aren’t infinitely accurate. The only common situation I
can think of where you do NOT have this problem is in studios, between
local audio interfaces synchronized via wordclock or similar.

However, one can usually get away with just inserting or dropping a
single sample frame every once in a while, so I’m kind of wondering if
you even have the right nominal sample rates… For example, 44 kHz vs
the standard 44.1 kHz would be a problem, and would probably require
proper resampling to avoid constant audible artifacts.


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org