Skip to content

Commit 9e9147a

Browse files
committed
Refactor setting of min/max decibel on AnalyserNode
1 parent 39feb7e commit 9e9147a

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

src/analysis.rs

Lines changed: 13 additions & 35 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;
@@ -188,8 +177,8 @@ impl Analyser {
188177
ring_buffer,
189178
fft_size: DEFAULT_FFT_SIZE,
190179
smoothing_time_constant: DEFAULT_SMOOTHING_TIME_CONSTANT,
191-
min_decibels: f64::NEG_INFINITY,
192-
max_decibels: f64::INFINITY,
180+
min_decibels: DEFAULT_MIN_DECIBELS,
181+
max_decibels: DEFAULT_MAX_DECIBELS,
193182
fft_planner: Mutex::new(fft_planner),
194183
fft_input,
195184
fft_scratch,
@@ -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,23 +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_max_decibels(DEFAULT_MAX_DECIBELS); // init value
640-
analyser.set_min_decibels(DEFAULT_MAX_DECIBELS);
626+
analyser.set_decibels(DEFAULT_MAX_DECIBELS, analyser.max_decibels());
641627
}
642628

643629
#[test]
644630
#[should_panic]
645631
fn test_max_decibels_constraints_lt_min_decibels() {
646632
let mut analyser = Analyser::new();
647-
analyser.set_min_decibels(DEFAULT_MIN_DECIBELS); // init value
648-
analyser.set_max_decibels(DEFAULT_MIN_DECIBELS);
649-
}
650-
651-
#[test]
652-
fn test_min_max_decibels_init() {
653-
let mut analyser = Analyser::new();
654-
analyser.set_min_decibels(-10.);
655-
analyser.set_max_decibels(20.);
633+
analyser.set_decibels(analyser.min_decibels(), DEFAULT_MIN_DECIBELS);
656634
}
657635

658636
#[test]

src/node/analyser.rs

Lines changed: 3 additions & 4 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

0 commit comments

Comments
 (0)