Skip to content

Sound ​

bare.sound plays sound effects and music. Sounds are .mp3 files in public/sounds/, played by name. They load on first play.

ts
bare.sound.Play('jump');
bare.sound.PlayWithRandomRate('hit');                 // slight pitch variation
bare.sound.Play('music', { loop: true, volume: 0.4 });
bare.sound.FadeOut('music', 2);

To avoid a delay the first time, load a sound up front:

ts
await bare.sound.Load('explosion');

Examples: the Platformer plays win and lose sounds, and the Showcase's sound demo uses Register and PlayWithRandomRate.

Play options ​

OptionDescription
volume0 to 1.
loopRepeat forever.
ratePlayback speed and pitch.
pan-1 (left) to 1 (right).
startStart position in seconds.
echo, reverbtrue, or an object with settings.
srcA different file than <name>.mp3.
loaded, completeCallbacks.

Methods ​

MethodDescription
Play(id, options?)Play a sound.
PlayWithRandomRate(id, range?, options?)Play with a random rate, default ±10%.
Load(id, src?)Load without playing.
Register(id, src)Map a name to a file in public/sounds/.
Get(id)The loaded sound.
Stop(id) / StopAll()Stop playback.
SetVolume(id, volume)Change volume.
FadeIn(id, seconds) / FadeOut(id, seconds) / FadeTo(id, volume, seconds)Fade volume.

Sounds don't play when SaveData.soundEnabled is false, so you can build a mute toggle with Save Data.

Bare Engine is open source under the MIT license.