- pitch-utils
- Installation
- Usage
- Conversion Overview
- Type Aliases
- Variables
- Functions
- centsToHz
- centsToRatio
- centsToSemitones
- cleanNoteName
- formatHz
- getNoteIndexInOctave
- getRoundingFunction
- hzToCents
- hzToNoteName
- hzToNoteObject
- hzToRatio
- hzToSemitones
- namedNoteToCents
- namedNoteToHz
- namedNoteToRatio
- namedNoteToSemitones
- quantizeHz
- ratioToCents
- ratioToHz
- ratioToSemitones
- semitonesToCents
- semitonesToHz
- semitonesToRatio
- Classes
This (ESM) module provides a collection of functions for converting between pitch and frequency units.
npm install pitch-utils
import { hzToSemitones } from "pitch-utils";
hzToSemitones(880, 440); // +12
→ hz | → ratio | → semitones | → cents | → named | → note object | |
---|---|---|---|---|---|---|
hz → | N/A | hzToRatio | hzToSemitones | hzToCents | hzToNoteName | hzToNoteObject |
ratio → | ratioToHz | N/A | ratioToSemitones | ratioToCents | Unimplemented | Unimplemented |
semitones → | semitonesToHz | semitonesToRatio | N/A | semitonesToCents | Unimplemented | Unimplemented |
cents → | centsToHz | centsToRatio | centsToSemitones | N/A | Unimplemented | Unimplemented |
named → | namedNoteToHz | namedNoteToRatio | namedNoteToSemitones | namedNoteToCents | N/A | Unimplemented |
Ƭ Cents: number
A granular pitch offset unit, e.g. +100
, -200
, 0
.
Supports positive and negative numbers.
Ƭ Hz: number
A frequency unit reflecting the number of cycles per second, e.g. 440
, 523.2511
, or 1600
(1.6kHz).
Supports positive numbers.
Ƭ NoteName: string
A note name with its octave, e.g. C4
, A♯3
, F♯5
.
Also accepts lowercase and keyboard-accessible accidentals like bb3
and b#3
.
Ƭ NoteObject: Object
Object with note properties for flexible formatting.
Name | Type |
---|---|
detune |
Cents |
hz |
Hz |
note |
NoteName |
octave |
Octave |
Ƭ Octave: number
Integer pitch grouping, e.g. -1
, 4
, 10
.
Ƭ Ratio: number
A frequency ratio, e.g. 1.5
, 2
, 0.5
.
Supports positive numbers.
Ƭ RoundingMethod: "nearest"
| "up"
| "down"
Rounding method for converting between frequency units.
Todo
maybe eventually this can include hz rounding in addition to pitch rounding
Ƭ Semitones: number
A semitone pitch offset, e.g. +3
, -5
, 0
.
Supports positive and negative numbers.
• Const
A4: 440
A4 frequency in Hz
• Const
chromaticScale: string
[]
Normalized note names in the chromatic scale, using sharps
• Const
enharmonicChromaticScale: string
[][]
Note names with alternate enharmonic names
▸ centsToHz(cents
, baseHz?
): Hz
Example
centsToHz(1200) // 880
Name | Type | Default value |
---|---|---|
cents |
number |
undefined |
baseHz |
number |
A4 |
▸ centsToRatio(cents
): Ratio
Example
centsToRatio(1200) // 2
Name | Type |
---|---|
cents |
number |
▸ centsToSemitones(cents
): Semitones
Example
centsToSemitones(100) // +1
Name | Type |
---|---|
cents |
number |
▸ cleanNoteName(dirtyNote
): string
Replaces keyboard-accessible accidentals with their unicode equivalents and makes note name uppercase.
Example
cleanNoteName("C#4") // "C♯4"
cleanNoteName("bb4") // "B♭4"
Name | Type | Description |
---|---|---|
dirtyNote |
string |
dirty note name, with name, optional accidental, and octave |
string
▸ formatHz(hz
, precision?
, alwaysIncludeSign?
): string
Formats a number in Hz to a string with kilohertz support Assumes tabular numeral usage, and includes trailing zeros for alignment.
Example
formatHz(232.5) // "232.50Hz"
formatHz(2325) // "2.33kHz"
formatHz(2325, 2, true) // "+2.33kHz"
Name | Type | Default value | Description |
---|---|---|---|
hz |
number |
undefined |
- |
precision |
number |
2 |
- |
alwaysIncludeSign |
boolean |
false |
whether to include (+) signs |
string
▸ getNoteIndexInOctave(note
): number
Name | Type |
---|---|
note |
string |
number
▸ getRoundingFunction(roundingMethod
): (x
: number
) => number
| (x
: number
) => number
| (x
: number
) => number
Selects a Math.* rounding function based on RoundingMethod union type
Name | Type |
---|---|
roundingMethod |
RoundingMethod |
(x
: number
) => number
| (x
: number
) => number
| (x
: number
) => number
▸ hzToCents(targetHz
, baseHz?
): Cents
Example
hzToCents(880, 440) // -1200
Name | Type | Default value |
---|---|---|
targetHz |
number |
undefined |
baseHz |
number |
A4 |
▸ hzToNoteName(hz
, roundingMethod?
): string
Example
hzToNoteName(260) // C
hzToNoteName(260, Math.floor) // B
hzToNoteName(263) // C
hzToNoteName(263, Math.ceil) // C♯
Name | Type | Default value | Description |
---|---|---|---|
hz |
number |
undefined |
frequency of note in hertz |
roundingMethod |
RoundingMethod |
"nearest" |
whether to round up, down, or naturally |
string
▸ hzToNoteObject(hz
): NoteObject
Name | Type | Description |
---|---|---|
hz |
number |
frequency of note in hertz |
▸ hzToRatio(targetHz
, baseHz?
): Ratio
Example
hzToRatio(880) // 2
hzToRatio(440, 880) // 0.5
Name | Type | Default value | Description |
---|---|---|---|
targetHz |
number |
undefined |
target frequency in hertz |
baseHz |
number |
A4 |
base frequency in hertz |
▸ hzToSemitones(targetHz
, baseHz?
): Semitones
Example
hzToSemitones(880, 440) // -12
Name | Type | Default value | Description |
---|---|---|---|
targetHz |
number |
undefined |
target frequency in hertz |
baseHz |
number |
A4 |
base frequency in hertz |
▸ namedNoteToCents(note
): Cents
Example
namedNoteToCents("C4") // -900
Name | Type | Description |
---|---|---|
note |
string |
note name, e.g. C4, A♯3, F♯5 |
▸ namedNoteToHz(note
): Hz
Example
namedNoteToHz("C4") // 261.6256
namedNoteToHz("A♯3") // 233.0819
Name | Type | Description |
---|---|---|
note |
string |
note name, e.g. C4, A♯3, F♯5 |
▸ namedNoteToRatio(note
, baseNote?
): Ratio
Example
namedNoteToRatio("A4") // 1
namedNoteToRatio("A♯3") // 0.5
Name | Type | Default value |
---|---|---|
note |
string |
undefined |
baseNote |
string |
"A4" |
▸ namedNoteToSemitones(note
): Semitones
Example
namedNoteToSemitones("C4") // +3
namedNoteToSemitones("A♯3") // -11
Name | Type |
---|---|
note |
string |
▸ quantizeHz(hz
, roundingMethod?
): Hz
Example
quantizeHz(450) // 440
quantizeHz(450, "down") // 440
quantizeHz(450, "up") // ~466.17
Name | Type | Default value |
---|---|---|
hz |
number |
undefined |
roundingMethod |
RoundingMethod |
"nearest" |
▸ ratioToCents(ratio
): Cents
Example
ratioToCents(2) // 1200
ratioToCents(3) // 1902
Name | Type | Description |
---|---|---|
ratio |
number |
decimal or fractional ratio |
▸ ratioToHz(ratio
, baseHz?
): Hz
Example
ratioToHz(2) // 880
ratioToHz(3) // 1320
Name | Type | Default value | Description |
---|---|---|---|
ratio |
number |
undefined |
decimal or fractional ratio |
baseHz |
number |
A4 |
optional base note |
▸ ratioToSemitones(ratio
): Semitones
Example
ratioToSemitones(2) // 12
ratioToSemitones(3) // ~19.02
Name | Type | Description |
---|---|---|
ratio |
number |
decimal or fractional ratio |
▸ semitonesToCents(semitones
): Cents
Example
semitonesToCents(-12) // -1200
semitonesToCents(0.5) // 50
Name | Type | Description |
---|---|---|
semitones |
number |
semitone offset |
▸ semitonesToHz(semitones
, baseHz?
): Hz
Example
semitonesToHz(3) // 523.2511
semitonesToHz(-3, 523.2511) // 440
Name | Type | Default value | Description |
---|---|---|---|
semitones |
number |
undefined |
semitone offset |
baseHz |
number |
A4 |
optional base note |
▸ semitonesToRatio(semitones
): Ratio
Example
semitonesToRatio(12) // 2
semitonesToRatio(-12) // 0.5
Name | Type | Description |
---|---|---|
semitones |
number |
semitone offset |
Example
const note = new Pitch(440)
note.noteObject.note // "A4"
note.modRatio(3/1)
note.noteObject.note // "E6"
• new Pitch(frequency?
)
Name | Type | Default value | Description |
---|---|---|---|
frequency |
number |
A4 |
frequency of note in hertz |
• detune: (cents
: number
) => Pitch
▸ (cents
): Pitch
####### Parameters
Name | Type |
---|---|
cents |
number |
####### Returns
• frequency: number
= A4
frequency of note in hertz
• hz: number
base value for calculations
• transpose: (semitones
: number
) => Pitch
▸ (semitones
): Pitch
####### Parameters
Name | Type |
---|---|
semitones |
number |
####### Returns
• get
cents(): number
number
• get
closestNoteAbove(): NoteObject
• get
closestNoteBelow(): NoteObject
• get
noteObject(): NoteObject
• get
ratio(): number
number
• get
semitones(): number
number
▸ addCents(cents
): Pitch
Name | Type |
---|---|
cents |
number |
▸ addSemitones(semitones
): Pitch
Name | Type |
---|---|
semitones |
number |
▸ modRatio(ratio
): Pitch
Name | Type |
---|---|
ratio |
number |
▸ quantize(roundingMethod?
): Pitch
Name | Type | Default value |
---|---|---|
roundingMethod |
RoundingMethod |
"nearest" |
▸ shift(hz
): Pitch
Name | Type |
---|---|
hz |
number |
▸ Static
fromNamedNote(note
): Pitch
initialize from NamedNote
Example
Pitch.fromNamedNote("A3").hz // 220
Name | Type |
---|---|
note |
string |