Jump to content

  • Log In with Google      Sign In   
  • Create Account


C and SDL for audio - Crossfade doesnt work


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 TCShin   Members   -  Reputation: 101

Like
0Likes
Like

Posted 14 May 2012 - 11:45 PM

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
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

If someone can help me to understand why it doest not work

Sponsor:

#2 Wooh   Members   -  Reputation: 543

Like
0Likes
Like

Posted 15 May 2012 - 06:04 AM

((Sint16 *)stream)[j] = sample;
I don't see sample being used anywhere else.

Is sound1 and sound2 used for anything?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS