Discussion:
[SDL] SDL_QueueAudio underrun
Timothy Macintyre
2016-10-19 22:42:47 UTC
Permalink
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.
David Olofson
2016-10-19 23:53:08 UTC
Permalink
On Thu, Oct 20, 2016 at 12:42 AM, Timothy Macintyre
<***@outlook.com> 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. :-)


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

.--- Games, examples, libraries, scripting, sound, music, graphics ---.
| http://consulting.olofson.net http://olofsonarcade.com |
'---------------------------------------------------------------------'
Timothy Macintyre
2016-10-20 07:34:40 UTC
Permalink
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. :-) -- //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 ***@lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
David Olofson
2016-10-20 08:12:21 UTC
Permalink
On Thu, Oct 20, 2016 at 9:34 AM, Timothy Macintyre
<***@outlook.com> 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 |
'---------------------------------------------------------------------'
Timothy Macintyre
2016-10-20 09:27:51 UTC
Permalink
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 <sdl-***@lists.libsdl.org> on behalf of David Olofson <***@olofson.net>
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
<***@outlook.com> 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 |
'---------------------------------------------------------------------'
Loading...