Skip to main content

Module audio

Module audio 

Source
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:

  1. The name as given ("sounds/step.ogg").
  2. Inside the sound directory set with Engine::sound_dir (default "sounds").
  3. In a macOS app bundle, both of the above inside Contents/Resources.
  4. If the name has no extension, .ogg, .wav, .mp3, .flac, .opus and .m4a are 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.
SoundBuilder
Configures a sound before playing it. Created by Audio::sound.
SoundData
Decoded PCM audio kept in memory: interleaved f32 samples.
Synth
Builds a sound from waves and envelopes. Pass it to Audio::register or Ui::set_sound_data, or call Synth::build.

Enums§

Group
Volume group a voice belongs to. Each group has its own volume control.
Wave
Basic wave shapes.