Files
strudel/tutorial/assets/index.dd197254.js
T
Felix Roos 43823d3fc3 Updates
2022-06-16 22:09:22 +02:00

2 lines
3.7 KiB
JavaScript

var v=Object.defineProperty,A=Object.defineProperties;var T=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var f=(e,t,r)=>t in e?v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,h=(e,t)=>{for(var r in t||(t={}))C.call(t,r)&&f(e,r,t[r]);if(x)for(var r of x(t))b.call(t,r)&&f(e,r,t[r]);return e},w=(e,t)=>A(e,T(t));var u=(e,t,r)=>(f(e,typeof t!="symbol"?t+"":t,r),r);import{S as N,E as q,C as O}from"./index.5a47cda7.js";const M=e=>"("+e+")();",R=e=>URL.createObjectURL(new Blob([M(e)],{type:"text/javascript"})),S=e=>new Worker(R(e));class V{constructor(t,r,i=this.interval){u(this,"worker");u(this,"audioContext");u(this,"interval",.2);u(this,"lastEnd",0);this.audioContext=t,this.interval=i,this.worker=S(()=>{let n,o=null;const s=()=>{o&&(clearInterval(o),o=null)},a=()=>{if(s(),!n)throw new Error("no interval set! call worker.postMessage({interval}) before starting.");o=setInterval(()=>postMessage("tick"),n*1e3)};self.onmessage=function(l){l.data=="start"?a():l.data.interval?(n=l.data.interval,o&&a()):l.data=="stop"&&s()}}),this.worker.postMessage({interval:i}),this.worker.onmessage=n=>{if(n.data==="tick"){const o=this.lastEnd||this.audioContext.currentTime,s=this.audioContext.currentTime+this.interval;this.lastEnd=s,r(o,s)}}}start(){console.log("start..."),this.audioContext.resume(),this.worker.postMessage("start")}stop(){console.log("stop..."),this.worker.postMessage("stop")}}class j{constructor({audioContext:t,interval:r=.2,onEvent:i,latency:n=.2}){u(this,"worker");u(this,"pattern");this.worker=new V(t,(o,s)=>{this.pattern.query(new N(new q(o+n,s+n))).forEach(a=>{!a.part.begin.equals(a.whole.begin)||(a.context.onTrigger&&a.context.onTrigger(a.whole.begin,a,t.currentTime,1),i&&(i==null||i(a)))})},r)}start(){if(!this.pattern)throw new Error("Scheduler: no pattern set! call .setPattern first.");this.worker.start()}stop(){this.worker.stop()}setPattern(t){this.pattern=t}}const{Pattern:c,getFrequency:_,patternify2:F}=O;let d;const p=()=>(d||(d=new AudioContext),d),g=.2,I=(e,t,r,i,n,o,s)=>{const a=p().createGain();return a.gain.setValueAtTime(0,o),a.gain.linearRampToValueAtTime(n,o+e),a.gain.linearRampToValueAtTime(r*n,o+e+t),a.gain.setValueAtTime(r*n,s),a.gain.linearRampToValueAtTime(0,s+i),a};c.prototype.withAudioNode=function(e){return this._withHap(t=>t.setContext(w(h({},t.context),{createAudioNode:(r,i)=>{var n,o;return e(r,i,(o=(n=t.context).createAudioNode)==null?void 0:o.call(n,r,t))}})))};c.prototype._wave=function(e){return this.withAudioNode((t,r)=>{const i=p().createOscillator();i.type=e;const n=_(r);i.frequency.value=n;const o=t!=null?t:r.whole.begin.valueOf()+g,s=o+r.duration.valueOf();return i.start(o),i.stop(s),i})};c.prototype.adsr=function(e=.01,t=.05,r=1,i=.01){return this.withAudioNode((n,o,s)=>{var y;const a=((y=o.context)==null?void 0:y.velocity)||1,l=n!=null?n:o.whole.begin.valueOf()+g,k=l+o.duration.valueOf()+g,m=I(e,t,r,i,a,l,k);return s==null||s.connect(m),m})};c.prototype._filter=function(e="lowpass",t=1e3){return this.withAudioNode((r,i,n)=>{const o=p().createBiquadFilter();return o.type=e,o.frequency.value=t,n==null||n.connect(o),o})};c.prototype.filter=function(e,t){return F(c.prototype._filter)(reify(e),reify(t),this)};c.prototype.out=function(){const e=p().createGain();return e.gain.value=.1,e.connect(p().destination),this.withAudioNode((t,r,i)=>{i||console.warn("out: no source! call .osc() first"),i==null||i.connect(e)})._withHap(t=>{const r=(i,n)=>{var o,s;return(s=(o=n.context)==null?void 0:o.createAudioNode)==null?void 0:s.call(o,i,n)};return t.setContext(w(h({},t.context),{onTrigger:r}))})};c.prototype.define("wave",(e,t)=>t.wave(e),{patternified:!0});export{V as ClockWorker,j as Scheduler,p as getAudioContext};