pac-dev / protoplug

Create audio plugins on-the-fly with LuaJIT.
Other
285 stars 36 forks source link

Understanding midi.noteToFreq #43

Closed interstar closed 3 years ago

interstar commented 3 years ago

I'm having a bit of trouble figuring out midi.noteToFreq

I expected I'd have a function to turn MIDI notes into Hertz, as I already do the calculation to turn Hertz into the phaser delta in my synth.

But the documentation says :

midi.noteToFreq (note) Convert a MIDI note number to frequency. Call this function to get a note's frequency. Parameters:

    note the MIDI note (0-127)

Returns:

    the frequency in samples^-1 

I'm guessing that means frequency is samples to the power of -1. Ie. 1 / frequency in samples?

But how do I calculate the Hz from that? Or how do I use it exactly?

interstar commented 3 years ago

OK. I found a formula and ended up adapting it.

In my VCO object ...

    set_midi_note = function(self,mn)
        a = 440 -- frequency of A (common value is 440Hz)
        n = (a / 32) * math.pow(2,((mn - 9) / 12))  -- (2 ** ((mn - 9) / 12))
        self:set_freq(n)
        self.phase = 0
    end
pac-dev commented 3 years ago

When working at a low level, I find "cycle per sample" easier to use than "cycle per second" (aka Hertz). To convert between the two, you multiply or divide by the samplerate. Sine Organ is one example of how to use this kind of value (the noteFreq field is populated using midi.noteToFreq). Maybe providing frequencies in Hertz would be better for clarity though!