Skip to content

Commit

Permalink
Merge pull request #406 from orottier/bugfix/panic-low-sample-rate
Browse files Browse the repository at this point in the history
Avoid HRTF panic when constructing an AudioContext with low sample rate
  • Loading branch information
orottier authored Dec 3, 2023
2 parents f43ce60 + 072be1a commit 935de1b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/node/panner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ pub(crate) fn load_hrtf_processor(sample_rate: u32) -> (HrtfProcessor, usize) {
static INSTANCE: OnceLock<Mutex<HashMap<u32, (HrtfProcessor, usize)>>> = OnceLock::new();
let cache = INSTANCE.get_or_init(|| Mutex::new(HashMap::new()));

// There's an upstream bug for low sample rates, so work around it by forcing sample_rate to be
// 27k minimum. The HRTF response will be a bit distorted but I assume you won't be using it
// anyway when running these low sample rates. <https://github.com/mrDIMAS/hrtf/issues/9>
let sample_rate = sample_rate.max(27_000);

// To avoid poisening the cache mutex, don't use the `entry()` API on HashMap
{
if let Some(value) = cache.lock().unwrap().get(&sample_rate) {
Expand Down
2 changes: 1 addition & 1 deletion tests/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ fn test_none_sink_id() {
}

#[test]
#[should_panic] // https://github.com/mrDIMAS/hrtf/issues/9
fn test_weird_sample_rate() {
let options = AudioContextOptions {
sink_id: "none".into(),
sample_rate: Some(24000.),
..AudioContextOptions::default()
};

// would crash due to <https://github.com/mrDIMAS/hrtf/issues/9>
let _ = AudioContext::new(options);
}

Expand Down

0 comments on commit 935de1b

Please sign in to comment.