Skip to content

Commit 0b77741

Browse files
authored
Merge pull request #425 from orottier/feature/audio-param-numerical-values
AudioParam better handling of non finite values in arguments
2 parents 8fced8d + 7958cff commit 0b77741

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/node/audio_buffer_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl AudioBufferSourceNode {
257257
assert_valid_time_value(duration);
258258
assert!(
259259
!self.source_started,
260-
"InvalidStateError: Cannot call `start` twice"
260+
"InvalidStateError - Cannot call `start` twice"
261261
);
262262

263263
self.source_started = true;

src/node/delay.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ impl AudioNode for DelayNode {
131131
input: usize,
132132
) -> &'a dyn AudioNode {
133133
if self.context() != dest.context() {
134-
panic!("InvalidAccessError: Attempting to connect nodes from different contexts");
134+
panic!("InvalidAccessError - Attempting to connect nodes from different contexts");
135135
}
136136
if self.number_of_outputs() <= output {
137-
panic!("IndexSizeError: output port {} is out of bounds", output);
137+
panic!("IndexSizeError - output port {} is out of bounds", output);
138138
}
139139
if dest.number_of_inputs() <= input {
140-
panic!("IndexSizeError: input port {} is out of bounds", input);
140+
panic!("IndexSizeError - input port {} is out of bounds", input);
141141
}
142142

143143
self.context().connect(

src/node/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,13 @@ pub trait AudioNode {
268268
input: usize,
269269
) -> &'a dyn AudioNode {
270270
if self.context() != dest.context() {
271-
panic!("InvalidAccessError: Attempting to connect nodes from different contexts");
271+
panic!("InvalidAccessError - Attempting to connect nodes from different contexts");
272272
}
273273
if self.number_of_outputs() <= output {
274-
panic!("IndexSizeError: output port {} is out of bounds", output);
274+
panic!("IndexSizeError - output port {} is out of bounds", output);
275275
}
276276
if dest.number_of_inputs() <= input {
277-
panic!("IndexSizeError: input port {} is out of bounds", input);
277+
panic!("IndexSizeError - input port {} is out of bounds", input);
278278
}
279279

280280
self.context().connect(

src/param.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ use crate::node::{
1111
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
1212
};
1313
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};
14-
use crate::{AtomicF32, RENDER_QUANTUM_SIZE};
14+
use crate::{assert_valid_time_value, AtomicF32, RENDER_QUANTUM_SIZE};
1515

1616
/// For SetTargetAtTime event, that theoretically cannot end, if the diff between
1717
/// the current value and the target is below this threshold, the value is set
1818
/// to target value and the event is considered ended.
1919
const SNAP_TO_TARGET: f32 = 1e-10;
2020

21-
// arguments sanity check functions for automation methods
2221
#[track_caller]
23-
fn assert_non_negative(value: f64) {
24-
if value < 0. {
25-
panic!(
26-
"RangeError - timing value ({:?}) should not be negative",
27-
value
28-
);
22+
fn assert_is_finite(value: f32) {
23+
if !value.is_finite() {
24+
panic!("TypeError - The provided value is non-finite.");
2925
}
3026
}
3127

3228
#[track_caller]
3329
fn assert_strictly_positive(value: f64) {
30+
if !value.is_finite() {
31+
panic!("TypeError - The provided value is non-finite.");
32+
}
33+
3434
if value <= 0. {
3535
panic!(
3636
"RangeError - duration ({:?}) should be strictly positive",
@@ -41,6 +41,8 @@ fn assert_strictly_positive(value: f64) {
4141

4242
#[track_caller]
4343
fn assert_not_zero(value: f32) {
44+
assert_is_finite(value);
45+
4446
if value == 0. {
4547
panic!(
4648
"RangeError - value ({:?}) should not be equal to zero",
@@ -379,6 +381,7 @@ impl AudioParam {
379381
}
380382

381383
fn set_value_raw(&self, value: f32) -> AudioParamEvent {
384+
assert_is_finite(value);
382385
// current_value should always be clamped
383386
let clamped = value.clamp(self.raw_parts.min_value, self.raw_parts.max_value);
384387
self.raw_parts.shared_parts.store_current_value(clamped);
@@ -406,7 +409,8 @@ impl AudioParam {
406409
}
407410

408411
fn set_value_at_time_raw(&self, value: f32, start_time: f64) -> AudioParamEvent {
409-
assert_non_negative(start_time);
412+
assert_is_finite(value);
413+
assert_valid_time_value(start_time);
410414

411415
AudioParamEvent {
412416
event_type: AudioParamEventType::SetValueAtTime,
@@ -430,7 +434,8 @@ impl AudioParam {
430434
}
431435

432436
fn linear_ramp_to_value_at_time_raw(&self, value: f32, end_time: f64) -> AudioParamEvent {
433-
assert_non_negative(end_time);
437+
assert_is_finite(value);
438+
assert_valid_time_value(end_time);
434439

435440
AudioParamEvent {
436441
event_type: AudioParamEventType::LinearRampToValueAtTime,
@@ -457,7 +462,7 @@ impl AudioParam {
457462

458463
fn exponential_ramp_to_value_at_time_raw(&self, value: f32, end_time: f64) -> AudioParamEvent {
459464
assert_not_zero(value);
460-
assert_non_negative(end_time);
465+
assert_valid_time_value(end_time);
461466

462467
AudioParamEvent {
463468
event_type: AudioParamEventType::ExponentialRampToValueAtTime,
@@ -488,8 +493,9 @@ impl AudioParam {
488493
start_time: f64,
489494
time_constant: f64,
490495
) -> AudioParamEvent {
491-
assert_non_negative(start_time);
492-
assert_non_negative(time_constant);
496+
assert_is_finite(value);
497+
assert_valid_time_value(start_time);
498+
assert_valid_time_value(time_constant);
493499

494500
// [spec] If timeConstant is zero, the output value jumps immediately to the final value.
495501
if time_constant == 0. {
@@ -526,7 +532,7 @@ impl AudioParam {
526532
}
527533

528534
fn cancel_scheduled_values_raw(&self, cancel_time: f64) -> AudioParamEvent {
529-
assert_non_negative(cancel_time);
535+
assert_valid_time_value(cancel_time);
530536

531537
AudioParamEvent {
532538
event_type: AudioParamEventType::CancelScheduledValues,
@@ -551,7 +557,7 @@ impl AudioParam {
551557
}
552558

553559
fn cancel_and_hold_at_time_raw(&self, cancel_time: f64) -> AudioParamEvent {
554-
assert_non_negative(cancel_time);
560+
assert_valid_time_value(cancel_time);
555561

556562
AudioParamEvent {
557563
event_type: AudioParamEventType::CancelAndHoldAtTime,
@@ -584,7 +590,7 @@ impl AudioParam {
584590
duration: f64,
585591
) -> AudioParamEvent {
586592
assert_sequence_length(values);
587-
assert_non_negative(start_time);
593+
assert_valid_time_value(start_time);
588594
assert_strictly_positive(duration);
589595

590596
// When this method is called, an internal copy of the curve is
@@ -1645,17 +1651,6 @@ mod tests {
16451651

16461652
use super::*;
16471653

1648-
#[test]
1649-
#[should_panic]
1650-
fn test_assert_non_negative_fail() {
1651-
assert_non_negative(-1.);
1652-
}
1653-
1654-
#[test]
1655-
fn test_assert_non_negative() {
1656-
assert_non_negative(0.);
1657-
}
1658-
16591654
#[test]
16601655
#[should_panic]
16611656
fn test_assert_strictly_positive_fail() {

0 commit comments

Comments
 (0)