mirror of
https://codeberg.org/uzu/strudel
synced 2026-07-23 13:42:56 -04:00
2 lines
2.1 KiB
JavaScript
2 lines
2.1 KiB
JavaScript
var d=Object.defineProperty;var g=(s,t,e)=>t in s?d(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var i=(s,t,e)=>(g(s,typeof t!="symbol"?t+"":t,e),e);import{G as y,I,K as S,O as T,J as B,L as W,N as j,M as q}from"./index.469372db.js";const k=s=>"("+s+")();",w=s=>URL.createObjectURL(new Blob([k(s)],{type:"text/javascript"})),m=s=>new Worker(w(s));class x{constructor(t,e=this.interval){i(this,"worker");i(this,"interval",.1);i(this,"tick",0);this.interval=e,this.worker=m(()=>{let r,o=null;const h=()=>{o&&(clearInterval(o),o=null)},c=()=>{if(h(),!r)throw new Error("no interval set! call worker.postMessage({interval}) before starting.");postMessage("tick"),o=setInterval(()=>postMessage("tick"),r*1e3)};self.onmessage=function(n){n.data=="start"?c():n.data.interval?(r=n.data.interval,o&&c()):n.data=="stop"&&h()}}),this.worker.postMessage({interval:e}),this.worker.onmessage=r=>{r.data==="tick"&&t(this.tick++,this.interval)}}start(){this.worker.postMessage("start")}stop(){this.worker.postMessage("stop"),this.tick=0}setInterval(t){this.worker.postMessage({interval:t})}}class v{constructor({audioContext:t,interval:e=.1,onEvent:r,latency:o=.1}){i(this,"worker");i(this,"pattern");i(this,"phase");i(this,"audioContext");i(this,"cps",1);this.audioContext=t,this.worker=new x((h,c)=>{const n=this.phase,l=this.phase+c*this.cps;this.phase=l,this.pattern.queryArc(n,l).forEach(a=>{var p,u;if(typeof((p=a.value)==null?void 0:p.cps)=="number"&&this.setCps((u=a.value)==null?void 0:u.cps),!!a.part.begin.equals(a.whole.begin)){if(a.context.onTrigger){const f=(a.whole.begin-n)/this.cps+this.audioContext.currentTime+o;a.context.onTrigger(f,a,this.audioContext.currentTime,this.cps)}r&&(r==null||r(a))}})},e)}start(){if(!this.pattern)throw new Error("Scheduler: no pattern set! call .setPattern first.");this.audioContext.resume(),this.phase=0,this.worker.start()}stop(){this.worker.stop()}setPattern(t){this.pattern=t}setCps(t=1){this.cps=t}}export{x as ClockWorker,v as Scheduler,y as getAudioContext,I as getCachedBuffer,S as getLoadedBuffer,T as getLoadedSamples,B as loadBuffer,W as loadGithubSamples,j as resetLoadedSamples,q as samples};
|