What does gen~ phasor() do? (Translating Max/MSP gen to C++)

480 views Asked by At

We have two places in the same gen~ code box object with phasor:

wander = phasor(in8/dense);

...some code later...

phas = (triangle(phasor(freq), sharp)*len-rot_x/(2*pi))%1;

I understand that phasor() produces a rising sawtooth, outputting values of 0 to 1. I understand the argument of phasor() is frequency. What I don't understand is how phasor() can output a value of 0 to 1 given frequency, when you would need frequency over time to produce a value other than 0. It would seem that phasor(frequency) should always output 0 unless somehow phasor() is keeping track of time and its own phase.

If phasor is keeping track of time/phase, how can we call phasor() twice in the same gen code box? It would seem impossible that we could have two time values. Unless...

...we have one time/phase value shared between all calls to phasor() but it is the last call to phasor() that sets the final frequency before phasor() increments its phase, which happens at the end of the code block.

Am I correct?

Edit: No that can't be, then why would you ever put a frequency into phasor twice? It wouldn't change the output under my logic.

1

There are 1 answers

2
Elan Hickler On BEST ANSWER

From my tests, phasor is indeed a sawtooth oscillator object where each call to phasor is a unique oscillator, so, calling phasor twice in the same code box will instantiate two objects.

class Phasor
{
public:
    double getSample() 
    { 
        double ret = phase/PI_z_2;
        phase = fmod(phase+phase_inc, TAU); //increment phase
        return ret;
    }
    void setSampleRate(double v) { sampleRate = v; calculateIncrement(); }
    void setFrequency(double v) { frequency = v; calculateIncrement(); }
    void reset() { phase = 0.0; }
protected:
    void calculateIncrement() { phase_inc = TAU * frequency / sampleRate; }
    double sampleRate = 44100.0; 
    double frequency = 1.0;
    double phase = 0.0;
    double phase_inc = 0.0;
    const double TAU = 2*PI;
    const double PI_z_2 = PI/2.0;
};