Freeverb

The function mono_freeverb implements the Freeverb reverberation algorithm for one channel of audio at 44.1kHz.

The program has two parameters: damping and roomsize, both in the range 0.0 to 1.0.

With the help of ffmpeg, you can process an audio file through this program:

  1. Save the code into a file freeverb.arrp, then compile it:
    arrp freeverb.arrp -x freeverb
  2. Run the program, reading the file input.wav through ffmpeg into the program and writing the output through ffmpeg into the file output.wav:
    ffmpeg -i input.wav -ac 1 -ar 44.1k -f f32le pipe:1 |
    ./freeverb -f raw damping=0.5 roomsize=0.5 |
    ffmpeg -y -ac 1 -ar 44.1k -f f32le -i pipe:0 file:output.wav
    

Implementation:

input roomsize : real64;
input damping : real64;
input audio_in : [~]real32;
output audio_out : [~]real32;

scaleroom = 0.28;
offsetroom = 0.7;
scaledamp = 0.4;

COMB_FEEDBACK = real32(max(0.0, min(1.0, roomsize)) * scaleroom + offsetroom);
COMB_DAMPING = real32(max(0.0, min(1.0, damping)) * scaledamp);
ALLPASS_FEEDBACK = real32(0.5);
INPUT_GAIN = real32(0.015);

audio_out = audio_in + mono_freeverb(audio_in * INPUT_GAIN);

mono_freeverb(x) = y0 where {
    y0 = allpass_comb(225, y1);
    y1 = allpass_comb(341, y2);
    y2 = allpass_comb(441, y3);
    y3 = allpass_comb(556, y4);
    y4 =
        lbcf(1116,x) +
        lbcf(1188,x) +
        lbcf(1277,x) +
        lbcf(1356,x) +
        lbcf(1422,x) +
        lbcf(1491,x) +
        lbcf(1557,x) +
        lbcf(1617,x);
};

allpass_comb(D, x) = y where {
    y = -ALLPASS_FEEDBACK * w1 + w2;
    w1 = w2 * ALLPASS_FEEDBACK + x;
    w2 = delay(D, w1);
};

lbcf(D,x) = y where {
    y = delay(D, w * COMB_FEEDBACK + x);
    w = delay(1,y) * (real32(1) - COMB_DAMPING) + delay(1,w) * COMB_DAMPING;
};

delay(N,x) = y where y[n] = {
    real32(0), if n < N;
    x[n-N], otherwise;
};