UNPKG

twelvetet

Version:

A minimalistic twelve-tone equal temperament libray for Javascript

2 lines (1 loc) 3.99 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.TwelveTet=t()}(this,function(){"use strict";var n=Math.log2,t=Math.pow,e=Math.round,r=t(2,1/12);function i(n,e){return n*t(r,e)}function u(t,e){return 12*n(e/t)}function o(n){return"number"==typeof n&&isFinite(n)&&n>0}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]],c=function(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n},s=function(n){if(!function(n){return/^\[object Array\]$/.test(Object.prototype.toString.call(n))}(n))throw Error("Missing or invalid value. Array expected.");var t=n[0],e=n[1];if(!c(t))throw Error("Missing or invalid pitch class. Integer expected.");if(!c(e))throw Error("Missing or invalid octave. Integer expected.");return l.filter(function(n){return n[1]===t}).map(function(n){return null!=n[0]?n[0]+e:null})},f=function(n){if("string"!=typeof n)throw new TypeError("Missing or invalid value. String expected.");var t=n.match(/^(.*?)(-?\d+)$/i);if(null==t)return null;var e=t[1].toLowerCase().replace(/^./,function(n){return n.toUpperCase()}),r=l.find(function(n){return n[0]===e});return null==r?null:[r[1],+t[2]]},a=function(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")},p=Math.floor,y=Math.round,h=function(){function n(t,r){if(a(this,n),!o(t))throw new TypeError("Missing or invalid argument, 'inputFrequency'.");if(!o(r))throw new TypeError("Missing or invalid argument, 'tuningFrequency'");this._inputFrequency=t,this._tuningFrequency=r,this._frequency=function(n,t){return i(t,e(u(t,n)))}(t,r)}return n.prototype.class=function(){return(21+y(u(this._tuningFrequency,this._frequency))%12)%12},n.prototype.octave=function(){return p(4+(9+u(this._tuningFrequency,this._frequency))/12)},n.prototype.offset=function(){return u(this._inputFrequency,this._frequency)},n.prototype.next=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;if(!v(t))throw new TypeError("Missing or invalid argument, 'semitones'. Integer expected.");return new n(i(this._frequency,t),this._tuningFrequency)},n.prototype.previous=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;if(!v(t))throw new TypeError("Missing or invalid argument, 'semitones'. Integer expected.");return new n(i(this._frequency,-t),this._tuningFrequency)},n.prototype.intervalTo=function(n){return y(u(this._frequency,g(n,this._tuningFrequency)))},n.prototype.intervalFrom=function(n){return y(u(g(n,this._tuningFrequency),this._frequency))},n.prototype.toString=function(){var n=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t=s([this.class(),this.octave()]);return t[2]||t[n?3:1]},n.prototype.valueOf=function(){return this._frequency},n.prototype.equals=function(t){var e=n.create(t,this._tuningFrequency);return this.class()===e.class()&&this.octave()===e.octave()},n}();function g(n,t){if(n instanceof h)return n._inputFrequency;if("string"==typeof n){var e=f(n);if(null==e)throw Error("Invalid argument, 'value'.");return i(t,12*e[1]+e[0]-57)}if(!o(n))throw new TypeError("Missing or invalid argument, 'value'.");return n}function v(n){return"number"==typeof n&&isFinite(n)&&p(n)===n}return h.create=function(n,t){return new h(g(n,t),t)},function(){function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:440;a(this,n),this._tuningFrequency=t}return n.prototype.pitch=function(n){return h.create(n,this._tuningFrequency)},n}()});