interval-parser
Parses music intervals in shorthand notation:
var parser = parser// => { num: 4, q: 'P', dir: 1, simple: 4, type: 'P', alt: 0, oct: 0, semitones: 5 } // accepts reverse shorthand notationparser// => { num: 6, q: 'm', dir: 1, simple: 6, type: 'M', alt: -1, oct: 0, semitones: 8 }
If you only need a property, you can use a function with the property name:
parser // => 8parser // => 2
Interval string format
It accepts two different interval string formats:
- In standard shorthand notation:
quality+[dir]+num
. Examples: 'P8', 'M-3' - In reverse shorthand notation:
[dur]+num+quality
. Examples: '8P', '-3M'
API
parse(str)
Parse a string with an interval in shorthand notation and returns an object with interval properties
Parameters
str
String
the string with the interval
Examples
var parser = parser// => { num: 4, q: 'P', dir: 1, simple: 4, type: 'P', alt: 0, oct: 0, semitones: 5 }// accepts reverse shorthand notationparser// => { num: 6, q: 'm', dir: 1, simple: 6, type: 'M', alt: -1, oct: 0, semitones: 8 }
Returns an object with interval properties or null if not valid interval string:
num
: the interval numberq
: the interval quality string (M is major, m is minor, P is perfect...)simple
: the simplified number (from 1 to 7)dir
: the interval direction (1 ascending, -1 descending)type
: the interval type (P is perfectable, M is majorable)alt
: the alteration, a numeric representation of the qualityoct
: the number of octaves the interval spans. 0 for simple intervals.semitones
: the size of the interval in semitones
Helper functions
For each property of the interval there's a function with the same name that returns only that property:
parser // => 9parser // => 'm'parser // => 2parserdir'9m' // => 1parsertype'9m' // => 'M'parseralt'9m' // => -1parser // => 1parser // => 13
License
MIT License