'use strict'
var props = require('../array/to-props')
var cache = {}
/**
* Get a string with a [shorthand interval notation](https://en.wikipedia.org/wiki/Interval_(music)#Shorthand_notation)
* from interval in [array notation](https://github.com/danigb/music.array.notation)
*
* The returned string has the form: `number + quality` where number is the interval number
* (positive integer for ascending intervals, negative integer for descending intervals, never 0)
* and the quality is one of: 'M', 'm', 'P', 'd', 'A' (major, minor, perfect, dimished, augmented)
*
* @name interval.str
* @function
* @param {Array} interval - the interval in array notation
* @return {String} the interval string in shorthand notation or null if not valid interval
*
* @example
* var str = require('music-notation/interval/str')
* str([1, 0, 0]) // => '2M'
* str([1, 0, 1]) // => '9M'
*/
module.exports = function (arr) {
if (!Array.isArray(arr) || arr.length !== 2) return null
var str = '|' + arr[0] + '|' + arr[1]
return str in cache ? cache[str] : cache[str] = build(arr)
}
var ALTER = {
P: ['dddd', 'ddd', 'dd', 'd', 'P', 'A', 'AA', 'AAA', 'AAAA'],
M: ['ddd', 'dd', 'd', 'm', 'M', 'A', 'AA', 'AAA', 'AAAA']
}
var TYPES = 'PMMPPMM'
function build (coord) {
var p = props(coord)
var t = TYPES[p[0]]
var dir, num, alt
// if its descening, invert number
if (p[2] < 0) {
dir = -1
num = (8 - p[0]) - 7 * (p[2] + 1)
alt = t === 'P' ? -p[1] : -(p[1] + 1)
} else {
dir = 1
num = p[0] + 1 + 7 * p[2]
alt = p[1]
}
var q = ALTER[t][4 + alt]
return dir * num + q
}
|