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:
| File | Id |
|---|---|
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) |
- Formats: wav, aiff, flac, ogg, opus, mp3, m4a, aac, mp4, webm. Keep your best quality (WAV is fine): the build turns WAV/AIFF into lossless FLAC and makes Opus for the lite build (media settings).
- Ids are typed (
AudioId): a wrong one is an error. - Nothing downloads until it's played or preloaded.
- The current sounds are placeholders made with a synth script; replace the files and keep the names.
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 music | Plays on entering the map. null = silence; left out = keep the current track. |
Map ambient / weather | See below |
| Sound zones | See below |
Line style voice | say('anna', 'Hello!', { voice: 12 }) (the line's voice number): see Voices |
Character blip | A short typing sound every two letters, for lines without a voice |
| UI sounds | Focus 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 music | During 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.
- In Tiled (preferred): draw a rectangle, class
zone, string propertiesmusicand/orambient. An empty value means silence. - In code: any object with an area and
music/ambient:{ at: { x: 22, y: 12 }, size: { w: 5, h: 5 }, music: null, ambient: 'ambient/fire' }
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?
- Ducking: while a voice plays the music drops to
voice.duckMusic(0.4) of its volume; a movie fades it out. Numbers insrc/content/scenes.ts. - Music room (Extras → Music): list your tracks in
scenes.ts:
A track unlocks when it starts playing anywhere in the game (title, map, battle or event music), even if the player leaves at once. Map music counts too: a map you only pass through (like an intro map) still unlocks its track. Picking a track in the music room plays it without unlocking it (so "Show all" stays temporary); the music from before returns when the tab closes.musicRoom: { tracks: [{ id: 'music/town', title: 'Town', artist: 'You' }], unlock: 'heard', // a track appears once it has played; 'always' = all from the start },game.extras.music()lists them;game.audio.music(id, { heard: false })plays a track without unlocking it. - Music per voice language:
audio/music/ja/town.oggandaudio/music/en/town.oggare both'music/town'. It plays in the voice language the player picked (Settings → Audio), else the next language ofvoice.languagesthat has it (en/may be missing whileja/is there), else a plainmusic/town.ogg. Tracks without language folders work as before. The language is picked when the track starts. - Sounds per language:
sfx/shout.pl.oggnext toshout.oggplays instead while Polish text is chosen. Works for any sound but voices and music in language folders (they follow the voice language); the language is a folder insrc/content/lang(see Translations).
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)
| Cue | Said when |
|---|---|
title_call | The 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_loaded | Quick save / quick load |
saved, loaded | The Saves window (src/content/windows/saves) after a save / load |
picked | The player picks this character as the system voice |
quit | The pause menu's Quit (data-voice="quit"); the app waits for it before it closes (at most voice.system.quitWaitMs, 2500) |
- A cue the character has no clip for plays nothing: no error, no warning. Name files in lowercase with
_. - Volume: the Voice slider × that character's own volume and mute in Settings → Voices (no slider of its own). It never cuts off a line's voice, moves no mouth and does not lower the music.
- Nothing plays while a scene replays (a loaded save, Back).
- New players' pick:
voice.system.defaultinsrc/content/scenes.ts('random' when left out; also a character id or 'off').
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
- Four sliders in Settings: master, music, sound effects (also ambient), voice. They apply live and are saved per device.
- Under them: Voices play to the end, Lower music under voices and Voice language (see Voices), and each voiced character's volume and mute button are in the Voices tab (see Voices).
- Browsers only allow sound after the player has pressed something. The title screen's "press any key" does that; before it, nothing plays.
- Sound pauses when the browser tab is hidden.
- Without audio support everything simply stays silent (no errors).