Skip to content

Commit 87d3eae

Browse files
authored
Merge pull request #485 from b-ma/fix/sample-rate
Fix - Add upper bound for valid sample rates
2 parents 3df85b2 + 0d0e257 commit 87d3eae

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/lib.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,30 @@ impl AtomicF64 {
125125
/// voice based applications, e.g. see phone bandwidth) and 96000Hz (for very high
126126
/// quality audio applications and spectrum manipulation).
127127
/// Most common sample rates for musical applications are 44100 and 48000.
128-
/// - see <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-createbuffer-samplerate>
128+
///
129+
/// - see <https://webaudio.github.io/web-audio-api/#dom-baseaudiocontext-samplerate>
130+
/// > An implementation MUST support sample rates in at least the range 8000 to 96000.
129131
///
130132
/// # Panics
131133
///
132134
/// This function will panic if:
133-
/// - the given sample rate is zero
135+
/// - the given sample rate is lower than 4000 or greater than 192000
134136
///
135137
#[track_caller]
136138
#[inline(always)]
137139
pub(crate) fn assert_valid_sample_rate(sample_rate: f32) {
138-
// 1000 Hertz is a just a random cutoff, but it helps a if someone accidentally puts a
139-
// timestamp in the sample_rate variable
140+
// Arbitrary cutoffs defined as:
141+
// min_sample_rate = min_required_in_spec / 4
142+
// max_sample_rate = max_required_in_spec * 4
143+
let min_sample_rate = 2_000.;
144+
let max_sample_rate = 384_000.;
145+
140146
assert!(
141-
sample_rate > 1000.,
142-
"NotSupportedError - Invalid sample rate: {:?}, should be greater than 1000",
143-
sample_rate
147+
sample_rate >= min_sample_rate && sample_rate <= max_sample_rate,
148+
"NotSupportedError - Invalid sample rate: {:?}, should be in the range [{:?}, {:?}]",
149+
sample_rate,
150+
min_sample_rate,
151+
max_sample_rate,
144152
);
145153
}
146154

@@ -228,20 +236,18 @@ mod tests {
228236

229237
#[test]
230238
#[should_panic]
231-
fn test_invalid_sample_rate_zero() {
232-
assert_valid_sample_rate(0.);
233-
}
234-
235-
#[test]
236-
#[should_panic]
237-
fn test_invalid_sample_rate_subzero() {
238-
assert_valid_sample_rate(-48000.);
239+
fn test_invalid_sample_rate_too_small() {
240+
// invalid lower value used in wpt check
241+
// <the-audio-api/the-audiocontext-interface/audiocontextoptions.html>
242+
assert_valid_sample_rate(1.);
239243
}
240244

241245
#[test]
242246
#[should_panic]
243-
fn test_invalid_sample_rate_too_small() {
244-
assert_valid_sample_rate(100.);
247+
fn test_invalid_sample_rate_too_big() {
248+
// invalid upper value used in wpt check
249+
// <the-audio-api/the-audiocontext-interface/audiocontextoptions.html>
250+
assert_valid_sample_rate(1_000_000.);
245251
}
246252

247253
#[test]

src/node/delay.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ mod tests {
669669

670670
#[test]
671671
fn test_audioparam_value_applies_immediately() {
672-
let context = OfflineAudioContext::new(1, 128, 48000.);
672+
let context = OfflineAudioContext::new(1, 128, 48_000.);
673673
let options = DelayOptions {
674674
delay_time: 0.12,
675675
..Default::default()
@@ -681,7 +681,7 @@ mod tests {
681681
#[test]
682682
fn test_sample_accurate() {
683683
for delay_in_samples in [128., 131., 197.].iter() {
684-
let sample_rate = 48000.;
684+
let sample_rate = 48_000.;
685685
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
686686

687687
let delay = context.create_delay(2.);
@@ -710,7 +710,7 @@ mod tests {
710710
fn test_sub_sample_accurate() {
711711
{
712712
let delay_in_samples = 128.5;
713-
let sample_rate = 48000.;
713+
let sample_rate = 48_000.;
714714
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
715715

716716
let delay = context.create_delay(2.);
@@ -737,7 +737,7 @@ mod tests {
737737

738738
{
739739
let delay_in_samples = 128.8;
740-
let sample_rate = 48000.;
740+
let sample_rate = 48_000.;
741741
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
742742

743743
let delay = context.create_delay(2.);
@@ -766,7 +766,7 @@ mod tests {
766766
#[test]
767767
fn test_multichannel() {
768768
let delay_in_samples = 128.;
769-
let sample_rate = 48000.;
769+
let sample_rate = 48_000.;
770770
let mut context = OfflineAudioContext::new(2, 2 * 128, sample_rate);
771771

772772
let delay = context.create_delay(2.);
@@ -799,7 +799,7 @@ mod tests {
799799
#[test]
800800
fn test_input_number_of_channels_change() {
801801
let delay_in_samples = 128.;
802-
let sample_rate = 48000.;
802+
let sample_rate = 48_000.;
803803
let mut context = OfflineAudioContext::new(2, 3 * 128, sample_rate);
804804

805805
let delay = context.create_delay(2.);
@@ -843,7 +843,7 @@ mod tests {
843843
fn test_node_stays_alive_long_enough() {
844844
// make sure there are no hidden order problem
845845
for _ in 0..10 {
846-
let sample_rate = 48000.;
846+
let sample_rate = 48_000.;
847847
let mut context = OfflineAudioContext::new(1, 5 * 128, sample_rate);
848848

849849
// Set up a source that starts only after 5 render quanta.
@@ -878,7 +878,7 @@ mod tests {
878878
#[test]
879879
fn test_subquantum_delay() {
880880
for i in 0..128 {
881-
let sample_rate = 48000.;
881+
let sample_rate = 48_000.;
882882
let mut context = OfflineAudioContext::new(1, 128, sample_rate);
883883

884884
let delay = context.create_delay(1.);
@@ -905,7 +905,7 @@ mod tests {
905905

906906
#[test]
907907
fn test_min_delay_when_in_loop() {
908-
let sample_rate = 480000.;
908+
let sample_rate = 48_000.;
909909
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
910910

911911
let delay = context.create_delay(1.);
@@ -942,7 +942,7 @@ mod tests {
942942
// that everything works even if order of processing is not guaranteed
943943
// (i.e. when delay is in a loop)
944944
for _ in 0..10 {
945-
let sample_rate = 480000.;
945+
let sample_rate = 48_000.;
946946
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
947947

948948
// this will be internally clamped to 128 * sample_rate
@@ -984,7 +984,7 @@ mod tests {
984984

985985
// set delay and max delay time exactly 1 render quantum
986986
{
987-
let sample_rate = 48000.;
987+
let sample_rate = 48_000.;
988988
let mut context = OfflineAudioContext::new(1, 256, sample_rate);
989989

990990
let delay = context.create_delay(1.);
@@ -1010,7 +1010,7 @@ mod tests {
10101010

10111011
// set delay and max delay time exactly 2 render quantum
10121012
{
1013-
let sample_rate = 48000.;
1013+
let sample_rate = 48_000.;
10141014
let mut context = OfflineAudioContext::new(1, 3 * 128, sample_rate);
10151015

10161016
let delay = context.create_delay(2.);
@@ -1037,7 +1037,7 @@ mod tests {
10371037

10381038
#[test]
10391039
fn test_subquantum_delay_dynamic_lifetime() {
1040-
let sample_rate = 48000.;
1040+
let sample_rate = 48_000.;
10411041
let mut context = OfflineAudioContext::new(1, 3 * 128, sample_rate);
10421042

10431043
// Setup a source that emits for 120 frames, so it deallocates after the first render

0 commit comments

Comments
 (0)