TinyShade
    Preparing search index...

    Class GPUSynth

    GPU-accelerated audio synthesizer that uses WebGPU compute shaders to generate audio samples.

    Generates audio in chunks by dispatching compute work to the GPU, then reads back the results and schedules them for playback through the Web Audio API.

    GPUSynth is originally written by Crush for SYTYCD 2025

    const device = await navigator.gpu.requestAdapter().requestDevice();
    const wgslShader = `@compute @workgroup_size(64) fn main(...) { ... }`;
    const synth = new GPUSynth(device, wgslShader);
    await synth.play();

    Implements

    Index

    Constructors

    Properties

    Methods

    Constructors

    • Initializes a TinyAudio instance with GPU compute capabilities for audio processing.

      Parameters

      • device: GPUDevice

        The GPU device used for creating buffers and compute pipelines

      • wgslSource: string

        The WGSL shader source code for the compute pipeline

      Returns GPUSynth

      This constructor sets up:

      • An AudioContext for audio output with the system's sample rate
      • GPU storage buffers for audio sample data and readback operations
      • A uniform buffer for passing parameters to the compute shader
      • A compute pipeline from the provided WGSL source
      • A bind group linking the uniform and storage buffers to the pipeline

      The storage buffer size is calculated based on bufferSamples property (4 bytes per sample).

    Properties

    isPlaying: boolean = false

    Indicates whether audio is currently playing.

    Methods

    • Plays audio using the Web Audio API scheduler pattern.

      Resumes the audio context if it's suspended, then schedules audio buffer chunks to be played with a 0.5 second lookahead buffer. Each chunk is generated for both left and right channels and queued for playback.

      Parameters

      • Optionalcb: () => void

        Optional callback function to execute immediately after scheduling begins

      Returns Promise<void>

      A promise that resolves after the scheduler is initiated

      • The scheduler runs on a 100ms interval to maintain the audio playback queue
      • Audio chunks are scheduled with a minimum 0.1 second offset from current time
      • The absolute sample count is incremented to track the total samples processed
      await audioPlayer.play(() => console.log('Playback started'));