EXPI

1307 words ~ 7-14 mins — Mathieu 'p01' Henri on August 5th, 2023

EXPI

EXPI lives in the Brotli dictionary. RIP JPEG encoder. RIP Compo stream.

The Assembly has been going on for over 30 years. It is the demoscene event with the most anticipated 1kb, 4kb and full size demo competitions. I am so happy to join the competition with Veikko "pestis" Sariola.

Exploring the JPEG artifacts, Brotli, and beautiful synth

EXPI started from the idea of exploring JPEG artifacts with a first prototype back in March 2023. While it worked perfectly, life happened and it is only until Veikko "pestis" Sariola offered to team up, that things went to the next level and made EXPI in just 10 days.

Pestis delivered a beautiful and moody music, with the cleanest sound I've hear in 1kb. He used his experience developing pakettic, the best packer for TIC-80 to write a custom JavaScript parser and minifier and started diving into the default dictionnary of Brotli.

Working with more tooling, was a different experience for me, but it felt much smoother than M22. Also this gave us many ideas to develop tools dedicated to 1k and 4k intros using Brotli.

HD video capture

Here's an HD video capture to watch EXPIT in the best possible way in case your current web browser or set up isn't suitable

Technical break down

It is still very early after the release, and I promise to go into much more details in the comming days... working with Veikko "pestis" Sariola

Source code

The entire source code is 5562 bytes long and compresses down to 1024 bytes using our custom minifier and Brotli-11

