Thread: C and SDL for audio - Crossfade doesnt work

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    C and SDL for audio - Crossfade doesnt work

    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
    Last edited by TCShin; 05-14-2012 at 11:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesnt this work
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2006, 11:57 AM
  2. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 03:22 PM
  3. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  4. How come this doesnt work?
    By correlcj in forum C Programming
    Replies: 2
    Last Post: 07-11-2002, 06:36 PM
  5. Why doesnt this work?
    By dougaerb in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2001, 08:51 AM