Fix looping for ZSM files

This commit is contained in:
Zachary Hall 2024-10-15 12:23:47 -07:00
parent e35316fc58
commit 1372b758ee
3 changed files with 17 additions and 6 deletions

View file

@ -40,7 +40,7 @@
</TabInfo>
<TabInfo>
<wxString Value="/home/catmeow/neko-player/backends/playback/zsm/zsm_backend.cpp" Name="FileName"/>
<int Value="0" Name="FirstVisibleLine"/>
<int Value="228" Name="FirstVisibleLine"/>
<int Value="0" Name="CurrentLine"/>
<wxArrayString Name="Bookmarks"/>
<IntVector Name="CollapsedFolds"/>
@ -131,8 +131,8 @@
</TabInfo>
<TabInfo>
<wxString Value="/home/catmeow/neko-player/backends/playback/zsm/zsm_backend.hpp" Name="FileName"/>
<int Value="54" Name="FirstVisibleLine"/>
<int Value="74" Name="CurrentLine"/>
<int Value="60" Name="FirstVisibleLine"/>
<int Value="77" Name="CurrentLine"/>
<wxArrayString Name="Bookmarks"/>
<IntVector Name="CollapsedFolds"/>
</TabInfo>

View file

@ -1,4 +1,5 @@
#include "zsm_backend.hpp"
#include <algorithm>
extern "C" {
#include "x16emu/glue.h"
#include "x16emu/vera_pcm.h"
@ -47,12 +48,17 @@ void ZsmBackend::load(const char *filename) {
pcm_offset++;
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 time = 0.0;
double tmpDelayTicks = 0.0;
loop_pos = -1.0;
while (true) {
tmpDelayTicks -= get_delay_per_frame();
if (tmpDelayTicks < 0.0) {
if (file->get_pos() == this->loop_point) loop_pos = time;
if (file->get_pos() >= this->loop_point && this->loop_pos < 0) {
loop_pos = time;
this->loop_point = file->get_pos();
}
ZsmCommand cmd = get_command();
if (cmd.id == ZsmEOF) {
break;
@ -62,6 +68,10 @@ void ZsmBackend::load(const char *filename) {
}
}
}
if (this->loop_pos < 0.0) {
this->loop_pos = 0.0;
this->loop_point = music_data_start;
}
length = time;
music_data_len = file->get_pos();
switch_stream(0);
@ -101,7 +111,7 @@ void ZsmBackend::tick(bool step) {
case ZsmEOF: {
if (step) {
file->seek(this->loop_point, SeekType::SET);
this->position = this->loop_pos;
this->position = loop_pos;
} else {
throw std::exception();
}
@ -352,7 +362,7 @@ void ZsmBackend::seek_internal(double position, bool loop) {
return;
}
}
size_t samples = (this->position - position) * PSG_FREQ;
size_t samples = std::min((size_t)((this->position - position) * PSG_FREQ), audio_buf.size());
while (samples--) {
audio_buf.pop();
}

View file

@ -72,6 +72,7 @@ class ZsmBackend : public PlaybackBackend {
uint8_t ym_data[256];
uint32_t loop_rem;
uint32_t pcm_data_offs;
uint8_t pcm_data_instruments;
uint32_t loop;
bool islooped;
uint32_t remain;