JFLib

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 Rolloff how the gain falls beyond near

    • Linear -- to 0 at far
    • Inverse(f64) -- near / (near + rolloff (d - near)), d clamped to far (OpenAL's AL_INVERSE_DISTANCE_CLAMPED)
  • struct Attenuation the gain by distance (metres)

    • near: f64
    • far: f64
    • rolloff: 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) -> Attenuation linear from near to far

  • fn gain(&self, d: f64) -> f64 the gain at distance d (0 ..= 1)

  • struct Gains what a listener hears of a source

    • left: f32
    • right: f32
    • distance: f64 -- the distance (metres), the pan (-1 left .. 1 right), the gain before the pan (volume x attenuation)
    • pan: f64
    • gain: f64
  • fn gains(listener: &View, at: Vec3, volume: f64, att: &Attenuation) -> Gains the gains of a sound at at with volume (0 ..= 1) for the listener (see the module header)

  • trait Mixer where voices play

    • fn play(&mut self, sound: &Sound, left: f32, right: f32) -> Result<Voice, AudioError> start sound with these gains (they hold from its first sample on)
  • struct Speakers plat::audio's shared mixer (the speakers; an app with a manifest needs audio.output)

  • struct Emitter a 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 ends

    • sound: Sound
    • volume: f32
    • att: Attenuation
    • looping: bool

impl Emitter

  • fn new(sound: Sound) -> Emitter once, at volume 1, with the default attenuation

  • fn with_volume(mut self, v: f32) -> Emitter

  • fn with_attenuation(mut self, a: Attenuation) -> Emitter

  • fn looping(mut self) -> Emitter

  • fn playing(&self) -> bool its voice plays now

  • fn finished(&self) -> bool it has ended (or was stopped, or the mixer refused it)

  • fn restart(&mut self) play it again from the start at the next update

  • fn stop(&mut self) end it now

  • fn gains(&self) -> Option<Gains> what the last update set

  • struct AudioStats what an update did

    • emitters: u32 -- emitters looked at, voices started, voices that ended, sounds at points still playing, voices with a gain above 0, starts the mixer refused
    • started: u32
    • ended: u32
    • shots: u32
    • audible: u32
    • refused: u32
  • struct SpatialAudio the 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() -> SpatialAudio a listener at the origin looking down -Z
  • fn set_listener(&mut self, v: &View) where the listener is and looks (the camera's View)
  • fn listener(&self) -> &View
  • fn 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 ends
  • fn update(&mut self, world: &mut World, alpha: f64, mixer: &mut dyn Mixer) -> AudioStats once a frame, after the listener is set: the emitters of world (their voices started, their gains at the poses interpolated at alpha), the sounds at points -> what it did
  • fn stop_all(&mut self) every sound at a point ends now (the emitters keep theirs: Emitter::stop)

Source: docs/API.md in the JFLib repository.