← Back to Docs
  • index.html
  • Clayground
  • Clayground Sound Module
  • Clayground 2026.14
  • Clayground Sound Module

    The Clayground.Sound module provides cross-platform audio for games:

    Stereo, pan and buses

    The engine mixes in stereo. Every instrument has a pan, from -1 (left) to +1 (right). The centre, 0 and the default, plays the same samples in both channels that the engine played in mono before; panning only turns the far channel down, to silence at either edge.

    Every instrument also names the bus its voices mix into: "sfx" (the default), "music" or "voice". SoundMixer holds one volume per bus, which scales everything on that bus on top of each instrument's own volume, so a game's settings screen sets three numbers instead of touching every instrument. SongPlayer.volume scales every note a song plays, whatever instrument plays it.

    Platform Support

    PlatformBackend
    WASM (Browser)Web Audio API via Emscripten
    Desktop (Windows/macOS/Linux)Qt Multimedia (QSoundEffect, QMediaPlayer)
    Mobile (iOS/Android)Qt Multimedia

    Usage

    Import the module:

    import Clayground.Sound

    Sound Effects

    Use Sound for short audio like jumps, clicks, or explosions:

    Sound {
        id: jumpSound
        source: "sounds/jump.wav"
        volume: 0.8
    }
    
    // Play sound (can overlap multiple times)
    onPlayerJumped: jumpSound.play()

    Background Music

    Use Music for longer tracks with playback controls:

    Music {
        id: bgMusic
        source: "music/theme.mp3"
        volume: 0.5
        loop: true
    }
    
    onGameStarted: bgMusic.play()
    onGamePaused: bgMusic.pause()
    onGameOver: bgMusic.stop()

    Lazy Loading

    By default, audio is preloaded when source is set. For memory optimization, enable lazy loading to defer loading until first play:

    Sound {
        source: "sounds/rare_event.wav"
        lazyLoading: true  // Only loads when play() is called
    }

    Technical Notes

    See also Sound and Music.

    EchoEffect

    SNES-style echo of one bus: a delay, feedback and an 8-tap FIR filter

    MoodPlayer

    SNES-style atmospheric music generator with customizable environments and scales

    Music

    Play background music with play/pause/stop/loop controls

    ReverbEffect

    Small reverb of one bus

    SampleInstrument

    PCM sample playback with loop points, root note, and ADSR

    SfxPlayer

    Plays an effect recipe (.sfx.json) with variation, and renders it offline the same way

    SongPlayer

    Plays a parsed .song.json file against a set of instruments

    Sound

    Play sound effects with support for overlapping playback

    SoundMixer

    Volume, echo and reverb of each mixer bus: sound effects, music and voices

    SynthInstrument

    Real-time oscillator voice with ADSR, pitch envelope, LFO and a resonant filter