TinyShade
    Preparing search index...

    Variable SWEET_DREAMS_WGSLConst

    SWEET_DREAMS_WGSL: "\n\nstruct AudioUniforms {\n bufferTime: f32,\n sampleRate: f32,\n channel: f32,\n _pad: f32,\n};\n\n@group(0) @binding(0) var<uniform> u: AudioUniforms;\n@group(0) @binding(1) var<storage, read_write> output: array<f32>;\n\nconst PI: f32 = 3.14159265359;\nconst BPS: f32 = 2.1;\n\nfn noteToFreq(n: f32) -> f32 {\n return pow(2.0, (n - 49.0) / 12.0) * 440.0;\n}\n\nfn adsr(t_abs: f32, env: vec4f, start: f32, duration: f32) -> f32 {\n let t = t_abs - start;\n let sustain = env.z;\n let t1 = env.x;\n let t2 = t1 + env.y;\n let t3 = max(t2, duration);\n let t4 = t3 + env.w;\n\n if (t < 0.0 || t > t4) { return 0.0; }\n if (t <= t1) { return smoothstep(0.0, t1, t); }\n if (t <= t2) { return sustain + smoothstep(t2, t1, t) * (1.0 - sustain); }\n if (t <= t3) { return sustain; }\n return sustain * smoothstep(t4, t3, t);\n}\n\nfn tri(t: f32, x: f32) -> f32 {\n return abs(1.0 - ((2.0 * t * x) % 2.0)) * 2.0 - 1.0;\n}\n\nfn synth(t: f32, f: f32) -> f32 {\n var time = t;\n // Stereo phase shift based on channel\n time += select(0.2, 0.6, u.channel > 0.5) * sin(t * 2.0) / f;\n return 0.3 * tri(time, f / 2.0) + 0.2 * tri(time, f / 4.0);\n}\n\n@compute @workgroup_size(64)\nfn main(@builtin(global_invocation_id) id: vec3u) {\n let idx = id.x;\n if (idx >= arrayLength(&output)) { return; }\n\n let t = u.bufferTime + f32(idx) / u.sampleRate;\n let m = (t * BPS * 2.0) % 16.0;\n \n var notes = array<f32, 16>(24., 24., 36., 48., 39., 51., 36., 48., 32., 32., 44., 48., 31., 31., 46., 48.);\n var sound: f32 = 0.0;\n\n for (var i: i32 = 0; i < 16; i = i + 1) {\n let is_second_half = ((t * BPS * 2.0) % 32.0) > 16.0;\n let pitch_factor = select(2.0, 1.0, is_second_half);\n \n sound += synth(t, pitch_factor * noteToFreq(notes[i])) * adsr(m, vec4f(0.1, 0.2, 0.7, 0.8), f32(i), 0.6);\n }\n\n // Add a simple kick drum pulse\n let beat_t = (t * BPS) % 2.0;\n let kick = tri(beat_t, 60.0 * smoothstep(0.4, 0.0, beat_t)) * adsr(beat_t, vec4f(0.01, 0.1, 0.0, 0.2), 0.0, 0.2);\n\n output[idx] = clamp((sound * 0.4) + (kick * 0.5), -1.0, 1.0);\n}" = ...

    WGSL compute shader for synthesizing "Sweet Dreams" audio.

    Generates a polyphonic synthesizer with 16 simultaneous voices playing a predefined melody pattern, combined with a kick drum pulse. The shader uses triangle wave oscillators with stereo phase shifting and ADSR envelope modulation.

    • Processes audio samples at the specified sample rate
    • Uses a 16-note repeating melody with BPS (beats per second) timing at 2.1 BPS
    • Each voice has an independent ADSR envelope starting at different times
    • Applies pitch shifting based on the second half of a 32-beat cycle
    • Includes stereo channel separation via phase modulation
    • Adds a percussive kick drum with its own envelope

    0 - AudioUniforms containing bufferTime, sampleRate, channel, and padding

    1 - Read-write storage buffer for output audio samples

    void - Writes processed audio samples to the output buffer

    // Used in a compute pipeline to generate audio in real-time
    // Output values are clamped to [-1.0, 1.0] range for valid audio