Files
strudel/tutorial/assets/index.dc147fb0.js
T
Felix Roos f8f4ebd35e Updates
2022-05-20 22:01:13 +02:00

2 lines
3.7 KiB
JavaScript

var x=Object.defineProperty,A=Object.defineProperties;var C=Object.getOwnPropertyDescriptors;var k=Object.getOwnPropertySymbols;var T=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var f=(e,t,r)=>t in e?x(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,h=(e,t)=>{for(var r in t||(t={}))T.call(t,r)&&f(e,r,t[r]);if(k)for(var r of k(t))b.call(t,r)&&f(e,r,t[r]);return e},w=(e,t)=>A(e,C(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.c7461729.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,n=this.interval){u(this,"worker");u(this,"audioContext");u(this,"interval",.2);u(this,"lastEnd",0);this.audioContext=t,this.interval=n,this.worker=S(()=>{let i,o=null;const s=()=>{o&&(clearInterval(o),o=null)},a=()=>{if(s(),!i)throw new Error("no interval set! call worker.postMessage({interval}) before starting.");o=setInterval(()=>postMessage("tick"),i*1e3)};self.onmessage=function(l){l.data=="start"?a():l.data.interval?(i=l.data.interval,o&&a()):l.data=="stop"&&s()}}),this.worker.postMessage({interval:n}),this.worker.onmessage=i=>{if(i.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 W{constructor({audioContext:t,interval:r=.2,onEvent:n}){u(this,"worker");u(this,"pattern");this.worker=new V(t,(i,o)=>{this.pattern.query(new N(new q(i,o))).forEach(s=>{!s.part.begin.equals(s.whole.begin)||(n?n==null||n(s):console.warn("unplayable event: no audio node nor onEvent callback",s))})},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,E=(e,t,r,n,i,o,s)=>{const a=p().createGain();return a.gain.setValueAtTime(0,o),a.gain.linearRampToValueAtTime(i,o+e),a.gain.linearRampToValueAtTime(r*i,o+e+t),a.gain.setValueAtTime(r*i,s),a.gain.linearRampToValueAtTime(0,s+n),a};c.prototype.withAudioNode=function(e){return this._withHap(t=>t.setContext(w(h({},t.context),{createAudioNode:(r,n)=>{var i,o;return e(r,n,(o=(i=t.context).createAudioNode)==null?void 0:o.call(i,r,t))}})))};c.prototype._wave=function(e){return this.withAudioNode((t,r)=>{const n=p().createOscillator();n.type=e;const i=_(r);n.frequency.value=i;const o=t!=null?t:r.whole.begin.valueOf()+g,s=o+r.duration.valueOf();return n.start(o),n.stop(s),n})};c.prototype.adsr=function(e=.01,t=.05,r=1,n=.01){return this.withAudioNode((i,o,s)=>{var v;const a=((v=o.context)==null?void 0:v.velocity)||1,l=i!=null?i:o.whole.begin.valueOf()+g,m=l+o.duration.valueOf()+g,y=E(e,t,r,n,a,l,m);return s==null||s.connect(y),y})};c.prototype._filter=function(e="lowpass",t=1e3){return this.withAudioNode((r,n,i)=>{const o=p().createBiquadFilter();return o.type=e,o.frequency.value=t,i==null||i.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,n)=>{n||console.warn("out: no source! call .osc() first"),n==null||n.connect(e)})._withHap(t=>{const r=(n,i)=>{var o,s;return(s=(o=i.context)==null?void 0:o.createAudioNode)==null?void 0:s.call(o,n,i)};return t.setContext(w(h({},t.context),{onTrigger:r}))})};c.prototype.define("wave",(e,t)=>t.wave(e),{patternified:!0});export{V as ClockWorker,W as Scheduler,p as getAudioContext};