SDL_Mixer sound effect problem causing my game to stutter

I’m having problems with my game stuttering when I use sound effects. I tried
pasting in the source code, but it keeps giving me problems with lines being
over 80 characters, so I saved everything as a .txt file, and put it on my
website. Hopefully this won’t be a problem.

Here’s the link:
http://www.nformant.net/SoundEffects.txt

I’ve got stuff explained in the text file.

Is there anything in the code I posted that could be causing this?

And if it’s not this code that’s causing it, anyone have any suggestions as to
what could be causing the stuttering now that I’ve switched over to the new way
of doing things?

El s?b, 05-11-2005 a las 09:53 +0000, James OMeara escribi?:

Is there anything in the code I posted that could be causing this?

No, but you should try increasing the buffer size you pass when you open
the mixer. Try 2048.

--Gabriel-- 

Gabriel Gambetta
Mystery Studio - http://www.mysterystudio.com
Gabriel on Graphics - http://gabrielongraphics.blogspot.com

Damn, so I’ve done nothing wrong with that code?

I already open my audio like this:
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048)

So it’s already set at 2048. Is there a possibility that my wav file
could be behind this? I’ve got my wav file set up as a mono 44KHz,
8 bit file, with PCM encoding.

If it’s not that, I’m really at my wits end as to what could be causing
this. Ugh.

James OMeara wrote:

Damn, so I’ve done nothing wrong with that code?
I already open my audio like this:
Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048)

I dont know if you’ve done anything wrong, but I would not load the sfx that
way.
Loading the wave files in the middle of the real-time game is a bad idea and
I would not be surprised if that was the problem here. Preload all the waves
and stash them away in your definition structs.

Is there a possibility that my wav file could be behind this? I’ve got my
wav
file set up as a mono 44KHz, 8 bit file, with PCM encoding.

The format difference could be the ultimate catalyst – the straw that
breaks the camel’s back, since CPU time is needed to convert the chunk to
the sound buffer format.

-Alex.

Thanks for the answer, I’ll look into that.

I just find it odd that I’m having problems when I’ve seen other game sue this
method without any problems whatsoever.

I’m a bit worried about the number of effects I’d have to load, however. I’d end
up having to load 50 or so sound effects, and I’m sure that would take up a bit
of memory.

[…]

I’m a bit worried about the number of effects I’d have to load,
however. I’d end up having to load 50 or so sound effects, and I’m
sure that would take up a bit of memory.

Assuming you’re aiming at low end systems and/or want a handy,
comfortable download size, or you want lots of high quality sound
effects, that could be a problem…

The download size can be dealt with by using Ogg Vorbis or something
for the sound effects, or taking it to extremes and use a custom
structured audio engine. (The latter can be extremely effective, but
creating structured audio is a completely different process, compared
to samples.) However, if you need to run on systems with very little
RAM, you’ll probably also have to deal with CPUs that are too slow to
decode or render sounds in real time. You can still shrink the
download by decoding/rendering in the installer or when loading
levels, or even let the user decide.

A slightly different approach is to make the most of a smaller set of
waveforms by means of simple real time effects, like changing the
pitch, looping, “coarse” granular syntesis (playing lots of short
sounds with varying volume and pitch), simple filters (a 12 dB/oct
resonant SVR, for example), feedback delays, waveshapers,
compressors… I think you can do at least some of this with
SDL_Mixer (fx plugins), so you shouldn’t need to roll your own sound
engine, unless you’re going for really advanced stuff.

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Tuesday 08 November 2005 09.27, James OMeara wrote:

James OMeara wrote:

I just find it odd that I’m having problems when I’ve seen other
game sue this method without any problems whatsoever.

You said you are using an 8bit PCM coding, so I am assuming it’s actually a
u-Law or a-Law coding (not plain PCM). That could make a difference as it
takes time to decode. However, if you really are using 8bit unsigned PCM,
I’d suggest a 22KHz 16bit rather – you’ll get better quality with that.

I’m a bit worried about the number of effects I’d have to load,
however. I’d end up having to load 50 or so sound effects, and
I’m sure that would take up a bit of memory.

Simple math really. Assuming your average sfx length is ~1 second:
50 * (22050 * 2 bytes per sec) = 2205000 – 2 Megs. Not a whole lot.

-Alex.

Thanks again for the replies, guys.

You’ve been a big help.

Alex Volkov <avcp-sdlmail usa.net> writes:

You said you are using an 8bit PCM coding, so I am assuming it’s actually a
u-Law or a-Law coding (not plain PCM). That could make a difference as it
takes time to decode. However, if you really are using 8bit unsigned PCM,
I’d suggest a 22KHz 16bit rather – you’ll get better quality with that.

Well, to tell you honestly, I have no idea about all the real tech stuff. Some
of my effects were recorded using Sound Recorder in Windows. I selected
Format>PCM and Attributes>44,100 8Bit Mono
I can easily re-record the files, so what will I get the best performance out
of? 22Khz, or 44.1Khz at 8 or 16 bit if I do this: Mix_OpenAudio(44100,
AUDIO_S16SYS, 2, 2048)

I’d imagine I’d get the best performance with the 44.1KHz, 16-bit wav files.

Is this a bad question? I try to keep my head out of stuff like this, and I
really shouldn’t, but it leaves me unaware of how to do things properly.

Also, you said: “Preload all the waves
and stash them away in your definition structs.”

How would I go about doing something like this? I was thinking about doing
something like making a vector of Mix_Chunk’s, and then having a function that
manually loaded each file into the vector.

Can you suggest something that will work better?

Again, thanks for all the help.

Alex Volkov writes:

I can easily re-record the files, so what will I get the best performance out
of? 22Khz, or 44.1Khz at 8 or 16 bit if I do this: Mix_OpenAudio(44100,
AUDIO_S16SYS, 2, 2048)

Usually I think 16-bit, 22 kHz are enough for simple sound effects -
and the clear advantage over 44 kHz is that it only occupies half the
space :slight_smile:
Also I think it should be up to the user to decide what sound output
quality he want - and let it be up to underlying (SDL_mixer) layer to
bother with conversion and performance questions. Anyway, when it is
such simple conversions it shouldn’t cause a performance impact.

How would I go about doing something like this? I was thinking about doing
something like making a vector of Mix_Chunk’s, and then having a function that
manually loaded each file into the vector.

If your game is very simple and you know beforehand exactly what sound
effects there’s going to be in it, the simplest solution is to have an
array of Mix_Chunks, load them at initialization time, unload them
when quitting, and have a void PlaySoundEffect(int fx) function to
play the effects (with fx simply being an index into your chunk
array).

It’s all very simple with SDL_mixer.–
Rasmus Neckelmann