mwg API
    Preparing search index...

    Class Music

    Background music with crossfade: switching tracks fades the old one out and the new one in over the same span, rather than cutting straight from one to the other. Like AnimatedSprite, nothing advances on its own - call update(dt) from the scene.

    import { Music } from '@datamoc/mwg/audio';

    const music = new Music({ volume: 0.7 });
    music.play('music/town.mp3', 2); // crossfades in over 2 seconds

    // in the game loop:
    music.update(1 / 60);
    Index
    volume: number
    • get duckLevel(): number

      the held duck attenuation, 1 when full

      Returns number

    • Ducks the music under dialogue or a menu: the current track glides to volume * level over fadeDuration and stays there until unduck, and tracks started while ducked start ducked. Pending fades are retargeted proportionally, so ducking mid-crossfade still lands quiet. Sound is untouched. Idempotent.

      Parameters

      • level: number = 0.5

        0 (silent) to 1 (full); clamped, 0.5 by default

      • fadeDuration: number = 0.5

      Returns void

    • Parameters

      • path: string
      • fadeDuration: number = 1

        seconds; 0 switches immediately with no crossfade

      Returns void

    • Plays the supplied sequence repeatedly. This is deliberately small and backend-agnostic: callers can repeat entries to weight a track, while HTMLAudioElement's optional onended advances the queue when available.

      Parameters

      • paths: readonly string[]
      • fadeDuration: number = 1

      Returns void

    • The counterpart to suspend: restarts the current track where it paused. A track stopped (not suspended) while hidden stays stopped - resume only resumes.

      Returns void

    • Parameters

      • fadeDuration: number = 1

      Returns void

    • Silences everything for a page hide: pauses the current track and every fading one, and freezes update until resume, so a crossfade mid-flight continues where it stopped rather than finishing inaudibly in a background tab. Idempotent.

      Returns void

    • releases a duck back to full volume through the same fade path

      Parameters

      • fadeDuration: number = 0.5

      Returns void