https://github.com/danigb/pitch-parser
Music pitch parser for javascript
https://github.com/danigb/pitch-parser
Last synced: 11 months ago
JSON representation
Music pitch parser for javascript
- Host: GitHub
- URL: https://github.com/danigb/pitch-parser
- Owner: danigb
- License: mit
- Created: 2015-10-12T02:40:20.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-10-17T16:47:14.000Z (over 10 years ago)
- Last Synced: 2025-05-17T18:08:26.284Z (about 1 year ago)
- Language: JavaScript
- Size: 148 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
# pitch-parser
[](https://travis-ci.org/danigb/pitch-parser)
[](https://codeclimate.com/github/danigb/pitch-parser)
[](https://github.com/feross/standard)
[](https://badge.fury.io/js/pitch-parser)
Music pitch parser for javascript.
Given a pitch string it returns a [pitch array](https://github.com/danigb/a-pitch), and given a pitch array it returns a pitch string:
```js
var pitch = require('pitch-parser')
pitch('C#4') // => [0, 2, 4]
pitch([0, 2, 4]) // => 'C#4'
pitch('Ebb3') // => [2, -2, 3]
pitch([2, -2, 3]) // => 'Ebb3'
pitch('A#') // => [5, 1, null]
pitch([5, 1, null]) // => 'A#'
```
The returned value is an array of 3 integers with the following form `[letter, accidentals, octave]` where:
- __letter__: a positive integer between 0 and 6 to represent C, D... to B
- __accidentals__: is an integer to represent pitch accidentals. 0 means no accidentals, negatives values are for flats and positives for sharps
- __octave__: (Optional) the octave number or null if not specified
## API
-
pitch(val) → {Array|String}
-
Converts pitches between strings and array notation
This functions parses a string in the form 'letter + accidentals [+ octave]'.
The letter can be upper or down case and the accidentals can be sharps #
flats b or double sharps x.
The pitch array notation is 3 integers is in the form [letter, accidentals, octave].
This function caches the result to get better performance. If for some
reason you don't want to cache, use pitch.parse and pitch.build
Parameters:
Name
Type
Description
val
String
|
Array
the pitch (can be a string or array)
Returns:
the converted val (string if it was an array,
and array if it was string)
-
Type
-
Array
|
String
Examples
pitch('C4') // => [0, 0, 4]
pitch([0, 0, 4]) // => 'C4'
// parse
pitch('c2') // => [0, 0, 2]
pitch('F#') // => [4, 1, null] (no octave)
// build
pitch([2, -1, 3]) // => 'Eb3'
pitch([6, -2, null]) // => 'Bbb'
// return scientific notation
pitch(pitch('cbb')) // => 'Cbb'
pitch(pitch('fx')) // => 'F##'
*generated with [docme](https://github.com/thlorenz/docme)*
## License
MIT License