[SDL] Creating audio samples via code

David Olofson david at olofson.net
Mon May 21 13:47:54 PDT 2007


On Monday 21 May 2007, Gabriel Gambetta wrote:
> Hi guys,
> 
> We're making a game where we need to time-stretch some audio
> samples. My idea is to load the samples normally and then create the
> stretched samples on the fly.

Have you considered the alternative of (re)synthesizing the sounds in 
real time? For example, use one or more looping waveforms along with 
and a bunch of "transient" samples that are actually synchronized 
with their respective visual events. If applicable, this method might 
be simpler, less CPU intensive and/or better sounding than true 
time-stretching.


> The thing is, I can't find anything in SDL_mixer.h to create and
> manipulate samples, except for the Mix_Chunk itself.

Mix_QuickLoad_RAW()...? However, if you want to do this on-the-fly, in 
real time, I don't think this is the API for you. You could probably 
abuse the Mix_Chunk as an intermediate buffer, allowing your 
time-stretch code to run with a bit more relaxed timing (ie not 
hard-sync'ed to the SDL audio buffer size), but you'd still need to 
stay in sync with the channel playback position.

Is there some reason why your algorithm cannot generate N samples at a 
time, on demand? I'm thinking about abusing the effect API. (See 
Mix_RegisterEffect etc.) Keep the channel alive using a dummy looping 
sample, or hack SDL_mixer to support channels starting with some sort 
of constantly playing generator "effect". Have your effect/generator 
ignore the normal input and produce output based on it's 
own "private" data.


> Second, would you be interested in a patch to add some functions to
> do that? Say, Mix_CreateChunk(), Mix_GetSample() and
> Mix_PutSample()?

Not sure if this is the correct type of interface for this - but then 
I'm not sure I understand what you actually want to do. :-)


> BTW, if someone has SDL_Mixer code to do time-stretching, you'll
> save me a lot of work ;)

Sorry, haven't played with time-stretching since the Amiga days, and 
no code at all for SDL_mixer...


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

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'


More information about the SDL mailing list