diff --git a/.codelite/neko-player.session b/.codelite/neko-player.session
index 36f76f3..ccaef38 100644
--- a/.codelite/neko-player.session
+++ b/.codelite/neko-player.session
@@ -40,7 +40,7 @@
-
+
@@ -131,8 +131,8 @@
-
-
+
+
diff --git a/backends/playback/zsm/zsm_backend.cpp b/backends/playback/zsm/zsm_backend.cpp
index 84dc143..3fd16c4 100644
--- a/backends/playback/zsm/zsm_backend.cpp
+++ b/backends/playback/zsm/zsm_backend.cpp
@@ -1,4 +1,5 @@
#include "zsm_backend.hpp"
+#include
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();
}
diff --git a/backends/playback/zsm/zsm_backend.hpp b/backends/playback/zsm/zsm_backend.hpp
index 43c17ca..64148a0 100644
--- a/backends/playback/zsm/zsm_backend.hpp
+++ b/backends/playback/zsm/zsm_backend.hpp
@@ -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;