twelvetet-spn
Version:
Scientific pitch notation parser/formatter for Node.js and the browser
2 lines (1 loc) • 1.61 kB
JavaScript
!function(n,l){"object"==typeof exports&&"undefined"!=typeof module?l(exports):"function"==typeof define&&define.amd?define(["exports"],l):l(n.TwelveTetSPN={})}(this,function(n){"use strict";var l=[[null,0],["B#",0],["C",0],[null,0],["Dbb",0],["Bx",1],["C#",1],[null,1],["Db",1],[null,1],["Cx",2],[null,2],["D",2],[null,2],["Ebb",2],[null,3],["D#",3],[null,3],["Eb",3],["Fbb",3],["Dx",4],[null,4],["E",4],["Fb",4],[null,4],[null,5],["E#",5],["F",5],[null,5],["Gbb",5],["Ex",6],["F#",6],[null,6],["Gb",6],[null,6],["Fx",7],[null,7],["G",7],[null,7],["Abb",7],[null,8],["G#",8],[null,8],["Ab",8],[null,8],["Gx",9],[null,9],["A",9],[null,9],["Bbb",9],[null,10],["A#",10],[null,10],["Bb",10],["Cbb",10],["Ax",11],[null,11],["B",11],["Cb",11],[null,11]],e=function(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n};n.format=function(n){if(!function(n){return/^\[object Array\]$/.test(Object.prototype.toString.call(n))}(n))throw Error("Missing or invalid value. Array expected.");var r=n[0],t=n[1];if(!e(r))throw Error("Missing or invalid pitch class. Integer expected.");if(!e(t))throw Error("Missing or invalid octave. Integer expected.");return l.filter(function(n){return n[1]===r}).map(function(n){return null!=n[0]?n[0]+t:null})},n.parse=function(n){if("string"!=typeof n)throw new TypeError("Missing or invalid value. String expected.");var e=n.match(/^(.*?)(-?\d+)$/i);if(null==e)return null;var r=e[1].toLowerCase().replace(/^./,function(n){return n.toUpperCase()}),t=l.find(function(n){return n[0]===r});return null==t?null:[t[1],+e[2]]},Object.defineProperty(n,"__esModule",{value:!0})});