From f092b4bd369e839738a38f47c0af333acaee0f41 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Tue, 15 Oct 2024 13:01:07 -0700 Subject: [PATCH] Prefer previous position used when loop position is not exactly at a command --- backends/playback/zsm/zsm_backend.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/backends/playback/zsm/zsm_backend.cpp b/backends/playback/zsm/zsm_backend.cpp index 3fd16c4..d5f7f5a 100644 --- a/backends/playback/zsm/zsm_backend.cpp +++ b/backends/playback/zsm/zsm_backend.cpp @@ -49,23 +49,33 @@ void ZsmBackend::load(const char *filename) { pcm_data_offs = (loop_point[0] * 16) + pcm_offset; file->seek(music_data_start, SeekType::SET); this->loop_point = std::max(this->loop_point, (uint32_t)music_data_start); + double prev_time = 0.0; double time = 0.0; double tmpDelayTicks = 0.0; loop_pos = -1.0; + uint32_t prev_pos = 0; while (true) { tmpDelayTicks -= get_delay_per_frame(); if (tmpDelayTicks < 0.0) { - if (file->get_pos() >= this->loop_point && this->loop_pos < 0) { - loop_pos = time; - this->loop_point = file->get_pos(); - } ZsmCommand cmd = get_command(); + size_t cur_pos = file->get_pos(); + if (cur_pos >= this->loop_point && this->loop_pos < 0) { + if (cur_pos > this->loop_point) { + this->loop_point = prev_pos; + loop_pos = prev_time; + } else { + loop_pos = time; + this->loop_point = cur_pos; + } + } if (cmd.id == ZsmEOF) { break; } else if (cmd.id == Delay) { time += ((double)cmd.delay) / ((double)(tick_rate)); tmpDelayTicks += cmd.delay; } + prev_pos = file->get_pos(); + prev_time = time; } } if (this->loop_pos < 0.0) {