SDL_mixer problems

I’ve been using SDL_Mixer for quite a few months now and so far I cannot get
it to work properly. I can get good sound and music playing but there is an
unacceptable delay in playing the sounds. This delay
seems to be anywhere from 0.5 secs to 1 sec. This won’t do for things like
pressing interface buttons or creatures attacking.

I can’t understand why there should be a delay in playing a sound. To me it
should just play immediately.

I have tried different values using Mix_OpenAudio but I can’t get an
acceptable result. If the buffer size is below 8192 the sounds and music
start to stutter. I’ve tried this on a Win 95 P200 and a Win 2000 P1.5 Gig
machine and I get the same result.

When I run the examples supplied with SDL_Mixer I get the same problems so
it doesn’t seem to be a problem with my code.

Has anyone managed to get past this or is SDL_Mixer not suitable for PCs?

Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 2048) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 8192) // good but lags_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com

I’ve been using SDL_Mixer for quite a few months now and so far I cannot
get it to work properly. I can get good sound and music playing but there
is an unacceptable delay in playing the sounds. This delay
seems to be anywhere from 0.5 secs to 1 sec. This won’t do for things like
pressing interface buttons or creatures attacking.

I can’t understand why there should be a delay in playing a sound. To me it
should just play immediately.

I have tried different values using Mix_OpenAudio but I can’t get an
acceptable result. If the buffer size is below 8192 the sounds and music
start to stutter. I’ve tried this on a Win 95 P200 and a Win 2000 P1.5 Gig
machine and I get the same result.

When I run the examples supplied with SDL_Mixer I get the same problems so
it doesn’t seem to be a problem with my code.

Has anyone managed to get past this or is SDL_Mixer not suitable for PCs?

I’ve been working on a game at home, that uses that on the main menu plays
a sound as the user uses the up and down arrows to navigate the menu items,
I have not seen a .5 to 1 second delay, although I have only tested under
Linux.

Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 1024) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 2048) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) // stutter
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 8192) // good but lags

-EvilTypeGuyOn Thu, Apr 25, 2002 at 10:00:37AM +1000, David Moffatt wrote:

I’ve been using SDL_Mixer for quite a few months now and so far I
cannot get it to work properly. I can get good sound and music playing
but there is an unacceptable delay in playing the sounds. This delay
seems to be anywhere from 0.5 secs to 1 sec. This won’t do for things
like pressing interface buttons or creatures attacking.

I can’t understand why there should be a delay in playing a sound. To
me it should just play immediately.

Wow… If it was that easy, I’d still be using WinNT. Audio latency was
the single most important reason why I gave up on Windows - and MS still
don’t have an acceptable solution.

Anyway, there’s no way a “standard” OS will provide “instant” response of
any form, at least not in a remotely reliable fashion. You need real time
scheduling for that, and the only such OS that may eventually support
that would be future Linux versions (2.5+). (Some claim that BeOS can
too, but where are the figures…?)

The best you can do in this case is using memory mapped audio IO, and
"mix over + mix ahead". Quite hairy, doesn’t prevent occasional glitches
in the beginning of sounds, and doesn’t work too well with “global” DSP
effects, like reverb. It seems to be quite sufficient for games, though.

Problem is that SDL doesn’t support this method, and as a result, nor
does SDL_mixer. (At least DirectX, ALSA and OSS do support it, though, so
there is hope.)

I have tried different values using Mix_OpenAudio but I can’t get an
acceptable result. If the buffer size is below 8192 the sounds and
music start to stutter. I’ve tried this on a Win 95 P200 and a Win 2000
P1.5 Gig machine and I get the same result.

You would find that even on standard Linux, buffer sizes in the range of
hundreds of samples will work just fine. I’m not sure why Windows sucks
this bad in this respect - I can only conclude it does, on every single
version and machine I’ve tested it on. It seems that Windows simply
refuses to schedule threads frequently enough for smaller buffers.

When I run the examples supplied with SDL_Mixer I get the same problems
so it doesn’t seem to be a problem with my code.

