npm.io
4.13.0 • Published 22h ago

@tonaljs/note

Licence
MIT
Version
4.13.0
Deps
0
Vulns
0
Weekly
0

@tonaljs/note tonal npm version

A collection of functions to manipulate musical notes

import { Note } from "tonal";

Note.get("C4"); // => { midi: 60 ...}
Note.name("fx4"); // => "F##4"
Note.pitchClass("Ab5"); // => "Ab"
Note.octave("C4"); // => 4
Note.midi("A4"); // => 69
Note.fromMidi(61); // => "Db4"
Note.enharmonic("C#"); // => "Db"

Usage

ES6:

import { Note } from "tonal";

nodejs:

const { Note } = require("tonal");

single module:

import { get } from "@tonaljs/note";

Note properties

Note.get

get(noteName: string) => Note

Given a note name, it returns an object with the following properties:

  • name: the note name
  • pc: the pitch class name
  • letter: the note letter
  • step: the letter number (0..6)
  • acc: the note accidentals
  • alt: the accidental number (..., -1 = 'b', 0 = '', 1 = '#', ...)
  • oct: the octave (or null if not present)
  • chroma: the note chroma (0..11)
  • height: a number to compare and sort notes by pitch. Equals the midi number when the note has an octave (even outside the 0..127 midi range). Pitch classes get a large negative value, so they sort before any note with octave
  • midi: the note midi or null if octave is not present
  • freq: the note frequency in Hertzes, or null if the octave is not present
Note.get("C4"); // => { name: "C4", midi: 60, ... }

It has several shorthands to retrieve properties easily:

Note.name("fx4"); // => "F##4"
Note.pitchClass("Ab5"); // => "Ab"
Note.accidentals("Eb"); // => 'b'
Note.octave("C4"); // => 4
Note.midi("A4"); // => 69
Note.freq("A4"); // => 440
Note.chroma("D"); // => 2

["C", "D", "E"].map(Note.chroma); // => [0, 2, 4]
Note.fromMidi

fromMidi(midi: number) => string

Given a midi number, returns the note name. This function is the same as midiToNoteName from @tonaljs/midi

Note.fromMidi(61); // => "Db4"
Note.fromMidi(61.7); // => "D4"
[60, 61, 62].map(Note.fromMidi); // => ["C4", "Db4", "D4"]

There's also a Note.fromMidiSharps version:

Note.fromMidiSharps(61); // => "C#4"
Note.fromFreq

fromFreq(freq: number) => string

Given a frequency in Hz, returns the note name.

Note.fromFreq(440); // => "A4"

It rounds to the nearest name:

[440, 550, 660].map(t.Note.fromFreq); // => [ 'A4', 'Db5', 'E5' ]

There's also a Note.fromFreqSharps version:

[440, 550, 660].map(t.Note.fromFreqSharps); // => [ 'A4', 'C#5', 'E5' ]

Transposition and distances

Note.transpose

transpose(note: string, interval: string) => string

Transpose a note by an interval. It returns the note name or "" if not valid parameters.

Examples:

Note.transpose("d3", "3M"); // => "F#3"
Note.transpose("D", "3M"); // => "F#"

transposeBy and transposeFrom are curried versions of this function to make easy work with arrays:

["C", "D", "E"].map(Note.transposeBy("5P"));
// => ["G", "A", "B"]
["1P", "3M", "5P"].map(Note.transposeFrom("C"));
// => ["C", "E", "G"]

Note.tr, Note.trBy and Note.trFrom are short aliases of Note.transpose, Note.transposeBy and Note.transposeFrom (and Note.trFifths of Note.transposeFifths).

Note.transposeFifths

transposeFifths(noteName: string, fifths: number) => string

Transpose a note a given number of fifths:

Note.transposeFifths("G4", 3); // => "E6"
Note.transposeFifths("G", 3); // => "E"

[0, 1, 2, 3, 4, 5, 6].map((n) => Note.transposeFifths("F#", n));
// => ["F#", "C#", "G#", "D#", "A#", "E#", "B#"]
[0, -1, -2, -3, -4, -5, -6].map((n) => Note.transposeFifths("Bb", n));
// => ["Bb", "Eb", "Ab", "Db", "Gb", "Cb", "Fb"]
Note.transposeOctaves

transposeOctaves(noteName: string, octaves: number) => string

Transpose a note a given number of octaves. Pitch classes (notes without octave) stay the same:

Note.transposeOctaves("C4", 2); // => "C6"
Note.transposeOctaves("C4", -1); // => "C3"
Note.transposeOctaves("C", 1); // => "C"
Note.distance

distance(from: string, to: string) => string

Find the interval between two notes:

Note.distance("C", "D"); // => "2M"
Note.distance("C3", "E3"); // => "3M"
Note.distance("C3", "E4"); // => "10M"

