Skip to content

Commit 935de1b

Browse files
authored
Merge pull request #406 from orottier/bugfix/panic-low-sample-rate
Avoid HRTF panic when constructing an AudioContext with low sample rate
2 parents f43ce60 + 072be1a commit 935de1b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/node/panner.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ pub(crate) fn load_hrtf_processor(sample_rate: u32) -> (HrtfProcessor, usize) {
2424
static INSTANCE: OnceLock<Mutex<HashMap<u32, (HrtfProcessor, usize)>>> = OnceLock::new();
2525
let cache = INSTANCE.get_or_init(|| Mutex::new(HashMap::new()));
2626

27+
// There's an upstream bug for low sample rates, so work around it by forcing sample_rate to be
28+
// 27k minimum. The HRTF response will be a bit distorted but I assume you won't be using it
29+
// anyway when running these low sample rates. <https://github.com/mrDIMAS/hrtf/issues/9>
30+
let sample_rate = sample_rate.max(27_000);
31+
2732
// To avoid poisening the cache mutex, don't use the `entry()` API on HashMap
2833
{
2934
if let Some(value) = cache.lock().unwrap().get(&sample_rate) {

tests/online.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ fn test_none_sink_id() {
108108
}
109109

110110
#[test]
111-
#[should_panic] // https://github.com/mrDIMAS/hrtf/issues/9
112111
fn test_weird_sample_rate() {
113112
let options = AudioContextOptions {
114113
sink_id: "none".into(),
115114
sample_rate: Some(24000.),
116115
..AudioContextOptions::default()
117116
};
118117

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

0 commit comments

Comments
 (0)