Files
strudel/tutorial/assets/index.fc80f7ff.js
T
Felix Roos 05669d1a27 Updates
2022-05-18 20:12:15 +02:00

2 lines
3.7 KiB
JavaScript

var x=Object.defineProperty,k=Object.defineProperties;var m=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var A=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var p=(e,t,r)=>t in e?x(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,f=(e,t)=>{for(var r in t||(t={}))A.call(t,r)&&p(e,r,t[r]);if(y)for(var r of y(t))C.call(t,r)&&p(e,r,t[r]);return e},d=(e,t)=>k(e,m(t));var u=(e,t,r)=>(p(e,typeof t!="symbol"?t+"":t,r),r);import{S as T,E as b,C as N}from"./index.db2658eb.js";const q=e=>"("+e+")();",O=e=>URL.createObjectURL(new Blob([q(e)],{type:"text/javascript"})),M=e=>new Worker(O(e));class R{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=M(()=>{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 I{constructor({audioContext:t,interval:r=.2,onEvent:n}){u(this,"worker");u(this,"pattern");this.worker=new R(t,(i,o)=>{this.pattern.query(new T(new b(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:S,patternify2:V}=N,h=.2,_=(e,t,r,n,i,o,s)=>{const a=getAudioContext().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(d(f({},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=getAudioContext().createOscillator();n.type=e;const i=S(r);n.frequency.value=i;const o=t!=null?t:r.whole.begin.valueOf()+h,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 g;const a=((g=o.context)==null?void 0:g.velocity)||1,l=i!=null?i:o.whole.begin.valueOf()+h,v=l+o.duration.valueOf()+h,w=_(e,t,r,n,a,l,v);return s==null||s.connect(w),w})};c.prototype._filter=function(e="lowpass",t=1e3){return this.withAudioNode((r,n,i)=>{const o=getAudioContext().createBiquadFilter();return o.type=e,o.frequency.value=t,i==null||i.connect(o),o})};c.prototype.filter=function(e,t){return V(c.prototype._filter)(reify(e),reify(t),this)};c.prototype.out=function(){const e=getAudioContext().createGain();return e.gain.value=.1,e.connect(getAudioContext().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(d(f({},t.context),{onTrigger:r}))})};c.prototype.define("wave",(e,t)=>t.wave(e),{patternified:!0});export{R as ClockWorker,I as Scheduler};