Add sounds and music
Put sound files in your game's folder (public/games/<game>/sounds/) and name them in the view by what happens: a unit's attack, death, selection, an ability, victory, the music.
// in src/game.ts
import { type GameView } from '@v12-engine/client'
import { createGame } from '@v12-engine/engine'
import { rules } from './rules'
export const view: GameView = {
units: {
looks: {}
},
files: {
// the game's folder of files: models, sounds, textures, icons
assets: 'games/mygame'
},
sounds: {
click: 'click',
victory: 'victory',
defeat: 'defeat',
// every unit and building, then each type's own over them
unit: { select: 'unit-ready', order: 'unit-yes', death: 'unit-dies' },
building: { select: 'building-select', death: 'building-falls' },
types: {
archer: { attack: 'bow', select: 'archer-ready', order: 'archer-yes', death: 'archer-dies' },
barracks: { select: 'drums' }
},
abilities: { fireball: 'fireball-cast' },
strikes: { arrow: 'arrow-hit' },
// tracks, one after the other, over and over
music: ['theme', 'battle'],
ambience: ['wind']
},
cinematics: {
// recorded lines of dialog, by the key of their words
voices: { briefingHold: 'commander-briefing' }
}
}
export const game = createGame(rules).view(view)Done: units answer when selected and ordered, archers twang, fireballs whoosh, and two tracks loop with a crossfade.
Name files without folder or extension. Takes named bow_1.mp3, bow_2.mp3 play one at random.
Tune how a sound plays
Set volume, pitch range, reach and bus per sound in sounds.json (the editor's Sounds panel writes it):
{
"bow": { "volume": 0.7, "pitch": [0.9, 1.1], "reach": 24, "cutoff": 18 },
"horn": { "volume": 1.4, "pitch": [1, 1], "bus": "music" }
}import { layFiles, SOUNDS_FILE } from '@v12-engine/client'
import soundsFile from './sounds.json'
export const view = layFiles({ ...myView, sounds: { ...myView.sounds, defs: { chop: { volume: 0.5 } } } }, [[SOUNDS_FILE, soundsFile]])Play music on a menu
menuScene({ id: 'home', title: 'My game', buttons, music: '/games/mygame/sounds/music/theme.mp3' })It crossfades from what's playing and carries on past the menu until a scene says otherwise. music: null fades it out.
Duck, muffle and mix
sounds: {
music: ['theme'],
mix: {
// buses of the game’s own: a volume of the player’s in the HUD’s mixer, kept like the others
buses: { radio: { label: 'Radio', level: 0.6, into: 'voice', speaks: true, ducks: false } },
// how far and how fast what ducks goes down while someone speaks
duck: { to: 0.2, over: 0.5 },
// everything heard through a low-pass while the game is paused (nothing unless said)
onPause: { lowpass: 800, over: 0.3 },
snapshots: { inside: { buses: { ambience: 0.3, effects: 0.8 }, lowpass: 1200 } },
// seconds music takes to go over to other music
crossfade: 3
}
}The same in code, through the app's one mixer (AppMixer):
import type { GameClient } from '@v12-engine/client'
declare const client: GameClient
const mixer = client.sounds.mixer
mixer.bus('radio', { label: 'Radio' }) // .node to play into, .level(), .setLevel()
mixer.duck({ to: 0.2, over: 0.5 })
mixer.onPause({ lowpass: 800 })
mixer.pause(true) // what the client says of its pause menu and a server’s pause
mixer.snapshot('inside', { buses: { ambience: 0.3 }, lowpass: 1200 })
mixer.snapshot('before') // the mix as it stands now, kept under a name
mixer.recall('inside', 0.5) // over half a second
mixer.recall() // back to the mix as the player set it
mixer.setMusic(['/a.mp3', '/b.mp3'], { crossfade: 2 })
mixer.playFile('/line.mp3', { bus: 'voice' })Outside a game, get it with await app.runtime.mixer() (or context.runtime.mixer() in a scene).
In the editor
- Drop sound files on the Assets panel. MP3s go to
sounds/; turn on MP3s are music to send them tosounds/music/. - Open the Sounds panel to set how each plays and on which bus.
- Use the sound tool (Markers) to place an emitter on the map with its reach and volume.
- Set the map's music in the Look panel.
Reference
sounds.json field | What it does |
|---|---|
volume | Times what the caller asks; above 1 is louder. |
pitch | [lowest, highest] rate, picked each time. Default 0.94–1.06; [1, 1] for none. |
reach | How far it's heard, in cells. Defaults to the view's hearing. |
cutoff | Past this distance it isn't started at all. Defaults to reach. |
bus | music, effects, voice, ambience, interface, or one of yours. |
A map's own tracks and emitters are in MapEnvironment.music and sounds. A cinematic's music is its audio lane. In code: SoundPlayer.define(defs), defOf(name), audition(name, def?). Snapshots and bus scales never change the player's saved volumes. See Shared art and Cinematics.