Click<canvas id=c>
<script>
    // EXPI by Mathieu 'p01' Henri + Veikko 'pestis' Sariola
    // 1kb intro for Assembly 2023
    //
    // EXPI lives in the Brotli dictionary. RIP Jpeg decoder. RIP Compo live stream.
    //
    // This file compressess down 1024 bytes using a custom made packer/minifier + Brotli-11
    onclick = d => {
    b = c.getContext`2d`;
    S = Math.sin;
    min = Math.min;
    P = Math.PI;
    A = new AudioContext;
    // A single channel buffer with chunks of 2048 samples (it must be a power of 2), and 1024 can be tight
    // the sampleFrequency depends on the browser and audio drivers, but it's typically 44100 or 48000Hz
    a = A.createScriptProcessor(2048, onclick = delayIndex = T = t = 0, 1);
    a.connect(A.destination);
    // abuse the audioprocess to update the audio buffer, and the viusals
    // This gives a funky framerate due to the number of samples (1024, 2048, ...) and sampleRate (44100, 48000)
    // But that's what I've used for a while. It works fine and no one complained :p
    a.onaudioprocess = d => {
        // clear and reset the canvas
        c.width = 1024;
        c.height = 576;
        d = d.outputBuffer.getChannelData(0);
        M = min(1, .1 + S(min(1, t / 128) * P) * 4);

        // πŸŽΉπŸŽΊπŸ“»πŸ‰ Sound Synth
        for (i = 0; i < 2048; i++) {
        b[i] = syncsSum = 0; // b is the canvas, but we reuse it as an array to store the envelopes of music channels for syncs
        part = t / 16;
        y = A[++delayIndex] || 0; // t = signal output. A is the AudioContext, but we reuse it as an array to store the delay buffer
        a = min(part, 7) & 7;

        // 🎻 The additive synthesizer
        for (j = "20020002"[a]; j < "41446664"[a]; j++) { // only some of the channels are active, depending on the part
            r = part * (1 << 5 - j); // r = row within pattern
            v = r % 1; // v = position within row
            r = part * (132 << j) * P * [10, 0, 15, 10, 18, 10, 15, 20][r & 7]; // r = frequency
            syncsSum += b[j] = v = r && 3 ** -(.006 / v + j / 2 + v * 2); // b[j] = v = channel envelope
            for (f = 1; f < 7; f++)
            y += S(part * f / 8) * S(r * f) * v / 3 // S(r * f) is the "partial", S(part * f / 8) is the slowly changing weight of the partial
        }

        // πŸ₯ Drums
        r = t * 8 + 1; // r = row within pattern
        v = r % 1; // v = position within row
        j = (r & 15) ** "14212421"[a] % "19939971"[a]; // j = what drum to play, 0 = no drum 1 = kick 2+ = noisy snare/hihats
        r = 3 ** -(.006 / v + j / 4 + v * j); // r = envelope
        syncsSum += b6 = j == 1 && r; // store the kick drum envelope to b6 for syncs
        y += S(256 * j ** 16 * v ** .1) * r; // add the kick drum to signal output


        A[delayIndex %= A.sampleRate / 2] = y / 3; // store the signal to the delay buffer
        d[i] = y * .6 * M; // store the signal into "d" the output buffer and move "t" forward
        t += 1 / A.sampleRate
        }

        // ⚞⚟ Horizon
        b.strokeStyle = "#fc9";
        b.translate(512, 288);
        for (i = r = 8 + part; i < 64; i++) {
        b.beginPath();
        b.lineWidth = min(part, 1) ** 8 * r / 1024;
        b.ellipse(0, 0, (t & 32) * r + r, r, 0, 0, P * 2);
        b.stroke();
        r += r / 8
        }

        zzzz = min(4, t / 4) + syncsSum ** 4 * 2;

        // πŸ₯¨ Twister
        for (i = 0; i < 2; i += 1 / 256) {
        a = t + S(i * P + S(i * P + S(t)));
        b.rotate(P / 256);
        for (v = j = 0; j <= 4; j++) {
            f = v;
            y = S(a + P / 2 * j);
            v = min(part - 1, M) * (t / 4 % 2 + 1) * (32 + zzzz + 8 * S(a + P * S(t / 3)) - min(-M, 8 * S(a) - 8 * b6) * y);
            if (v * j > 0 & v > f) {
            b.fillStyle = `hsl(${i * 180 + j * 90},50%,${48 + 48 * y}%`;
            b.fillRect(0, f, 2, v - f)
            }
        }
        b.fillStyle = b.shadowColor = "#fc9";
        b.fillRect(0, S(i * 64) * (512 + t), 2, 2) // stars
        }

        // 🌁 Blur the twister
        b.filter = "blur(10px";
        b.globalAlpha = .2;
        r = 1024;
        b.drawImage(c, -r, -r / 2, r * 2, r);
        b.globalAlpha = .1;
        r = r * 4;
        b.drawImage(c, -r, -r / 2, r * 2, r);
        b.filter = "none";

        // πŸπŸ”  Tiny EXPI text
        b.globalAlpha = b[1] + b[2];
        for (i = 0; i < 16; i++)
        b.fillText("EXPI"[t / 2 & 3], i * 200 - 512, 0);

        // ©️ End credits
        b.globalAlpha = min(1, t / 128) ** 32;
        b.fillText("p01+pestis", -t, 8);
        b.font = "900 30px sans-serif";
        b.fillText("EXPI", -t, 0);

        // πŸ˜πŸ”  Big EXPI text
        b.globalAlpha = b[4];
        b.font = "900 250px sans-serif";
        for (i = 0; i < 16; i++)
        b.fillText("EXPIEXPI", i * 200 - t * 16 - 800, i * 200 - t * 16);

        // πŸ“Ί Grab jpeg
        if (t > T)
        T++,
            jpg = c.toDataURL("image/jpeg").split("");

        // β˜€οΈ Sun
        b.beginPath(
        b.shadowBlur = 30);
        b.arc(0, 0, 8 * zzzz, b.globalAlpha = 1, 8);
        b.fill();

        // πŸ’₯ Glitch the jpg
        if (part > 3 & syncsSum > .1)
        jpg[Math.random() * jpg.length | 0] = 0;

        // πŸ–₯️⚫βšͺ Display the jpg, perhaps in grayscale
        c.style = `position:fixed;top:0px;left:0px; height:100%;width:100%;filter:grayscale(${(part - 1) % 3 >> 1}); background:radial-gradient(#303,#0004),url(${jpg.join("")})50%/90%`
    }
    }
</script>

That's it.

You see, no external libraries or resources.

Copy and paste this into a new file or even some web playground if you want to see for yourself.

Additional links