FIR filter in Gen (embeded in RNBO patch) with buffer IR

Ifa's icon

Hi

i want to buil a FIR filter with Gen~ in an RNBO patch based on a buffer with the IR in the RNBO patch and a codebox in Gen~ object. I have no error or issue in the Gen~ compilator with my code but the compilator returne this code :

expr_1 = 0;

out1 = expr_1;

so my code is not compiled by the Gen~ object... :( and no error to check ! but no sound is going out of the RNBO patch.

If anybody want to help i will appreciate :) !

The IR is a lowpass filter at 125Hz / 24 dB oct

I'm using maxMSP version 8.6.5 // RNBO 1.4 // Windows 10

FIR_IN_RNBO_IN_GEN_2.maxpat
Max Patch

Ifa's icon
impulse_response_24dBoct.wav

this is the IR wav file, after downloaded you have to rename the file : impulse_response_24dBoct.wav

then put de wav file inside the folder of the maxpatch or in searchfile with filepreference !

Graham Wakefield's icon

The code inside your gen~ codebox is not valid gen~ code. The language inside a gen~ codebox is called GenExpr, documented here: https://docs.cycling74.com/userguide/gen/gen_genexpr/

The changes needed are pretty simple -- you don't need to wrap everything in a Process(in1), you don't need var, and you don't need to declare In in1 etc.

The other change is in how buffers are declared. In gen~, you reference a buffer~ using Buffer buffername; and you create local data with Data dataname(sizeinsamples, numchannels);

So I end up with this:

// Buffers RNBO pour la réponse impulsionnelle et les échantillons
Buffer b_impulse_response_24dBoct_wav;
Data sample_buf(1024, 1);

// Index circulaire
History s_index(0);

// Taille du filtre (nombre de coefficients)
buffersize = dim(b_impulse_response_24dBoct_wav);

// Stockage du nouvel échantillon (canal 0)
poke(sample_buf, in1, s_index, 0);

// Initialisation de la somme
somme = 0;

// Parcours pour la convolution
for (i = 0; i < buffersize; i += 1) {
        // Récupération du coefficient i
        cur_coef = peek(b_impulse_response_24dBoct_wav, i, 0);

        // Calcul de l'indice circulaire
        // Récupération de l'échantillon retardé
        cur_sample = peek(sample_buf, s_index - i, 0, boundmode="wrap");

        // Accumulation du produit
        somme += cur_sample * cur_coef;
}

// Incrémentation de l'index circulaire
s_index += 1;
if (s_index >= buffersize) {
    s_index = 0;
}

// Envoi du résultat
out1 = somme;

Ifa's icon

thank you so much !! now it works like a charm !

for me it was not so easy to find/create a GenExpr code for FIR convolution so i hope this code will help other peoples who they also want to deal with FIR filters in GEN codebox :)

to be precise for peoples that use this exemple with the IR wav file replace:

Data sample_buf(1024, 1);

with

Data sample_buf(741, 1);

simply because the IR wav file is a 741 taps (samples).