foxygit / fake-08-xbox360 Log in
commit 449db7454e30e614fe7bfac89867b8e0f387a97b
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Wed Mar 9 19:23:22 2022 -0700
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Wed Mar 9 19:23:22 2022 -0700

    Honor length limit for sfx
---
 source/Audio.cpp | 10 ++++++++--
 source/PicoRam.h |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index 3d943fe..84ea520 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -105,6 +105,12 @@ void Audio::api_sfx(int sfx, int channel, int offset){
         _audioState._sfxChannels[channel].phi = 0.f;
         _audioState._sfxChannels[channel].can_loop = true;
         _audioState._sfxChannels[channel].is_music = false;
+        _audioState._sfxChannels[channel].length = 32;
+
+        auto &sfxObj = _memory->sfx[sfx];
+		if(sfxObj.loopRangeStart != 0 && sfxObj.loopRangeEnd == 0){
+			_audioState._sfxChannels[channel].length  = std::min(_audioState._sfxChannels[channel].length , sfxObj.loopRangeStart);
+		}
         // Playing an instrument starting with the note C-2 and the
         // slide effect causes no noticeable pitch variation in PICO-8,
         // so I assume this is the default value for “previous key”.
@@ -360,7 +366,7 @@ int16_t Audio::getSampleForChannel(int channel){
         //volume all the way off. return silence, but make sure to set stuff
         _audioState._sfxChannels[channel].offset = next_offset;

-        if (next_offset >= 32.f){
+        if (next_offset >= _audioState._sfxChannels[channel].length) {
             _audioState._sfxChannels[channel].sfxId = -1;
         }
         else if (next_note_idx != note_idx){
@@ -443,7 +449,7 @@ int16_t Audio::getSampleForChannel(int channel){

     _audioState._sfxChannels[channel].offset = next_offset;

-    if (next_offset >= 32.f){
+    if (next_offset >= _audioState._sfxChannels[channel].length){
         _audioState._sfxChannels[channel].sfxId = -1;
     }
     else if (next_note_idx != note_idx){
diff --git a/source/PicoRam.h b/source/PicoRam.h
index a5d2eb3..0291edc 100644
--- a/source/PicoRam.h
+++ b/source/PicoRam.h
@@ -138,6 +138,7 @@ struct sfxChannel {
     bool is_music = false;
     int8_t prev_key = 0;
     float prev_vol = 0;
+    uint8_t length = 0;
 };

 struct audioState {