Skip to content

Add sounds and music

Sounds and songs are synth data in game/audio/ (asset type synth), rendered when the game loads.

A sound effect, game/audio/zap.json:

{
"kind": "sound",
"wave": "square",
"duty": 0.25,
"frequency": 880,
"slideTo": 220,
"duration": 0.2,
"volume": 0.3,
"vibrato": { "rate": 20, "depth": 0.5 }
}

Register it in game.json under an asset bundle and play it from a rule:

[{ "on": "enemy:stomped", "do": [{ "action": "playSound", "sound": "sfx.zap" }] }]

with the asset entry { "key": "sfx.zap", "type": "synth", "src": "audio/zap.json", "bus": "sfx" }.

A song: tracks of space-separated steps (C4, F#3, . rest, - hold), patterns played in sequence, and a drum line (k kick, s snare, h hi-hat). All tracks should have the same number of steps.

{
"kind": "song",
"bpm": 120,
"tracks": [
{ "instrument": { "wave": "square", "duty": 0.25, "volume": 0.2 }, "patterns": { "A": "C5 - E5 - G5 - E5 -", "B": "D5 - F5 - A5 - - -" }, "sequence": ["A", "B"] },
{ "instrument": { "wave": "triangle", "volume": 0.5 }, "patterns": { "A": "C3 . . . G2 . . .", "B": "D3 . . . A2 . . ." }, "sequence": ["A", "B"] },
{ "drums": true, "patterns": { "D": "k . h . s . h ." }, "sequence": ["D", "D"] }
]
}

Play it with the scene’s music ({ "track": "music.theme" }) or a playMusic action.