Same problem with Kobo Deluxe, any version. (The latest versions could
be worse though, as they need more CPU time. Haven’t tested thoroughly
yet.)

Has anyone managed to get past this or is SDL_Mixer not suitable for
PCs?

It’s not SDL_Mixer’s fault. It’s just Windows that cannot deliver
acceptable latency without “dirty” hacks. (There’s a reason why the few
reasonably low latency soft synths there are for Windows are rather
expensive and surrounded by lots of hush-hush and/or patents.)

Maybe there are some tricks you could try to improve performance slightly
on Win32, but as I’ve said, I’ve given up on in. Using the “shared memory
method” is the best advice I can give you regarding Win32.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 25 April 2002 02:00, David Moffatt wrote:

[…latency…]

I’ve been working on a game at home, that uses that on the main menu
plays a sound as the user uses the up and down arrows to navigate the
menu items, I have not seen a .5 to 1 second delay, although I have
only tested under Linux.

Exactly. Prepare to be disappointed. :-/ Despite having done serious
audio hacking on Windows, I didn’t really think it was that bad. (Also,
I thought standard Linux would be much worse than it is… It’s in fact
very good; just not totally reliable, which is to be expected without a
real time patch, of course.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 25 April 2002 15:29, EvilTypeGuy wrote:

Please don’t do this with OSS though. If you have to use memory
mapped audio, ALSA is a much better bet. Not all cards support it in a
reasonable way, and the OSS API for mmaping the DMA buffer is not
reliable. I have spoken with 4Front about this, and they are basically
trying to phase out that part of the API.

This technique is one of the main reasons that Quake 3 audio would
often just not work.

If you must, though, my book explains how to do it in chapter 5.

You can significantly reduce latency in a safe way by adjusting OSS’
buffering parameters. Most drivers use fairly large buffers by
default. I’ve seen latency drop from 1 second to a very small fraction
of a second by using a single ioctl() call to reduce the number of
fragments in the driver’s ring buffer. This is not hard to do.

-JohnOn Thu, Apr 25, 2002 at 04:04:23PM +0200, David Olofson wrote:

The best you can do in this case is using memory mapped audio IO, and
"mix over + mix ahead". Quite hairy, doesn’t prevent occasional glitches
in the beginning of sounds, and doesn’t work too well with “global” DSP
effects, like reverb. It seems to be quite sufficient for games, though.


John R. Hall - KG4RUO - Stranded in the Sol System
Student, Georgia Tech; Author, Programming Linux Games

The best you can do in this case is using memory mapped audio IO, and
"mix over + mix ahead". Quite hairy, doesn’t prevent occasional
glitches in the beginning of sounds, and doesn’t work too well with
"global" DSP effects, like reverb. It seems to be quite sufficient
for games, though.

Please don’t do this with OSS though. If you have to use memory
mapped audio, ALSA is a much better bet.

Good point. (I’d say, use ALSA if it’s an option at all.)

Not all cards support it in a
reasonable way, and the OSS API for mmaping the DMA buffer is not
reliable. I have spoken with 4Front about this, and they are basically
trying to phase out that part of the API.

(Maybe another reason to avoid OSS altogether…)

This technique is one of the main reasons that Quake 3 audio would
often just not work.

I know. heh

If you must, though, my book explains how to do it in chapter 5.

You can significantly reduce latency in a safe way by adjusting OSS’
buffering parameters. Most drivers use fairly large buffers by
default. I’ve seen latency drop from 1 second to a very small fraction
of a second by using a single ioctl() call to reduce the number of
fragments in the driver’s ring buffer. This is not hard to do.

Right. In fact, I’d say that there is no real problem with Linux. Just
set up the I/O properly, as you would with any audio API. (What’s the
news here…?)

However, there is a problem with Windows, and it’s very hard to solve
any other way than using the memory mapped method.

Then again, DirectX seems to be a lot more reliable than OSS in that
regard, so for once, it seems like the “trick” will usually work where
it’s really needed.

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 25 April 2002 16:43, John R. Hall wrote:

On Thu, Apr 25, 2002 at 04:04:23PM +0200, David Olofson wrote:

[…]

This technique is one of the main reasons that Quake 3 audio would
often just not work.

BTW, isn’t the main reason why Q3 does this, that it “has to” because the
audio engine doesn’t have it’s own thread? (Very, very bad design, from
an audio hacker POV…)

I’ve been using the new Kobo Deluxe audio engine as a real time soft
synth (with the game intro running!) while composing the demo songs, and
the latency is comparable to that of a h/w synth. No even using a
lowlatency kernel. In my experience, this is to be expected from any
recently modern Linux system without heavy disk or CPU stress from other
processes. (To do it with stress, you’ll definitely need a lowlatency
kernel, though.)

Consequently, I cannot see any reason at all to use memory mapped mixing
on Linux - especially not for games. Why ask for trouble?

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 25 April 2002 16:43, John R. Hall wrote:

I’ve been using the new Kobo Deluxe audio engine as a real time soft
synth (with the game intro running!) while composing the demo songs, and
the latency is comparable to that of a h/w synth. No even using a
lowlatency kernel. In my experience, this is to be expected from any
recently modern Linux system without heavy disk or CPU stress from other
processes. (To do it with stress, you’ll definitely need a lowlatency
kernel, though.)

Consequently, I cannot see any reason at all to use memory mapped mixing
on Linux - especially not for games. Why ask for trouble?

I think SDL/SDL_mixer should provide a decent latency on all OS, wether
it has to use ugly hacks to achieve it or not. I experinced this latency
problem with my games too, but I thought it was my fault. Now that I
know it’s not, I want my money back… hummm… well :slight_smile: I don’t think
it’s acceptable to have a latency that high. Of course, it’s easier said
than done and as I don’t know anything about sound programming, I leave
it up to you guys :wink:

btw: I got those latency problems under linux too. Could someone explain
me clearly (linux newbie here) how to avoid it?

Good luck,
Ga?tan.

[…]

I think SDL/SDL_mixer should provide a decent latency on all OS, wether
it has to use ugly hacks to achieve it or not.

Right. It’s kind of hard to motivate why SDL/Win32 games have noticable
through annoying audio latencies, while other Win32 games have no such
problems whatsoever.

I experinced this
latency problem with my games too, but I thought it was my fault. Now
that I know it’s not, I want my money back… hummm… well :slight_smile: I don’t
think it’s acceptable to have a latency that high. Of course, it’s
easier said than done and as I don’t know anything about sound
programming, I leave it up to you guys :wink:

The problem with the shared memory method required for Win32 is that it
works quite differently from the normal method, so it would basically
require an alternative API “mode” for SDL, and major modifications to
SDL_Mixer. I’m not saying it shouldn’t be done; just that it’s probably
quite some work, and not likely to get done before SDL 1.3. (Well,
SDL_Mixer could bypass SDL on Win32…)

As to hacking anything, my major problem is that I don’t really do
Win32. I do try to make my code run on Win32 as well, but I’m not
strongly motivated to hack a “new” mixer specifically for dealing with
what seems to be a Win32-only issue.

btw: I got those latency problems under linux too. Could someone
explain me clearly (linux newbie here) how to avoid it?

Well, we’d need more info to be specific, but first of all, make sure
you’re not running your audio through a sound daemon, such as aRts.
(Various workarounds for aRts related problems have been posted here
before.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |-------------------------------------> http://olofson.net -'On Thursday 25 April 2002 20:50, Ga?tan de Menten wrote:

David Olofson

[…]

I think SDL/SDL_mixer should provide a decent latency on all OS, wether
it has to use ugly hacks to achieve it or not.

Right. It’s kind of hard to motivate why SDL/Win32 games have noticable
through annoying audio latencies, while other Win32 games have no such
problems whatsoever.

Thanks for the explanation, Dave.

Now if I can only figure out why you can load sounds with a RWOPS structure,
but not music :)> From: sdl-admin at libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of

Sent: Thursday, April 25, 2002 12:30 PM
To: sdl at libsdl.org
Subject: Re: [SDL] SDL_mixer problems
On Thursday 25 April 2002 20:50, Ga?tan de Menten wrote:


John Hattan Sweet software for a saturnine world
@John_Hattan http://www.thecodezone.com

The problem with the shared memory method required for Win32 is that it
works quite differently from the normal method, so it would basically
require an alternative API “mode” for SDL, and major modifications to
SDL_Mixer. I’m not saying it shouldn’t be done; just that it’s probably
quite some work, and not likely to get done before SDL 1.3. (Well,
SDL_Mixer could bypass SDL on Win32…)

As to hacking anything, my major problem is that I don’t really do
Win32. I do try to make my code run on Win32 as well, but I’m not
strongly motivated to hack a “new” mixer specifically for dealing with
what seems to be a Win32-only issue.

Just to clarify something (sometimes it’s hard to tell when David is
talking about existing code and when he’s talking about his own ideas),
currently the SDL Win32 sound code uses the primary mixing buffer of
DirectSound, whenever possible.

What would be helpful to debug the latency problems on Windows is:

  1. What version of Windows are you running?
  2. What version of DirectX are you using?
  3. What audio buffering size are you using? I’ve found 1024 to work well.
  4. What kind of latency are you seeing?
  5. Are you seeing consistent latency across different Windows/DirectX versions?
  6. Are you seeing consistent latency across different SDL applications?
  7. Can you post a link to binary and source code that you’re using to test?

Thanks,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Now if I can only figure out why you can load sounds with a RWOPS structure,
but not music :slight_smile:

Nobody has taken the effort to rewrite all the music back ends to use
SDL_rwops. I’m pretty sure Ryan’s SDL_sound project is doing that though,
so you might take a look at that.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

The best you can do in this case is using memory mapped audio IO, and
"mix over + mix ahead". Quite hairy, doesn’t prevent occasional glitches
in the beginning of sounds, and doesn’t work too well with “global” DSP
effects, like reverb. It seems to be quite sufficient for games, though.

Please don’t do this with OSS though. If you have to use memory
mapped audio, ALSA is a much better bet. Not all cards support it in a
reasonable way, and the OSS API for mmaping the DMA buffer is not
reliable. I have spoken with 4Front about this, and they are basically
trying to phase out that part of the API.

This technique is one of the main reasons that Quake 3 audio would
often just not work.

If you must, though, my book explains how to do it in chapter 5.

You can significantly reduce latency in a safe way by adjusting OSS’
buffering parameters. Most drivers use fairly large buffers by
default. I’ve seen latency drop from 1 second to a very small fraction
of a second by using a single ioctl() call to reduce the number of
fragments in the driver’s ring buffer. This is not hard to do.

Regardless, adjusting the buffer still never gives the lowest latency,
mmap always gives the lowest latency, it may not be as portable,
but it’s always worked on any sound card i’ve ever owned…

And if it was really this simple, I would find it hard to believe
that it wasn’t done in Q3…really…

Give the man some credit…On Thu, Apr 25, 2002 at 10:43:48AM -0400, John R. Hall wrote:

On Thu, Apr 25, 2002 at 04:04:23PM +0200, David Olofson wrote:

-John

Nobody has taken the effort to rewrite all the music back ends to use
SDL_rwops. I’m pretty sure Ryan’s SDL_sound project is doing that though,
so you might take a look at that.

And we’re working towards a 1.0 release:

http://icculus.org/SDL_sound/

Eventually SDL_sound will either replace the guts of SDL_mixer or be used
in a new library that heavily cuts-and-pastes from SDL_mixer’s
codebase…which way that goesdepends on backward compatibility
issues vs. clean design blahblahblah.

–ryan.

Hey guys. I started this thread a while ago and since then I learned
something new about the delay with sounds played on PC’s. What I have
noticed is the large delay is only evident when you are playing a sound file
and a music file simultaneuosly.

Sam:

I’ve been real busy with my coding lately but when these last pieces are
done I’ll put together some sound/music code with the delay and give you
those system specs you asked for._________________________________________________________________
Join the world?s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

Hey guys. I started this thread a while ago and since then I learned
something new about the delay with sounds played on PC’s. What I have
noticed is the large delay is only evident when you are playing a sound file
and a music file simultaneuosly.

Interesting.

Sam:

I’ve been real busy with my coding lately but when these last pieces are
done I’ll put together some sound/music code with the delay and give you
those system specs you asked for.

Great, thanks David.

See ya!
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Looking at the old messages I’ve seen some posts of people complaining
of sound glitches under LINUX with SDL_mixer. Well, I suffer from the
same problem under Windows. My problem is as follows:

  • When I start my application, everything sounds ugly.
  • But If for some reason my application quits from SDL and restarts it.
    Everything sounds good now.

I think that SDL_mixer have some problem with initializing when the
application is starting. Maybe some timings are not well computed
because as the application is loading all the DLLs, a lot of disk usage
is performed, thus diminishing the performance of your machine for a while.

I’ve got one solution that works in all machines where I’ve tested my
program:

  • Start the application.
  • Make a timmer that waits for 5 seconds.
  • Call SDL_Quit
  • Call SDL_Init again.

Now everything sounds good. Anyone knows why? Anyone has some other
"nicer" solution?

What Windows versions are you testing with? Could you post code so we
could try it, too?On Thu, Nov 14, 2002 at 07:18:24PM +0200, Santi Onta??n wrote:

I’ve got one solution that works in all machines where I’ve tested my
program:

  • Start the application.
  • Make a timmer that waits for 5 seconds.
  • Call SDL_Quit
  • Call SDL_Init again.

Now everything sounds good. Anyone knows why? Anyone has some other
"nicer" solution?


Glenn Maynard

Ok, my initialization code is as follows:

SDL_Surface* initializeSDL(int moreflags)
{
SDL_Surface *screen;

int flags = SDL_HWPALETTE|moreflags;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)<0) return 0;
atexit(SDL_Quit);

