Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 39827e2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -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
Expand Down
22 changes: 12 additions & 10 deletions music.c
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 3 additions & 2 deletions music_mad.c
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion music_mad.h
Expand Up @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions wavestream.c
Expand Up @@ -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 ) {
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -225,6 +229,7 @@ void WAVStream_PlaySome(Uint8 *stream, int len)
}
}
}
return left;
}

/* Stop playback of a stream previously started with WAVStream_Start() */
Expand Down
2 changes: 1 addition & 1 deletion wavestream.h
Expand Up @@ -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);
Expand Down

0 comments on commit 39827e2

Please sign in to comment.