-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebfontplayer.min.js
1 lines (1 loc) · 3.05 KB
/
webfontplayer.min.js
1
"use strict";function WebAudioFontPlayer(){return this.envelopes=[],this.onCacheFinish=null,this.onCacheProgress=null,this.afterTime=.05,this.nearZero=1e-6,this.limitVolume=function(e){return e?e*=1:e=.5,e},this.resumeContext=function(e){try{"suspended"==e.state&&"AudioContext"==e.constructor.name&&(console.log("audioContext.resume",e),e.resume())}catch(e){}},this.queueWaveTable=function(e,o,t,u,r,i,n,a){this.resumeContext(e),n=this.limitVolume(n);var l=t.zones[0];if(l.buffer){var s=l.originalPitch-0-l.fineTune,c=1*Math.pow(2,(100*r-s)/1200),d=u;d<e.currentTime&&(d=e.currentTime);var f=i+this.afterTime;f>l.buffer.duration/c&&(f=l.buffer.duration/c);var h=this.findEnvelope(e,o,d,f);if(this.setupEnvelope(e,h,l,n,d,f,i),h.audioBufferSourceNode=e.createBufferSource(),h.audioBufferSourceNode.playbackRate.setValueAtTime(c,0),a&&a.length>0){h.audioBufferSourceNode.playbackRate.setValueAtTime(c,u);for(var m=0;m<a.length;m++){var p=1*Math.pow(2,(100*a[m].pitch-s)/1200),v=u+a[m].when;h.audioBufferSourceNode.playbackRate.linearRampToValueAtTime(p,v)}}return h.audioBufferSourceNode.buffer=l.buffer,h.audioBufferSourceNode.loop=!1,h.audioBufferSourceNode.connect(h),h.audioBufferSourceNode.start(d,l.delay),h.audioBufferSourceNode.stop(d+f),h.when=d,h.duration=f,h.pitch=r,h.preset=t,h}console.log("empty buffer ",l)},this.noZeroVolume=function(e){return e>this.nearZero?e:this.nearZero},this.setupEnvelope=function(e,o,t,u,r,i,n){o.gain.setValueAtTime(this.noZeroVolume(0),e.currentTime);var a=0,l=0,s=n,c=t.ahdsr;i<s+this.afterTime&&(s=i-this.afterTime),c?c.length>0||(c=[{duration:0,volume:1},{duration:.5,volume:1},{duration:1.5,volume:.5},{duration:3,volume:0}]):c=[{duration:0,volume:1},{duration:s,volume:1}],o.gain.cancelScheduledValues(r),o.gain.setValueAtTime(this.noZeroVolume(c[0].volume*u),r);for(var d=0;d<c.length;d++)if(c[d].duration>0){if(c[d].duration+a>s){var f=l-(1-(c[d].duration+a-s)/c[d].duration)*(l-c[d].volume);o.gain.linearRampToValueAtTime(this.noZeroVolume(u*f),r+s);break}a+=c[d].duration,l=c[d].volume,o.gain.linearRampToValueAtTime(this.noZeroVolume(u*l),r+a)}o.gain.linearRampToValueAtTime(this.noZeroVolume(0),r+s+this.afterTime)},this.findEnvelope=function(e,o,t,u){for(var r=null,i=0;i<this.envelopes.length;i++){var n=this.envelopes[i];if(n.target==o&&e.currentTime>n.when+n.duration+.001){try{n.audioBufferSourceNode.disconnect(),n.audioBufferSourceNode.stop(0),n.audioBufferSourceNode=null}catch(e){}r=n;break}}return r||((r=e.createGain()).target=o,r.connect(o),r.cancel=function(){r.when+r.duration>e.currentTime&&(r.gain.cancelScheduledValues(0),r.gain.setTargetAtTime(1e-5,e.currentTime,.1),r.when=e.currentTime+1e-5,r.duration=0)},this.envelopes.push(r)),r},this.cancelQueue=function(e){for(var o=0;o<this.envelopes.length;o++){var t=this.envelopes[o];t.gain.cancelScheduledValues(0),t.gain.setValueAtTime(this.nearZero,e.currentTime),t.when=-1;try{t.audioBufferSourceNode.disconnect()}catch(e){console.log(e)}}},this}"object"==typeof module&&module.exports&&(module.exports=WebAudioFontPlayer),"undefined"!=typeof window&&(window.WebAudioFontPlayer=WebAudioFontPlayer);