game/src/audio.rs#
Source: lib/game/src/audio.rs
audio -- (G7b part 2) spatial audio over plat::audio's mixer: a listener (the camera), sounds on entities and at
points, the volume by distance (near, far, a rolloff), the pan by direction (docs/GAMECORE.md "Raumklang")
Attenuation { near, far, rolloff } the gain by distance: 1 up to near; Linear falls to 0 at far, Inverse
(OpenAL's inverse distance, clamped) near / (near + rolloff (d - near)) up to far
and stays there beyond it
gains(&View, at, volume, &att) -> Gains left and right gain of a sound at `at` for a listener: equal power
(left = cos, right = sin of (pan + 1) pi / 4, so -3 dB each in the middle), pan
the sine of the direction's angle to the right (front and back sound the same:
stereo), drawn to the middle within PAN_RADIUS of the listener
Emitter (a component) a sound on an entity, played at its Pose (interpolated at the frame's alpha)
SpatialAudio set_listener(&View), update(&mut World, alpha, &mut dyn Mixer) once a frame:
starts the emitters' voices, sets every voice's gains; play_at(sound, at, ..):
a sound at a point (a shot, a hit) until it ends
Mixer where voices play: Speakers (plat::audio's shared mixer, play_gains), or
plat::audio::MixTest (the same mixer without a device: tests, sample by sample)
No Doppler: plat's mixer plays a voice at its rate (a pitch per voice would need its resampler steered). A voice's
gains change at the mixer's next buffer (about 20 ms), without a ramp. Like the rest of lib/game the gains are the
same bits on every target: + - x / and lib/physics' math (its square root, rt's portable sine and cosine).
Re-exports: plat::audio::{AudioError, Sound, Voice}
/// a source this close to the listener drifts to the middle (metres): no jump from left to right through the head
PAN_RADIUS: f64 = 0.5
enum Rolloffhow the gain falls beyond nearLinear-- to 0 at farInverse(f64)-- near / (near + rolloff (d - near)), d clamped to far (OpenAL's AL_INVERSE_DISTANCE_CLAMPED)
struct Attenuationthe gain by distance (metres)near: f64far: f64rolloff: Rolloff
impl Attenuation
const DEFAULT: Attenuation = Attenuation { near: 1.0, far: 50.0, rolloff: Rolloff::Inv ...near 1 m, far 50 m, inverse with rolloff 1 (half the gain at 2 m, a fiftieth at 50 m and beyond)const fn linear(near: f64, far: f64) -> Attenuationlinear from near to farfn gain(&self, d: f64) -> f64the gain at distance d (0 ..= 1)struct Gainswhat a listener hears of a sourceleft: f32right: f32distance: f64-- the distance (metres), the pan (-1 left .. 1 right), the gain before the pan (volume x attenuation)pan: f64gain: f64
fn gains(listener: &View, at: Vec3, volume: f64, att: &Attenuation) -> Gainsthe gains of a sound atatwithvolume(0 ..= 1) for the listener (see the module header)trait Mixerwhere voices playfn play(&mut self, sound: &Sound, left: f32, right: f32) -> Result<Voice, AudioError>startsoundwith these gains (they hold from its first sample on)
struct Speakersplat::audio's shared mixer (the speakers; an app with a manifest needsaudio.output)struct Emittera sound on an entity (a component): it plays at the entity's Pose (the origin without one) from the next SpatialAudio::update on; looping: started again when it endssound: Soundvolume: f32att: Attenuationlooping: bool
impl Emitter
fn new(sound: Sound) -> Emitteronce, at volume 1, with the default attenuationfn with_volume(mut self, v: f32) -> Emitterfn with_attenuation(mut self, a: Attenuation) -> Emitterfn looping(mut self) -> Emitterfn playing(&self) -> boolits voice plays nowfn finished(&self) -> boolit has ended (or was stopped, or the mixer refused it)fn restart(&mut self)play it again from the start at the next updatefn stop(&mut self)end it nowfn gains(&self) -> Option<Gains>what the last update setstruct AudioStatswhat an update didemitters: u32-- emitters looked at, voices started, voices that ended, sounds at points still playing, voices with a gain above 0, starts the mixer refusedstarted: u32ended: u32shots: u32audible: u32refused: u32
struct SpatialAudiothe listener and the voices of a game's sounds (see the module header)master: f32-- every gain times this (0 ..= 1)
impl SpatialAudio
fn new() -> SpatialAudioa listener at the origin looking down -Zfn set_listener(&mut self, v: &View)where the listener is and looks (the camera's View)fn listener(&self) -> &Viewfn play_at(&mut self, sound: &Sound, at: Vec3, volume: f32, att: Attenuation, mixer: &mut dyn Mixer) -> Result<(), AudioError>a sound at a point, once (a shot, a hit): its gains follow the listener until it endsfn update(&mut self, world: &mut World, alpha: f64, mixer: &mut dyn Mixer) -> AudioStatsonce a frame, after the listener is set: the emitters ofworld(their voices started, their gains at the poses interpolated atalpha), the sounds at points -> what it didfn stop_all(&mut self)every sound at a point ends now (the emitters keep theirs: Emitter::stop)