Skip to content

Commit 97f4d7b

Browse files
b-maorottier
authored andcommitted
fix: better handling of non finite values in arguments
1 parent 8fced8d commit 97f4d7b

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/param.rs

Lines changed: 31 additions & 25 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,16 +1651,16 @@ 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-
}
1654+
// #[test]
1655+
// #[should_panic]
1656+
// fn test_assert_valid_time_value_fail() {
1657+
// assert_valid_time_value(-1.);
1658+
// }
16531659

1654-
#[test]
1655-
fn test_assert_non_negative() {
1656-
assert_non_negative(0.);
1657-
}
1660+
// #[test]
1661+
// fn test_assert_valid_time_value() {
1662+
// assert_valid_time_value(0.);
1663+
// }
16581664

16591665
#[test]
16601666
#[should_panic]

0 commit comments

Comments
 (0)