[sdiy] Detuning in Digital

Tom Wiltshire tom at electricdruid.net
Mon Apr 2 12:17:22 CEST 2012


Matthew, 

> Wondering if anyone can give me the Magic Formula for calculating a de-tuned frequency. I understand (rightly or wrongly!) that a cent above a note is one hundredth of one twelfth of twice that original frequency and ditto with half the original frequency for one cent down.
> 
> My mathematical ability, unfortunately, does not stretch to getting this into a simple formula where I plug in a frequency and detune value and come out with a new frequency. (Or even one formula for up and one for down, so I don't have to muck around with signed numbers.)

If you're using a phase accumulator plus frequency increment design (DDS or NCO or whatever you want to call it) then the frequency for a given increment is:

	frequency = (sample_rate * freq_inc) / max_accumulator_value

Since the smallest increment is one, the frequency resolution of your oscillator is:

	frequency resolution = sample_rate / max_accumulator_value

This is also the amount that a change of +1 or -1 will make to the frequency if added to the increment for a given note. It's a linear detune.

We can rearrange the top equation for the frequency increment:

	freq_inc = (frequency * max_accumulator_value) / sample_rate

I'm pretty much in agreement with Olivier over the best method for converting the exponential Pitch into the linear Frequency. My most recent designs have used the same method he did - a one octave table based on MIDI note numbers.

However, I have also done this a different way, which has different advantages/disadvantages. I used a 16-bit word for frequency. The top 4-bits represented the octave, whilst the lower 12-bits represent the position within the octave. Since this is binary, it's actually impossible to generate note frequencies *exactly*, not that you'd ever know. Again, you can use a single octave table and shifts, but you don't even have to count the notes, since you have the octave provided. Instead I used a small table (128 entries) to convert from MIDI note to 16-bit pitch, but this only needs using when the MIDI note changes, not every sample or anything costly.
It's a workable alternative, and it's sometimes nice to have everything nicely scaled to octaves (like you said, why aren't there 16 notes in the scale?!) but it can also be a pain to *not* have everything nicely scaled to semitones.
Ultimately, you choose.

HTH,
Tom




More information about the Synth-diy mailing list