Audio Code Library: Allpass Filter


The allpass filter structure is very similar to the comb filter, but there is a feedforward path that bypasses the delayline. First, allocate enough memory to hold the necessary number of samples (sampling rate time delay time). Set a pointer to the start of the delayline. For each sample, read the value in the delayline . Write a new value - the input plus the read value weighted by the gain. The output that read value plus the weighted input value. Then increment the pointer to the next location in the delayline. If the pointer reaches the end of the allocated space, set it to the first memory location.

The code allows for the delay time (milliseconds) and gain to be specified.

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

Allpass.h

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

Allpass.h - An allpass filter designed to build 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 ALLPASS_H
#define ALLPASS_H

#include "Processor.h"

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

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


#endif

Allpass.cpp

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

Allpass.cpp - An allpass filter designed to build 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.
*********************************************************/


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

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

  SetNumInputs(1);
  SetNumOutputs(1);

  gain = feedbackGain;
  delay = delayTime;  //delay time is in milliseconds

}


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

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

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

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

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

  return;
}


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

void Allpass :: Process (void)
{
  float temp;

  for(i=0; i<frameLength; i++) {  //for each sample...
    temp = *readPtr;
    *readPtr = gain * temp + inputSignal[i];
    outputSignal[i] = temp - gain * *readPtr;

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

}


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

void Allpass :: 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.