use super::*;
impl Default for AudioBus {
fn default() -> Self {
Self {
master_volume: 0.85,
muted: false,
queue: Vec::new(),
total_played: 0,
}
}
}
impl AudioBus {
pub fn play(&mut self, cue: SoundCue, volume: f32, pan: f32, pitch: f32) {
if self.get_muted() {
return;
}
let eff_vol: f32 = (volume.clamp(0.0, 1.0) * self.get_master_volume()).clamp(0.0, 1.0);
if eff_vol <= 0.001 {
return;
}
self.get_mut_queue().push(AudioCommand::new(
cue,
eff_vol,
pan.clamp(-1.0, 1.0),
pitch.clamp(0.25, 4.0),
));
let next_total: u64 = self.get_total_played().saturating_add(1);
self.set_total_played(next_total);
}
pub fn drain(&mut self) -> Vec<AudioCommand> {
take(self.get_mut_queue())
}
pub fn play_spatial_3d(&mut self, listener: &AudioListener3D, source: &SpatialAudioSource3D) {
let delta: Vec3 = source.get_position().sub(listener.get_position());
let dist: f32 = delta.length();
let max_d: f32 = source.get_max_distance().max(1.0);
if dist >= max_d {
return;
}
let min_d: f32 = source.get_min_distance().clamp(0.25, max_d);
let atten: f32 = if dist <= min_d {
1.0
} else {
let norm: f32 = ((dist - min_d) / (max_d - min_d).max(0.01)).clamp(0.0, 1.0);
(1.0 - norm) * (1.0 - norm)
};
let to_src: Vec3 = if dist > 1e-4 {
delta.scale(1.0 / dist)
} else {
Vec3::zero()
};
let pan: f32 = to_src.dot(listener.get_right()).clamp(-1.0, 1.0);
let rel_vel: f32 = source
.get_velocity()
.sub(listener.get_velocity())
.dot(to_src);
let speed_of_sound: f32 = 343.0;
let doppler: f32 =
(speed_of_sound / (speed_of_sound + rel_vel).clamp(120.0, 680.0)).clamp(0.5, 2.0);
self.play(
source.get_cue(),
source.get_volume() * atten,
pan,
source.get_pitch() * doppler,
);
}
}
impl AudioListener3D {
#[must_use]
pub fn from_camera(camera: &Camera3D, velocity: Vec3) -> Self {
Self::new(camera.get_position(), velocity, camera.right())
}
}
impl SpatialAudioSource3D {
#[must_use]
pub fn new(cue: SoundCue, position: Vec3) -> Self {
Self {
cue,
position,
velocity: Vec3::zero(),
volume: 1.0,
pitch: 1.0,
min_distance: 2.0,
max_distance: 80.0,
}
}
#[must_use]
pub fn with_velocity(mut self, velocity: Vec3) -> Self {
self.set_velocity(velocity);
self
}
#[must_use]
pub fn with_range(mut self, volume: f32, min_distance: f32, max_distance: f32) -> Self {
self.set_volume(volume.clamp(0.0, 1.0));
self.set_min_distance(min_distance.max(0.25));
self.set_max_distance(max_distance.max(min_distance + 0.5));
self
}
}