note-transposer
Version:
Music transposition made easy
39 lines (36 loc) • 1.4 kB
JavaScript
var parse = require('music-notation/pitch/parse')
var str = require('music-notation/pitch/str')
var operation = require('music-notation/operation')(parse, str)
/**
* Transposes a note by an interval.
*
* Given a note and an interval it returns the transposed note. It can be used
* to add intervals if both parameters are intervals.
*
* The order of the parameters is indifferent.
*
* This function is currified so it can be used to map arrays of notes.
*
* @name transpose
* @function
* @param {String|Array} interval - the interval. If its false, the note is not
* transposed.
* @param {String|Array} note - the note to transpose
* @return {String|Array} the note transposed
*
* @example
* var transpose = require('note-transposer')
* transpose('3m', 'C4') // => 'Eb4'
* transpose('C4', '3m') // => 'Eb4'
* tranpose([1, 0, 2], [3, -1, 0]) // => [3, 0, 2]
* ['C', 'D', 'E'].map(transpose('3M')) // => ['E', 'F#', 'G#']
*/
var transpose = operation(function (i, n) {
if (i === false) return n
else if (!Array.isArray(i) || !Array.isArray(n)) return null
else if (i.length === 1 || n.length === 1) return [n[0] + i[0]]
var d = i.length === 2 && n.length === 2 ? null : n[2] || i[2]
return [n[0] + i[0], n[1] + i[1], d]
})
if (typeof module === 'object' && module.exports) module.exports = transpose
if (typeof window !== 'undefined') window.transpose = transpose