JSPM

interval-parser

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q28295F
  • License MIT

Parse musical intervals in shorthand notation

Package Exports

  • interval-parser

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (interval-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

interval-parser npm

js-standard-style license

Parses music intervals in shorthand notation:

var parser = require('interval-parser')
parser.parse('P4')
// => { num: 4, q: 'P', dir: 1, simple: 4, type: 'P', alt: 0, oct: 0, semitones: 5 }

// accepts reverse shorthand notation
parser.parse('6m')
// => { 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.semitones('6m') // => 8
parser.simple('9M') // => 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 = require('interval-parser')
parser.parse('P4')
// => { num: 4, q: 'P', dir: 1, simple: 4, type: 'P', alt: 0, oct: 0, semitones: 5 }
// accepts reverse shorthand notation
parser.parse('6m')
// => { 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 number
  • q: 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 quality
  • oct: 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.num('9m') // => 9
parser.q('9m') // => 'm'
parser.simple('9m') // => 2
parser.dir('9m') // => 1
parser.type('9m') // => 'M'
parser.alt('9m') // => -1
parser.oct('9m') // => 1
parser.semitones('9m') // => 13

License

MIT License