Hi,
i am currently doing a crossfade system with SDL but i got a problem with the fadeout.
This is my code for the callback function
Code:
int i, j;
/* Loop that selects the audio slots which are running */
for (i = 0; i < NBR_OF_AUDIO_SLOTS; i++)
{
if (soundplayer_isplaying(&audio_slots[i]))
{
soundplayer * player = &audio_slots[i];
sound * toplay = player->played_sound;
/* Number of samples */
Uint32 nbSamples = len/sizeof(Sint16);
for (j = 0; j < nbSamples; j++)
{
Sint16 sound1 = ((Sint16 *)stream)[j];
/* Current sample or 0 if the sound is finished */
Sint16 sound2 = (player->pos < toplay->length) ? (*(Sint16 *)(toplay->data + player->pos)) : 0;
/* X is the number of sample for the crossfade, equal to 0 everytime i run a new sound */
if (x < 8191 )
{
/* Cosinus function for the crossfade */
double fadeout = cos( (x * M_PI) / ((nbSamples-1) *2) );
double fadein = 1 - cos( (x * M_PI) / ((nbSamples-1) *2) );
/* Fadeout application */
Uint8 volume = (double)((double)global_volume * fadeout);
sound1 = sample_volume(sound1, volume);
/* Fade in application*/
volume = (double)((double)global_volume * fadein);
sound2 = sample_volume(sound2, volume);
/* Mix the 2 sounds */
sound2 = sample_mix(sound2, sound1);
x++;
}
else {
/* Crossfade is over, keep only the new sound */
sound2 = sample_volume(sound2, 255);
}
/* Add to the audio buffer */
((Sint16 *)stream)[j] = sample;
player->pos += sizeof(Sint16);
}
/* Update */
soundplayer_update(player);
} } }
So when i run 2 sounds one after the other, the first sound stop suddently and the second one fades in. I dont understand why the fadeout doesnt work :S