Skip to content

Commit

Permalink
fix: omni sources can be played again
Browse files Browse the repository at this point in the history
  • Loading branch information
anon committed Feb 17, 2021
1 parent 5c9ae53 commit f2089f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
11 changes: 7 additions & 4 deletions examples/stress.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
Playing many sources simultaneously will lead to stuttering if compiled in Debug mode
but hopefully not in Release mode.
*/

use ambisonic::AmbisonicBuilder;
use std::thread::sleep;
use std::time::Duration;
Expand All @@ -6,10 +11,8 @@ fn main() {
use rodio::Source;
let scene = AmbisonicBuilder::default().build();

let mut f: u64 = 1;
for _ in 0..1 {
f = (f + f * f * 7 + f * f * f * 3 + 1) % 800;
let source = rodio::source::SineWave::new(440); //.amplify(0.001);
for _ in 0..500 {
let source = rodio::source::SineWave::new(440).amplify(0.001);
let _ = scene.play_omni(source);
}

Expand Down
40 changes: 38 additions & 2 deletions src/bstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,44 @@ fn compute_doppler_rate(
let dist =
(position[0] * position[0] + position[1] * position[1] + position[2] * position[2]).sqrt();

let relative_velocity =
(position[0] * velocity[0] + position[1] * velocity[1] + position[2] * velocity[2]) / dist;
let relative_velocity;

if dist.abs() < EPS {
relative_velocity =
(velocity[0] * velocity[0] + velocity[1] * velocity[1] + velocity[2] * velocity[2])
.sqrt();
} else {
relative_velocity =
(position[0] * velocity[0] + position[1] * velocity[1] + position[2] * velocity[2])
/ dist;
}

speed_of_sound / (speed_of_sound + doppler_factor * relative_velocity)
}

const EPS: f32 = 1e-6;

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn no_doppler_effect_if_velocity_is_zero() {
let position = [0.0, 1.0, 0.0];
let velocity = [0.0, 0.0, 0.0];

let rate = compute_doppler_rate(position, velocity, 1.0, 1.0);

assert_eq!(rate, 1.0);
}

#[test]
fn doppler_effect_depends_on_velocity_if_position_is_zero() {
let position = [0.0, 0.0, 0.0];
let velocity = [1.0, 1.0, 1.0];

let rate = compute_doppler_rate(position, velocity, 1.0, 1.0);

assert_eq!(rate, 1.0 / (1.0 + f32::sqrt(3.0)));
}
}

0 comments on commit f2089f6

Please sign in to comment.