@@ -186,10 +186,23 @@ impl ConvolverNode {
186
186
///
187
187
/// Panics when the sample rate of the provided AudioBuffer differs from the audio context
188
188
/// sample rate.
189
- pub fn set_buffer ( & mut self , mut buffer : AudioBuffer ) {
190
- // resample if necessary
191
- buffer. resample ( self . context ( ) . sample_rate ( ) ) ;
189
+ pub fn set_buffer ( & mut self , buffer : AudioBuffer ) {
190
+ // If the buffer number of channels is not 1, 2, 4, or if the sample-rate of the buffer is
191
+ // not the same as the sample-rate of its associated BaseAudioContext, a NotSupportedError
192
+ // MUST be thrown.
193
+
192
194
let sample_rate = buffer. sample_rate ( ) ;
195
+ assert_eq ! (
196
+ sample_rate,
197
+ self . context( ) . sample_rate( ) ,
198
+ "NotSupportedError - sample rate of the convolution buffer must match the audio context"
199
+ ) ;
200
+
201
+ let number_of_channels = buffer. number_of_channels ( ) ;
202
+ assert ! (
203
+ [ 1 , 2 , 4 ] . contains( & number_of_channels) ,
204
+ "NotSupportedError - the convolution buffer must consist of 1, 2 or 4 channels"
205
+ ) ;
193
206
194
207
// normalize before padding because the length of the buffer affects the scale
195
208
let scale = if self . normalize {
@@ -201,7 +214,7 @@ impl ConvolverNode {
201
214
// Pad the response buffer with zeroes so its size is a power of 2, with 2 * 128 as min size
202
215
let length = buffer. length ( ) ;
203
216
let padded_length = length. next_power_of_two ( ) . max ( 2 * RENDER_QUANTUM_SIZE ) ;
204
- let samples: Vec < _ > = ( 0 ..buffer . number_of_channels ( ) )
217
+ let samples: Vec < _ > = ( 0 ..number_of_channels)
205
218
. map ( |_| {
206
219
let mut samples = vec ! [ 0. ; padded_length] ;
207
220
samples[ ..length]
@@ -461,6 +474,36 @@ mod tests {
461
474
assert_eq ! ( & input, & [ 4 , 5 , 6 , 7 , 8 , 9 , 10 , 0 , 0 , 0 ] ) ;
462
475
}
463
476
477
+ #[ test]
478
+ #[ should_panic]
479
+ fn test_buffer_sample_rate_matches ( ) {
480
+ let context = OfflineAudioContext :: new ( 1 , 128 , 44100. ) ;
481
+
482
+ let ir = vec ! [ 1. ] ;
483
+ let ir = AudioBuffer :: from ( vec ! [ ir; 1 ] , 48000. ) ; // sample_rate differs
484
+ let options = ConvolverOptions {
485
+ buffer : Some ( ir) ,
486
+ ..ConvolverOptions :: default ( )
487
+ } ;
488
+
489
+ let _ = ConvolverNode :: new ( & context, options) ;
490
+ }
491
+
492
+ #[ test]
493
+ #[ should_panic]
494
+ fn test_buffer_must_have_1_2_4_channels ( ) {
495
+ let context = OfflineAudioContext :: new ( 1 , 128 , 48000. ) ;
496
+
497
+ let ir = vec ! [ 1. ] ;
498
+ let ir = AudioBuffer :: from ( vec ! [ ir; 3 ] , 48000. ) ; // three channels
499
+ let options = ConvolverOptions {
500
+ buffer : Some ( ir) ,
501
+ ..ConvolverOptions :: default ( )
502
+ } ;
503
+
504
+ let _ = ConvolverNode :: new ( & context, options) ;
505
+ }
506
+
464
507
#[ test]
465
508
fn test_constructor_options_buffer ( ) {
466
509
let sample_rate = 44100. ;
@@ -580,19 +623,4 @@ mod tests {
580
623
assert ! ( !output[ ..IR_LEN ] . iter( ) . any( |v| * v <= 1E-6 ) ) ;
581
624
assert_float_eq ! ( & output[ IR_LEN ..] , & [ 0. ; 512 - IR_LEN ] [ ..] , abs_all <= 1E-6 ) ;
582
625
}
583
-
584
- #[ test]
585
- fn test_resample ( ) {
586
- let ctx_sample_rate = 44100. ;
587
- let ir_sample_rate = 48000. ;
588
-
589
- let context = OfflineAudioContext :: new ( 1 , 128 , ctx_sample_rate) ;
590
-
591
- let mut conv = ConvolverNode :: new ( & context, ConvolverOptions :: default ( ) ) ;
592
- let ir = vec ! [ 1. ; 128 ] ;
593
- let ir_buffer = AudioBuffer :: from ( vec ! [ ir] , ir_sample_rate) ;
594
- conv. set_buffer ( ir_buffer) ;
595
-
596
- assert_eq ! ( conv. buffer( ) . unwrap( ) . sample_rate( ) , ctx_sample_rate) ;
597
- }
598
626
}
0 commit comments