https://github.com/danigb/music-pitch
Note names, midi, frequencies
https://github.com/danigb/music-pitch
Last synced: about 1 year ago
JSON representation
Note names, midi, frequencies
- Host: GitHub
- URL: https://github.com/danigb/music-pitch
- Owner: danigb
- License: mit
- Created: 2015-10-13T09:03:59.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-15T17:44:20.000Z (over 10 years ago)
- Last Synced: 2025-03-25T13:46:30.771Z (about 1 year ago)
- Language: JavaScript
- Size: 156 KB
- Stars: 19
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
# music-pitch
[](https://travis-ci.org/danigb/music-pitch)
[](https://codeclimate.com/github/danigb/music-pitch)
[](https://github.com/feross/standard)
[](https://badge.fury.io/js/music-pitch)
`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.
```js
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))
```
## Installation
#### For node
Install via npm: `npm install --save music-pitch` and require it.
#### Browsers
No distribution (yet). Use webpack, browserify or a similar tool.
## Usage
#### Note names
The `str` function returns the [scientific notation](https://en.wikipedia.org/wiki/Scientific_pitch_notation) of a given note if valid. Can be used to check if its a valid note:
```js
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:
```js
pitch.pitchClass('c##5') // => 'C##'
pitch.letter('eb3') // => 'E'
pitch.octave('eb3') // => 3
pitch.accidentals('eb3') // => 'b'
```
#### Working with midi
You have two functions for converting from and to midi numbers:
```js
pitch.toMidi('A4') // => '69'
pitch.fromMidi(69) // => 'A4'
```
#### Working with frequencies
The same way, you have two frequency related functions:
```js
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](https://github.com/danigb/a-pitch) for every function that expects a string:
```js
pitch.toFreq([5, 0, 3]) // => 220
```
Also you can covert from scientific notation to pitch array notation with the pitch function:
```js
pitch('A3') // => [5, 0, 3]
```
#### More...
That's all for this library, but maybe you need:
- [pitch-transpose](https://github.com/danigb/pitch-transpose)
- [music-interval](https://github.com/danigb/music-interval)
- [music-scale](https://github.com/danigb/music-scale)
## API
-
accidentals(pitch) → {String}
-
Get the accidentals from a pitch
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch
- Source:
Returns:
the pitch accidentals
-
Type
-
String
Example
pitch.accidentals('C##3') // => '##'
pitch.accidentals('Bb4') // => 'b'
pitch.accidentals('E') // => ''
-
cents(from, to, decimals) → {Integer}
-
Get the distance in cents between pitches or frequencies
Parameters:
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:
Returns:
the distance in cents
-
Type
-
Integer
Example
cents(440, 444) // => 15.66
cents('A4', 444) // => 15.66
cents('A4', 'A#4') // => 100
-
fromFreq(freq) → {String}
-
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.
Parameters:
Name
Type
Description
freq
Float
the frequency
- Source:
- See:
-
Returns:
the pitch
-
Type
-
String
Example
pitch.fromFreq(440) // => 'A4'
pitch.fromFreq(443) // => 'A4'
pitch.cents(443, 'A4') // => ... to get the difference
-
fromMidi(midi) → {String}
-
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.
Parameters:
Name
Type
Description
midi
Integer
the midi number
- Source:
Returns:
the pitch
-
Type
-
String
Example
pitch.fromMidi(69) // => 'A4'
-
letter(pitch) → {String}
-
Get letter of the pitch (in uppercase)
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch as string or array
Returns:
the letter of the pitch in uppercase
-
Type
-
String
Example
pitch.letter('fx') // => 'F'
-
octave(pitch) → {Integer}
-
Get the octave of a pitch
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch
Returns:
the octave number
-
Type
-
Integer
Example
pitch.octave('F#3') // => 3
-
pitchClass(pitch) → {String}
-
Get the pitch class (pitch name without octaves) from a pitch
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch to get the pitchClass number from
Returns:
the pitch class
-
Type
-
String
Example
pitch.pitchClass('a4') // => 'A'
pitch.pitchClass('ab') // => 'Ab'
pitch.pitchClass('cx2') // => 'C##'
-
str(pitch) → {String}
-
Get pitch name in scientific notation
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch string or array
Returns:
the name of the pitch
-
Type
-
String
Example
pitch.str('cb') // => 'Cb'
pitch.str('fx2') // => 'F##2'
-
toFreq(pitch, tuning) → {Float}
-
Get the pitch frequency in hertzs
Parameters:
Name
Type
Description
pitch
String
the pitch
tuning
Integer
optional tuning, 440 by default
- Source:
Returns:
- the pitch frequency
-
Type
-
Float
Example
pitch.toFreq('A4') // => 440
pitch.toFreq('A3', 444) // => 222
-
toMidi(pitch) → {Integer}
-
Get the midi number of a pitch
Parameters:
Name
Type
Description
pitch
String
|
Array
the pitch string (or pitch array)
- Source:
Returns:
the midi number
-
Type
-
Integer
Example
pitch.toMidi('A4') // => 69
pitch.toMidi('A3') // => 57
*generated with [docme](https://github.com/thlorenz/docme)*
## License
MIT License