Expand description
High level sound: one-liners for effects, 3D positioned sounds and music.
Available as ctx.audio.
// Fire and forget.
ctx.audio.play("door_open");
// A looping sound placed in the world. Keep the handle to move or stop it later.
let engine = ctx.audio.play_looped_at("car_engine", vec3(10.0, 5.0, 0.0));
engine.set_pitch(1.3);
// Full control with the builder.
ctx.audio.sound("explosion").volume(0.8).pitch(0.9).at(vec3(3.0, 0.0, 0.0)).play();
// Background music with automatic cross fading.
ctx.audio.play_music("music/level1");§Finding files
Sound names are resolved like this:
- The name as given (
"sounds/step.ogg"). - Inside the sound directory set with
Engine::sound_dir(default"sounds"). - In a macOS app bundle, both of the above inside
Contents/Resources. - If the name has no extension,
.ogg,.wav,.mp3,.flac,.opusand.m4aare tried.
So with the default settings ctx.audio.play("step") finds sounds/step.ogg.
Loaded files are cached, the second play of the same name costs nothing. Missing or broken files print a warning once and return a silent handle, so a typo never crashes your game.
Structs§
- Audio
- The audio system. Reach it through
ctx.audio. - Sound
- Handle to a playing sound. Cheap to clone. Dropping it does not stop the sound.
- Sound
Builder - Configures a sound before playing it. Created by
Audio::sound. - Sound
Data - Decoded PCM audio kept in memory: interleaved
f32samples. - Synth
- Builds a sound from waves and envelopes. Pass it to
Audio::registerorUi::set_sound_data, or callSynth::build.