SDL_Mixer sound effects

Hello all,

I’m trying to figure out how to play a sample so the sound appears to
move from the left to the right relative to the listener.

First I register the sound effect function and the channel to use.
right?

Mix_RegisterEffect(1, AUD_Effects, NULL, NULL);

Here’s the function itself.

static void
AUD_Effects
( int chan, void *stream, int len, void *udata )
{
Mix_SetPanning( 1, 90, 100 );
}

And. inside my sample object, is my play function.
if( (m_iChannel = Mix_PlayChannelTimed( iChannel, m_pChunk, iLoops,
iTimeLimit )) < 0 )

m_iChannel being passed in as 1.

So.It didn’t magically go left to right which was a huge disappointment
:stuck_out_tongue: So I tried calling the Mix_SetPanning inside a for loop with the
left and right values as variable. lowering the left and
Increasing the right. That was ineffective.and complained a great deal.

I’ve had a few passing notions as to how to accomplish this. but I’m
still fairly new to programming. Googling for examples on sample
modification for effects and stuff turned up little.
I also looked at OpenAl. but it seemed a bit too much for my purposes. I
just wanted some simple panning in all directions on a 2d plane.

I appreciate any advice, pointers to good resources or an outright
solution!

Thanks!

-Kevin

[…]

static void
AUD_Effects
( int chan, void *stream, int len, void *udata )
{
Mix_SetPanning( 1, 90, 100 );
}

I have no experience with SDL_mixer, but I’m quite sure these effect
callbacks are meant to process audio; not make various mixer API
calls. Pretty much like the SDL audio callback, that is.

[…]

I appreciate any advice, pointers to good resources or an outright
solution!

It sounds like you want to pan during playback, rather than just
setting the position for each waveform. Don’t know if SDL mixer does
this, but if it does “click free” volume control, panning isn’t far
off…

If you have to do it as a DSP effect, it’s rather simple in theory
(just scale the samples), but you have to ramp the scaling factors to
avoid clicks and pops. For 8 and 16 bit audio, fixed point works
fine. Inner loop:

int lvol, rvol;		/* Start levels (16:16 fixed point) */
int dlvol, drvol;	/* Per-sample delta (16:16 fixed point) */
samples >>= 1;		/* Stereo! */
for(s = 0; s < samples; ++s)
{
	buffer[i] = buffer[i] * (lvol>>16) >> 16;
	buffer[i+1] = buffer[i+1] * (rvol>>16) >> 16;
	lvol += dlvol;
	rvol += drvol;
}

(Note that this assumes stereo in/stereo out. It’s actually a
balance control, rather than pan, but if you have centered mono
input, there’s no difference.)

lvol and rvol can be calculated in various ways. The simplest way:

rvol = (pan + 65536) >> 1;
lvol = 65536 - rvol;

(pan is 16:16 fixed point; -1.0 = hard left, 0.0 = center, 1.0 = hard
right.)

Finally, to ramp from the current value to the new value over one
buffer:

dlvol = (new_lvol - lvol) / samples;
drvol = (new_rvol - rvol) / samples;

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

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -’
http://olofson.nethttp://www.reologica.se —On Saturday 28 December 2002 13.13, proteus wrote:

Mix_RegisterEffect(1, AUD_Effects, NULL, NULL);

Here’s the function itself.

static void
AUD_Effects
( int chan, void *stream, int len, void *udata )
{
Mix_SetPanning( 1, 90, 100 );
}

You register an effect to alter the audio stream before sending it to the
audio device. Mix_SetPanning() registers such an effect. So you should:

Mix_PlayChannel(1, whatever…);
Mix_SetPanning(1, 90, 100);
…do some stuff until you want to change the panning…
Mix_SetPanning(1, newLeft, newRight);

You don’t need to play with callbacks and registering effects. In the case
of SetPanning, this is all done behind the scenes for you.

–ryan.

Well, I wanted to make one sample, seem to move from left to right in
one playback. So I can’t just call the set panning over and over right?

Can I modify the stream in my AUD_Effects function so that the volume is
changed for each “side” of the sound? ( I think this is what David
Olofson was getting at in his reply.> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Ryan C. Gordon
Sent: Saturday, December 28, 2002 6:54 PM
To: sdl at libsdl.org
Subject: Re: [SDL] SDL_Mixer sound effects

Mix_RegisterEffect(1, AUD_Effects, NULL, NULL);

Here’s the function itself.

static void
AUD_Effects
( int chan, void *stream, int len, void *udata )
{
Mix_SetPanning( 1, 90, 100 );
}

You register an effect to alter the audio stream before sending it to
the
audio device. Mix_SetPanning() registers such an effect. So you should:

Mix_PlayChannel(1, whatever…);
Mix_SetPanning(1, 90, 100);
…do some stuff until you want to change the panning…
Mix_SetPanning(1, newLeft, newRight);

You don’t need to play with callbacks and registering effects. In the
case
of SetPanning, this is all done behind the scenes for you.

–ryan.

Well, I wanted to make one sample, seem to move from left to right in
one playback. So I can’t just call the set panning over and over
right?

Can I modify the stream in my AUD_Effects function so that the volume
is
changed for each “side” of the sound? ( I think this is what David
Olofson was getting at in his reply.

I think I found what I’ve been looking for in the playwave.c that came
with the source for SDL_Mixer. After compiling it with the panning
update defined… it did exactly what I’ve been trying to do… But I’m
sort of confused as to where still_playing() is? I ran a search through
all source files in sdl / sdl_mixer with no luck. Anyone else with the
sdl_mixer source distribution know what this is?

Thanks David O. and Ryan G. again for your insight :slight_smile:

(god I hope I don’t come back stumped again )

-Kevin

Er…I found it. Don’t bother looking! My mistake being that I searched
for “still_playing()” instead of “still_playing”.

Ok! Honest, I’ll stop spamming the list now :/> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
proteus
Sent: Saturday, December 28, 2002 11:08 PM
To: sdl at libsdl.org
Subject: RE: [SDL] SDL_Mixer sound effects

Well, I wanted to make one sample, seem to move from left to right in
one playback. So I can’t just call the set panning over and over
right?

Can I modify the stream in my AUD_Effects function so that the volume
is
changed for each “side” of the sound? ( I think this is what David
Olofson was getting at in his reply.

I think I found what I’ve been looking for in the playwave.c that came
with the source for SDL_Mixer. After compiling it with the panning
update defined… it did exactly what I’ve been trying to do… But I’m
sort of confused as to where still_playing() is? I ran a search through
all source files in sdl / sdl_mixer with no luck. Anyone else with the
sdl_mixer source distribution know what this is?

Thanks David O. and Ryan G. again for your insight :slight_smile:

(god I hope I don’t come back stumped again )

-Kevin


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Ok! Honest, I’ll stop spamming the list now :confused:

Glad you got it working! :slight_smile:

–ryan.