-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemper.min.js
1 lines (1 loc) · 11.1 KB
/
temper.min.js
1
(function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F={}.hasOwnProperty,G=function(a,b){function c(){this.constructor=a}for(var d in b)F.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};E={type:function(a){var b;return void 0===a||null===a?!1:(b={"[object Number]":"number","[object String]":"string","[object Array]":"array","[object Object]":"object"},b[toString.call(a)])},normalize:function(a,b){var c;return null==b&&(b=3),c=Math.pow(10,b),Math.round(a*c)/c},list:function(a,b){var c,d,e,f,g,h;if(a&&this[a])return this[a];if(b){for(g=[],e=0,f=this.length;f>e;e++)d=this[e],g.push(d);return g}h=[];for(c in this)h.push(c);return h},centOffset:function(a,b){return Math.round(1200*Math.log(a/b)/Math.log(2))},ratioFromCents:function(a){return Math.pow(2,a/100/12)},stepRatio:Math.log(Math.pow(2,1/12))},y=["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"],z=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],s=["C","F","Bb","Eb","Ab","Db","Gb","Cb"],i=function(){function a(a,b){var c,d,e,f=this;"undefined"!=typeof window&&null!==window&&(this._play=b.play,this._pluck=b.pluck),this.rootFrequency=b.rootFrequency(),this.temperament=b._temperament,e=function(){return f.rootFrequency*Math.pow(2,f.octave)},d=function(b){var c,d;if(c=function(){var a,b,c;return a=f.getNoteArray(f.letter),b=a.indexOf(f.letter),c=E.ratioFromCents(D[f.temperament][b]),E.normalize(c*e())},d=/([A-Ga-g])([b#]?)(\d+)/.exec(b),null!=d)return f.octave=parseInt(d[3],10),f.accidental=d[2],f.letter=d[1].toUpperCase(),null!=f.accidental&&(f.letter+=f.accidental),f.frequency=E.normalize(c()),f.name=a;throw new TypeError("Note name "+b+" is not a valid argument")},c=function(a){var b,c;if(c=function(){var a,b,c,d;return a=Math.log(f.frequency/e()),b=E.getCentOffset(f.frequency,e),console.log(b),d=Math.round(a/E.stepRatio),12===d&&(f.octave+=1),c=f.getNoteArray(),c[d%12]},3e4>a&&a>0)return f.octave=Math.floor(Math.log(a/f.rootFrequency)/Math.log(2)),f.frequency=E.normalize(a),f.letter=c(),f.name=f.letter+f.octave.toString(),b=f.name.match(/[b#]/),f.accidental=null!=b?b:"";throw new RangeError("Frequency "+a+" is not valid")},"string"===E.type(a)&&d(a),"number"===E.type(a)&&c(a),this.midiNote=Math.round(12*Math.log(this.frequency/440)/Math.log(2)+69)}return a.prototype.getNoteArray=function(a){return s.indexOf(a)>-1?y:z},a}(),"undefined"!=typeof window&&null!==window&&(i.prototype.play=function(a){return this._play.call(this,a)},i.prototype.pluck=function(a){return this._pluck.call(this,a)}),o="U",u="m2",e="M2",v="m3",f="M3",k="P4",r="d5",l="P5",w="m6",g="M6",x="m7",h="M7",j="O",a="d5",t=[o,u,e,v,f,k,r,l,w,g,x,h,j],d=function(a){function b(a,c,d,e){var f,g,h=this;this.direction=null!=d?d:"up",null==e&&(e="0"),this.tonic=c.tonic,g=function(a,b){var c,d,e,f,g;return h.intervalName=a,g=t.indexOf(a),f=h.tonic.getNoteArray(),e=h.tonic.octave,"up"===h.direction?(e+=parseInt(b,10),d=f.indexOf(h.tonic.letter)+g,d>=12&&(e+=1)):"down"===h.direction&&(e-=parseInt(b,10),d=f.indexOf(h.tonic.letter)-g,0>d&&(d+=12,e-=1)),c=f[d%12]+e.toString()},f=function(){var a,b,c,d,e;return e=h.getNoteArray.call(h.tonic),a=h.getNoteArray(),d=e.indexOf(h.tonic.letter),b=a.indexOf(h.letter),h.direction="down",c=(12-b+d)%12,(h.tonic.octave<h.octave||h.tonic.octave===h.octave&&b>d)&&(d>b&&(b+=12),c=b-d,h.direction="up"),t[c]},t.indexOf(a)>-1&&(a=g(a,e)),b.__super__.constructor.call(this,a,c),E.type(this.intervalName)||(this.intervalName=f()),this.tonic.name===this.name&&(this.direction="-"),this.frequencies=[this.tonic.frequency,this.frequency],this.names=[this.tonic.name,this.name],this.midiNotes=[this.tonic.midiNote,this.midiNote]}return G(b,a),b}(i),"undefined"!=typeof window&&null!==window&&(d.prototype.play=function(a){return this.play.call(this,a,2),this.play.call(this.tonic,a,2)},d.prototype.pluck=function(a){return this.pluck.call(this,a,2),this.pluck.call(this.tonic,a,2)}),c=function(){function a(a,b,c){var e,f,g,h,i,j,k=this;for(this.tonic=b.tonic,this.notes=[],this.name="unknown","undefined"!=typeof window&&null!==window&&(this._play=b.play,this._pluck=b.pluck),f=function(a){var e,f,g,h,i;if(c[a]){for(k.name=a,h=c[a],i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(k.notes.push(new d(e,b)));return i}throw new TypeError("Name "+a+" is not a valid argument")},e=function(a){var e,f,g,h,i,j,l,m,n,o,p,q;for(m=[],g=o=0,p=a.length;p>o;g=++o)j=a[g],j.indexOf(",")>-1?(e=j.split(","),j=e[0].trim(),f=e[1].trim(),l=e[2].trim(),h=new d(j,b,f,l)):h=new d(j,b),m.push(h.intervalName),k.notes.push(h);q=[];for(i in c){if(n=c[i],JSON.stringify(n)===JSON.stringify(m)){k.name=i;break}q.push(void 0)}return q},"string"===E.type(a)&&f(a),"array"===E.type(a)&&e(a),this.frequencies=[this.tonic.frequency],this.names=[this.tonic.name],this.midiNotes=[this.tonic.midiNote],j=this.notes,h=0,i=j.length;i>h;h++)g=j[h],this.frequencies.push(g.frequency),this.names.push(g.name),this.midiNotes.push(g.midiNote)}return a}(),p={maj:[f,l],maj6:[f,l,g],maj7:[f,l,h],majb6:[f,l,w],majb7:[f,l,x],min6:[v,l,w],min7:[v,l,x],minM7:[v,l,h],min:[v,l],aug:[f,w],aug7:[f,w,h],dim:[v,r],dim7:[v,r,g],sus4:[k,l],sus2:[e,l],dream:[k,r,l]},b=function(a){function b(a,c){b.__super__.constructor.call(this,a,c,p)}return G(b,a),b}(c),"undefined"!=typeof window&&null!==window&&(b.prototype.play=function(a){var b,c,d,e,f,g=this;for(this._play.call(this.tonic,a,this.notes.length+1),f=this.notes,c=function(b){return g._play.call(b,a,g.notes.length+1)},d=0,e=f.length;e>d;d++)b=f[d],c(b);return this},b.prototype.pluck=function(a){var b,c,d,e,f,g=this;for(this._pluck.call(this.tonic,a,this.notes.length+1),f=this.notes,c=function(b){return g._pluck.call(b,a,g.notes.length+1)},d=0,e=f.length;e>d;d++)b=f[d],c(b);return this}),B={Major:[e,f,k,l,g,h,j],"Melodic Minor":[e,v,k,l,w,x,j],"Harmonic Minor":[e,v,k,l,w,h,j],Chromatic:[u,e,v,f,k,r,l,w,g,x,h,j],Ionian:[e,f,k,l,g,h,j],Dorian:[e,v,k,l,g,x,j],Phrygian:[u,v,k,l,w,x,j],Lydian:[e,f,a,l,g,h,j],Mixolydian:[e,f,k,l,g,x,j],Aeolian:[e,v,k,l,w,x,j],Locrian:[u,v,k,r,w,x,j],"Whole Tone":[e,f,r,w,x,j],Acoustic:[e,f,r,l,g,x,j],Enigmatic:[u,f,r,w,x,h,j],"Enigmatic Minor":[u,v,r,l,x,h,j],Neapolitan:[u,v,k,k,l,w,h,j],Prometheus:[e,f,r,g,x,j],"In Sen":[u,k,l,x,j],In:[u,k,l,w,j],Hirajoshi:[u,k,l,w,j],Yo:[e,k,l,g,j],Iwato:[u,k,r,x,j],Pentatonic:[e,f,l,g,j],"Octatonic I":[e,v,k,r,w,g,h,j],"Octatonic II":[u,v,f,r,l,g,x,j],Tritone:[u,f,r,l,x,j],Hungarian:[e,v,r,l,w,h,j]},m=function(a){function b(a,c){b.__super__.constructor.call(this,a,c,B)}return G(b,a),b}(c),"undefined"!=typeof window&&null!==window&&(m.prototype.play=function(a){var b,c,d,e,f,g,h=this;for(null==a&&(a=1),this._play.call(this.tonic,a,3),g=this.notes,d=function(c){return setTimeout(function(){return h._play.call(c,a,3)},1e3*a/2*(b+1))},b=e=0,f=g.length;f>e;b=++e)c=g[b],d(c);return this},m.prototype.pluck=function(a){var b,c,d,e,f,g,h=this;for(null==a&&(a=1),this._pluck.call(this.tonic,a,3),f=this.notes,g=[],b=d=0,e=f.length;e>d;b=++d)c=f[b],g.push(function(c){return setTimeout(function(){return h._pluck.call(c,a,3)},1e3*a/2*(b+1)),h}(c));return g}),D={Equal:[0,100,200,300,400,500,600,700,800,900,1e3,1100,1200],Pythagorean:[0,113.69,203.91,294.13,407.82,498.04,611.73,701.95,815.64,905.87,996.09,1109.78,120],Natural:[0,111.74,203.91,315.64,386.31,498.05,603,702,813.69,884.36,1017.6,1088.27,1200],"1/3 comma Meantone":[0,63.5,189.57,315.64,379.14,505.21,568.72,694.79,758.29,884.36,1010.43,1073.93,1200],"2/7 comma Meantone":[0,70.67,191.62,312.57,383.24,504.19,574.86,695.81,766.48,887.43,1008.38,1079.05,1200],"1/4 comma Meantone":[0,76.05,193.16,310.26,386.31,503.42,579.47,696.58,772.63,889.74,1006.84,1082.89,1200],"1/5 comma Meantone":[0,83.58,195.31,307.04,390.61,502.35,585.92,697.65,781.23,892.96,1004.69,1088.27,1200],"1/6 comma Meantone":[0,88.59,196.74,304.89,393.48,501.63,590.22,698.37,786.96,895.11,1003.26,1091.85,1200],"1/8 comma Meantone":[0,94.87,198.53,302.2,397.07,500.73,595.6,699.27,794.13,897.8,1001.47,1096.33,1200],Ordinaire:[0,86.81,193.16,296.09,386.31,503.42,584.85,696.58,788.76,889.74,1005.22,1082.89,1200],"Werckmeister III":[0,90.22,192.18,294.14,390.23,498.05,588.27,696.09,792.18,888.27,996.09,1092.18,1200],Kirnberger:[0,91,192,296,387,498,591,696,792,890,996,1092,1200],"Kirnberger III":[0,90.18,193.16,294.13,386.31,498.04,590.22,696.58,792.18,889.74,996.09,1088.27,1200],Young:[0,93.69,195.91,298.13,391.82,502.04,591.73,697.96,796.18,893.87,1000.09,1089.78,1200]},D["1/11 comma Meantone"]=D.Equal,D.Salinas=D["1/3 comma Meantone"],D.Aaron=D["1/4 comma Meantone"],D.Silbermann=D["1/5 comma Meantone"],n=function(){function a(a,b,c){this.tuningFrequency=null!=b?b:440,this._temperament=null!=c?c:"Equal",null!=a&&(this.tonic=new i(a,this)),this.tonic}return a.prototype.rootFrequency=function(){var a;return a=E.ratioFromCents(D[this._temperament][9]),this.tuningFrequency/Math.pow(2,4)/a},a.prototype.note=function(a){return null==a?this.tonic:(this.tonic=new i(a,this),null!=this._interval&&this.interval(this._interval.name),null!=this._scale&&this.scale(this._scale.names),null!=this._chord?this.chord(this._chord.name):void 0)},a.prototype.temperament=function(a){return null!=a?(D[a]&&(this._temperament=a,this.tonic=this.note(this.tonic.name),null!=this._interval&&this.interval(this._interval.name),null!=this._scale&&this.scale(this._scale.names),null!=this._chord&&this.chord(this._chord.name)),this):this._temperament},a.prototype.interval=function(a,b,c){return null!=a?this._interval=new d(a,this,b,c):this._interval},a.prototype.scale=function(a){return null!=a?this._scale=new m(a,this):this._scale},a.prototype.chord=function(a){return null!=a?this._chord=new b(a,this):this._chord},a}(),C=function(a,b,c){return new n(a,b,c)},C.note=function(a){return new n(a)},C.tonic=function(a){return new n(a)},C.chords=function(a){return E.list.call(p,a)},C.scales=function(a){return E.list.call(B,a)},C.intervals=function(a){return E.list.call(t,a,!0)},C.temperaments=function(a){return E.list.call(D,a)},C.centOffset=E.centOffset,A=this,"undefined"!=typeof exports?module.exports=C:A.temper=C,"undefined"!=typeof window&&null!==window&&(window.AudioContext=window.AudioContext||window.webkitAudioContext,q=new AudioContext,n.prototype.play=function(a,b){var c,d,e,f,g;return null==a&&(a=2),null==b&&(b=1),g=.9/b,c=q.currentTime,d=c+a,e=q.createOscillator(),e.type="sine",e.frequency.value=this.frequency?this.frequency:this.tonic.frequency,f=q.createGain(),f.gain.setValueAtTime(0,c),f.gain.linearRampToValueAtTime(g,d-.5*a),f.gain.linearRampToValueAtTime(0,d),e.connect(f),f.connect(q.destination),e.start(c),e.stop(d)},n.prototype.pluck=function(a,b){var c,d,e,f,g,h,i,j,k;return null==a&&(a=2),null==b&&(b=1),j=.9/b,d=q.currentTime,e=d+a,k=function(a){var b,c,d,e,f,g,h,i,j;for(f=[],i=new Float32Array(q.sampleRate),g=Math.floor(q.sampleRate/a),d=0;g>d;)f[d]=2*Math.random()-1,d++;for(h=0,j=function(){var a,b;return a=f.shift(),b=(a+h)/2,h=b,f.push(b),b},b=.5*Math.random()+.4,e=0;e<q.sampleRate;)i[e]=j(),c=b-e/q.sampleRate*b,i[e]=i[e]*c,e++;return i},i=q.createGain(),g=q.createBufferSource(),f=this.frequency?this.frequency:this.tonic.frequency,h=k(f),c=q.createBuffer(1,h.length,q.sampleRate),c.getChannelData(0).set(h),g.buffer=c,g.connect(i),i.connect(q.destination),i.gain.value=j,g.start(d),g.stop(e)})}).call(this);