Skip to content

Commit 8fced8d

Browse files
authored
Merge pull request #422 from b-ma/fix/analyser-node-init-values
fix: unwanted panic at Analiser initialization
2 parents 9cf136d + 4c61bf0 commit 8fced8d

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/analysis.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ fn assert_valid_smoothing_time_constant(smoothing_time_constant: f64) {
6060
}
6161
}
6262

63-
// [spec] If the value of this attribute is set to a value more than or equal
64-
// to maxDecibels, an IndexSizeError exception MUST be thrown.
65-
fn assert_valid_min_decibels(min_decibels: f64, max_decibels: f64) {
63+
// [spec] If the value of minDecibels is set to a value more than or equal to maxDecibels, an
64+
// IndexSizeError exception MUST be thrown.
65+
fn assert_valid_decibels(min_decibels: f64, max_decibels: f64) {
6666
if min_decibels >= max_decibels {
6767
panic!(
6868
"IndexSizeError - Invalid min decibels: {:?} is greater than or equals to max decibels {:?}",
@@ -71,17 +71,6 @@ fn assert_valid_min_decibels(min_decibels: f64, max_decibels: f64) {
7171
}
7272
}
7373

74-
// [spec] If the value of this attribute is set to a value less than or equal to
75-
// minDecibels, an IndexSizeError exception MUST be thrown.
76-
fn assert_valid_max_decibels(max_decibels: f64, min_decibels: f64) {
77-
if max_decibels <= min_decibels {
78-
panic!(
79-
"IndexSizeError - Invalid max decibels: {:?} is lower than or equals to min decibels {:?}",
80-
max_decibels, min_decibels
81-
);
82-
}
83-
}
84-
8574
// as the queue is composed of AtomicF32 having only 1 render quantum of extra
8675
// room should be enough
8776
const RING_BUFFER_SIZE: usize = MAX_FFT_SIZE + RENDER_QUANTUM_SIZE;
@@ -237,18 +226,16 @@ impl Analyser {
237226
self.min_decibels
238227
}
239228

240-
pub fn set_min_decibels(&mut self, value: f64) {
241-
assert_valid_min_decibels(value, self.max_decibels());
242-
self.min_decibels = value;
243-
}
244-
245229
pub fn max_decibels(&self) -> f64 {
246230
self.max_decibels
247231
}
248232

249-
pub fn set_max_decibels(&mut self, value: f64) {
250-
assert_valid_max_decibels(value, self.min_decibels());
251-
self.max_decibels = value;
233+
pub fn set_decibels(&mut self, min: f64, max: f64) {
234+
// set them together to avoid invalid intermediate min/max combinations
235+
assert_valid_decibels(min, max);
236+
237+
self.min_decibels = min;
238+
self.min_decibels = max;
252239
}
253240

254241
pub fn frequency_bin_count(&self) -> usize {
@@ -636,14 +623,14 @@ mod tests {
636623
#[should_panic]
637624
fn test_min_decibels_constraints_lt_max_decibels() {
638625
let mut analyser = Analyser::new();
639-
analyser.set_min_decibels(DEFAULT_MAX_DECIBELS);
626+
analyser.set_decibels(DEFAULT_MAX_DECIBELS, analyser.max_decibels());
640627
}
641628

642629
#[test]
643630
#[should_panic]
644631
fn test_max_decibels_constraints_lt_min_decibels() {
645632
let mut analyser = Analyser::new();
646-
analyser.set_max_decibels(DEFAULT_MIN_DECIBELS);
633+
analyser.set_decibels(analyser.min_decibels(), DEFAULT_MIN_DECIBELS);
647634
}
648635

649636
#[test]

src/node/analyser.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl AnalyserNode {
112112
let mut analyser = Analyser::new();
113113
analyser.set_fft_size(fft_size);
114114
analyser.set_smoothing_time_constant(smoothing_time_constant);
115-
analyser.set_min_decibels(min_decibels);
116-
analyser.set_max_decibels(max_decibels);
115+
analyser.set_decibels(min_decibels, max_decibels);
117116

118117
let render = AnalyserRenderer {
119118
ring_buffer: analyser.get_ring_buffer_clone(),
@@ -184,7 +183,7 @@ impl AnalyserNode {
184183
/// This function panics if the value is set to a value more than or equal
185184
/// to max decibels.
186185
pub fn set_min_decibels(&mut self, value: f64) {
187-
self.analyser.set_min_decibels(value);
186+
self.analyser.set_decibels(value, self.max_decibels());
188187
}
189188

190189
/// Maximum power value in the scaling range for the FFT analysis data for
@@ -204,7 +203,7 @@ impl AnalyserNode {
204203
/// This function panics if the value is set to a value less than or equal
205204
/// to min decibels.
206205
pub fn set_max_decibels(&mut self, value: f64) {
207-
self.analyser.set_max_decibels(value);
206+
self.analyser.set_decibels(self.min_decibels(), value);
208207
}
209208

210209
/// Number of bins in the FFT results, is half the FFT size
@@ -290,7 +289,10 @@ impl AudioProcessor for AnalyserRenderer {
290289

291290
#[cfg(test)]
292291
mod tests {
293-
use crate::context::{AudioContext, AudioContextOptions, BaseAudioContext};
292+
use super::*;
293+
use crate::context::{
294+
AudioContext, AudioContextOptions, BaseAudioContext, OfflineAudioContext,
295+
};
294296
use crate::node::{AudioNode, AudioScheduledSourceNode};
295297
use float_eq::assert_float_eq;
296298

@@ -325,4 +327,15 @@ mod tests {
325327
// should contain the most recent frames available
326328
assert_float_eq!(&buffer[..], &[1.; 128][..], abs_all <= 0.);
327329
}
330+
331+
#[test]
332+
fn test_construct_decibels() {
333+
let context = OfflineAudioContext::new(1, 128, 44_100.);
334+
let options = AnalyserOptions {
335+
min_decibels: -10.,
336+
max_decibels: 20.,
337+
..AnalyserOptions::default()
338+
};
339+
let _ = AnalyserNode::new(&context, options);
340+
}
328341
}

0 commit comments

Comments
 (0)