/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / site / assets / ensembleWorklet-2juu9zm0.js
1 line · 5.7 KBBlameHistoryRaw
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){let{m1:t,m2:n,l1:r,l2:i,g:a}=e.cfg,o=(t+n)*r*r,s=n*r*i,c=n*i*i,l=(t+n)*a*r,u=n*a*i,d=o*c-s*s,f=(l*c+u*o)/d,p=l*u/d,m=(f-Math.sqrt(Math.max(0,f*f-4*p)))/2;return Math.sqrt(Math.max(m,1e-9))}function r(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 i(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 a=class{x=0;y=0;run(e,t){let n=e-this.x+t*this.y;return this.x=e,this.y=n,n}},o=class extends AudioWorkletProcessor{s=null;v=[];rand=t(777);k=0;lastActiveSetting=-1;beat=0;sinceBeat=1e9;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 a,trail:/* @__PURE__ */ new Float32Array(384)})}this.port.onmessage=e=>{let t=e.data;if(t.type===`settings`){this.s=t.s;for(let e=0;e<6;e++)this.v[e].p.cfg.m2=t.s.voices[e].massRatio,this.v[e].p.cfg.l2=t.s.voices[e].lengthRatio;t.s.active!==this.lastActiveSetting&&(this.lastActiveSetting=t.s.active,this.k=t.s.active)}else if(t.type===`active`)this.k=Math.max(0,Math.min(6,t.k));else if(t.type===`reset`){for(let e of this.v)e.p.y.fill(0);this.beat=0}}}process(e,t){let a=t[0],o=a[0],s=a[1]??a[0],c=this.s;if(!c)return!0;let l=o.length,u=60/Math.max(1,c.tempo)*sampleRate,d=1-2*Math.PI*c.dcCut/sampleRate,f=this.v.map((e,t)=>{let r=c.voices[t],i=2*Math.PI*r.pitch/n(e.p),a=i/sampleRate,o=Math.max(1,Math.ceil(a/.006)),s=(c.spread*(2*t/5-1)+1)*Math.PI/4;return e.p.damping=r.ringTime>0?13.8156/(r.ringTime*i):0,{h:a/o,sub:o,gl:Math.cos(s)*r.level,gr:Math.sin(s)*r.level}});for(let e=0;e<l;e++){if(++this.sinceBeat>=u){this.sinceBeat=0,this.beat++,c.build>0&&this.beat%Math.round(c.build)===0&&this.k<6&&this.k++;for(let e=0;e<this.k;e++)r(this.v[e].p,c.pulseAdds*this.v[e].p.vTop,this.rand)}let t=0,n=0;for(let e=0;e<6;e++){let r=this.v[e],a=f[e],o=r.p;for(let e=0;e<a.sub;e++)o.step(a.h);Number.isFinite(o.y[0])||o.y.fill(0);let s=o.derived(),l=Math.tanh(c.gain*r.dc.run(i(o,c.signal,s.w1,s.w2),d));t+=a.gl*l,n+=a.gr*l}if(o[e]=t,s[e]=n,++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+=l,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={type:`snap`,active:this.k,beat:this.beat,sinceBeat:this.sinceBeat/sampleRate,energy:this.v.map(e=>e.p.energyNorm()),trails:e};this.port.postMessage(t,[e.buffer])}return!0}};registerProcessor(`pendulum-ensemble`,o);
↑↓ move↵openescclose