potentially breaking change: better freq formula

now using a 4096-entry-long table for calculating final period/frequency
see issue #303
This commit is contained in:
tildearrow 2022-03-25 02:52:41 -05:00
parent 03da02711a
commit ed857b20c4
3 changed files with 22 additions and 5 deletions

View File

@ -832,9 +832,17 @@ double DivEngine::calcBaseFreq(double clock, double divider, int note, bool peri
int DivEngine::calcFreq(int base, int pitch, bool period, int octave) { int DivEngine::calcFreq(int base, int pitch, bool period, int octave) {
if (song.linearPitch) { if (song.linearPitch) {
pitch+=2048;
if (pitch<0) pitch=0;
if (pitch>4095) pitch=4095;
return period?
(base*reversePitchTable[pitch])>>10:
(base*pitchTable[pitch])>>10;
/*
return period? return period?
base*pow(2,-(double)pitch/(12.0*128.0))/(98.0+globalPitch*6.0)*98.0: base*pow(2,-(double)pitch/(12.0*128.0))/(98.0+globalPitch*6.0)*98.0:
(base*pow(2,(double)pitch/(12.0*128.0))*(98+globalPitch*6))/98; (base*pow(2,(double)pitch/(12.0*128.0))*(98+globalPitch*6))/98;
*/
} }
return period? return period?
base-pitch: base-pitch:
@ -2850,6 +2858,10 @@ bool DivEngine::init() {
for (int i=0; i<64; i++) { for (int i=0; i<64; i++) {
vibTable[i]=127*sin(((double)i/64.0)*(2*M_PI)); vibTable[i]=127*sin(((double)i/64.0)*(2*M_PI));
} }
for (int i=0; i<4096; i++) {
reversePitchTable[i]=round(1024.0*pow(2.0,(2048.0-(double)i)/(12.0*128.0)));
pitchTable[i]=round(1024.0*pow(2.0,((double)i-2048.0)/(12.0*128.0)));
}
for (int i=0; i<DIV_MAX_CHANS; i++) { for (int i=0; i<DIV_MAX_CHANS; i++) {
isMuted[i]=0; isMuted[i]=0;

View File

@ -227,6 +227,8 @@ class DivEngine {
} sPreview; } sPreview;
short vibTable[64]; short vibTable[64];
int reversePitchTable[4096];
int pitchTable[4096];
blip_buffer_t* samp_bb; blip_buffer_t* samp_bb;
size_t samp_bbInLen; size_t samp_bbInLen;

View File

@ -22,10 +22,12 @@
#include <math.h> #include <math.h>
#define rWrite(a,v) {regPool[(a)]=(v)&0xff; vic_sound_machine_store(vic,a,(v)&0xff);} #define rWrite(a,v) {regPool[(a)]=(v)&0xff; vic_sound_machine_store(vic,a,(v)&0xff);}
#define CHIP_DIVIDER 32
#define SAMP_DIVIDER 4 #define SAMP_DIVIDER 4
const int chipDividers[4]={
128, 64, 32, 64
};
const char* regCheatSheetVIC[]={ const char* regCheatSheetVIC[]={
"CH1_Pitch", "0A", "CH1_Pitch", "0A",
"CH2_Pitch", "0B", "CH2_Pitch", "0B",
@ -93,6 +95,7 @@ void DivPlatformVIC20::writeOutVol(int ch) {
void DivPlatformVIC20::tick() { void DivPlatformVIC20::tick() {
for (int i=0; i<4; i++) { for (int i=0; i<4; i++) {
int CHIP_DIVIDER=chipDividers[i];
chan[i].std.next(); chan[i].std.next();
if (chan[i].std.hadVol) { if (chan[i].std.hadVol) {
int env=chan[i].std.vol; int env=chan[i].std.vol;
@ -121,10 +124,9 @@ void DivPlatformVIC20::tick() {
} }
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true); chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true);
if (i<3) chan[i].freq>>=(2-i); printf("%d freq: %d\n",i,chan[i].freq);
else chan[i].freq>>=1;
if (chan[i].freq<1) chan[i].freq=1; if (chan[i].freq<1) chan[i].freq=1;
if (chan[i].freq>127) chan[i].freq=0; if (chan[i].freq>127) chan[i].freq=127;
if (isMuted[i]) chan[i].keyOn=false; if (isMuted[i]) chan[i].keyOn=false;
if (chan[i].keyOn) { if (chan[i].keyOn) {
if (i<3) { if (i<3) {
@ -150,6 +152,7 @@ void DivPlatformVIC20::tick() {
} }
int DivPlatformVIC20::dispatch(DivCommand c) { int DivPlatformVIC20::dispatch(DivCommand c) {
int CHIP_DIVIDER=chipDividers[c.chan];
switch (c.cmd) { switch (c.cmd) {
case DIV_CMD_NOTE_ON: { case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins); DivInstrument* ins=parent->getIns(chan[c.chan].ins);