Exemplo n.º 1
0
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_loaded[music_position]) {
					/* Play a music file: */ 

					if ((music_sound[music_position]->flags&SOUND_SAMPLEFLAG_EOF)) {
						/* End of file: */ 
						if (music_loaded[music_position+1]) {
							music_position++;
						} /* if */ 
						Sound_Rewind(music_sound[music_position]);
					} else {
						/* In the middle of the file: */ 
						int decoded=0;
						Sint16 *ptr;

						Sound_SetBufferSize(music_sound[music_position], len-act);
						
						decoded=Sound_Decode(music_sound[music_position]);
						ptr=(Sint16 *)music_sound[music_position]->buffer;
						for(i=0;i<decoded;i+=2,ptr++,ptr2++) {
							*ptr2=((Sint32(*ptr)*Sint32(music_volume))/127);
						} /* for */ 
						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 */ 
Exemplo n.º 2
0
/*
** Audio stream callback.
**
** Called by the SDL background thread each time the audio buffer is ready
** to receive more data.  The function decodes PCM samples from the sound
** stream and copies them to the buffer for playback. When an end-of-stream
** is reached, closes the audio stream and exits cleanly.
*/
static void
stream_callback (void *userdata, Uint8 *buffer, int length)
{
  Sound_Sample *stream = (Sound_Sample *) userdata;

  Uint32 bytes_decoded;

  /* Decode a chunk of PCM samples from the sound stream. */
  Sound_SetBufferSize (stream, length);
  bytes_decoded = Sound_Decode (stream);

  if (bytes_decoded == 0) {
    /* End-of-stream reached; exit cleanly. */
    Sound_FreeSample (stream);
    Sound_Quit ();
    SDL_Quit ();
    exit (0);
  }

  /* Copy the decoded samples to the audio buffer. */
  memcpy (buffer, stream->buffer, length);
}