Parse and manipulate music notes in scientific notation
tonal-note is a collection of functions to manipulate musical notes in scientific notation
This is part of tonal music theory library.
import * as Note from "tonal-note"
// or const Note = require("tonal-note")
Note.name("bb2") // => "Bb2"
Note.chroma("bb2") // => 10
Note.midi("a4") // => 69
Note.freq("a4") // => 440
Note.oct("G3") // => 3// part of tonal
const Tonal = require("tonal")
// or import Note from "tonal"
Tonal.Note.midi("d4") // => 62
* Note
* _static_
* .midi ⇒ Number
* .freqToMidi ⇒ Integer
* .from
* .simplify ⇒ string
* .props() ⇒ string
* .name() ⇒ string
* .midiToFreq(note) ⇒ Number
* .chroma(note) ⇒ Integer
* [.altToAcc(props, [baseNote])](#module_Note.altToAcc) ⇒ string
* .build(midi, useSharps) ⇒ string
* _inner_
* ~props ⇒ Object
* ~names(accTypes) ⇒ Array
* ~tokenize(str) ⇒ Array
* ~midi(note) ⇒ Integer
* ~freqToMidi(frequency) ⇒ Number
* ~stepToLetter(step) ⇒ string
* ~altToAcc(alt) ⇒ string
* ~simplify(note, useSameAccType) ⇒ string
Get the frequency from midi number
Kind: static property of Note
Returns: Number -
the frequency or null if not valid note midi
| Param | Type | Description |
| --- | --- | --- |
| midi | Number |
the note midi number
|Number | (Optional) 440 by default
|Return the chroma of a note. The chroma is the numeric equivalent to the
pitch class, where 0 is C, 1 is C# or Db, 2 is D... 11 is B
Kind: static property of Note
Returns: Integer -
the chroma number
| Param | Type | Description |
| --- | --- | --- |
| note | string |
the note name
|Example
``js`
Note.chroma("Cb") // => 11
["C", "D", "E", "F"].map(Note.chroma) // => [0, 2, 4, 5]
Deprecated. This is kept for backwards compatibility only.
Use Note.from instead
Kind: static property of Note
Get the simplified and enhramonic note of the given one.
Kind: static property of Note
Returns: string -
the enhramonic note
| Param | Type |
| --- | --- |
| note | string |
Example
`js`
Note.enharmonic("Db") // => "C#"
Note.enhramonic("C") // => "C"
Given a note name, return the note name or null if not valid note.
The note name will ALWAYS have the letter in upercase and accidentals
using # or b
Can be used to test if a string is a valid note name.
Kind: static method of Note
| Param | Type |
| --- | --- |
| | Pitch \| string |
Example
`js`
Note.name("cb2") // => "Cb2"
["c", "db3", "2", "g+", "gx4"].map(Note.name) // => ["C", "Db3", null, null, "G##4"]
Get pitch class of a note. The note can be a string or a pitch array.
Kind: static method of Note
Returns: string -
the pitch class
| Param | Type |
| --- | --- |
| | string \| Pitch |
Example
`js`
Note.pc("Db3") // => "Db"
["db3", "bb6", "fx2"].map(Note.pc) // => [ "Db", "Bb", "F##"]
Get the frequency of a note
Kind: static method of Note
Returns: Number -
the frequency
| Param | Type | Description |
| --- | --- | --- |
| note | string \| Number |
the note name or midi note number
|Example
`js`
Note.freq("A4") // => 440
Note.freq(69) // => 440
Get the octave of the given pitch
Kind: static method of Note
Returns: Integer -
the octave or null if doesn"t have an octave or not a valid note
| Param | Type | Description |
| --- | --- | --- |
| note | string |
the note
|Example
`js`
Note.oct("C#4") // => 4
Note.oct("C") // => null
Note.oct("blah") // => undefined
Creates a note name in scientific notation from note properties,
and optionally another note name.
It receives an object with:
Optionally it receives another note as a "base", meaning that any prop not explicitly
received on the first parameter will be taken from that base note. That way it can be used
as an immutable "set" operator for a that base note
Kind: static method of Note
Returns: string -
the note name in scientific notation or null if not valid properties
| Param | Type | Description |
| --- | --- | --- |
| props | Object |
the note properties
|string | note to build the result from. If given, it returns the result of applying the given props to this note.
|Example
`js`
Note.from({ step: 5 }) // => "A"
Note.from({ step: 1, acc: -1 }) // => "Db"
Note.from({ step: 2, acc: 2, oct: 2 }) // => "E##2"
Note.from({ step: 7 }) // => null
Note.from({alt: 1, oct: 3}, "C4") // => "C#3"
Given a midi number, returns a note name. The altered notes will have
flats unless explicitly set with the optional useSharps parameter.
Kind: static method of Note
Returns: string -
the note name
| Param | Type | Description |
| --- | --- | --- |
| midi | number |
the midi note number
|boolean | (Optional) set to true to use sharps instead of flats
|Example
`js`
Note.fromMidi(61) // => "Db4"
Note.fromMidi(61, true) // => "C#4"
// it rounds to nearest note
Note.fromMidi(61.7) // => "D4"
Get note properties. It returns an object with the following information:
This function always returns an object with all this properties, but if it"s
not a valid note all properties will be null.
The returned object can"t be mutated.
Kind: inner constant of Note
Returns: Object -
an object with the properties (or an object will all properties
set to null if not valid note)
| Param | Type | Description |
| --- | --- | --- |
| note | string |
the note name in scientific notation
|Example
`js`
Note.props("fx-3").name // => "F##-3"
Note.props("invalid").name // => null
Note.props("C#3").oct // => 3
Note.props().oct // => null
Get a list of note names (pitch classes) within a octave
Kind: inner method of Note
| Param | Type | Description |
| --- | --- | --- |
| accTypes | string |
(Optional, by default " b#"). A string with the accidentals types: " " means no accidental, "#" means sharps, "b" mean flats, can be combined (see examples)
|Example
`js`
Note.names(" b") // => [ "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" ]
Note.names(" #") // => [ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" ]
Split a string into tokens related to note parts.
It returns an array of strings [letter, accidental, octave, modifier]
It always returns an array
Kind: inner method of Note
Returns: Array -
an array of note tokens
| Param | Type |
| --- | --- |
| str | string |
Example
`js`
Note.tokenize("C#2") // => ["C", "#", "2", ""]
Note.tokenize("Db3 major") // => ["D", "b", "3", "major"]
Note.tokenize("major") // => ["", "", "", "major"]
Note.tokenize("##") // => ["", "##", "", ""]
Note.tokenize() // => ["", "", "", ""]
Get the note midi number. It always return a number between 0 and 127
Kind: inner method of Note
Returns: Integer -
the midi number or null if not valid pitch
| Param | Type | Description |
| --- | --- | --- |
| note | string \| Number |
the note to get the midi number from
|Example
`js`
Note.midi("C4") // => 60
Note.midi(60) // => 60
Get the midi number from a frequency in hertz. The midi number can
contain decimals (with two digits precission)
Kind: inner method of Note
| Param | Type |
| --- | --- |
| frequency | Number |
Example
`js`
Note.freqToMidi(220)); //=> 57;
Note.freqToMidi(261.62)); //=> 60;
Note.freqToMidi(261)); //=> 59.96;
Given a step number return it's letter (0 = C, 1 = D, 2 = E)
Kind: inner method of Note
Returns: string -
the letter
| Param | Type |
| --- | --- |
| step | number |
Example
`js`
Note.stepToLetter(3) // => "F"
Given an alteration number, return the accidentals
Kind: inner method of Note
| Param | Type |
| --- | --- |
| alt | Number |
Example
`js`
Note.altToAcc(-3) // => "bbb"
Simplify the note: find an enhramonic note with less accidentals.
Kind: inner method of Note
Returns: string -
the simplfiied note or null if not valid note
| Param | Type | Description |
| --- | --- | --- |
| note | string |
the note to be simplified
|boolean | (optional, true by default) set to true to ensure the returned note has the same accidental types that the given note
|Example
`js``
Note.simplify("C##") // => "D"
Note.simplify("C###") // => "D#"
Note.simplify("C###", false) // => "Eb"
Note.simplify("B#4") // => "C5"