@@ -15,20 +15,27 @@ use super::{
15
15
AudioNode , ChannelConfig , ChannelConfigOptions , ChannelCountMode , ChannelInterpretation ,
16
16
} ;
17
17
18
- /// Load the HRIR sphere for the given sample_rate
18
+ /// Load the HRTF processor for the given sample_rate
19
19
///
20
20
/// The included data contains the impulse responses at 44100 Hertz, so it needs to be resampled
21
21
/// for other values (which can easily take 100s of milliseconds). Therefore cache the result (per
22
22
/// sample rate) in a global variable and clone it every time a new panner is created.
23
- fn load_hrir_sphere ( sample_rate : u32 ) -> HrirSphere {
24
- static INSTANCE : OnceLock < Mutex < HashMap < u32 , HrirSphere > > > = OnceLock :: new ( ) ;
23
+ fn load_hrtf_processor ( sample_rate : u32 ) -> ( HrtfProcessor , usize ) {
24
+ static INSTANCE : OnceLock < Mutex < HashMap < u32 , ( HrtfProcessor , usize ) > > > = OnceLock :: new ( ) ;
25
25
let cache = INSTANCE . get_or_init ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
26
26
let mut guard = cache. lock ( ) . unwrap ( ) ;
27
27
guard
28
28
. entry ( sample_rate)
29
29
. or_insert_with ( || {
30
30
let resource = include_bytes ! ( "../../resources/IRC_1003_C.bin" ) ;
31
- HrirSphere :: new ( & resource[ ..] , sample_rate) . unwrap ( )
31
+ let hrir_sphere = HrirSphere :: new ( & resource[ ..] , sample_rate) . unwrap ( ) ;
32
+ let len = hrir_sphere. len ( ) ;
33
+
34
+ let interpolation_steps = 1 ; // TODO?
35
+ let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
36
+ let processor = HrtfProcessor :: new ( hrir_sphere, interpolation_steps, samples_per_step) ;
37
+
38
+ ( processor, len)
32
39
} )
33
40
. clone ( )
34
41
}
@@ -187,14 +194,7 @@ struct HrtfState {
187
194
}
188
195
189
196
impl HrtfState {
190
- fn new ( hrir_sphere : HrirSphere ) -> Self {
191
- let len = hrir_sphere. len ( ) ;
192
-
193
- let interpolation_steps = 1 ;
194
- let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
195
-
196
- let processor = HrtfProcessor :: new ( hrir_sphere, interpolation_steps, samples_per_step) ;
197
-
197
+ fn new ( processor : HrtfProcessor , len : usize ) -> Self {
198
198
Self {
199
199
len,
200
200
processor,
@@ -570,8 +570,8 @@ impl PannerNode {
570
570
PanningModelType :: EqualPower => None ,
571
571
PanningModelType :: HRTF => {
572
572
let sample_rate = self . context ( ) . sample_rate ( ) as u32 ;
573
- let hrir_sphere = load_hrir_sphere ( sample_rate) ;
574
- Some ( HrtfState :: new ( hrir_sphere ) )
573
+ let ( processor , len ) = load_hrtf_processor ( sample_rate) ;
574
+ Some ( HrtfState :: new ( processor , len ) )
575
575
}
576
576
} ;
577
577
0 commit comments