Skip to content

Sound

await engine.load({ pop: 'sfx/pop.mp3' })
engine.sound.play('pop', { volume: 0.8 })
engine.sound.tone(880, 0.15) // a "ding", without a sound file

engine.sound is Web Audio, as small as it can be: decoded sounds by name, and simple tones for games without sound files.

  • sound.play(name, options?) plays a sound loaded with engine.load (.mp3, .ogg, .wav, .m4a, .aac, .webm), and returns a function that stops it. Options: volume (0 to 1, default 1), rate (the playback rate, which changes the pitch; 1 normal), loop (start again at the end, until stopped; default false).
  • sound.tone(freq, seconds?, options?) plays a short tone at freq Hz (0.25 seconds by default), fading out. Options: volume (default 0.12), type (the waveform: 'sine' by default, 'square', 'sawtooth', 'triangle'), to (glide to this frequency over the tone).
  • sound.has(name) says whether a sound is loaded, and sound.volume is the master volume (0 to 1) for the sounds started after it is set.
const music = engine.sound.play('music', { loop: true, volume: 0.4 })
engine.sound.tone(440, 0.3, { to: 880, type: 'triangle' }) // a rising "boing"
music() // stops the music

You don’t handle audio around Fabi Play’s pauses and ads:

  • while the host pauses the game or shows a break, and while the page is hidden, the audio context is suspended; it resumes afterwards (and after engine.pause() / engine.resume());
  • when the screen’s sound is off at the start of the game (muted), sounds and tones don’t play;
  • browsers allow audio only after the page receives an event: the engine resumes its audio on the first touch, key or click the page gets.

On TVs, prefer MP3 or AAC: they decode everywhere.