Skip to content
Permalink
Browse files

Sam Lantinga - Fri Oct 2 07:15:35 PDT 2009

 * Implemented seamless looping for music playback
  • Loading branch information
slouken committed Oct 2, 2009
1 parent 041e3be commit 39827e25b35a526459bb5294389c892b32acd3a3
Showing with 27 additions and 17 deletions.
  1. +2 −0 CHANGES
  2. +12 −10 music.c
  3. +3 −2 music_mad.c
  4. +1 −1 music_mad.h
  5. +8 −3 wavestream.c
  6. +1 −1 wavestream.h
@@ -1,4 +1,6 @@
1.2.9:
Sam Lantinga - Fri Oct 2 07:15:35 PDT 2009
* Implemented seamless looping for music playback
Forrest Voight - 2009-06-13 20:31:38 PDT
* ID3 files are now recognized as MP3 format
Austen Dicken - Tue Feb 26 23:28:27 PST 2008
22 music.c
@@ -242,6 +242,8 @@ static int music_halt_or_loop (void)
/* Mixing function */
void music_mixer(void *udata, Uint8 *stream, int len)
{
int left = 0;

if ( music_playing && music_active ) {
/* Handle fading */
if ( music_playing->fading != MIX_NO_FADING ) {
@@ -280,7 +282,7 @@ void music_mixer(void *udata, Uint8 *stream, int len)
#endif
#ifdef WAV_MUSIC
case MUS_WAV:
WAVStream_PlaySome(stream, len);
left = WAVStream_PlaySome(stream, len);
break;
#endif
#if defined(MOD_MUSIC) || defined(LIBMIKMOD_MUSIC)
@@ -372,34 +374,34 @@ void music_mixer(void *udata, Uint8 *stream, int len)
#ifdef OGG_MUSIC
case MUS_OGG:

len = OGG_playAudio(music_playing->data.ogg, stream, len);
if (len > 0 && music_halt_or_loop())
OGG_playAudio(music_playing->data.ogg, stream, len);

left = OGG_playAudio(music_playing->data.ogg, stream, len);
break;
#endif
#ifdef FLAC_MUSIC
case MUS_FLAC:
len = FLAC_playAudio(music_playing->data.flac, stream, len);
if (len > 0 && music_halt_or_loop())
FLAC_playAudio(music_playing->data.flac, stream, len);
left = FLAC_playAudio(music_playing->data.flac, stream, len);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len);
left = (len - smpeg.SMPEG_playAudio(music_playing->data.mp3, stream, len));
break;
#endif
#ifdef MP3_MAD_MUSIC
case MUS_MP3_MAD:
mad_getSamples(music_playing->data.mp3_mad, stream, len);
left = mad_getSamples(music_playing->data.mp3_mad, stream, len);
break;
#endif
default:
/* Unknown music type?? */
break;
}
}

/* Handle seamless music looping */
if (left > 0 && left < len && music_halt_or_loop()) {
music_mixer(udata, stream+(len-left), left);
}
}

/* Initialize the music players with a certain desired audio format */
@@ -233,7 +233,7 @@ decode_frame(mad_data *mp3_mad) {
/*assert(mp3_mad->output_end <= MAD_OUTPUT_BUFFER_SIZE);*/
}

void
int
mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
int bytes_remaining;
int num_bytes;
@@ -258,7 +258,7 @@ mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
end-of-file. Stop. */
memset(out, 0, bytes_remaining);
mp3_mad->status &= ~MS_playing;
return;
return bytes_remaining;
}
} else {
decode_frame(mp3_mad);
@@ -289,6 +289,7 @@ mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len) {
mp3_mad->output_begin += num_bytes;
bytes_remaining -= num_bytes;
}
return 0;
}

void
@@ -67,7 +67,7 @@ void mad_start(mad_data *mp3_mad);
void mad_stop(mad_data *mp3_mad);
int mad_isPlaying(mad_data *mp3_mad);

void mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
int mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
void mad_seek(mad_data *mp3_mad, double position);
void mad_setVolume(mad_data *mp3_mad, int volume);

@@ -174,9 +174,10 @@ void WAVStream_Start(WAVStream *wave)
}

/* Play some of a stream previously started with WAVStream_Start() */
void WAVStream_PlaySome(Uint8 *stream, int len)
int WAVStream_PlaySome(Uint8 *stream, int len)
{
long pos;
int left = 0;

if ( music && ((pos=SDL_RWtell(music->rw)) < music->stop) ) {
if ( music->cvt.needed ) {
@@ -196,7 +197,9 @@ void WAVStream_PlaySome(Uint8 *stream, int len)
music->cvt.len = original_len;
}
if ( (music->stop - pos) < original_len ) {
original_len = (music->stop - pos);
left = (original_len - (music->stop - pos));
original_len -= left;
left = (int)((double)left*music->cvt.len_ratio);
}
original_len = SDL_RWread(music->rw, music->cvt.buf,1,original_len);
/* At least at the time of writing, SDL_ConvertAudio()
@@ -214,7 +217,8 @@ void WAVStream_PlaySome(Uint8 *stream, int len)
} else {
Uint8 *data;
if ( (music->stop - pos) < len ) {
len = (music->stop - pos);
left = (len - (music->stop - pos));
len -= left;
}
data = SDL_stack_alloc(Uint8, len);
if (data)
@@ -225,6 +229,7 @@ void WAVStream_PlaySome(Uint8 *stream, int len)
}
}
}
return left;
}

/* Stop playback of a stream previously started with WAVStream_Start() */
@@ -52,7 +52,7 @@ extern WAVStream *WAVStream_LoadSong_RW(SDL_RWops *rw, const char *magic);
extern void WAVStream_Start(WAVStream *wave);

/* Play some of a stream previously started with WAVStream_Start() */
extern void WAVStream_PlaySome(Uint8 *stream, int len);
extern int WAVStream_PlaySome(Uint8 *stream, int len);

/* Stop playback of a stream previously started with WAVStream_Start() */
extern void WAVStream_Stop(void);

0 comments on commit 39827e2

Please sign in to comment.