1
1
use std:: any:: Any ;
2
- use std:: sync:: atomic:: { AtomicBool , Ordering } ;
3
- use std:: sync:: { Arc , Mutex } ;
2
+ use std:: sync:: Arc ;
4
3
5
4
use realfft:: { num_complex:: Complex , ComplexToReal , RealFftPlanner , RealToComplex } ;
6
5
@@ -95,7 +94,7 @@ pub struct ConvolverOptions {
95
94
/// let mut src = context.create_buffer_source();
96
95
/// src.set_buffer(audio_buffer);
97
96
///
98
- /// let convolve = ConvolverNode::new(&context, ConvolverOptions::default());
97
+ /// let mut convolve = ConvolverNode::new(&context, ConvolverOptions::default());
99
98
/// convolve.set_buffer(impulse_buffer);
100
99
///
101
100
/// src.connect(&convolve);
@@ -114,9 +113,9 @@ pub struct ConvolverNode {
114
113
/// Info about audio node channel configuration
115
114
channel_config : ChannelConfig ,
116
115
/// Perform equal power normalization on response buffer
117
- normalize : AtomicBool ,
116
+ normalize : bool ,
118
117
/// The response buffer, nullable
119
- buffer : Mutex < Option < AudioBuffer > > ,
118
+ buffer : Option < AudioBuffer > ,
120
119
}
121
120
122
121
impl AudioNode for ConvolverNode {
@@ -156,14 +155,14 @@ impl ConvolverNode {
156
155
channel_config,
157
156
} = options;
158
157
159
- let node = context. base ( ) . register ( move |registration| {
158
+ let mut node = context. base ( ) . register ( move |registration| {
160
159
let renderer = ConvolverRenderer { inner : None } ;
161
160
162
161
let node = Self {
163
162
registration,
164
163
channel_config : channel_config. into ( ) ,
165
- normalize : AtomicBool :: new ( !disable_normalization) ,
166
- buffer : Mutex :: new ( None ) ,
164
+ normalize : !disable_normalization,
165
+ buffer : None ,
167
166
} ;
168
167
169
168
( node, Box :: new ( renderer) )
@@ -178,9 +177,8 @@ impl ConvolverNode {
178
177
}
179
178
180
179
/// Get the current impulse response buffer
181
- #[ allow( clippy:: missing_panics_doc) ]
182
- pub fn buffer ( & self ) -> Option < AudioBuffer > {
183
- self . buffer . lock ( ) . unwrap ( ) . clone ( )
180
+ pub fn buffer ( & self ) -> Option < & AudioBuffer > {
181
+ self . buffer . as_ref ( )
184
182
}
185
183
186
184
/// Set or update the impulse response buffer
@@ -189,13 +187,13 @@ impl ConvolverNode {
189
187
///
190
188
/// Panics when the sample rate of the provided AudioBuffer differs from the audio context
191
189
/// sample rate.
192
- pub fn set_buffer ( & self , mut buffer : AudioBuffer ) {
190
+ pub fn set_buffer ( & mut self , mut buffer : AudioBuffer ) {
193
191
// resample if necessary
194
192
buffer. resample ( self . context ( ) . sample_rate ( ) ) ;
195
193
let sample_rate = buffer. sample_rate ( ) ;
196
194
197
195
// normalize before padding because the length of the buffer affects the scale
198
- let scale = if self . normalize ( ) {
196
+ let scale = if self . normalize {
199
197
normalize_buffer ( & buffer)
200
198
} else {
201
199
1.
@@ -219,18 +217,17 @@ impl ConvolverNode {
219
217
let convolve = ConvolverRendererInner :: new ( padded_buffer) ;
220
218
221
219
self . registration . post_message ( Some ( convolve) ) ;
222
-
223
- * self . buffer . lock ( ) . unwrap ( ) = Some ( buffer) ;
220
+ self . buffer = Some ( buffer) ;
224
221
}
225
222
226
223
/// Denotes if the response buffer will be scaled with an equal-power normalization
227
224
pub fn normalize ( & self ) -> bool {
228
- self . normalize . load ( Ordering :: SeqCst )
225
+ self . normalize
229
226
}
230
227
231
228
/// Update the `normalize` setting. This will only have an effect when `set_buffer` is called.
232
- pub fn set_normalize ( & self , value : bool ) {
233
- self . normalize . store ( value , Ordering :: SeqCst ) ;
229
+ pub fn set_normalize ( & mut self , value : bool ) {
230
+ self . normalize = value ;
234
231
}
235
232
}
236
233
@@ -504,7 +501,7 @@ mod tests {
504
501
src. set_buffer ( input) ;
505
502
src. start ( ) ;
506
503
507
- let conv = ConvolverNode :: new ( & context, ConvolverOptions :: default ( ) ) ;
504
+ let mut conv = ConvolverNode :: new ( & context, ConvolverOptions :: default ( ) ) ;
508
505
if let Some ( ir) = impulse_resp {
509
506
conv. set_buffer ( AudioBuffer :: from ( vec ! [ ir. to_vec( ) ] , sample_rate) ) ;
510
507
}
@@ -592,7 +589,7 @@ mod tests {
592
589
593
590
let context = OfflineAudioContext :: new ( 1 , 128 , ctx_sample_rate) ;
594
591
595
- let conv = ConvolverNode :: new ( & context, ConvolverOptions :: default ( ) ) ;
592
+ let mut conv = ConvolverNode :: new ( & context, ConvolverOptions :: default ( ) ) ;
596
593
let ir = vec ! [ 1. ; 128 ] ;
597
594
let ir_buffer = AudioBuffer :: from ( vec ! [ ir] , ir_sample_rate) ;
598
595
conv. set_buffer ( ir_buffer) ;
0 commit comments