clau_ste wrote:Let's say that i pass to this method 65.41 which is a C2 note

I suggest that you run your oscillator in normalized frequency. That is, treat frequency as a floating point value ranging 0-1, where 1 is the sample rate. This is convenient because that value is also your normalized phase increment. In other words, a frequency (and phase increment) of 0.5 means that the table is sampled exactly twice per cycle. The phasor runs 0-1, incremented by the phase increment every sample, wrapping. When you need an output, the phasor value is multiple by the length of your wavetable to produce an index.

Here is the code for my oscillator—you can see how simple and efficient it makes things, while also making frequency calculations obvious and trivial:

- Code: Select all
`// note: if you don't keep this in the range of 0-1, you'll need to make changes elsewhere`

inline void WaveTableOsc::setFrequency(double inc) {

phaseInc = inc;

}

inline void WaveTableOsc::updatePhase() {

phasor += phaseInc;

if (phasor >= 1.0)

phasor -= 1.0;

}

The sample lookup becomes, essentially, "waveTable[int(phasor * waveTableLen)]"—a bit more to select the appropriate table, dependent on frequency, and any interpolation, but you can see how simple normalized frequency makes things.

For your example of 65.41 Hz, you'd set the oscillator frequency to 65.41 / sampleRate (or 65.41 * samplePeriod). Using normalized frequency also makes it convenient for placing exponential converters in front of the oscillator frequency input.