pitch-parser

October 17, 2015 · View on GitHub

Build Status Code Climate js-standard-style npm version

Music pitch parser for javascript.

Given a pitch string it returns a pitch array, and given a pitch array it returns a pitch string:

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)

Source:
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

License

MIT License