Audio Code Library: Comb Filter


The comb filter is essentially a delay with feedback, but the input signal is not passed thru. The delay times used are short (30 - 40 ms) when used to create reverb. Comb filters are typically used in parallel for reverb. The operation is very simple - simply allocate space for the delayline. Read a value, write a new one (the input signal plus a portion of the read value), and increment. When the read pointer reaches the end of the buffer, wrap it back to the beginning.

The code allows for the delay time (in milliseconds) and the gain.

See also:

Please do not redistribute this code. In the event that it contains a bug, this will ensure that it can be fixed without the buggy copies floating around indefinitely.

Last Modified: 6/28/98

Comb.h

/*********************************************************

Comb.cpp - A comb filter for building reverberators

Copyright (c) 1998, Scott Lehman, slehman@harmony-central.com
This code may be used and modified freely provided that credit
is given to the author in any public release. Any derivative
programs must be distributed freely and/or the modified source
code made publicly available.  All code is provided AS IS and
without warranty of any kind.
*********************************************************/


#ifndef COMB_H
#define COMB_H

#include "Processor.h"

class Comb : public Processor {
public:
  Comb(float delay, float gain);  //delay is in milliseconds
  void Initialize(void);
  void Process(void);
  void Cleanup(void);
  ~Comb(){;}

private:
  float delay, gain;
  float * bufferStart, * bufferEnd, * readPtr;
  float * inputSignal, * outputSignal;
  int i, numSamples;
  Comb(Comb&){};
};

#endif

Comb.cpp

/*********************************************************

Comb.cpp - A comb filter for building reverberators

Copyright (c) 1998, Scott Lehman, slehman@harmony-central.com
This code may be used and modified freely provided that credit
is given to the author in any public release. Any derivative
programs must be distributed freely and/or the modified source
code made publicly available.  All code is provided AS IS and
without warranty of any kind.
*********************************************************/


#include "Comb.h"


// ***************  Comb(float, float)  ***************

Comb :: Comb(float delayTime, float feedbackGain)
{

  SetNumInputs(1);
  SetNumOutputs(1);

  gain = feedbackGain;
  delay = delayTime;  //delaytime in milliseconds

}


// ***************  Initialize(void)  ********************

void Comb :: Initialize (void)
{
  int bufferLength = int(delay*samplingRate/1000);
  bufferStart = new float[bufferLength];
  bufferEnd = bufferStart + bufferLength;

  //zero out the buffer (silence)
  for(i=0; i<bufferLength; i++)
    bufferStart[i] = 0.0;

  //set read pointer to start of buffer
  readPtr = bufferStart;

  //assign pointers for the only input and output
  inputSignal = inputs[0];
  outputSignal = outputs[0];

  return;
}


// **************** Process(void)  ********************

void Comb :: Process (void)
{
  for(i=0; i<frameLength; i++) {  //for each sample...
    outputSignal[i] = *readPtr;  //read the value
    //write the scaled value + the new input sample
    *readPtr = (*readPtr)*gain + inputSignal[i];

    if(++readPtr >= bufferEnd) //if reach end of buffer, wrap
      readPtr = bufferStart;
  }

}


// *****************  Cleanup(void)  ******************

void Comb :: Cleanup (void)
{
  delete [] bufferStart;

  return;
}

Back to the Audio Programming Page

Back to Harmony Central® Home Page

Email: webmaster@harmony-central.com
Copyright © 1995-98 Harmony Central, Inc. All rights reserved.