if (Mix_OpenAudio(22050, AUDIO_S16, 2, 2048)) {
    sound_enabled=false;
} /* if */

// pause(1000);

screen = SDL_SetVideoMode(SCREEN_X, SCREEN_Y, COLOUR_DEPTH, flags);

SDL_EnableUNICODE(1);

return screen;

} /* initializeSDL */

Then I load a sound file and play it with:

Mix_Chunk *s=Mix_LoadWAV(“sword.wav”);
Mix_PlayChannel(-1,s,0);

Without the pause(1000) (one second of pause) everything sounds
horrible, with a lot of craps, etc… but by adding the pause of 1
second before initizlizing the video mode, I’ve discovered that
everything sounds better… some explanation or better solution for this?

Hi all. First time posting here. Got a problem and wondered if you could help.
I’m writing a Mac OS X game using SDL_mixer for sound, and it seems that I must be using the
library wrong.

Here’s the download site (it’s a work in progress): http://student.bard.edu/~dm488/Asteron.dmg

You may notice in the game if you fire steadily at a certain rate, the laser sound sometimes gets
played twice for a single shot. Why does this happen? I call the function only one time per
keydown event.

The tutorials I’ve found on SDL_mixer have only demonstrated how to play a sound repeatedly
when a key is pressed (which is of course not what the laser gun requires; it fires one shot at a
time).

However, the engine thrust sound DOES need a looping sample. That works fine except for when
the thrust key is released, whereupon you hear a noisy click from the sound stopping. In no
tutorials or documentation for SDL_mixer (the SDL_mixer docs and Roger Ostrander’s SDL_Mixer
Tutorial specifically) have I found a way to stop a looping sound without causing a click.

There is a FadeOut function, but it seems mainly geared toward slow fades (3 seconds or so), and
even using that you still hear clicks. Is there any way to gracefully silence a looping sound? Am I
just doing it wrong?

Many thanks for any help.

-Duncan Malashock

P.S. If you can’t answer these questions without some source code, I’ll be happy to provide it.