How to trigger a timed fade-out with SDL_mixer?

This sounds extremely simple, and yet I can’t figure it out: I have to
play a sound (a Mix_Chunk*) with fade-in and fade-out. The fade-in
needs to be, say, 1000ms at the start of the sound. OK, this part is easy:

Mix_FadeInChannel(channel, chunk, 0, 1000);

The part I can’t figure out is: The fade-out needs to also be 1000ms, at
the end of the sound. But, how do I do this? If I do:

Mix_FadeOutChannel(channel, 1000);

The channel starts to fade out immediately rather than 1000 milliseconds
before the end :-/ Any pointers on how to solve this?

[…]

The part I can’t figure out is: The fade-out needs to also be 1000ms, at
the end of the sound. But, how do I do this? If I do:

Mix_FadeOutChannel(channel, 1000);

The channel starts to fade out immediately rather than 1000 milliseconds
before the end :-/ Any pointers on how to solve this?

What you’re looking for is an envelope (or “automation”) linked to the sound
itself, whereas the Mix_*() calls are mostly of the “do this now” kind.

I think you’ll need to hack SDL_mixer a little to do this. There is an effect
API, but it doesn’t seem to provid the information you need, like length and
current position of the currently playing sound.

Is this a looping sound? Otherwise, the easiest way would be to just apply the
fades “destructively” in an audio editor.On Tuesday 05 October 2010, at 02.10.57, Nikos Chantziaras wrote:


//David Olofson - Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://kobodeluxe.com http://audiality.org |
| http://eel.olofson.net http://zeespace.net http://reologica.se |
’---------------------------------------------------------------------’

If you know the sound length maybe you could setup a timer at the time
you start playing the sound to (sound length) - (fadeout length) and
fire the fadeout when the timer triggers?

Just my 2 cents, don’t know how well this would work…On 05/10/2010 12:14, David Olofson wrote:

On Tuesday 05 October 2010, at 02.10.57, Nikos Chantziaras wrote:
[…]

The part I can’t figure out is: The fade-out needs to also be 1000ms, at
the end of the sound. But, how do I do this? If I do:

Mix_FadeOutChannel(channel, 1000);

The channel starts to fade out immediately rather than 1000 milliseconds
before the end :-/ Any pointers on how to solve this?
What you’re looking for is an envelope (or “automation”) linked to the sound
itself, whereas the Mix_*() calls are mostly of the “do this now” kind.

I think you’ll need to hack SDL_mixer a little to do this. There is an effect
API, but it doesn’t seem to provid the information you need, like length and
current position of the currently playing sound.

Is this a looping sound? Otherwise, the easiest way would be to just apply the
fades “destructively” in an audio editor.

[…]

[…]

The part I can’t figure out is: The fade-out needs to also be 1000ms, at
the end of the sound. But, how do I do this? If I do:

Mix_FadeOutChannel(channel, 1000);

The channel starts to fade out immediately rather than 1000 milliseconds
before the end :-/ Any pointers on how to solve this?
[…]
If you know the sound length maybe you could setup a timer at the time
you start playing the sound to (sound length) - (fadeout length) and
fire the fadeout when the timer triggers?

Just my 2 cents, don’t know how well this would work…

This is what I ended up doing. It’s a bit hairy, but it works.On 10/06/2010 02:26 AM, Andre Leiradella wrote:

On Tuesday 05 October 2010, at 02.10.57, Nikos Chantziaras wrote: