Skip to content

Commit f5ae678

Browse files
authored
Merge pull request #441 from orottier/feature/convolvernode-assertions
Convolver: assert sample rate and number of channels on buffer
2 parents 2367afe + 2aaccf5 commit f5ae678

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

src/node/convolver.rs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,23 @@ impl ConvolverNode {
186186
///
187187
/// Panics when the sample rate of the provided AudioBuffer differs from the audio context
188188
/// 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+
192194
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+
);
193206

194207
// normalize before padding because the length of the buffer affects the scale
195208
let scale = if self.normalize {
@@ -201,7 +214,7 @@ impl ConvolverNode {
201214
// Pad the response buffer with zeroes so its size is a power of 2, with 2 * 128 as min size
202215
let length = buffer.length();
203216
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)
205218
.map(|_| {
206219
let mut samples = vec![0.; padded_length];
207220
samples[..length]
@@ -461,6 +474,36 @@ mod tests {
461474
assert_eq!(&input, &[4, 5, 6, 7, 8, 9, 10, 0, 0, 0]);
462475
}
463476

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+
464507
#[test]
465508
fn test_constructor_options_buffer() {
466509
let sample_rate = 44100.;
@@ -580,19 +623,4 @@ mod tests {
580623
assert!(!output[..IR_LEN].iter().any(|v| *v <= 1E-6));
581624
assert_float_eq!(&output[IR_LEN..], &[0.; 512 - IR_LEN][..], abs_all <= 1E-6);
582625
}
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-
}
598626
}

0 commit comments

Comments
 (0)