From 11478fbdfe503717a997fd1e507927a37fd3cb68 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Mon, 27 Dec 2021 15:22:57 -0500 Subject: [PATCH] prevent clipping in ADPCM samples --- src/engine/engine.cpp | 6 ++++++ src/engine/engine.h | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 678a5bd05..28ff1373d 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1367,6 +1367,8 @@ void DivEngine::renderSamples() { step=adSteps[index]; short sample=(jrendLength)?((s->depth==16)?(s->rendData[j]>>4):(s->rendData[j]<<4)):0; + if (sample>0x7d0) sample=0x7d0; + if (sample<-0x7d0) sample=-0x7d0; diff=sample-predsample; if (diff>=0) { encoded=0; @@ -1389,6 +1391,9 @@ void DivEngine::renderSamples() { if (diff>=tempstep) encoded|=1; acc+=jediTable[decstep+encoded]; + if (acc>0x7ff || acc<-0x800) { + logW("clipping! %d\n",acc); + } acc&=0xfff; if (acc&0x800) acc|=~0xfff; decstep+=adStepSeek[encoded&7]*16; @@ -1552,6 +1557,7 @@ void DivEngine::reset() { extValuePresent=0; speed1=song.speed1; speed2=song.speed2; + globalPitch=0; dispatch->reset(); } diff --git a/src/engine/engine.h b/src/engine/engine.h index 1d252a968..a4eae07fc 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -80,7 +80,7 @@ class DivEngine { bool extValuePresent; bool repeatPattern; int ticks, cycles, curRow, curOrder, remainingLoops, nextSpeed, clockDrift; - int changeOrd, changePos, totalTicks, totalCmds, lastCmds, cmdsPerSecond; + int changeOrd, changePos, totalTicks, totalCmds, lastCmds, cmdsPerSecond, globalPitch; unsigned char extValue; unsigned char speed1, speed2; DivStatusView view;