Skip to content

audio family ​

Music playback ownership with an injected backend.

Import these from gamekit/src/audio (a few are also re-exported by the package root, as noted on each).

2 symbols, 2 of them also in the package root.

SymbolSummary
IMusicBackendinterface. Small boundary around music playback so a game may adapt core Audio and tests may use a deterministic fake.
MusicDirectorclass. Owns the current music request, avoiding duplicate changes and restoring temporarily stacked tracks.

IMusicBackend ​

ts
import { IMusicBackend } from "gamekit";
interface IMusicBackend

Small boundary around music playback so a game may adapt core Audio and tests may use a deterministic fake.

Members

ts
loadMusic(trackId: string): Promise<any>

Loads one track and returns its opaque core audio handle.

  • trackId: Project-relative track identifier accepted by the core audio backend.
  • Returns: A promise for the loaded handle.
ts
playMusicHandle(handle: any, volume: float, loop: boolean): void

Starts a loaded track without a transition.

  • handle: Opaque handle returned by loadMusic.
  • volume: Target gain, where 1 is normal volume.
  • loop: Whether playback repeats after completion.
ts
crossfadeTo(handle: any, durationMs: int, volume: float, loop: boolean): void

Transitions from the active track to a loaded track.

  • handle: Opaque handle returned by loadMusic.
  • durationMs: Transition duration in milliseconds.
  • volume: Target gain, where 1 is normal volume.
  • loop: Whether playback repeats after completion.
ts
unloadMusic(handle: any): void

Cancels and releases a loaded track that is no longer current.

  • handle: Opaque handle returned by loadMusic.

MusicDirector ​

ts
import { MusicDirector } from "gamekit";
class MusicDirector

Owns the current music request, avoiding duplicate changes and restoring temporarily stacked tracks. Supply an IMusicBackend implementation or a small adapter that forwards to core Audio; the checker does not currently accept typeof Audio directly as the interface. An outgoing handle remains retained for the full following transition and releases only on the next transition or dispose, so the mixer never fades from a freed track.

Members

ts
constructor(backend: IMusicBackend, crossfadeMs: int = 500)

Creates a director over backend with a fixed crossfade length.

  • backend: Music boundary, normally an adapter over core Audio or a test fake.
  • crossfadeMs: Crossfade length in milliseconds.
  • Default: 500
ts
async play(trackId: string): Promise<void>

Makes trackId current. Repeating the current id makes no backend request. A replaced handle remains loaded throughout this transition and releases when a later transition begins or this director is disposed.

  • trackId: Project-relative track identifier.
  • Returns: A promise that resolves after the requested track is loaded and started or crossfaded.
ts
async push(trackId: string): Promise<void>

Temporarily makes trackId current and remembers the prior track for pop.

  • trackId: Project-relative track identifier.
  • Returns: A promise that resolves after the requested track is loaded and started or crossfaded.
ts
async pop(): Promise<void>

Brings back the most recently pushed track; does nothing when the temporary-track stack is empty.

  • Returns: A promise that resolves after the prior track is loaded and crossfaded.
ts
dispose(): void

Releases the current handle and the one retained for a completed fade, then clears all track requests. Call this when the director's owner is finished with music playback.