Sam Lantinga - Fri Oct 2 07:15:35 PDT 2009
authorSam Lantinga <slouken@libsdl.org>
Fri, 02 Oct 2009 14:16:12 +0000
changeset 40773a92ea2c061
parent 406 b67f52365db0
child 408 fc6c4861e8ec
Sam Lantinga - Fri Oct 2 07:15:35 PDT 2009
* Implemented seamless looping for music playback
CHANGES
music.c
music_mad.c
music_mad.h
wavestream.c
wavestream.h
     1.1 --- a/CHANGES	Fri Oct 02 13:50:36 2009 +0000
     1.2 +++ b/CHANGES	Fri Oct 02 14:16:12 2009 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.9:
     1.5 +Sam Lantinga - Fri Oct  2 07:15:35 PDT 2009
     1.6 + * Implemented seamless looping for music playback
     1.7  Forrest Voight - 2009-06-13 20:31:38 PDT
     1.8   * ID3 files are now recognized as MP3 format
     1.9  Austen Dicken - Tue Feb 26 23:28:27 PST 2008
     2.1 --- a/music.c	Fri Oct 02 13:50:36 2009 +0000
     2.2 +++ b/music.c	Fri Oct 02 14:16:12 2009 +0000
     2.3 @@ -242,6 +242,8 @@
     2.4  /* Mixing function */
     2.5  void music_mixer(void *udata, Uint8 *stream, int len)
     2.6  {
     2.7 +	int left = 0;
     2.8 +
     2.9  	if ( music_playing && music_active ) {
    2.10  		/* Handle fading */
    2.11  		if ( music_playing->fading != MIX_NO_FADING ) {
    2.12 @@ -280,7 +282,7 @@
    2.13  #endif
    2.14  #ifdef WAV_MUSIC
    2.15  			case MUS_WAV:
    2.16 -				WAVStream_PlaySome(stream, len);
    2.17 +				left = WAVStream_PlaySome(stream, len);
    2.18  				break;
    2.19  #endif
    2.20  #if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
    2.21 @@ -372,27 +374,22 @@
    2.22  #ifdef OGG_MUSIC
    2.23  			case MUS_OGG:
    2.24  				
    2.25 -				len = OGG_playAudio(music_playing->data.ogg, stream, len);
    2.26 -				if (len > 0 && music_halt_or_loop())
    2.27 -					OGG_playAudio(music_playing->data.ogg, stream, len);
    2.28 -			
    2.29 +				left = OGG_playAudio(music_playing->data.ogg, stream, len);
    2.30  				break;
    2.31  #endif
    2.32  #ifdef FLAC_MUSIC
    2.33  			case MUS_FLAC:
    2.34 -				len = FLAC_playAudio(music_playing->data.flac, stream, len);
    2.35 -				if (len > 0 && music_halt_or_loop())
    2.36 -					FLAC_playAudio(music_playing->data.flac, stream, len);
    2.37 +				left = FLAC_playAudio(music_playing->data.flac, stream, len);
    2.38  				break;
    2.39  #endif
    2.40  #ifdef MP3_MUSIC
    2.41  			case MUS_MP3:
    2.42 -				smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len);
    2.43 +				left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
    2.44  				break;
    2.45  #endif
    2.46  #ifdef MP3_MAD_MUSIC
    2.47  			case MUS_MP3_MAD:
    2.48 -				mad_getSamples(music_playing->data.mp3_mad, stream, len);
    2.49 +				left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
    2.50  				break;
    2.51  #endif
    2.52  			default:
    2.53 @@ -400,6 +397,11 @@
    2.54  				break;
    2.55  		}
    2.56  	}
    2.57 +
    2.58 +	/* Handle seamless music looping */
    2.59 +	if (left > 0 && left < len && music_halt_or_loop()) {
    2.60 +		music_mixer(udata, stream+(len-left), left);
    2.61 +	}
    2.62  }
    2.63  
    2.64  /* Initialize the music players with a certain desired audio format */
     3.1 --- a/music_mad.c	Fri Oct 02 13:50:36 2009 +0000
     3.2 +++ b/music_mad.c	Fri Oct 02 14:16:12 2009 +0000
     3.3 @@ -233,7 +233,7 @@
     3.4    /*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
     3.5  }
     3.6  
     3.7 -void
     3.8 +int
     3.9  mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
    3.10    int bytes_remaining;
    3.11    int num_bytes;
    3.12 @@ -258,7 +258,7 @@
    3.13  			 end-of-file.  Stop. */
    3.14  		  memset(out, 0, bytes_remaining);
    3.15  		  mp3_mad->status &= ~MS_playing;
    3.16 -		  return;
    3.17 +		  return bytes_remaining;
    3.18  		}
    3.19  	  } else {
    3.20  		decode_frame(mp3_mad);
    3.21 @@ -289,6 +289,7 @@
    3.22  	mp3_mad->output_begin += num_bytes;
    3.23  	bytes_remaining -= num_bytes;
    3.24    }
    3.25 +  return 0;
    3.26  }
    3.27  
    3.28  void
     4.1 --- a/music_mad.h	Fri Oct 02 13:50:36 2009 +0000
     4.2 +++ b/music_mad.h	Fri Oct 02 14:16:12 2009 +0000
     4.3 @@ -67,7 +67,7 @@
     4.4  void mad_stop(mad_data *mp3_mad);
     4.5  int mad_isPlaying(mad_data *mp3_mad);
     4.6  
     4.7 -void mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
     4.8 +int mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
     4.9  void mad_seek(mad_data *mp3_mad, double position);
    4.10  void mad_setVolume(mad_data *mp3_mad, int volume);
    4.11  
     5.1 --- a/wavestream.c	Fri Oct 02 13:50:36 2009 +0000
     5.2 +++ b/wavestream.c	Fri Oct 02 14:16:12 2009 +0000
     5.3 @@ -174,9 +174,10 @@
     5.4  }
     5.5  
     5.6  /* Play some of a stream previously started with WAVStream_Start() */
     5.7 -void WAVStream_PlaySome(Uint8 *stream, int len)
     5.8 +int WAVStream_PlaySome(Uint8 *stream, int len)
     5.9  {
    5.10  	long pos;
    5.11 +	int left = 0;
    5.12  
    5.13  	if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
    5.14  		if ( music->cvt.needed ) {
    5.15 @@ -196,7 +197,9 @@
    5.16  				music->cvt.len = original_len;
    5.17  			}
    5.18  			if ( (music->stop - pos) < original_len ) {
    5.19 -				original_len = (music->stop - pos);
    5.20 +				left = (original_len - (music->stop - pos));
    5.21 +				original_len -= left;
    5.22 +				left = (int)((double)left*music->cvt.len_ratio);
    5.23  			}
    5.24  			original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
    5.25  			/* At least at the time of writing, SDL_ConvertAudio()
    5.26 @@ -214,7 +217,8 @@
    5.27  		} else {
    5.28  			Uint8 *data;
    5.29  			if ( (music->stop - pos) < len ) {
    5.30 -				len = (music->stop - pos);
    5.31 +				left = (len - (music->stop - pos));
    5.32 +				len -= left;
    5.33  			}
    5.34  			data = SDL_stack_alloc(Uint8, len);
    5.35  			if (data)
    5.36 @@ -225,6 +229,7 @@
    5.37  			}	
    5.38  		}
    5.39  	}
    5.40 +	return left;
    5.41  }
    5.42  
    5.43  /* Stop playback of a stream previously started with WAVStream_Start() */
     6.1 --- a/wavestream.h	Fri Oct 02 13:50:36 2009 +0000
     6.2 +++ b/wavestream.h	Fri Oct 02 14:16:12 2009 +0000
     6.3 @@ -52,7 +52,7 @@
     6.4  extern void WAVStream_Start(WAVStream *wave);
     6.5  
     6.6  /* Play some of a stream previously started with WAVStream_Start() */
     6.7 -extern void WAVStream_PlaySome(Uint8 *stream, int len);
     6.8 +extern int WAVStream_PlaySome(Uint8 *stream, int len);
     6.9  
    6.10  /* Stop playback of a stream previously started with WAVStream_Start() */
    6.11  extern void WAVStream_Stop(void);