tonal-note

Parse and manipulate music notes in scientific notation

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
tonal-note
2.2.24 years ago7 years agoMinified + gzip package size for tonal-note in KB

Readme

Note

npm version tonal

tonal-note is a collection of functions to manipulate musical notes in scientific notation

This is part of tonal music theory library.

Usage

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

Install

npm install tonal-note

API Documentation


* _static_
    * [.midi](#module_Note.midi) ⇒ <code>Number</code>
    * [.freqToMidi](#module_Note.freqToMidi) ⇒ <code>Integer</code>
    * [.from](#module_Note.from)
    * [.simplify](#module_Note.simplify) ⇒ <code>string</code>
    * [.props()](#module_Note.props) ⇒ <code>string</code>
    * [.name()](#module_Note.name) ⇒ <code>string</code>
    * [.midiToFreq(note)](#module_Note.midiToFreq) ⇒ <code>Number</code>
    * [.chroma(note)](#module_Note.chroma) ⇒ <code>Integer</code>
    * [.altToAcc(props, [baseNote])](#module_Note.altToAcc) ⇒ <code>string</code>
    * [.build(midi, useSharps)](#module_Note.build) ⇒ <code>string</code>
* _inner_
    * [~props](#module_Note..props) ⇒ <code>Object</code>
    * [~names(accTypes)](#module_Note..names) ⇒ <code>Array</code>
    * [~tokenize(str)](#module_Note..tokenize) ⇒ <code>Array</code>
    * [~midi(note)](#module_Note..midi) ⇒ <code>Integer</code>
    * [~freqToMidi(frequency)](#module_Note..freqToMidi) ⇒ <code>Number</code>
    * [~stepToLetter(step)](#module_Note..stepToLetter) ⇒ <code>string</code>
    * [~altToAcc(alt)](#module_Note..altToAcc) ⇒ <code>string</code>
    * [~simplify(note, useSameAccType)](#module_Note..simplify) ⇒ <code>string</code>

Note.midi ⇒ Number

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

| | tuning | Number |

(Optional) 440 by default

|

Note.freqToMidi ⇒ Integer

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
Note.chroma("Cb") // => 11
["C", "D", "E", "F"].map(Note.chroma) // => [0, 2, 4, 5]

Note.from

Deprecated. This is kept for backwards compatibility only. Use Note.from instead


Kind: static property of Note

Note.simplify ⇒ string

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
Note.enharmonic("Db") // => "C#"
Note.enhramonic("C") // => "C"

Note.props() ⇒ string

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
Note.name("cb2") // => "Cb2"
["c", "db3", "2", "g+", "gx4"].map(Note.name) // => ["C", "Db3", null, null, "G##4"]

Note.name() ⇒ string

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
Note.pc("Db3") // => "Db"
["db3", "bb6", "fx2"].map(Note.pc) // => [ "Db", "Bb", "F##"]

Note.midiToFreq(note) ⇒ Number

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
Note.freq("A4") // => 440
Note.freq(69) // => 440

Note.chroma(note) ⇒ Integer

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
Note.oct("C#4") // => 4
Note.oct("C") // => null
Note.oct("blah") // => undefined

Note.altToAcc(props, baseNote) ⇒ string

Creates a note name in scientific notation from note properties, and optionally another note name. It receives an object with:

  • step: the note step (0 = C, 1 = D, ... 6 = B)
  • alt: (optional) the alteration. Negative numbers are flats, positive sharps
  • oct: (optional) the octave

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

| | baseNote | string |

note to build the result from. If given, it returns the result of applying the given props to this note.

|
Example
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"

Note.build(midi, useSharps) ⇒ string

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

| | useSharps | boolean |

(Optional) set to true to use sharps instead of flats

|
Example
Note.fromMidi(61) // => "Db4"
Note.fromMidi(61, true) // => "C#4"
// it rounds to nearest note
Note.fromMidi(61.7) // => "D4"

Note~props ⇒ Object

Get note properties. It returns an object with the following information:

  • name {string}: the note name. The letter is always in uppercase
  • letter {string}: the note letter, always in uppercase
  • acc {string}: the note accidentals
  • octave {Number}: the octave or null if not present
  • pc {string}: the pitch class (letter + accidentals)
  • step {Number}: number equivalent of the note letter. 0 means C ... 6 means B.
  • alt {Number}: number equivalent of accidentals (negative are flats, positive sharps)
  • chroma {Number}: number equivalent of the pitch class, where 0 is C, 1 is C# or Db, 2 is D...
  • midi {Number}: the note midi number (IMPORTANT! it can be outside 0 to 127 range)
  • freq {Number}: the frequency using an equal temperament at 440Hz

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
Note.props("fx-3").name // => "F##-3"
Note.props("invalid").name // => null
Note.props("C#3").oct // => 3
Note.props().oct // => null

Note~names(accTypes) ⇒ Array

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
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" ]

Note~tokenize(str) ⇒ Array

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
Note.tokenize("C#2") // => ["C", "#", "2", ""]
Note.tokenize("Db3 major") // => ["D", "b", "3", "major"]
Note.tokenize("major") // => ["", "", "", "major"]
Note.tokenize("##") // => ["", "##", "", ""]
Note.tokenize() // => ["", "", "", ""]

Note~midi(note) ⇒ Integer

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


See: midi.toMidi
| Param | Type | Description | | --- | --- | --- | | note | string \| Number |

the note to get the midi number from

|
Example
Note.midi("C4") // => 60
Note.midi(60) // => 60

Note~freqToMidi(frequency) ⇒ Number

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
Note.freqToMidi(220)); //=> 57;
Note.freqToMidi(261.62)); //=> 60;
Note.freqToMidi(261)); //=> 59.96;

Note~stepToLetter(step) ⇒ string

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
Note.stepToLetter(3) // => "F"

Note~altToAcc(alt) ⇒ string

Given an alteration number, return the accidentals


Kind: inner method of Note
| Param | Type | | --- | --- | | alt | Number |
Example
Note.altToAcc(-3) // => "bbb"

Note~simplify(note, useSameAccType) ⇒ string

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

| | useSameAccType | boolean |

(optional, true by default) set to true to ensure the returned note has the same accidental types that the given note

|
Example
Note.simplify("C##") // => "D"
Note.simplify("C###") // => "D#"
Note.simplify("C###", false) // => "Eb"
Note.simplify("B#4") // => "C5"