Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.37k stars 976 forks source link

`Tone.ftom()` method precision #1119

Open joex92 opened 1 year ago

joex92 commented 1 year ago

The feature you'd like when converting from MIDI value to Frequency, you can use decimals on the midi values to get a more precise frequency, but it doesn't the other way around. It would be really useful to be able to get MIDI decimals values when converting from Frequency to Midi.

Any alternatives you've considered I haven't go through the source code, but if there is already code to use MIDI decimal values to convert to frequency, then there must be some Math.round method used on the ftom method. Honestly I don't want to use an extra library on my project just to get a precise value instead of a round value...

Additional context here's an example of how it should work using Max/MSP. if you use ftom by default, it would round the result value: image But if you initialized it with a float number, it won't round the result value: image

the Tone.ftom() method could have and additional input boolean value to either round the result value or not... If there was already a way, then I don't know how to use it, because there is no info about the mtof and ftom methods on the Tone.JS docs...

tmhglnd commented 1 year ago

The formula for converting a frequency to a midi value (without rounding) is:

function ftom(f) {
  return Math.log2(f / 440) * 12 + 69;
}