A collection of functions to work with midi numbers.
ES6:
import { Midi } from "@tonaljs/tonal";
nodejs:
const { Midi } = require("@tonaljs/tonal");
Given a note name or number, return the midi number. Midi numbers are always in range 0..127
Examples:
toMidi("C4"); // => 60
toMidi("#"); // => null
toMidi(60); // => 60
toMidi("60"); // => 60
toMidi(-1); // => null
Given a midi number, return the frequency:
Examples:
midiToFreq(60); // => 261.6255653005986
midiToFreq(69); // => 440
midiToFreq(69, 443); // => 443
Given a midi number, returns a note name. The altered notes will have flats unless explicitly set with the optional useSharps
parameter.
Examples:
midiToNoteName(61); // => "Db4"
midiToNoteName(61, { pitchClass: true }); // => "Db"
midiToNoteName(61, { sharps: true }); // => "C#4"
midiToNoteName(61, { pitchClass: true, sharps: true }); // => "C#"
// it rounds to nearest note
midiToNoteName(61.7); // => "D4"
Given a frequency in hertz, returns the midi number. The midi number can have decimals (with two digits precision)
Examples:
freqToMidi(220); //=> 57
freqToMidi(261.62); //=> 60
freqToMidi(261); //=> 59.96