From 2c0a5339482cda97690917d4922f5b672e67c231 Mon Sep 17 00:00:00 2001 From: Jeffery Myers <jeffm2501@gmail.com> Date: Mon, 22 Mar 2021 12:36:13 -0700 Subject: [PATCH] [AUDIO] Music Looping enhancements (#1665) * Add loop functions for music files. Tell xm tracker to loop tracker when loop state changes. Don't let looped xm tracker streams restart, they are infinite Use modulo to make time tracker for xm looped streams work correctly. * Remove loop functions, set XM loop in update based on flag. Formatting cleanups. Co-authored-by: Jeffery Myers <JefMyers@blizzard.com> --- src/raudio.c | 25 ++++++++++++++++++++----- src/raylib.h | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/raudio.c b/src/raudio.c index 1f7f3531f..75cd69604 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -1575,7 +1575,11 @@ void StopMusicStream(Music music) // Update (re-fill) music buffers if data already processed void UpdateMusicStream(Music music) { - if (music.stream.buffer == NULL) return; + if (music.stream.buffer == NULL) + return; + + if (music.ctxType == MUSIC_MODULE_XM) + jar_xm_set_max_loop_count(music.ctxData, music.looping ? 0 : 1); bool streamEnding = false; @@ -1590,6 +1594,8 @@ void UpdateMusicStream(Music music) //ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(music.stream.buffer->dsp.formatConverterIn.config.formatIn)*music.stream.buffer->dsp.formatConverterIn.config.channels; int sampleLeft = music.sampleCount - (music.stream.buffer->totalFramesProcessed*music.stream.channels); + if (music.ctxType == MUSIC_MODULE_XM && music.looping) sampleLeft = subBufferSizeInFrames*4; + while (IsAudioStreamProcessed(music.stream)) { if ((sampleLeft/music.stream.channels) >= subBufferSizeInFrames) samplesCount = subBufferSizeInFrames*music.stream.channels; @@ -1649,10 +1655,10 @@ void UpdateMusicStream(Music music) UpdateAudioStream(music.stream, pcm, samplesCount); - if ((music.ctxType == MUSIC_MODULE_XM) || (music.ctxType == MUSIC_MODULE_MOD)) + if ((music.ctxType == MUSIC_MODULE_XM) || music.ctxType == MUSIC_MODULE_MOD) { - if (samplesCount > 1) sampleLeft -= samplesCount/2; - else sampleLeft -= samplesCount; + if (samplesCount > 1) sampleLeft -= samplesCount / 2; + else sampleLeft -= samplesCount; } else sampleLeft -= samplesCount; @@ -1711,8 +1717,17 @@ float GetMusicTimeLength(Music music) // Get current music time played (in seconds) float GetMusicTimePlayed(Music music) { +#if defined(SUPPORT_FILEFORMAT_XM) + if (music.ctxType == MUSIC_MODULE_XM) + { + uint64_t samples = 0; + jar_xm_get_position(music.ctxData, NULL, NULL, NULL, &samples); + samples = samples % (music.sampleCount); + + return (float)(samples) / (music.stream.sampleRate * music.stream.channels); + } +#endif float secondsPlayed = 0.0f; - if (music.stream.buffer != NULL) { //ma_uint32 frameSizeInBytes = ma_get_bytes_per_sample(music.stream.buffer->dsp.formatConverterIn.config.formatIn)*music.stream.buffer->dsp.formatConverterIn.config.channels; diff --git a/src/raylib.h b/src/raylib.h index 4b493b31b..e4811a4ba 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1489,9 +1489,9 @@ RLAPI void UpdateMusicStream(Music music); // Updates RLAPI void StopMusicStream(Music music); // Stop music playing RLAPI void PauseMusicStream(Music music); // Pause music playing RLAPI void ResumeMusicStream(Music music); // Resume playing paused music -RLAPI bool IsMusicPlaying(Music music); // Check if music is playing RLAPI void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level) RLAPI void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level) +RLAPI void SetMusicLooping(Music *music, bool loop); // Set the music to loop or not RLAPI float GetMusicTimeLength(Music music); // Get music time length (in seconds) RLAPI float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)