Prefer previous position used when loop position is not exactly at a command

This commit is contained in:
Zachary Hall 2024-10-15 13:01:07 -07:00
parent 1372b758ee
commit 187e6bab24

View file

@ -49,23 +49,33 @@ void ZsmBackend::load(const char *filename) {
pcm_data_offs = (loop_point[0] * 16) + pcm_offset; pcm_data_offs = (loop_point[0] * 16) + pcm_offset;
file->seek(music_data_start, SeekType::SET); file->seek(music_data_start, SeekType::SET);
this->loop_point = std::max(this->loop_point, (uint32_t)music_data_start); this->loop_point = std::max(this->loop_point, (uint32_t)music_data_start);
double prev_time = 0.0;
double time = 0.0; double time = 0.0;
double tmpDelayTicks = 0.0; double tmpDelayTicks = 0.0;
loop_pos = -1.0; loop_pos = -1.0;
uint32_t prev_pos = music_data_start;
while (true) { while (true) {
tmpDelayTicks -= get_delay_per_frame(); tmpDelayTicks -= get_delay_per_frame();
if (tmpDelayTicks < 0.0) { 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(); 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) { if (cmd.id == ZsmEOF) {
break; break;
} else if (cmd.id == Delay) { } else if (cmd.id == Delay) {
time += ((double)cmd.delay) / ((double)(tick_rate)); time += ((double)cmd.delay) / ((double)(tick_rate));
tmpDelayTicks += cmd.delay; tmpDelayTicks += cmd.delay;
} }
prev_pos = file->get_pos();
prev_time = time;
} }
} }
if (this->loop_pos < 0.0) { if (this->loop_pos < 0.0) {