What can I do to fix this algorithm?
My goal is to create a basic convolution plugin, using my own algorithm. Right now it outputs very loud and high-pitched frequencies and doesn’t do its job at all. I can’t seem to find what’s wrong with it.
My goal is to take in 100 samples at a time (and while that’s happening the output is just 0), process them all at once in a loop, then output them one by one while taking in new inputs to process next.
Note: I just started learning about DSP and I’ve barely even started to work on this algorithm. I’m not at all sure how the ProcessBlock function works, but I’m only processing one sample at a time. Not sure if newIns and newOuts should be of type sample or double, but from what I’ve tried either type works.
I’d also be more than happy to receive additional tips because I’m very new to C++.
sample newIns[100]; // Input array
int newInsCount = 0;
double ir[4] = { 0.8, -0.3, -0.2, -0.1 }; // The impulse response
sample newOuts[100 + 4 - 1] = {0}; // Output array
int newOutsCount = 0;
bool outsDone = false;
#if IPLUG_DSP
void MyNewPlugin::ProcessBlock(sample** inputs, sample** outputs, int nFrames)
{
const double gain = DBToAmp(GetParam(kGain)->Value());
const double clip = DBToAmp(GetParam(kClip)->Value());
const int nChans = NOutChansConnected();
for (int s = 0; s < nFrames; s++) {
for (int c = 0; c < nChans; c++) {
// Basic clipping process
sample input = inputs[c][s];
sample processed;
if (input*gain >= 0) processed = fmin(input*gain, clip);
else processed = fmax(input*gain, -clip);
// Inserts a sample into the inputs array
bool isTaken = false;
for (int i = 0; i < 100; i++) {
if (i == newInsCount) {
newIns[i] = processed;
isTaken = true;
newInsCount++;
break;
}
}
if (isTaken == true) outputs[c][s] = 0;
else {
if (outsDone != true) { // Convolving the inputs with the impulse response
for (int i = 0; i < 100; i++) {
for (int r = 0; r < 4; r++) newOuts[i + r] += newIns[i] * ir[r];
}
outsDone = true;
newInsCount = 0;
}
for (int i = 0; i < 103; i++) { // Loading a sample from the output array into the actual audio output
if (i == newOutsCount) {
outputs[c][s] = newOuts[i];
newOutsCount++;
break;
}
}
if (newOutsCount == 103) {
outsDone = false;
newOutsCount = 0;
}
}
}
}
}
#endif