Audio

Music, sound effects, voices and ambient loops, with crossfades and per-type volumes. Drop files in a folder and use their ids.

Sound files

Put files anywhere under src/content/audio/. The id is the path without the extension:

FileId
audio/music/town.ogg'music/town'
audio/sfx/click.wav'sfx/click'
audio/voice/en/anna/hello/anna_0001.mp3'voice/en/anna/hello/anna_0001' (a voice clip: Voices)

Playing sounds

game.audio.sfx('sfx/click')                          // fire and forget
game.audio.sfx('sfx/hit', { volume: 0.6, rate: 1.2 })  // rate also changes the pitch

game.audio.music('music/town')                       // crossfades (1 s); the same track again does nothing
game.audio.music('music/forest', { fadeMs: 400, volume: 0.8 })
game.audio.stopMusic(800)
game.audio.currentMusic()                            // 'music/forest'

game.audio.voice('voice/en/anna/hello/anna_0001')  // cuts off the previous voice line
game.audio.stopVoice()

const engine = await game.audio.play('sfx/engine', { loop: true, bus: 'sfx', fadeInMs: 300 })
engine.stop(500)                                     // a handle you stop yourself

await game.audio.preload(['music/forest', 'sfx/hit'])

Where sound comes from automatically

Source
Map musicPlays on entering the map. null = silence; left out = keep the current track.
Map ambient / weatherSee below
Sound zonesSee below
Line style voicesay('anna', 'Hello!', { voice: 12 }) (the line's voice number): see Voices
Character blipA short typing sound every two letters, for lines without a voice
UI soundsFocus moves, clicks, tabs, switches, windows opening and closing: set in content/look/sounds.ts, see UI sounds. A button that plays its own sound needs data-sound="none".
content/battle.ts musicDuring battles; the map's music returns afterwards
A troop's music, or game.battles.start({ music })For that fight only

Music with an intro

// src/content/music.ts
export default defineMusic({
  fadeMs: 1000,                                   // crossfade when the music or ambient sound changes
  loops: {
    'music/boss': { start: 12.48, end: 96.0 },    // plays from 0 once, then repeats 12.48 → 96.0 (seconds)
    'music/town': { volume: 0.8 },                // evens out a track that was mixed louder
  },
})

Leave a track out to repeat the whole file. Without end it repeats from start to the end of the file. Find the times in an audio editor; the loop is sample-accurate.

Ambient sound

A looping background sound under the music (rain, wind, a crackling fire). It crossfades like music and uses the sound effects volume.

game.audio.ambient('ambient/wind'); game.audio.ambient(null)

defineMap({ ambient: 'ambient/cave' })     // this map's loop; null = none

// src/content/ambient.ts — used when a map doesn't set its own `ambient`
export default defineAmbient({ weather: { rain: 'ambient/rain', snow: null } })

Sound zones

An area with its own music and/or ambient sound. While the player stands in it, it wins over the map; leaving goes back.

Example: campfire_zone in town.tmx: the town music fades out near the fire and the fire crackles.

Voices, ducking, music room

game.audio.voice('voice/en/anna/hello/anna_0001', { volume: 0.8 })
game.audio.voices([{ id: 'voice/en/ev/jack_0003', who: 'jack' }, { id: 'voice/en/ev/anna_0007', who: 'anna' }]) // together
game.audio.voicePlaying()                       // true until every clip has ended
game.audio.voiceSpan()                          // { startedAt, endsAt } (performance.now() ms; the longest clip), or null
const stop = game.audio.onVoiceSpan(() => …)    // it changed (the voice bar uses it)
game.audio.voiceLevel('anna')                   // how loud Anna's clip is now, 0..1
const off = game.audio.onVoice((playing) => …)  // starts / ends
const release = game.audio.duck(0.3, 400)       // hold the music at 30% … release()
game.audio.has('sfx/door')                      // does the file exist?

System voice

Short clips a character says for the menus, as in VN games: the title call, "Saved!", "Goodbye". The player picks who speaks in Settings → Voices → System voice (a character, Random or Off). Random picks, each time, one of the characters who have that clip.

src/content/audio/system_voice/anna/title_call.ogg
src/content/audio/system_voice/anna/saved.ogg
src/content/audio/system_voice/anna/quit.ogg
<Button data-voice="shop_open">Shop</Button>   // any button or row: the clip plays with the click sound
game.systemVoice.play('saved')                    // from code (typed: the file names)
game.systemVoice.playAfterReplay('loaded')         // after a loaded mid-scene save has caught up
await game.systemVoice.finish()                     // wait for the clip playing now (quitting does)
CueSaid when
title_callThe title screen appears (in a browser: at the first press, since sound may not play before)
new, continue, load, extras, settings, quit…A title menu entry is picked: the entry's key in src/content/start.ts
quick_saved, quick_loadedQuick save / quick load
saved, loadedThe Saves window (src/content/windows/saves) after a save / load
pickedThe player picks this character as the system voice
quitThe pause menu's Quit (data-voice="quit"); the app waits for it before it closes (at most voice.system.quitWaitMs, 2500)

Memory

A loaded sound takes about 21 MB per minute of stereo music, whatever the file size. Sounds nothing is playing are dropped, least recently used first, once they pass the budget; they load again when needed.

// src/content/config/media.ts
audio: { memoryMB: { desktop: 256, mobile: 64, browser: 128 } },

A sound that is playing is never dropped. The dev panel shows the megabytes in use (game.audio.memoryMB()).

Volumes and browser rules