Sound loading/saving in game using SDL & mixer

Hi,

i'm currently working on a game and i want to implement save/load

feature. But for me headaches makes audio saving & loading:
Requirements are, that i want that sound gets fully resumed- sound &
music (music is not so important). So is it possible to know at what offset
every sample currently playing is (eg. how much of sample is played)? And is
it possible to start playing sample from selected offset, not from
beginning?
I wanted to know, maybe someone has already similar opinion how to work
on this?

Kovacs

Well, of course it’s possible (unless you’re using some hardware mixer
with weird libitations :-), but you’d probably need some support from the
mixing engine.

I’d say it’s pretty simple in theory, if the mixer is designed with it in
mind - otherwise, it might end up in quite a messy hack.

One example would be the audio engine of “Kobo Deluxe” (probably the
final name of “SKobo”) - as most other mixers, it generates one buffer
for each SDL_audio call to the callback. There are 16 channels with state
info looking like this:

typedef struct
{
audio_samp_t samp;
int nsamp; /
number of samples /
int position;
int vol; /
percent of default */
int ctl[AC_COUNT];
} audio_channel_t;

As you probably guessed, this is not suitable for save/restore
functionality as it looks now, as you can’t grab the “samp” pointer and
store it somewhere. The pointer (which is a pointer to raw sample data;
not a struct!) would have to be replaced by an “index” that’s guaranteed
to always map to the same sound. (You could translate the indexes to real
names in text format to be on the safe side - just in case there would be
a reason to change the internals of the game later on.)

That fixed, you could just grab all fields of these structs and save them
to disk. (NOT by writing the structs as binary data, though…!
Non-portable and unreliable.) Writing the values back should restore the
engine to exactly where it was when you saved the state, with single
buffer accuracy. (Provided the engine does the sound look-up once per
buffer - or you’d need an “audio_force_load()” feature as well.)

Hmm… I might just go ahead and do this. OTOH, who needs to save a Kobo
game…? :slight_smile:

//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 |--------------------------------------> david at linuxdj.com -'On Friday 14 September 2001 21:45, Kovacs wrote:

Hi,

i'm currently working on a game and i want to implement save/load

feature. But for me headaches makes audio saving & loading:
Requirements are, that i want that sound gets fully resumed- sound
& music (music is not so important). So is it possible to know at what
offset every sample currently playing is (eg. how much of sample is
played)? And is it possible to start playing sample from selected
offset, not from beginning?

Well, of course it’s possible (unless you’re using some hardware mixer
with weird libitations :-), but you’d probably need some support from the
mixing engine.

I was talking about sdl_mixer lib, if forgot to mention.

I’d say it’s pretty simple in theory, if the mixer is designed with it in
mind - otherwise, it might end up in quite a messy hack.

I totally agree- it is simple. But does sdl_mixer support this, as i don’t
want to write a mixer myself.

As you probably guessed, this is not suitable for save/restore
functionality as it looks now, as you can’t grab the “samp” pointer and
store it somewhere. The pointer (which is a pointer to raw sample data;
not a struct!) would have to be replaced by an “index” that’s guaranteed
to always map to the same sound. (You could translate the indexes to real
names in text format to be on the safe side - just in case there would be
a reason to change the internals of the game later on.)

In generally i don’t need it so difficult- it would be enough if Mix_Chunk
had member long offset, so you know how far this sample has been mixed in
the general audio output stream. And another function like Mix_PlayChannel
who starts mixing sample starting from offset & when at end, it loops it
from beginning (if loops are needed).

So, opinions?

Kovacs

I totally agree- it is simple. But does sdl_mixer support this, as i don’t
want to write a mixer myself.

SDL_mixer doesn’t support this. Is it really necessary to save and restore
the sound at exactly the same offset? Most games just save the game state
and if there were sounds playing, either discard them (action sounds) or
replay them (cinematic sequences, etc.)

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

Well, if you’re not making a game, this feature can be very handy (for
example, if you are working on some sort of SDL powered audio mixing studio
:wink:

Heck, you could even use something like this in a game, if anyone were ever
to start a game similar to MTV’s Muisc Generator ;-)On Sunday 23 September 2001 10:10, Sam Lantinga wrote:

I totally agree- it is simple. But does sdl_mixer support this, as i
don’t want to write a mixer myself.

SDL_mixer doesn’t support this. Is it really necessary to save and restore
the sound at exactly the same offset? Most games just save the game state
and if there were sounds playing, either discard them (action sounds) or
replay them (cinematic sequences, etc.)


Sam “Criswell” Hart <@Sam_Hart> AIM, Yahoo!:
Homepage: < http://www.geekcomix.com/snh/ >
PGP Info: < http://www.geekcomix.com/snh/contact/ >
Tux4Kids: < http://www.geekcomix.com/tux4kids/ >