Real-time sound processing with SDL_mixer

Hi!
For a project I need to implement an application that read a wav file and play a
filtered version of this in real-time. The parameters of the filter can be
modified in real-time.
The filter is composed of two blocks, the first block is a delay line, and the
two channel (left and right) must be delayed of differents values.
For example if delay[0] is the delay for the left channel, and delay[1] is the
delay for the right channel, I must do this:
Shift the left channel data of delay[0] values and then add is original values,
shift the right channel data of delay[1] values and then add is original values.
Note that I don’t deletes the original values, but I shift them.
If I do this in “no real-time”:

  • Read wav file
  • Shift left and right channal
  • Save the results as a new wav file
    With this “no real-time” approach, it’s run.
    But for my project I need to change the delay value on real-time, and I need to
    see the results in real-time.
    It’s possibile to do this with SDL_mixer and with Mix_RegisterEffect() function?
    I can call a my filter, where I change dinamically the delay line?
    Thanks.

Hi!
For a project I need to implement an application that read a wav
file and play a filtered version of this in real-time. The
parameters of the filter can be modified in real-time.

What parts actually have to be done in “real time”? Can you read the
whole file first, or do you have to stream it while playing?

If you need to stream, you should probably set up a dedicated thread
for the reading. That way you can set up whatever buffering you need
to avoid “hickups” caused by the file system, and still have small
buffers (low latency) on the filtering part.

[…]

But for my project I need to change the delay value on real-time,
and I need to see the results in real-time.
It’s possibile to do this with SDL_mixer and with
Mix_RegisterEffect() function?

Yes, I think so… I’m not very familiar with SDL_mixer, but it seems
like the *Effect API was designed for things like this.

I can call a my filter, where I change dinamically the delay line?

If there’s no control/parameter interface in the API, you’ll have to
implement your own. What’s important to keep in mind here is that the
audio processing is done in a different context from your main
program (usually a dedicated thread), so you need to ensure that any
transactions are thread/interrupt safe.

The easiest way is to call SDL_LockAudio(), mess around with whatever
parameters (shared variables) you like, and then call and
SDL_UnlockAudio() when you’re done. This will prevent the audio
processing (SDL_mixer, your effect plugin etc) from running right
while you’re changing parameters. Just keep in mind that if you hold
on to the audio lock for too long, there will be glitches in the
output.

//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 --'On Wednesday 12 November 2008, enigma wrote:

The effect that I would obtain is “spatialization”. Immagine that I’ve a
imaginary circumference around the head, I must move the source around this
circumference and the sound must move from left to right ear or from right to
left ear.
I must implement that with a my filter (delay line).
So, I load a wav file and I play them on loop, when position on circumference is
changed, I must change the dalay on the two channels.
But I don’t understand if I can implement that with SDL_mixer functions, and how.
Thanks.

That should be doable, I think. Any “normal” effect plugin API,
including the one in SDL_mixer, will support that kind of processing.
You insert a callback function that then regularly gets a buffer of
audio data that it’s supposed to process.

Can’t really help you with specific API details as I’ve never used
SDL_mixer in a project of my own, but you might find this useful:
http://jcatki.no-ip.org:8080/SDL_mixer/SDL_mixer.html#SEC69

//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 --'On Wednesday 12 November 2008, enigma wrote:

The effect that I would obtain is “spatialization”. Immagine that
I’ve a imaginary circumference around the head, I must move the
source around this circumference and the sound must move from left
to right ear or from right to left ear.
I must implement that with a my filter (delay line).
So, I load a wav file and I play them on loop, when position on
circumference is changed, I must change the dalay on the two
channels.
But I don’t understand if I can implement that with SDL_mixer
functions, and how.