b7d8818Add "ensemble" as track 5; louder piano in descentJeremy Magland 1var e=class{cfg;y=/* @__PURE__ */ new Float64Array(4);t=0;eTarget=0;tau=2;controlled=!0;damping=0;k1=/* @__PURE__ */ new Float64Array(4);k2=/* @__PURE__ */ new Float64Array(4);k3=/* @__PURE__ */ new Float64Array(4);k4=/* @__PURE__ */ new Float64Array(4);tmp=/* @__PURE__ */ new Float64Array(4);constructor(e){this.cfg={...e}}get vTop(){let{m1:e,m2:t,l1:n,l2:r,g:i}=this.cfg;return 2*(e+t)*i*n+2*t*i*r}get p2Scale(){let{m2:e,l2:t}=this.cfg;return Math.sqrt(e*t*t*this.vTop)}derived(e=this.y){let{m1:t,m2:n,l1:r,l2:i,g:a}=this.cfg,[o,s,c,l]=[e[0],e[1],e[2],e[3]],u=o-s,d=Math.cos(u),f=Math.sin(u),p=t+n*f*f,m=(i*c-r*l*d)/(r*r*i*p),h=(r*(t+n)*l-i*n*c*d)/(r*i*i*n*p);return{w1:m,w2:h,T:.5*(c*m+l*h),V:(t+n)*a*r*(1-Math.cos(o))+n*a*i*(1-Math.cos(s))}}energy(e=this.y){let t=this.derived(e);return t.T+t.V}energyNorm(){return this.energy()/this.vTop}deriv(e,t){let{m1:n,m2:r,l1:i,l2:a,g:o}=this.cfg,s=e[0],c=e[1],l=e[2],u=e[3],d=s-c,f=Math.cos(d),p=Math.sin(d),m=n+r*p*p,h=(a*l-i*u*f)/(i*i*a*m),g=(i*(n+r)*u-a*r*l*f)/(i*a*a*r*m),_=l*u*p/(i*a*m),v=(a*a*r*l*l+i*i*(n+r)*u*u-2*i*a*r*l*u*f)*Math.sin(2*d)/(2*i*i*a*a*m*m),y=.5*(l*h+u*g),b=(n+r)*o*i*(1-Math.cos(s))+r*o*a*(1-Math.cos(c)),x=0;if(this.controlled){let e=.02*this.vTop,t=4/this.tau;x=(this.eTarget-(y+b))/(this.tau*(2*y+e)),x>t?x=t:x<-t&&(x=-t)}x-=this.damping,t[0]=h,t[1]=g,t[2]=-(n+r)*o*i*Math.sin(s)-_+v+x*l,t[3]=-r*o*a*Math.sin(c)+_-v+x*u}step(e){let{y:t,k1:n,k2:r,k3:i,k4:a,tmp:o}=this;this.deriv(t,n);for(let r=0;r<4;r++)o[r]=t[r]+.5*e*n[r];this.deriv(o,r);for(let n=0;n<4;n++)o[n]=t[n]+.5*e*r[n];this.deriv(o,i);for(let n=0;n<4;n++)o[n]=t[n]+e*i[n];this.deriv(o,a);for(let o=0;o<4;o++)t[o]+=e/6*(n[o]+2*r[o]+2*i[o]+a[o]);this.t+=e}randomize(e,t){let{m1:n,m2:r,l1:i,l2:a}=this.cfg,o=Math.max(e,1e-6)*this.vTop,s=Math.min(Math.PI,3*Math.sqrt(o/(r*this.cfg.g*Math.min(i,a)))),c=0,l=0,u=0;for(let e=0;e<1e3&&(c=(2*t()-1)*s,l=(2*t()-1)*s,u=this.derived([c,l,0,0]).V,!(u<.9*o));e++)c=l=0,u=0;let d=o-u,f=t()*2*Math.PI,p=Math.cos(f),m=Math.sin(f),h=Math.cos(c-l),g=(n+r)*i*i,_=r*i*a*h,v=r*a*a,y=.5*(g*p*p+2*_*p*m+v*m*m),b=Math.sqrt(d/y),x=b*p,S=b*m;this.y[0]=c,this.y[1]=l,this.y[2]=g*x+_*S,this.y[3]=_*x+v*S,this.t=0}};function t(e){let t=e>>>0;return()=>{t=t+0x6d2b79f5>>>0;let e=t;return e=Math.imul(e^e>>>15,e|1),e^=e+Math.imul(e^e>>>7,e|61),((e^e>>>14)>>>0)/4294967296}}function n(e,t,n){let r=e.y;if(!(t>0))return;let i=r[2],a=r[3],o=Math.hypot(i,a);if(o>1e-9)i/=o,a/=o;else{let e=n()*2*Math.PI;i=Math.cos(e),a=Math.sin(e)}let s=e.derived(),c=i*s.w1+a*s.w2,l=e.derived([r[0],r[1],i,a]),u=i*l.w1+a*l.w2,d=(-c+Math.sqrt(Math.max(0,c*c+2*u*t)))/u;r[2]+=d*i,r[3]+=d*a}function r(e,t,n,r){let{l1:i,l2:a,m2:o}=e.cfg,s=e.y,c=i+a,l=Math.sqrt(2*e.vTop/o)+1e-9,u=Math.sin(s[0]),d=Math.cos(s[0]),f=Math.sin(s[1]),p=Math.cos(s[1]);switch(t){case`x2`:return(i*u+a*f)/c;case`y2`:return-(i*d+a*p)/c;case`vx2`:return(i*d*n+a*p*r)/l;case`vy2`:return(i*u*n+a*f*r)/l;case`x1`:return u;case`vx1`:return i*d*n/l;case`θ1`:return Math.atan2(u,d)/Math.PI;case`θ2`:return Math.atan2(f,p)/Math.PI;case`θ2−θ1`:return Math.atan2(Math.sin(s[1]-s[0]),Math.cos(s[1]-s[0]))/Math.PI;case`ω1`:return n*i/l;case`ω2`:return r*a/l;case`p1`:return s[2]/(l*(1+o)*i*i+1e-9);case`p2`:return s[3]/(l*o*a*a+1e-9);case`speed2`:return Math.hypot(i*d*n+a*p*r,i*u*n+a*f*r)/l}}var i=class{x=0;y=0;run(e,t){let n=e-this.x+t*this.y;return this.x=e,this.y=n,n}},a=class extends AudioWorkletProcessor{s=null;v=[];rand=t(777);seed=0;clock=0;step=0;trailW=0;count=0;sinceSnap=0;constructor(){super();for(let t=0;t<6;t++){let t=new e({m1:1,m2:1,l1:1,l2:1,g:1});t.controlled=!1,this.v.push({p:t,dc:new i,trail:/* @__PURE__ */ new Float32Array(384),struck:1e9})}this.port.onmessage=e=>{let r=e.data;if(r.type===`settings`){this.s=r.s,r.s.seed!==this.seed&&(this.seed=r.s.seed,this.rand=t(r.s.seed));for(let e=0;e<6;e++){let t=this.v[e].p.cfg,n=r.s.voices[e];t.m1=1,t.g=1,t.m2=n.m2,t.l1=n.l1,t.l2=n.l2}}else if(r.type===`strike`){let e=this.v[r.i];e&&this.s&&(n(e.p,this.s.pulseAdds*e.p.vTop,this.rand),e.struck=0)}else if(r.type===`reset`)for(let e of this.v)e.p.y.fill(0)}}process(e,t){let i=t[0],a=i[0],o=i[1]??i[0],s=this.s;if(!s)return!0;let c=a.length,l=Math.max(1,s.stepSec*sampleRate),u=s.startDelay*sampleRate,d=1-2*Math.PI*s.dcCut/sampleRate,f=this.v.map((e,t)=>{let n=s.voices[t],r=s.k,i=r/sampleRate,a=Math.max(1,Math.ceil(40*n.upperHz/sampleRate)),o=(s.spread*(2*t/5-1)+1)*Math.PI/4;return e.p.damping=n.ringTime>0?13.8156/(n.ringTime*r):0,{h:i/a,sub:a,gl:Math.cos(o)*n.level,gr:Math.sin(o)*n.level}});for(let e=0;e<c;e++){if(this.step<s.score.length&&this.clock>=u+this.step*l){for(let{i:e,add:t}of s.score[this.step]){let r=this.v[e];r&&(n(r.p,t*r.p.vTop,this.rand),r.struck=0)}this.step++}this.clock++;let t=0,i=0;for(let e=0;e<6;e++){let n=this.v[e],a=f[e],o=n.p;for(let e=0;e<a.sub;e++)o.step(a.h);Number.isFinite(o.y[0])||o.y.fill(0);let c=o.derived(),l=Math.tanh(s.gain*n.dc.run(r(o,s.signal,c.w1,c.w2),d));t+=a.gl*l,i+=a.gr*l}a[e]=t,o[e]=i;for(let e of this.v)e.struck++;if(++this.count%3==0){for(let e of this.v){let t=e.p.y,n=e.p.cfg,r=2*this.trailW;e.trail[r]=n.l1*Math.sin(t[0])+n.l2*Math.sin(t[1]),e.trail[r+1]=n.l1*Math.cos(t[0])+n.l2*Math.cos(t[1])}this.trailW=(this.trailW+1)%192}}if(this.sinceSnap+=c,this.sinceSnap>=sampleRate/30){this.sinceSnap=0;let e=/* @__PURE__ */ new Float32Array(2304);this.v.forEach((t,n)=>{let r=n*2*192;e.set(t.trail.subarray(2*this.trailW),r),e.set(t.trail.subarray(0,2*this.trailW),r+2*(192-this.trailW))});let t=/* @__PURE__ */ new Float32Array(24);this.v.forEach((e,n)=>{let r=e.p.y,i=e.p.cfg,a=i.l1*Math.sin(r[0]),o=i.l1*Math.cos(r[0]);t.set([a,o,a+i.l2*Math.sin(r[1]),o+i.l2*Math.cos(r[1])],4*n)});let n={type:`snap`,joints:t,struck:this.v.map(e=>e.struck/sampleRate),time:this.clock/sampleRate,step:this.step,energy:this.v.map(e=>e.p.energyNorm()),trails:e};this.port.postMessage(n,[e.buffer,t.buffer])}return!0}};registerProcessor(`pendulum-ensemble`,a);