gen~ export - samplerate vs. delay calculation

g2312's icon

Hi,

Max Patch
Copy patch and select New From Clipboard in Max.

lets say, we have a simple delay unit like this:

This exports to something like this:
        while ((__n--)) {
            const double in1 = (*(__in1++));
            const double in2 = (*(__in2++));
            double expr_3885 = ((in2 * 0.2) * 44100);
            double tap_3883 = m_delay_1.read_linear(expr_3885);
            double out1 = tap_3883;
            m_delay_1.write(in1);
            m_delay_1.step();
            // assign results to output buffer;
            (*(__out1++)) = out1;

        };
The problem is the hard coded samplerate of "44100". If the resulting plugin runs in a host with a different samplerate all time values are wrong. Same applies for filter coeff calculations, e.g.:
double expr_3899 = safediv(((in2 * 2) * 3.1415926535898), 44100);
            double sin_3894 = sin(expr_3899);

I would propose to change the export mechanism in a way that the parameter "samplerate" multiplied with the time as factor is used in expressions like the above.

Thanks

Thomas

vichug's icon

indeed, it would be cool if gen replaced [samplerate] by something like :

# ifdef SAMPLERATE 
#  samplerate = SAMPLERATE
# else
#  samplerate = 44100

in the beginning of the output code, and with samplerate variable everywhere else.

well i don't know if the syntax is correct, but the idea would be to get the current samplerate value if it is defined somewhere in the architecture, which i guess is pretty standard but doesn't exist always maybe ; else it just goes with the current samplerate set as a global variable, which would be easier to change according to your currnet architecture anyway.

(Hmmm apparently it does export a "samplerate" variable somewhere in the code, but it's not used to replace user-written [samplerate]... strange)

Wesley Smith's icon

Thanks for the report. I've logged a request for future releases about this.