Skip to content

Commit ac50a0b

Browse files
committed
fix: add upper bound for valid sample rates
1 parent 3df85b2 commit ac50a0b

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,28 @@ 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) {
140+
let min_sample_rate = 4_000.;
141+
let max_sample_rate = 192_000.;
138142
// 1000 Hertz is a just a random cutoff, but it helps a if someone accidentally puts a
139143
// timestamp in the sample_rate variable
140144
assert!(
141-
sample_rate > 1000.,
142-
"NotSupportedError - Invalid sample rate: {:?}, should be greater than 1000",
143-
sample_rate
145+
sample_rate >= min_sample_rate && sample_rate <= max_sample_rate,
146+
"NotSupportedError - Invalid sample rate: {:?}, should be in the range [{:?}, {:?}]",
147+
sample_rate,
148+
min_sample_rate,
149+
max_sample_rate,
144150
);
145151
}
146152

@@ -228,20 +234,14 @@ mod tests {
228234

229235
#[test]
230236
#[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.);
237+
fn test_invalid_sample_rate_too_small() {
238+
assert_valid_sample_rate(3_000.);
239239
}
240240

241241
#[test]
242242
#[should_panic]
243-
fn test_invalid_sample_rate_too_small() {
244-
assert_valid_sample_rate(100.);
243+
fn test_invalid_sample_rate_too_big() {
244+
assert_valid_sample_rate(300_000.);
245245
}
246246

247247
#[test]

0 commit comments

Comments
 (0)