[SDL] Stereo files with SDL_mixer + SDL_sound

Santi Ontañón santi.ontanon at terra.es
Mon Jan 13 06:04:01 PST 2003


Hi,

I want to play a stereo OGG, WAV... files using SDL_sound + SDL_mixer. I 
use SDL_sound to decode the OGG file and SDL_mixer to mix it with other 
samples.

I've redefined the "musicplayer" playback in this way (assumes 16bit sound):


bool playing_music=true;
Sound_Sample *music_sample;

...

void myMusicPlayer(void *udata, Uint8 *stream, int len)
{
   int i,act=0;
   Sint16 *ptr2;

   if (stream!=0) {
      ptr2=(Sint16 *)stream;
      if (playing_music) {
         while(act<len) {
           if (music_sample!=0) {
              /* Play a music file: */
              if (music_sample->flags&SOUND_SAMPLEFLAG_EOF)) {
                 /* End of file: */
                 Sound_Rewind(music_sample);
              } else {
                 /* In the middle of the file: */
                 int decoded=0;
                 Sint16 *ptr;

                 Sound_SetBufferSize(music_sample, len-act);
                 decoded=Sound_Decode(music_sample);
                 ptr=(Sint16 *)music_sample->buffer;
                 for(i=0;i<decoded;i+=2,ptr++,ptr2++)
                    *ptr2=((Sint32(*ptr)*Sint32(music_volume))/127);

                 act+=decoded;
              } /* if */
           } else {
              /* No music file loaded: */
              for(i=act;i<len;i++) stream[i]=0;
              act=len;
           } /* if */
         } /* while */
      } else {
         /* No music to play: */
         for(i=0;i<len;i++) stream[i]=0;
      } /* if */
   } else {
      fprintf(stderr,"ERROR in myMusicPlayer(): null music stream!!\n");
   } /* if */
} /* myMusicPlayer */



But now, I want to extend it to be able to play stereo files, and I 
don't know how to do it, since the "Uint8 stream" is a single stream! 
How do I have to fill it with the right and left channel data?





More information about the SDL mailing list