diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 52ffaa649..422360861 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -836,7 +836,11 @@ bool DivEngine::init(String outName) { } } - sf_writef_short(outFile,ilBuffer,got.bufsize); + if (!remainingLoops) { + sf_writef_short(outFile,ilBuffer,totalProcessed); + } else { + sf_writef_short(outFile,ilBuffer,got.bufsize); + } } delete[] ilBuffer; sf_close(outFile); diff --git a/src/engine/engine.h b/src/engine/engine.h index 3b13cdf14..2c691f5a3 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -66,7 +66,7 @@ class DivEngine { bool playing; bool speedAB; bool endOfSong; - int ticks, cycles, curRow, curOrder, remainingLoops, nextSpeed; + int ticks, cycles, curRow, curOrder, remainingLoops, nextSpeed, clockDrift; int changeOrd, changePos, totalTicks, totalCmds, lastCmds, cmdsPerSecond; DivStatusView view; DivChannelState chan[17]; @@ -80,6 +80,8 @@ class DivEngine { short* bbIn[2]; short* bbOut[2]; + size_t totalProcessed; + int dispatchCmd(DivCommand c); void processRow(int i, bool afterDelay); void nextOrder(); @@ -126,6 +128,7 @@ class DivEngine { curOrder(0), remainingLoops(-1), nextSpeed(3), + clockDrift(0), changeOrd(-1), changePos(0), totalTicks(0), diff --git a/src/engine/platform/sound/gb/apu.c b/src/engine/platform/sound/gb/apu.c index 8836ddd29..4c473997f 100644 --- a/src/engine/platform/sound/gb/apu.c +++ b/src/engine/platform/sound/gb/apu.c @@ -1180,11 +1180,11 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) if ((value & 0x80)) { /* DMG bug: wave RAM gets corrupted if the channel is retriggerred 1 cycle before the APU reads from it. */ - if (!CGB && + /*if (!CGB && gb->apu.is_active[GB_WAVE] && gb->apu.wave_channel.sample_countdown == 0 && gb->apu.wave_channel.enable) { - unsigned offset = ((gb->apu.wave_channel.current_sample_index + 1) >> 1) & 0xF; + unsigned offset = ((gb->apu.wave_channel.current_sample_index + 1) >> 1) & 0xF;*/ /* This glitch varies between models and even specific instances: DMG-B: Most of them behave as emulated. A few behave differently. @@ -1193,7 +1193,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) Additionally, I believe DMGs, including those we behave differently than emulated, are all deterministic. */ - if (offset < 4) { + /*if (offset < 4) { gb->io_registers[GB_IO_WAV_START] = gb->io_registers[GB_IO_WAV_START + offset]; gb->apu.wave_channel.wave_form[0] = gb->apu.wave_channel.wave_form[offset / 2]; gb->apu.wave_channel.wave_form[1] = gb->apu.wave_channel.wave_form[offset / 2 + 1]; @@ -1206,7 +1206,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.wave_channel.wave_form + (offset & ~3) * 2, 8); } - } + }*/ if (!gb->apu.is_active[GB_WAVE]) { gb->apu.is_active[GB_WAVE] = true; update_sample(gb, GB_WAVE, diff --git a/src/engine/playback.cpp b/src/engine/playback.cpp index 016d3dc7d..ab111c2c1 100644 --- a/src/engine/playback.cpp +++ b/src/engine/playback.cpp @@ -569,15 +569,23 @@ void DivEngine::nextRow() { bool DivEngine::nextTick() { bool ret=false; + int divider=60; if (song.customTempo) { - cycles=dispatch->rate/song.hz; + divider=song.hz; } else { if (song.pal) { - cycles=dispatch->rate/60; + divider=60; } else { - cycles=dispatch->rate/50; + divider=50; } } + cycles=dispatch->rate/divider; + clockDrift+=dispatch->rate%divider; + if (clockDrift>=divider) { + clockDrift-=divider; + cycles++; + } + if (--ticks<=0) { ret=endOfSong; nextRow(); @@ -691,6 +699,7 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi size_t runLeft=runtotal; size_t runPos=0; + totalProcessed=0; while (runLeft) { if (!remainingLoops) { memset(bbIn[0]+runPos,0,runLeft*sizeof(short)); @@ -710,10 +719,12 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi } else { dispatch->acquire(bbIn[0],bbIn[1],runPos,runLeft); cycles-=runLeft; + runPos=runtotal; break; } } } + totalProcessed=(1+runPos)*got.rate/dispatch->rate; for (size_t i=0; i