import{e as D,j as R,l as E,q as O}from"./index.ec9f9930.js";var T={exports:{}};(function(b,N){(function(m,g){b.exports=g()})(self,()=>(()=>{var m={d:(e,t)=>{for(var s in t)m.o(t,s)&&!m.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{typeof Symbol<"u"&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},g={};function h(e,t){if(this.msg=e,this.ac=t.ac,this.sampleBank=t.sampleBank,this.outputNode=t.destination,this.cutGroups=t.cutGroups,this.eventCounter=t.eventCounter,this.audioOutputs=t.audioOutputs,this.workletsAvailable=t.workletsAvailable,typeof e.buffer=="object")this.bufferContainer=e.buffer;else{if(this.sampleBank==null)return void console.log("WebDirt: there is no sample bank, cancelling event");if(typeof this.sampleBank!="object")return void console.log("WebDirt: buffer not provided by calling application + no WebDirt sample bank");if(this.msg.n=parseInt(this.msg.n),isNaN(this.msg.n)&&(this.msg.n=0),!this.sampleBank.sampleNameExists(this.msg.s))return void console.log("WebDirt: no sample named "+this.msg.s+" exists in sample map");if(!this.sampleBank.getBufferMightSucceed(this.msg.s,this.msg.n))return}if(this.when=this.msg.when,isNaN(this.when))console.log("WebDirt: 'when' is null or not a number");else if(this.nudge=parseFloat(this.msg.nudge),isNaN(this.nudge)||(this.when=this.when+this.nudge),isNaN(parseFloat(this.msg.speed))&&(this.msg.speed=1),this.msg.speed!=0)if(isNaN(parseFloat(this.msg.note))&&(this.msg.note=0),this.msg.speed=this.msg.speed*Math.pow(2,this.msg.note/12),this.msg.begin=w(this.msg.begin,0),this.msg.end=w(this.msg.end,1),this.msg.end!=this.msg.begin){if(this.msg.begin>this.msg.end&&(this.msg.speed=-1*this.msg.speed),this.msg.speed<0&&(this.msg.begin=1-this.msg.begin,this.msg.end=1-this.msg.end),this.msg.begin>this.msg.end){let W=this.msg.begin;this.msg.begin=this.msg.end,this.msg.end=W}var s;if(this.source=s=this.ac.createBufferSource(),this.source.onended=this.disconnectHandler(),this.disconnectOnEnd(this.source),this.source.playbackRate.value=Math.abs(this.msg.speed),this.prepareBuffer(),this.buffer!=null)this.source.buffer=this.buffer,this.start();else{var a=this,n=1e3*(this.msg.when-this.ac.currentTime-.2);n<=0&&(n=1e3*(this.msg.when-this.ac.currentTime-.2)),n>0&&setTimeout(function(){a.prepareBuffer(),a.buffer!=null?(a.source.buffer=a.buffer,a.start()):(console.log("WebDirt: unable to access sample "+e.s+":"+e.n+" on second attempt"),a.stopAll())},n)}this.cut(this.msg.cut,this.msg.s),s=this.shape(s,this.msg.shape),s=this.lowPassFilter(s,this.msg.cutoff,this.msg.resonance),s=this.highPassFilter(s,this.msg.hcutoff,this.msg.hresonance),s=this.bandPassFilter(s,this.msg.bandf,this.msg.bandq),s=this.vowel(s,this.msg.vowel),s=this.delay(s,this.msg.delay,this.msg.delaytime,this.msg.delayfeedback),s=this.loop(s,this.msg.loop,this.msg.begin,this.msg.end,this.msg.speed),s=this.crush(s,this.msg.crush),s=this.coarse(s,this.msg.coarse),this.unit(this.msg.unit,this.msg.speed);var i=parseFloat(this.msg.gain);isNaN(i)&&(i=1),i>2&&(i=2),i<0&&(i=0);var o=parseFloat(this.msg.overgain);isNaN(o)||(i+=o),i=Math.pow(i,4);var r,c,l,u=parseFloat(e.pan);if(isNaN(u)&&(u=.5),this.audioOutputs==2)(u>1||u<0)&&(u-=Math.floor(u)),r=u,c=0,l=1;else{var A=(u-=Math.floor(u))*this.audioOutputs;(l=(c=Math.floor(A))+1)>=this.audioOutputs&&(l=0),r=A-Math.floor(A)}var v=this.ac.createGain();this.disconnectOnEnd(v);var S=this.ac.createGain();this.disconnectOnEnd(S),v.gain.value=Math.cos(r*Math.PI/2)*i,S.gain.value=Math.sin(r*Math.PI/2)*i,s.connect(v),s.connect(S),this.gain1=v,this.gain2=S;var k=this.ac.createChannelMerger(this.audioOutputs);this.disconnectOnEnd(k),v.connect(k,0,c),S.connect(k,0,l),k.connect(this.outputNode)}else this.stopAll()}function w(e,t){let s=parseFloat(e);return isNaN(s)?t:s>1?1:s<0?0:s}m.r(g),m.d(g,{WebDirt:()=>f}),h.prototype.prepareBuffer=function(){typeof this.bufferContainer=="object"?typeof this.bufferContainer.buffer=="object"&&(this.msg.speed>=0?this.buffer=this.bufferContainer.buffer:(typeof this.bufferContainer.reverseBuffer=="object"||(this.bufferContainer.reverseBuffer=this.reverseBuffer(this.ac,this.bufferContainer.buffer)),this.buffer=this.bufferContainer.reverseBuffer)):(this.msg.speed>=0?this.buffer=this.sampleBank.getBuffer(this.msg.s,this.msg.n):this.buffer=this.sampleBank.getReverseBuffer(this.msg.s,this.msg.n),this.buffer=this.accel(this.buffer,this.msg.accelerate,this.msg.speed))},h.prototype.disconnectOnEnd=function(e){this.source.disconnectQueue==null&&(this.source.disconnectQueue=new Array),this.source.disconnectQueue.unshift(e)},h.prototype.start=function(){let e=Math.abs(this.msg.speed),t=(this.msg.end-this.msg.begin)*this.source.buffer.duration/e,s=this.msg.begin*this.source.buffer.duration;this.source.start(this.when,s,t)},h.prototype.stopAll=function(){if(this.source!=null&&this.source.disconnectQueue!=null){for(var e=0;e1&&this.workletsAvailable){var s=new AudioWorkletNode(this.ac,"coarse-processor");return s.parameters.get("coarse").value=t,e.connect(s),this.disconnectOnEnd(s),s}return e},h.prototype.crush=function(e,t){if(t=parseInt(t),isNaN(t)&&(t=null),t!=null&&t>0&&this.workletsAvailable){var s=new AudioWorkletNode(this.ac,"crush-processor");return s.parameters.get("crush").value=t,e.connect(s),this.disconnectOnEnd(s),s}return e},h.prototype.cut=function(e,t){if(e=parseInt(e),!isNaN(e)&&e!=0){for(var s={cutGroup:e,node:this,sampleName:t},a=0;a2e4&&(t=2e4),isNaN(parseFloat(s))&&(s=0),s<0&&(s=0),s>1&&(s=1),s=1/(s=1-.999*(s*=s));var a=this.ac.createBiquadFilter();return this.disconnectOnEnd(a),a.type="highpass",a.frequency.value=t,a.Q.value=s,e.connect(a),a},h.prototype.lowPassFilter=function(e,t,s){if(isNaN(parseFloat(t))&&isNaN(parseFloat(s)))return e;isNaN(parseFloat(t))&&(t=440),t<20&&(t=20),t>2e4&&(t=2e4),isNaN(parseFloat(s))&&(s=0),s<0&&(s=0),s>1&&(s=1),s=1/(s=1-.999*(s*=s));var a=this.ac.createBiquadFilter();return this.disconnectOnEnd(a),a.type="lowpass",a.frequency.value=t,a.Q.value=s,e.connect(a),a},h.prototype.bandPassFilter=function(e,t,s){if(isNaN(parseFloat(t))&&isNaN(parseFloat(s)))return e;isNaN(parseFloat(t))&&(t=440),t<20&&(t=20),t>2e4&&(t=2e4),isNaN(parseFloat(s))&&(s=10),s<1&&(s=1),s>100&&(s=100);var a=this.ac.createBiquadFilter();return this.disconnectOnEnd(a),a.type="bandpass",a.frequency.value=t,a.Q.value=s,a.gain.value=s,e.connect(a),a},h.prototype.loop=function(e,t){if(isNaN(parseInt(t))||t==0)return e;try{var s=this.source.buffer.duration-this.msg.begin*this.source.buffer.duration-(1-this.msg.end)*this.source.buffer.duration;return this.source.loop=!0,this.source.loopStart=this.msg.begin*this.source.buffer.duration,this.source.loopEnd=this.msg.end*this.source.buffer.duration,this.source.stop(this.when+s*t/this.source.playbackRate.value),e}catch{return console.log("WebDirt Warning: buffer data not yet available to calculate loop time - no looping applied"),e}},h.prototype.accelerate=function(e,t){if(t=Math.abs(t),isNaN(parseFloat(e))&&(e=0),e!=0){e=parseFloat(e),this.source.playbackRate.setValueAtTime(t,this.when);var s=Math.abs((this.source.buffer.length*e/this.ac.sampleRate+t)/t),a=t+this.source.buffer.length*e/this.ac.sampleRate;if(a<0)this.source.buffer=this.negativeAccelerateBuffer(this.source.buffer,e,t),this.source.playbackRate.linearRampToValueAtTime(0,this.when+s),this.source.playbackRate.linearRampToValueAtTime(1,this.when+this.source.buffer.duration);else try{this.source.playbackRate.linearRampToValueAtTime(a,this.when+this.source.buffer.duration)}catch{console.log("WebDirt: Warning, buffer data not loaded, could not apply acclerate effect")}}},h.prototype.negativeAccelerateBuffer=function(e,t,s){var a=e.length,n=new Float32Array(a),i=this.ac.createBuffer(e.numberOfChannels,e.length,this.ac.sampleRate),o=new Float32Array(a),r=Math.abs(s/(s-t))*a;r=a*Math.abs((this.source.buffer.length*t/this.ac.sampleRate+s)/s)/this.source.buffer.duration,r=Math.trunc(r);for(var c=0;c=1&&(t=.999),t>0&&this.workletsAvailable){var s=new AudioWorkletNode(this.ac,"shape-processor");return s.parameters.get("shape").value=t,e.connect(s),this.disconnectOnEnd(s),s}return e},h.prototype.stop=function(e){this.gain1.gain.setValueAtTime(this.gain1.gain.value,e),this.gain1.gain.linearRampToValueAtTime(0,e+.02),this.gain2.gain.setValueAtTime(this.gain1.gain.value,e),this.gain2.gain.linearRampToValueAtTime(0,e+.02)},h.prototype.unit=function(e,t){e=="c"&&(this.source.playbackRate.value=this.source.playbackRate.value*this.source.buffer.duration)},h.prototype.vowel=function(e,t){if(typeof t!="string")return e;if((t=t.toLowerCase())=="a"||t=="e"||t=="i"||t=="o"||t=="u"){var s,a,n,i=this.ac.createGain();switch(this.disconnectOnEnd(i),t){case"a":s=p.a.freqs,a=p.a.qs,n=p.a.amps;break;case"e":s=p.e.freqs,a=p.e.qs,n=p.e.amps;break;case"i":s=p.i.freqs,a=p.i.qs,n=p.i.amps;break;case"o":s=p.o.freqs,a=p.o.qs,n=p.o.amps;break;case"u":s=p.u.freqs,a=p.u.qs,n=p.u.amps}for(var o=0;o<5;o++){var r=this.ac.createGain();this.disconnectOnEnd(r),r.gain.value=n[o];var c=this.ac.createBiquadFilter();this.disconnectOnEnd(c),c.type="bandpass",c.Q.value=a[o]/8,c.frequency.value=s[o],e.connect(c),c.connect(r),r.connect(i)}return i.gain.value=8,i}return e},h.prototype.reverseBuffer=function(e,t){for(var s=t.length,a=new Float32Array(s),n=e.createBuffer(t.numberOfChannels,t.length,e.sampleRate),i=new Float32Array(s),o=0;o{console.log("WebDirt: audio worklets added"),this.workletsAvailable=!0}).catch(t=>{console.log("WebDirt: error loading AudioWorklets.js: "+t),console.log("(WebDirt should still work but shape, coarse, and crush will have no effect)"),this.workletsAvailable=!1}):(console.log("WebDirt: browser does not support audio worklets"),console.log("(WebDirt should still work but shape, coarse, and crush will have no effect)"),this.workletsAvailable=!1),this.tempo={time:this.ac.currentTime,beats:0,bpm:30},this.clockDiff=Date.now()/1e3-this.ac.currentTime,this.sampleBank!=null&&(this.sampleBank.ac=this.ac),this.silentNote=this.ac.createOscillator(),this.silentNote.type="sine",this.silentNote.frequency.value=440,this.silentGain=this.ac.createGain(),this.silentGain.gain.value=0,this.silentNote.connect(this.silentGain),this.silentGain.connect(this.ac.destination),this.silentNote.start();var e=this;setTimeout(function(){e.silentGain.disconnect(e.ac.destination),e.silentNote.disconnect(e.silentGain),e.silentNote.stop(),e.silentGain=null,e.silentNote=null},500)}console.log("WebDirt: initializeWebAudio finished.")},f.prototype.playSample=function(e,t){if(t==null&&(t=this.latency),e.whenPosix!=null&&(e.when=e.whenPosix-(new Date().getTime()/1e3-this.ac.currentTime)),e.when==null&&(e.when=this.ac.currentTime),e.when=e.when+t,typeof e.maxLateness!="number"&&(e.maxLateness=this.maxLateness),this.ac.currentTime-e.when>e.maxLateness)return void console.log("WebDirt warning: dropping msg late by "+(this.ac.currentTime-e.when)+" seconds");let s=new h(e,this);return this.eventCounter++,s},f.prototype.playScore=function(e,t,s){this.initializeWebAudio(),t==null&&(t=this.latency);for(var a=this.ac.currentTime,n=0,i=0;in&&(n=o.when),o.when=o.when+a,o.s!=null&&(o.sample_name=o.s),o.n!=null&&(o.sample_n=o.n),this.playSample(o,t)}setTimeout(function(){typeof s=="function"&&s()},1e3*(n+t))},f.prototype.playScoreWhenReady=function(e,t,s,a){this.initializeWebAudio(),t==null&&(t=this.latency);for(var n=e.length,i=0;i{const N=async(m,g,h)=>{if(!y)throw new Error("WebDirt not initialized!");const w=m-h,{s:p,n:d=0,...f}=g.value||{};p||console.warn('Pattern.webdirt: no "s" was set!');const e=O();if(!(e!=null&&e[p])){y.playSample({s:p,n:d,...f},w);return}if(!(e!=null&&e[p]))console.warn(`Pattern.webdirt: sample "${p}" not found in loaded samples`,e);else{const t=e[p],s=t[d%t.length],a=R(s);if(!a)console.log(`Pattern.webdirt: load ${p}:${d} from ${s}`),E(s,y.ac);else{const n={buffer:{buffer:a},...f};y.playSample(n,w)}}};return b.setContext({...b.context,onTrigger:N})})};export{F as loadWebDirt};