# SDL\_mixer issue, no one talks about ?!

**URL:** <https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200>\
**Category:** SDL Development\
**Created:** [December 5, 2013, 7:10pm UTC](https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200 "2013-12-05T19:10:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JurisL85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/b487fb/32.png) [@JurisL85](https://discourse.libsdl.org/u/JurisL85)\
**Post date:** [December 5, 2013, 7:10pm UTC](https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200/1 "2013-12-05T19:10:40Z")

</div>

Hi, I have been coding my Engine and I have one thing left before I start coding first few games on it - Audio class with SDL\_mixer chunk playback.

I have spent 3-4 days researching this issue and found that no one is talking about it.

The Issue: using Mix\_PlayChannel(); to play sound rapidly to simulate things, like Shooting a gun, Car Engine Roaring, Laser Shooting… Something that has the same sound file and is played with (for example) 100ms delay to get this rapid, machine gun fire like sound when Button is DOWN.

I have used SDL\_GetTicks() and SDL\_GetPerformanceCounter() to measure (for example) 100ms and play the sound. But the problem is that playback is not consistent, even few ‘ms’ of time difference can be heard when sounds are played rapidly.

I have created simple demo do demonstrate this here: [http://goo.gl/PwHFNK](http://goo.gl/PwHFNK)

Funny thing is, most of tutorials and even Books (Focus on SDL, SDL Game Programming) show you how to create SDL\_mixer based sound class but skip the part where you have to play sounds in rapid progression like a shooting sound.

Anyone here had this issue and knows hot to go about it?

Regards.  
Juris.------------------------  
Engine programmer for ANN-Tech. Interactive  
[www.ann-tech.com](http://www.ann-tech.com)

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [December 6, 2013, 9:16pm UTC](https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200/2 "2013-12-06T21:16:52Z")

</div>

[…]

> I have used SDL\_GetTicks() and SDL\_GetPerformanceCounter() to measure (for  
> example) 100ms and play the sound. But the problem is that playback is not  
> consistent, even few ‘ms’ of time difference can be heard when sounds are  
> played rapidly.  
> […]

I suspect few actually understand the problem, and most just  
(incorrectly) assume it’s in the nature of things and work around it.  
I don’t actually know of any games, other than my own, that aren’t  
“hacking” around the problem by using looped samples for machine guns,  
engines and the like.

Anyway, what happens in most sound engines is, the API calls either  
queue up messages, or locks the engine and changes its state directly.  
The next time the audio callback runs, which is typically every 20 ms  
or so, new sound effects are started, right at the start of the audio  
buffer. So, all your commands are effectively quantized to whatever  
buffer period audio output is configured to!

A low latency musical application typically runs audio processing an  
order of magnitude more frequently (less than 1 ms is common), which  
reduces this issue to acceptable levels for most applications - but  
unfortunately, you can’t rely on that “solution” (it’s still a  
hack…) unless you’re on a machine that’s equipped and configured for  
serious music production.

What you can do is timestamp the messages from the API, and delay them  
as needed in the mixer to maintain constant latency. That takes the  
buffer setting out of the equation; more buffering only increases  
latency - not quantization.

However, that still leaves your game video frame rate dependent. If  
you just check the current time in the mixer API calls, timing still  
gets quantized, only now, it’s to the rendering frame rate. If you’re  
running a fixed logic frame rate with interpolation/“tweening,” that’s  
still not good enough!

The next step is to add explicit timestamping support in the API.  
(That’s what I’m doing in both generations of Audiality, used in Kobo  
Deluxe and Kobo II respectively.) Instead of using actual timestamps  
for commands, you derive audio command timestamps from game logic  
time.

If you tune this carefully enough, you should theoretically get away  
with triggering sound effects with millisecond accurate timing, or  
even better - but realistically, it’s never going to be that accurate  
on any normal operating system. So, I’m going to admit that I’m  
cheating a little in Kobo II: That 100 RPS minigun is indeed all  
realtime synthesis and scripted bullet by bullet - but that script  
runs in the audio engine context, and just takes start/stop commands  
from the game logic scripts. 🙂

TL:DR: This issue can of course be fixed, but it’s not trivial. You’d  
have to hack SDL\_mixer (not sure how deeply) and change its API  
slightly.On Thu, Dec 5, 2013 at 8:10 PM, JurisL85 wrote:

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

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

---

<div class="post-metadata">

**Author:** ![Eric\_Wing](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/ea5d25/32.png) [@Eric\_Wing](https://discourse.libsdl.org/u/Eric_Wing)\
**Post date:** [December 6, 2013, 10:34pm UTC](https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200/3 "2013-12-06T22:34:37Z")

</div>

David has a great answer.

But as for easy workarounds, I have two additional suggestions:

1. Use the looping feature of Mix\_PlayChannel  
This might minimize some overhead for looping reducing the latency and  
maybe even randomness.

2. Try ALmixer\_PlayChannel with infinite looping  
ALmixer is a library I wrote that is very API-close to SDL\_mixer so it  
is pretty easy to port to. It uses OpenAL under the hood. For infinite  
looping, ALmixer uses OpenAL’s native looping feature so I kind of  
expect the latency/randomness issues to be among the best you will be  
able to get.

In both cases, you’ll want to employ an explicit start and stop of the  
sound (e.g. on button down and up) instead of trying to explicitly  
play each loop yourself.

Thanks,  
EricOn 12/6/13, David Olofson wrote:

> On Thu, Dec 5, 2013 at 8:10 PM, JurisL85 wrote:  
> […]
> 
> > I have used SDL\_GetTicks() and SDL\_GetPerformanceCounter() to measure  
> > (for  
> > example) 100ms and play the sound. But the problem is that playback is  
> > not  
> > consistent, even few ‘ms’ of time difference can be heard when sounds are  
> > played rapidly.  
> > […]
> 
> I suspect few actually understand the problem, and most just  
> (incorrectly) assume it’s in the nature of things and work around it.  
> I don’t actually know of any games, other than my own, that aren’t  
> “hacking” around the problem by using looped samples for machine guns,  
> engines and the like.
> 
> Anyway, what happens in most sound engines is, the API calls either  
> queue up messages, or locks the engine and changes its state directly.  
> The next time the audio callback runs, which is typically every 20 ms  
> or so, new sound effects are started, right at the start of the audio  
> buffer. So, all your commands are effectively quantized to whatever  
> buffer period audio output is configured to!
> 
> A low latency musical application typically runs audio processing an  
> order of magnitude more frequently (less than 1 ms is common), which  
> reduces this issue to acceptable levels for most applications - but  
> unfortunately, you can’t rely on that “solution” (it’s still a  
> hack…) unless you’re on a machine that’s equipped and configured for  
> serious music production.
> 
> What you can do is timestamp the messages from the API, and delay them  
> as needed in the mixer to maintain constant latency. That takes the  
> buffer setting out of the equation; more buffering only increases  
> latency - not quantization.
> 
> However, that still leaves your game video frame rate dependent. If  
> you just check the current time in the mixer API calls, timing still  
> gets quantized, only now, it’s to the rendering frame rate. If you’re  
> running a fixed logic frame rate with interpolation/“tweening,” that’s  
> still not good enough!
> 
> The next step is to add explicit timestamping support in the API.  
> (That’s what I’m doing in both generations of Audiality, used in Kobo  
> Deluxe and Kobo II respectively.) Instead of using actual timestamps  
> for commands, you derive audio command timestamps from game logic  
> time.
> 
> If you tune this carefully enough, you should theoretically get away  
> with triggering sound effects with millisecond accurate timing, or  
> even better - but realistically, it’s never going to be that accurate  
> on any normal operating system. So, I’m going to admit that I’m  
> cheating a little in Kobo II: That 100 RPS minigun is indeed all  
> realtime synthesis and scripted bullet by bullet - but that script  
> runs in the audio engine context, and just takes start/stop commands  
> from the game logic scripts. 🙂
> 
> TL:DR: This issue can of course be fixed, but it’s not trivial. You’d  
> have to hack SDL\_mixer (not sure how deeply) and change its API  
> slightly.
> 
> –  
> //David Olofson - Consultant, Developer, Artist, Open Source Advocate
> 
> .— Games, examples, libraries, scripting, sound, music, graphics —.  
> | [http://consulting.olofson.net](http://consulting.olofson.net)[http://olofsonarcade.com](http://olofsonarcade.com) |  
> ‘---------------------------------------------------------------------’

–  
Beginning iPhone Games Development

> **[Learn how to program games for iOS](https://playcontrol.net/iphonegamebook/)**
>
> Beginning iPhone Games Development Book published by Apress for PlayControl Software, Eric Wing. The best iPhone Game Programming Book.

---

<div class="post-metadata">

**Author:** ![JurisL85](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/b487fb/32.png) [@JurisL85](https://discourse.libsdl.org/u/JurisL85)\
**Post date:** [December 9, 2013, 8:53am UTC](https://discourse.libsdl.org/t/sdl-mixer-issue-no-one-talks-about/20200/4 "2013-12-09T08:53:58Z")

</div>

This starts to make sense now guys. Will try some of the ideas I got from you  
today. I Don’t want to use any other Libs other then SDL and OpenGL at the moment as my code runs on desktops and mobiles. But will try some stuff with SDL\_mixer today. Will post back on how it goes.------------------------  
Engine programmer for ANN-Tech. Interactive  
[www.ann-tech.com](http://www.ann-tech.com)
