0

What can I do to fix this algorithm?

I'm using iPlug 2 to make a VST plugin that uses convolution. Right now it outputs very loud and high-pitched frequencies.

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.

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
aaaa123
  • 1
  • 1
  • Welcome to Stackoverflow. There are so many things to unpack here, I think you would probably be better aiming this question at the iPlug forums https://iplug2.discourse.group – fdcpp Jul 24 '21 at 07:17
  • 1
    Thank you. I forgot there’s an iPlug form, I’ll post it there. – aaaa123 Jul 24 '21 at 14:06

0 Answers0