Skip to content

Commit

Permalink
Fix (rarely audible) click bug in BLEP synthesis
Browse files Browse the repository at this point in the history
Fixes random clicks in specific pitch sweep tests I made. These clicks shouldn't have been present. This bug was introduced in v1.34.
  • Loading branch information
8bitbubsy committed Mar 13, 2022
1 parent 5165bd2 commit 9e8c845
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/pt2_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,13 @@ void paulaSetPeriod(int32_t ch, uint16_t period)
v->dOldVoiceDeltaMul = 1.0 / v->dOldVoiceDelta;
}

// to be read on next sampling step
v->AUD_PER_delta = v->dOldVoiceDelta;

v->AUD_PER_deltamul = v->dOldVoiceDeltaMul;

// set BLEP stuff
v->dDeltaMul = v->dOldVoiceDeltaMul;
if (v->dLastDelta == 0.0)
v->dLastDelta = v->AUD_PER_delta;
if (v->dLastDelta == 0.0) v->dLastDelta = v->AUD_PER_delta;
if (v->dLastDeltaMul == 0.0) v->dLastDeltaMul = v->AUD_PER_deltamul;
}

void paulaSetVolume(int32_t ch, uint16_t vol)
Expand Down Expand Up @@ -385,8 +386,10 @@ void paulaStartDMA(int32_t ch)
v->sampleCounter--;

// set BLEP stuff
v->dDeltaMul = v->AUD_PER_deltamul;
v->dLastPhase = 0.0;
v->dLastDelta = v->dDelta;
v->dLastDeltaMul = v->dDeltaMul;
v->dBlepOffset = 0.0;

v->dPhase = 0.0;
Expand Down Expand Up @@ -473,8 +476,15 @@ void mixChannels(int32_t numSamples)
{
v->dPhase -= 1.0;

// set BLEP stuff
v->dLastPhase = v->dPhase;
v->dLastDelta = v->dDelta;
v->dLastDeltaMul = v->dDeltaMul;
v->dBlepOffset = v->dLastPhase * v->dLastDeltaMul;

// Paula only updates period (delta) during period refetching (this stage)
v->dDelta = v->AUD_PER_delta;
v->dDeltaMul = v->AUD_PER_deltamul;

if (v->sampleCounter == 0)
{
Expand All @@ -495,18 +505,13 @@ void mixChannels(int32_t numSamples)
/* Pre-compute current sample point.
** Output volume is only read from AUDxVOL at this stage,
** and we don't emulate volume PWM anyway, so we can
** pre-multiply by volume at this point.
** pre-multiply by volume here.
*/
v->dSample = v->AUD_DAT[0] * v->AUD_VOL; // -128..127 * 0.0 .. 1.0

// progress AUD_DAT buffer
v->AUD_DAT[0] = v->AUD_DAT[1];
v->sampleCounter--;

// setup BLEP stuff
v->dBlepOffset = v->dPhase * v->dDeltaMul;
v->dLastPhase = v->dPhase;
v->dLastDelta = v->dDelta;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pt2_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ typedef struct voice_t
// registers modified by Paula functions
const int8_t* AUD_LC; // location
uint16_t AUD_LEN; // length (in words)
double AUD_PER_delta; // delta
double AUD_PER_delta, AUD_PER_deltamul; // delta
double AUD_VOL; // volume

double dDelta, dPhase;

// for BLEP synthesis
double dLastDelta, dLastPhase, dBlepOffset, dDeltaMul;
double dLastDelta, dLastPhase, dLastDeltaMul, dBlepOffset, dDeltaMul;

// period cache
int32_t oldPeriod;
Expand Down
2 changes: 1 addition & 1 deletion src/pt2_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "pt2_unicode.h"
#include "pt2_palette.h"

#define PROG_VER_STR "1.41"
#define PROG_VER_STR "1.42"

#ifdef _WIN32
#define DIR_DELIMITER '\\'
Expand Down

0 comments on commit 9e8c845

Please sign in to comment.