Helper functions that might to useful to others, and round out the mtof function in DaisySP/Source/Utility/dsp.h, which looks like this for completeness:
/** Midi to frequency helper
powf(2, (midi_note - A4_midi_note) / semitones_per_octave) * A4_frequency;
*/
inline float mtof(float m)
{
return powf(2, (m - 69.0f) / 12.0f) * 440.0f;
}
Please let me know if you find errors or can improve on these!
/** frequency to Midi helper
ceil(A4_midi_value + ( log(frequency / A4_reference_frequency) / log(2) ) * semitones_per_octave)
*/
inline float ftom(float f)
{
return ceil(69.0f + (logf(f / 440.0f) / logf(2.0f)) * 12.0f);
}
/** 1v/oct to Midi helper
ceil(midi_note_at_zero_volts + ceil(volts * semitones_per_octave));
*/
inline float vtom(float v)
{
return ceil(12.0f + ceil(v * 12.0f));
}
/** Midi to 1v/oct helper
fmax(midi_note - midi_note_at_zero_volts),0) * (1.0f / semitones_per_octave);
using fmax(float a, float b) to prevent negative results
*/
inline float mtov(float m)
{
return fmax((m - 12.0f),0) * (1.0f / 12.0f);
}
/** 1v/oct to frequency helper
powf(2, (((volts * semitones_per_octave) + midi_note_at_zero_volts) - A4_midi_note) / semitones_per_octave) * A4_frequency
*/
inline float vtof(float v)
{
return powf(2, (((v * 12.0f) + 12.0f) - 69.0f) / 12.0f) * 440.0f;
}
/** frequency to 1v/oct helper
(A4_midi_note + logf(frequency / A4_frequency) / logf(2.0f) * semitones_per_octave) - midi_note_at_zero_volts) * (1.0f / semitones_per_octave);
*/
inline float ftov(float f)
{
return (69.0f + (logf(f / 440.0f) / logf(2.0f) * 12.0f) - 12.0f) * (1.0f / 12.0f);
}