JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6227
  • Score
    100M100P100Q127603F
  • License MIT

Functions to work with midi numbers

Package Exports

  • @tonaljs/midi

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@tonaljs/midi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@tonaljs/midi tonal npm version

A collection of functions to work with midi numbers.

Install

npm i --save @tonaljs/midi

or

yarn add @tonaljs/midi

API

toMidi(note: string | number) => number | null

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

midiToFreq(midi: number, tuning = 440) => number

Given a midi number, return the frequency:

Examples:

midiToFreq(60); // => 261.6255653005986
midiToFreq(69); // => 440
midiToFreq(69, 443); // => 443

midiToNoteName(midi: number) => string

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"

freqToMidi(freq: number) => number

Get the midi number from a frequency in hertz. The midi number can have decimals (with two digits precission)

Examples:

freqToMidi(220)); //=> 57
freqToMidi(261.62)); //=> 60
freqToMidi(261)); //=> 59.96