all files / semitones/ index.js

87.5% Statements 7/8
83.33% Branches 5/6
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26                                  29× 29× 29× 29× 29×    
'use strict'
 
var parse = require('music-notation/pitch/parse')
 
/**
 * Get the size in semitones of an interval or a note. If applied to a note, it
 * get the size in semitones from 'C0' to that note.
 *
 * @name semitones
 * @function
 * @param {String|Array} pitch - the pitch to get the semitones size from (in
 * string or array notetion)
 * @return {Integer} the size in semitones, null if not valid pitch
 *
 * @example
 * var semitones = require('semitones')
 * semitones('P4') // => 5
 */
module.exports = function (pitch) {
  var p = Array.isArray(pitch) ? pitch : parse(pitch)
  Iif (!p) return null
  var f = p[0] * 7
  var o = p.length > 1 ? p[1] : -Math.floor(f / 12)
  return f + o * 12
}