// +---------------------------------------------+
// *    2024 nabr ^;.;^                          *
// *    https://dittytoy.net/ditty/bc2c18eddd    *
// +---------------------------------------------+


function inst(rt, t, msb, hrb, phb )
{ 
    let y = 0., an = 0.,
        p = 15. * elem((mod(1. * t, 4.) > 3. ? 10.97 : 8.26),(mod(2. * t, 4.) > 2. ? 9.28 : 12.25),10.85,12.25)[int(.5 * t) % 4],
        en = fract(t);

    an = en * exp(1.7 + log2(1. - en)); 
    y = mod( an + p * t , 1. / 10.);
    y *= cos( msb - 3.0 * mod( .1 * an + phb * p * t, 1. / hrb ));

    return pow(an, 4. ) * y;
}

synth.def(class
{
    constructor(){}
    process()
    { 
    
        let t = ditty.time;

        this.printmsg(2.*t);
        let o = { x : 0, y : 0 };

        let tmx = 0.5, // time speed
            dmx = 1.0, // delay speed
            msb = 1.5, // mix bass
            hrb = 4.0, // subharmonic bass
            phb = .25; // pitch bass

        if( t > 22. && t < 38.5)
        {

             tmx = (t > 31.) ? .0625 : .25 ;
             dmx = 2. ;
             msb = (t > 31.) ? (.5 + exp(-1.5 * fract(t))) : 1.;
             hrb = 8.;
             phb = .5;
             t = t + 25.;
        };


        let dlt = (tmx * t);
        let ya = [0, 0, 0, 0, 0, 0, 0, 0];

        for (let i = 0; i < 32; ++i)
        { 

            dlt += exp2( - (i%4) * (1 + i % 12) );

            ya[i % 8] = inst( t, (dmx *  dlt), msb, hrb, phb );
            
            dlt -= .2694 * exp2( 1./16. * (1 + i % 2) );

            //handpicked some polarities integer bits unpacked
            o.x += ya[i % 8] * ((0 < ((60958) & 1 << 15 - int(i) % 16 )) ? -1. : 1.);
            o.y += ya[i % 8] * ((0 < ((47947) & 1 << 15 - int(i) % 16 )) ? -1. : 1.);

        }
        return  [o.x, o.y];
    }
    
    // enter the void radio on yt inspired
     printmsg(t)
     {
        const msg = " • In the grand tapestry of existence, you have a unique and irreplaceable role, contributing to the intricate beauty and harmony of the whole • In the symphony of life, every note, every chord, every silence holds profound significance, waiting to be deciphered by the discerning ear";

        let result = '';

        [...msg].forEach( (v, i) => 
        {
            if(i % 98 === 0) result +=  "\n";
            result += msg[  ((t | 0) + i) %  msg.length];
        });
        debug.log(" ", result );
     };
     
}).play(0,{duration:240});

//_________UTILITIES
const{abs,acos,asin,atan,atan2,ceil,clz32,cos,exp,floor,imul,fround,log,max,min,pow,random,round,sin,sqrt,tan,log10,log2,log1p,expm1,cosh,sinh,tanh,acosh,asinh,atanh,hypot,trunc,sign,cbrt,/*E,*/LOG2E,LOG10E,LN2,LN10,PI,SQRT2,SQRT1_2}=Math;
const int = (x) => x|0;
const tau = PI*2.0;
const fract = (x) => (x-floor(x));
const mod = (x,y) => (x-floor(x/y)*y);
const exp2 = (x) => exp(x*log(2.0));
const dbg = (x) => debug.warn(' ',x);
const elem = (...args) => [...args];