Register

Note.fold

fold(notes: string[], range: [low: string, high: string], options?: { each?: boolean }) => string[]

Move notes by octaves into a range of notes (both ends included), keeping their spelling. By default the whole figure moves together, so it keeps its shape: it moves by the fewest octaves that put every note in the range, and doesn't move if it's already there:

Note.fold(["C2", "E2", "G2", "Bb2"], ["E2", "G3"]); // => ["C3", "E3", "G3", "Bb3"]
Note.fold(["C5", "E5", "G5"], ["C3", "C5"]); // => ["C4", "E4", "G4"]

With each: true, each note moves on its own by the fewest octaves that put it in the range:

Note.fold(["C2", "E2", "G2", "Bb2"], ["E2", "G3"], { each: true });
// => ["C3", "E2", "G2", "Bb2"]

The rules at the edges:

  • A figure that doesn't fit in the range at any octave has its lowest note put in the lowest octave at or above low: Note.fold(["C2", "E4"], ["E2", "G3"]) is ["C3", "E5"].
  • In a range under an octave, a note may have no octave inside it: with each: true it goes to the octave nearest the range, and a tie goes up.
  • To keep the shape where it fits and fix only the notes at the edges, fold the whole figure first and then each note: Note.fold(Note.fold(notes, range), range, { each: true }).

A pitch class (a note without octave) has no octave to keep, so it goes on its own to the lowest octave at or above low (or, in a range under an octave, to the octave nearest the range), whatever the mode. It places notes in a register:

Note.fold(["D", "G", "C"], ["E2", "D3"]); // => ["D3", "G2", "C3"]
Note.fold(["C2", "E2", "G"], ["E2", "G3"]); // => ["C3", "E3", "G2"]

It returns [] if the notes are empty or one isn't a note, or if the range isn't two notes with octave from low to high. (This fold moves notes into a register; it isn't the fold of functional programming, which is reduce.)

Named collections

Note.names

names(array?: any[]) => string[]

Get note names of an array of anything. Notice that names are normalized:

Note.names(["fx", "bb", 12, "nothing", {}, null]); // => ["F##", "Bb"]

Without parameters, it returns a list of natural pitch classes:

Note.names(); // => ["C", "D", "E", "F", "G", "A", "B"]

You may be in a situation where you have note names with octaves, but don't want them, remove them like this:

Note.names(["C2", "C#3", "Db4", 12, "nothing", {}, null]).map(Note.pitchClass); // => ["C", "C#", "Db"]
Note.sortedNames

sortedNames(array?: any[], comparator?: NoteComparator) => string[]

Sort an array of note names in ascending order. Pitch classes are listed before notes. Anything that is not a note is removed:

Note.sortedNames(["c2", "c5", "c1", "c0", "c6", "c"]);
// => ['C', 'C0', 'C1', 'C2', 'C5', 'C6']
Note.sortedNames(["c", "F", "G", "a", "b", "h", "J"]);
// => ['C', 'F', 'G', 'A', 'B']

An optional comparator can be passed as a second argument. Note.ascending (the default) and Note.descending are provided:

Note.sortedNames(["c2", "c5", "c1", "c0", "c6", "c"], Note.descending);
// => ['C6', 'C5', 'C2', 'C1', 'C0', 'C']
Note.sortedUniqNames

sortedUniqNames(array?: any[]) => string[]

Sort notes ascending and remove duplicates. Enharmonic notes are different names, so both are kept:

Note.sortedUniqNames(["C4", "c4", "B#3", "C4"]); // => ["C4", "B#3"]

Enharmonics

Note.simplify

simplify(noteName: string) => string

Given a note name, return the same note with less accidentals (or "" if not a valid note):

Note.simplify("C#"); // => "C#"
Note.simplify("C##"); // => "D"
Note.simplify("C###"); // => "D#"
Note.enharmonic

enharmonic(noteName: string, pitchClass?: string) => string

Given a note name, returns its enharmonic (or "" if not valid note):

Note.enharmonic("C#"); // => "Db"
Note.enharmonic("C##"); // => "D"
Note.enharmonic("C###"); // => "Eb"

Note.enharmonic("C##b"); // => ""

Using enharmonic on a natural will return the same value passed in:

Note.enharmonic("C"); // => "C"
Note.enharmonic("C4"); // => "C4"

The destination pitch class can be enforced to calculate the octave:

Note.enharmonic("F2", "E#"); // => "E#2"
Note.enharmonic("B2", "Cb"); // => "Cb3"
Note.enharmonic("C2", "B#"); // => "B#1"

Enforced pitch class must have the same chroma as the note, otherwise "" is returned:

Note.enharmonic("F2", "Eb"); // => ""

Keywords