Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/synth/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,5 @@ enum
#endif

#ifdef _WIN32
#define snprintf _snprintf
//#define snprintf _snprintf
Copy link
Author

@djtuBIG-MaliceX djtuBIG-MaliceX Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to comment this out as it was failing to build under MSYS2 MinGW-w64. This seems like a dirty hack anyway that likely no longer applies with more recent versions of MinGW-w64 GCC compiler suite.

#endif
59 changes: 34 additions & 25 deletions src/synth/reverb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "constants.h"
#include "reverb.h"
#include <math.h>
#include <string.h>
#include <cmath>
#include <cstring>
#include <climits>

void CReverb::Init()
{
Expand Down Expand Up @@ -48,7 +49,7 @@ void CReverb::Init()
b1 = 0;
}

void CReverb::SetPar(char param, float value)
void CReverb::SetPar(char param, double value)
{
switch (param)
{
Expand All @@ -64,20 +65,21 @@ void CReverb::SetPar(char param, float value)
}
}

void CReverb::CalcCoefLowPass(float frequencia)
void CReverb::CalcCoefLowPass(double frequencia)
{
float w = 2.0f * sr;
float fCut = 2.0f * PI * Key2Frequency(frequencia * MAXFREQFLT);
float Norm = 1.0f / (fCut + w);
double w = 2.0f * sr;
double fCut = 2.0f * PI * Key2Frequency(frequencia * MAXFREQFLT);
double Norm = 1.0f / (fCut + w);
b1 = lrintf((w - fCut) * Norm * 32768.f);
a0 = a1 = lrintf( fCut * Norm * 32768.f);
}

void CReverb::Process(int *b, int size)
{
int i = 0;
int ent = 0;
int aux = 0;
int i = 0;
long ent = 0;
long aux = 0;
long smp = 0;
int retorno = (int)(ti * 127.f);
if (REVDAant != da)
{
Expand All @@ -87,46 +89,53 @@ void CReverb::Process(int *b, int size)
for (i=0;i<size;i++)
{
ent = b[i];
b[i] = 0;
smp = 0;
// comb 1
b[i] += bcomb1[icomb1];
smp += bcomb1[icomb1];
bcomb1[icomb1] = ent + ((bcomb1[icomb1] * retorno)/128);
if (++icomb1>=TAMCOMB1) icomb1 = 0;
// comb 2
b[i] += bcomb2[icomb2];
smp += bcomb2[icomb2];
bcomb2[icomb2] = ent + ((bcomb2[icomb2] * retorno)/128);
if (++icomb2>=TAMCOMB2) icomb2 = 0;
// comb 3
b[i] += bcomb3[icomb3];
smp += bcomb3[icomb3];
bcomb3[icomb3] = ent + ((bcomb3[icomb3] * retorno)/128);
if (++icomb3>=TAMCOMB3) icomb3 = 0;
// comb 4
b[i] += bcomb4[icomb4];
smp += bcomb4[icomb4];
bcomb4[icomb4] = ent + ((bcomb4[icomb4] * retorno)/128);
if (++icomb4>=TAMCOMB4) icomb4 = 0;
// allpass 1
aux = ballp1[iallp1];
ballp1[iallp1] = ((aux * retorno)/128) + b[i];
b[i] = aux - ((ballp1[iallp1] * retorno)/128);
ballp1[iallp1] = ((aux * retorno)/128) + smp;
smp = aux - ((ballp1[iallp1] * retorno)/128);
if (++iallp1>=TAMALLP1) iallp1 = 0;
// allpass 2
aux = ballp2[iallp2];
ballp2[iallp2] = ((aux * retorno)/128) + b[i];
ballp2[iallp2] = ((aux * retorno)/128) + smp;
b[i] = aux - ((ballp2[iallp2] * retorno)/128);
if (++iallp2>=TAMALLP2) iallp2 = 0;
// DC filter
ou0 = b[i] - in1 + ((ou0*32674)/32768);
in1 = b[i];
b[i] = ou0>>2;
ou0 = smp - in1 + (((double)ou0*32674)/32768);
in1 = smp;
smp = ou0>>2;

b[i] = (smp > INT_MAX) ? INT_MAX :
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more work might be needed to prevent double-clipping.

(smp < INT_MIN) ? INT_MIN :
smp;
}
if (REVDAant < 1.f)
{
for (i=0;i<size;i++)
{
smp = b[i];
// low pass filter
ou0l = ((b[i] * a0)/32768) + ((in1l * a1)/32768) + ((ou0l * b1)/32768);
in1l = b[i];
b[i] = ou0l;
ou0l = ((smp * a0)/32768) + ((in1l * a1)/32768) + ((ou0l * b1)/32768);
in1l = smp;
b[i] = (ou0l > INT_MAX) ? INT_MAX :
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, this may contribute to double-clipping.

(ou0l < INT_MIN) ? INT_MIN :
ou0l;
}
}
state = ACTIVE;
Expand All @@ -139,7 +148,7 @@ char CReverb::GetState()
return state;
}

inline float CReverb::Key2Frequency(float valor)
inline double CReverb::Key2Frequency(double valor)
{
return C0 * powf(2.0f, valor / 12.0f);
}
58 changes: 29 additions & 29 deletions src/synth/reverb.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
class CReverb
{
private:
float sr;
float ti; // time
float da; // damp
double sr;
double ti; // time
double da; // damp
// combs buffers
int bcomb1[TAMCOMB1];
int bcomb2[TAMCOMB2];
int bcomb3[TAMCOMB3];
int bcomb4[TAMCOMB4];
long bcomb1[TAMCOMB1];
long bcomb2[TAMCOMB2];
long bcomb3[TAMCOMB3];
long bcomb4[TAMCOMB4];
// allpasses buffers
int ballp1[TAMALLP1];
int ballp2[TAMALLP2];
long ballp1[TAMALLP1];
long ballp2[TAMALLP2];
// buffers iterators
int icomb1;
int icomb2;
int icomb3;
int icomb4;
int iallp1;
int iallp2;
long icomb1;
long icomb2;
long icomb3;
long icomb4;
long iallp1;
long iallp2;
// DC filter
int in1;
int ou0;
long in1;
long ou0;
// low-pass filter
int in1l;
int ou0l;
int a0;
int a1;
int b1;
float REVDAant;
long in1l;
long ou0l;
long a0;
long a1;
long b1;
double REVDAant;
// other
char state;
// calculates low-pass coefs
void CalcCoefLowPass(float frequencia);
void CalcCoefLowPass(double frequencia);
public:
void Init();
char GetState(void);
void Process(int *b, int size);
inline float Key2Frequency(float valor);
void SetPar(char param, float value);
void Init();
char GetState(void);
void Process(int *b, int size);
inline double Key2Frequency(double valor);
void SetPar(char param, double value);
};