Skip to content

Commit

Permalink
Merge pull request #137 from bcc2528/master
Browse files Browse the repository at this point in the history
Added PCM Linear interpolation.
  • Loading branch information
captainys authored Oct 5, 2024
2 parents 979235b + fe669fb commit ed760f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
29 changes: 27 additions & 2 deletions src/rf5c68/rf5c68.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ static inline int Gain(int a,int b)
}
}

// Linear interpolation
static inline int lerp_int(int start, int end, float t)
{
if (t >= 1.0)
{
return end;
}
else if(t <= 0.0)
{
return start;
}
float ret = (1.0 - t) * start + t * end;
return (int)ret;
}


RF5C68::RF5C68()
Expand Down Expand Up @@ -82,6 +96,8 @@ void RF5C68::Clear(void)
state.IRQBank=0;
state.IRQBankMask=0;
state.timeBalance=0;
state.Lout_prev=0;
state.Rout_prev=0;
}

RF5C68::StartAndStopChannelBits RF5C68::WriteControl(unsigned char value)
Expand Down Expand Up @@ -455,10 +471,16 @@ unsigned int RF5C68::AddWaveForNumSamples(unsigned char waveBuf[],unsigned int n
;
}

float time = 0;
float time_add = static_cast<float>(SAMPLING_RATE) / state.timeBalance;

while(0<=state.timeBalance && nFilled<numSamples)
{
WordOp_Add(wavePtr ,Lout);
WordOp_Add(wavePtr+2,Rout);
//WordOp_Add(wavePtr ,Lout);
//WordOp_Add(wavePtr+2,Rout);
WordOp_Add(wavePtr ,lerp_int(state.Lout_prev,Lout,time));
WordOp_Add(wavePtr+2,lerp_int(state.Rout_prev,Rout,time));
time+=time_add;
state.timeBalance-=SAMPLING_RATE;
wavePtr+=4;
++nFilled;
Expand All @@ -469,6 +491,9 @@ unsigned int RF5C68::AddWaveForNumSamples(unsigned char waveBuf[],unsigned int n
}
}
state.timeBalance+=outSamplingRate;

state.Lout_prev=Lout;
state.Rout_prev=Rout;
}

for(unsigned int chNum=0; chNum<NUM_CHANNELS; ++chNum)
Expand Down
14 changes: 9 additions & 5 deletions src/rf5c68/rf5c68.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class RF5C68
public:
enum
{
SAMPLING_RATE=20725, // Supposed to be 19200. Where is this 20725 come from?
SAMPLING_RATE=20833, // Supposed to be 19200. Where is this 20833 come from? The master clock of the RF5C68 in FM TOWNS is 8 MHz, and dividing it by 384 gives 20.833 Hz.
WAVERAM_SIZE=65536,
NUM_CHANNELS=8,
FREQ=20725,
FREQ=20833,
FD_BIT_SHIFT=11,
BANK_SHIFT=12, // 2^12 bytes per bank.
LOOP_STOP_CODE=0xFF,
Expand Down Expand Up @@ -78,19 +78,23 @@ class RF5C68
std::vector <unsigned char> waveRAM;
Channel ch[NUM_CHANNELS];

int timeBalance; // For WAV generation.
int volume=WAVE_OUTPUT_AMPLITUDE_MAX_DEFAULT;

// For Linear interpolation
int Lout_prev;
int Rout_prev;

bool playing; // Bit 7 of I/O 04F7H
unsigned short Bank; // Bank x000H
unsigned char CB; // Channel
unsigned char chOnOff; // I/O 04F8H
int timeBalance; // For WAV generation.

inline bool IRQ() const
{
return 0!=IRQBank;
}
unsigned char IRQBank,IRQBankMask;

int volume=WAVE_OUTPUT_AMPLITUDE_MAX_DEFAULT;
};
State state;
bool chMute[NUM_CHANNELS]={false,false,false,false,false,false,false,false};
Expand Down

0 comments on commit ed760f6

Please sign in to comment.