music-pitch
is the bridge between midi, your app and your synthetizers. It's a small (2.5kb minified) and fast library to manipulate note names, midi notes and note frequencies.
var pitch = require('music-pitch')
// get a note from midi
var note = pitch.fromMidi( ... )
// write it to the console
console.log(note)
// give it to your synth
synth.play(pitch.toFreq(note))
Install via npm: npm install --save music-pitch
and require it.
#### Browsers
No distribution (yet). Use webpack, browserify or a similar tool.
The str
function returns the scientific notation of a given note if valid. Can be used to check if its a valid note:
pitch.str('bbb') // => 'Bbb'
pitch.str('fx4') // => 'F##4'
if (pitch.str(str) !== null) { /* valid pitch str */ }
You can get also pitch classes (pitches without octaves), note letter, octave and accidentals:
pitch.pitchClass('c##5') // => 'C##'
pitch.letter('eb3') // => 'E'
pitch.octave('eb3') // => 3
pitch.accidentals('eb3') // => 'b'
You have two functions for converting from and to midi numbers:
pitch.toMidi('A4') // => '69'
pitch.fromMidi(69) // => 'A4'
#### Working with frequencies
The same way, you have two frequency related functions:
pitch.toFreq('A4') // => 440
pitch.fromFreq(440) // => 'A4'
#### Using different pitch notation
In the case scientific notation is not what you need, you can always use pitch array notation for every function that expects a string:
pitch.toFreq([5, 0, 3]) // => 220
Also you can covert from scientific notation to pitch array notation with the pitch function:
pitch('A3') // => [5, 0, 3]
That's all for this library, but maybe you need:
## API
-
Get the accidentals from a pitch
Name Type Description pitch
String | Array the pitch
- Source:
the pitch accidentals
- Type
- String
pitch.accidentals('C##3') // => '##' pitch.accidentals('Bb4') // => 'b' pitch.accidentals('E') // => ''
-
Get the distance in cents between pitches or frequencies
Name Type Description from
String | Integer first pitch or frequency
to
String | Integer other pitch or frequency
decimals
Integer the decimal precision (2 by default)
- Source:
the distance in cents
- Type
- Integer
cents(440, 444) // => 15.66 cents('A4', 444) // => 15.66 cents('A4', 'A#4') // => 100
-
Get the pitch of a given frequency.
It will round the frequency to the nearest pitch frequency. Use
cents
function if you need to know the difference between the the frequency and the pitch.Name Type Description freq
Float the frequency
- Source:
- See:
the pitch
- Type
- String
pitch.fromFreq(440) // => 'A4' pitch.fromFreq(443) // => 'A4' pitch.cents(443, 'A4') // => ... to get the difference
-
Get the pitch of the given midi number
This method doesn't take into account diatonic spelling. Always the same pitch class is given to the same midi number.
Name Type Description midi
Integer the midi number
- Source:
the pitch
- Type
- String
pitch.fromMidi(69) // => 'A4'
-
Get letter of the pitch (in uppercase)
Name Type Description pitch
String | Array the pitch as string or array
the letter of the pitch in uppercase
- Type
- String
pitch.letter('fx') // => 'F'
-
Get the octave of a pitch
Name Type Description pitch
String | Array the pitch
the octave number
- Type
- Integer
pitch.octave('F#3') // => 3
-
Get the pitch class (pitch name without octaves) from a pitch
Name Type Description pitch
String | Array the pitch to get the pitchClass number from
the pitch class
- Type
- String
pitch.pitchClass('a4') // => 'A' pitch.pitchClass('ab') // => 'Ab' pitch.pitchClass('cx2') // => 'C##'
-
Get pitch name in scientific notation
Name Type Description pitch
String | Array the pitch string or array
the name of the pitch
- Type
- String
pitch.str('cb') // => 'Cb' pitch.str('fx2') // => 'F##2'
-
Get the pitch frequency in hertzs
Name Type Description pitch
String the pitch
tuning
Integer optional tuning, 440 by default
- Source:
- the pitch frequency
- Type
- Float
pitch.toFreq('A4') // => 440 pitch.toFreq('A3', 444) // => 222
-
Get the midi number of a pitch
Name Type Description pitch
String | Array the pitch string (or pitch array)
- Source:
the midi number
- Type
- Integer
pitch.toMidi('A4') // => 69 pitch.toMidi('A3') // => 57
generated with docme
MIT License