Manual: Convolver

[ javadoc ]

Convolver is an effect that convolves a signal with a kernal. The kernal can be thought of as the impulse response of an audio filter, or simply as a set of weighting coefficients.

// Constructs a Convolver with the kernal k 
// that expects buffers of length sigLength passed to process.
Convolver(float[] k, int sigLength)
// convolves signal with the kernal
void process(float[] signal)       
// convolves sigLeft and sigRight with the kernal
void process(float[] sigLeft, float[] sigRight)          
// Sets the kernal to k.
void setKernal(float[] k)

Convolver performs brute-force convolution, meaning that it is slow, relatively speaking. However, the algorithm is very straightforward. Each output sample i is calculated by multiplying each kernal value j with the input sample i - j and then summing the resulting values. The output will be kernal.length + signal.length - 1 samples long, so the extra samples are stored in an overlap array. The overlap array from the previous signal convolution is added into the beginning of the output array, which results in an output signal without pops.

Code Sample (online example)

import ddf.minim.*;
import ddf.minim.effects.*;
 
AudioPlayer groove;
Convolver lpf;
 
void setup()
{
  size(512, 200);
  // always start Minim first
  Minim.start(this);
  groove = Minim.loadFile("groove.mp3", 512);
  groove.loop();
  // the kernal can be thought of 
  // as the impulse response of a filter, 
  // or as a set of weighting coefficients.
  // this particular set is roughly 
  // the impulse response of a low pass filter
  float[] kernel = new float[] { 0, 0.005, 0.01, 0.018, 0.021, 
                        0.03, 0.034, 0.037, 0.04, 0.042, 
                        0.044, 0.046, 0.048, 0.049, 0.05,
                        0.049, 0.048, 0.046, 0.044, 0.042, 
                        0.04, 0.037, 0.034, 0.03, 0.021, 0.018, 
                        0.01, 0.005, 0 };
  // make a new Convolver, passing the kernal array and the buffer size 
  // of the signal that will be convolved with the kernal                              
  lpf = new Convolver(kernel, groove.bufferSize());
  groove.addEffect(lpf);
}
 
void draw()
{
  background(0);
  stroke(255);
  for(int i = 0; i < groove.bufferSize() - 1; i++)
  {
    line(i, 50 - groove.left.get(i)*50, i+1, 50 - groove.left.get(i+1)*50);
    line(i, 150 - groove.right.get(i)*50, i+1, 150 - groove.right.get(i+1)*50);
  }
}
 
void stop()
{
  // close the AudioPlayer before exiting
  groove.close();
  // always stop Minim before exiting
  Minim.stop();
 
  super.stop();
}