/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / site / assets / pendulumWorklet-B6HWci5S.js
1 line · 5.6 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}}var n=class extends AudioWorkletProcessor{s=null;p=new e({m1:1,m2:1,l1:1,l2:1,g:1});rand=t(12345);t=0;phase=`silent`;dcL={x:0,y:0};dcR={x:0,y:0};trail=/* @__PURE__ */ new Float32Array(1536);trailW=0;sinceSnap=0;sampleCount=0;constructor(){super(),this.p.controlled=!1,this.port.onmessage=e=>{if(e.data.type===`settings`){let t=!this.s;this.s=e.data.s,this.p.cfg.m2=this.s.massRatio,this.p.cfg.l2=this.s.lengthRatio,t&&this.strike()}else e.data.type===`strike`&&this.strike()}}lowMode(){let{m1:e,m2:t,l1:n,l2:r,g:i}=this.p.cfg,a=(e+t)*n*n,o=t*n*r,s=t*r*r,c=(e+t)*i*n,l=t*i*r,u=a*s-o*o,d=(c*s+l*a)/u,f=c*l/u,p=(d-Math.sqrt(Math.max(0,d*d-4*f)))/2;return Math.sqrt(Math.max(p,1e-9))}strike(){let e=this.s;e&&(this.t=0,e.mode===`swell`?(this.p.randomize(Math.min(.01,e.energy*.05),this.rand),this.phase=`swelling`):(this.p.randomize(e.energy,this.rand),this.phase=e.mode===`sustain`?`sustaining`:`ringing`))}signal(e,t,n){let{l1:r,l2:i,m2:a}=this.p.cfg,o=this.p.y,s=r+i,c=Math.sqrt(2*this.p.vTop/a)+1e-9,l=Math.sin(o[0]),u=Math.cos(o[0]),d=Math.sin(o[1]),f=Math.cos(o[1]);switch(e){case`x2`:return(r*l+i*d)/s;case`y2`:return-(r*u+i*f)/s;case`vx2`:return(r*u*t+i*f*n)/c;case`vy2`:return(r*l*t+i*d*n)/c;case`x1`:return l;case`vx1`:return r*u*t/c;case`θ1`:return Math.atan2(l,u)/Math.PI;case`θ2`:return Math.atan2(d,f)/Math.PI;case`θ2−θ1`:return Math.atan2(Math.sin(o[1]-o[0]),Math.cos(o[1]-o[0]))/Math.PI;case`ω1`:return t*r/c;case`ω2`:return n*i/c;case`p1`:return o[2]/(c*(1+a)*r*r+1e-9);case`p2`:return o[3]/(c*a*i*i+1e-9);case`speed2`:return Math.hypot(r*u*t+i*f*n,r*l*t+i*d*n)/c}}process(e,t){let n=t[0],r=n[0],i=n[1]??n[0],a=this.s;if(!a)return!0;let o=r.length,s=this.p,c=this.lowMode(),l=2*Math.PI*a.pitch/c,u=l/sampleRate,d=Math.max(1,Math.ceil(u/.004)),f=u/d,p=a.ringTime>0?13.8156/(a.ringTime*l):0,m=1-2*Math.PI*a.dcCut/sampleRate,h=2*Math.PI/c;s.tau=Math.max(.05,a.follow*h);for(let e=0;e<o;e++){if(a.mode===`sustain`)this.phase=`sustaining`,s.controlled=!0,s.eTarget=a.energy*s.vTop,s.damping=0;else if(a.mode===`swell`){if(this.t<a.swellTime){s.controlled=!0;let e=this.t/Math.max(.01,a.swellTime);s.eTarget=a.energy*s.vTop*e*e,s.damping=0,this.phase=`swelling`}else s.controlled=!1,s.damping=p,this.phase=`ringing`}else s.controlled=!1,s.damping=p,this.phase=`ringing`;for(let e=0;e<d;e++)s.step(f);this.t+=1/sampleRate,a.restrike>0&&this.t>=a.restrike&&this.strike();let t=s.derived(),n=this.signal(a.left,t.w1,t.w2),o=a.right===a.left?n:this.signal(a.right,t.w1,t.w2),c=n-this.dcL.x+m*this.dcL.y;this.dcL.x=n,this.dcL.y=c;let l=o-this.dcR.x+m*this.dcR.y;if(this.dcR.x=o,this.dcR.y=l,n=Math.tanh(a.gain*c),o=Math.tanh(a.gain*l),r[e]=Number.isFinite(n)?n:0,i[e]=Number.isFinite(o)?o:0,Number.isFinite(s.y[0])||this.strike(),++this.sampleCount%2==0){let e=s.y,t=s.cfg,n=4*this.trailW,r=t.l1*Math.sin(e[0]),i=t.l1*Math.cos(e[0]);this.trail[n]=r,this.trail[n+1]=i,this.trail[n+2]=r+t.l2*Math.sin(e[1]),this.trail[n+3]=i+t.l2*Math.cos(e[1]),this.trailW=(this.trailW+1)%384}}if(this.sinceSnap+=o,this.sinceSnap>=sampleRate/60){this.sinceSnap=0;let e=/* @__PURE__ */ new Float32Array(1536);e.set(this.trail.subarray(4*this.trailW)),e.set(this.trail.subarray(0,4*this.trailW),4*(384-this.trailW));let t={type:`snap`,energy:s.energyNorm(),phase:this.phase,trail:e,k:l};this.port.postMessage(t,[e.buffer])}return currentTime,!0}};registerProcessor(`pendulum-voice`,n);
↑↓ move↵openescclose