Skip to content

Commit 64f7cc0

Browse files
authored
Merge pull request #463 from b-ma/fix/audio-buffer
Fix: check length > 0 in constructor
2 parents 37b38d2 + ffd1691 commit 64f7cc0

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/buffer.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ use crate::{
55
assert_valid_channel_number, assert_valid_number_of_channels, assert_valid_sample_rate,
66
};
77

8+
#[track_caller]
9+
#[inline(always)]
10+
pub(crate) fn assert_valid_length(length: usize) {
11+
assert!(
12+
length > 0,
13+
"NotSupportedError - Invalid length: {:?} is less than or equal to minimum bound (0)",
14+
length,
15+
);
16+
}
17+
818
/// Options for constructing an [`AudioBuffer`]
919
// dictionary AudioBufferOptions {
1020
// unsigned long numberOfChannels = 1;
@@ -81,6 +91,7 @@ impl AudioBuffer {
8191
/// 32 being defined by the MAX_CHANNELS constant.
8292
pub fn new(options: AudioBufferOptions) -> Self {
8393
assert_valid_sample_rate(options.sample_rate);
94+
assert_valid_length(options.length);
8495
assert_valid_number_of_channels(options.number_of_channels);
8596

8697
let silence = ChannelData::new(options.length);
@@ -475,6 +486,18 @@ mod tests {
475486
AudioBuffer::from(samples, sample_rate); // should panic
476487
}
477488

489+
#[test]
490+
#[should_panic]
491+
fn test_invalid_length() {
492+
let options = AudioBufferOptions {
493+
number_of_channels: 1,
494+
length: 0,
495+
sample_rate: 48000.,
496+
};
497+
498+
AudioBuffer::new(options); // should panic
499+
}
500+
478501
#[test]
479502
fn test_channel_data_get_set() {
480503
let options = AudioBufferOptions {
@@ -710,12 +733,8 @@ mod tests {
710733

711734
#[test]
712735
fn test_resample_from_empty() {
713-
let options = AudioBufferOptions {
714-
number_of_channels: 1,
715-
length: 0,
716-
sample_rate: 48000.,
717-
};
718-
let mut buffer = AudioBuffer::new(options);
736+
let channel = ChannelData::from(vec![]);
737+
let mut buffer = AudioBuffer::from_channels(vec![channel], 48000.);
719738
buffer.resample(48000.);
720739

721740
assert_eq!(buffer.length(), 0);

0 commit comments

Comments
 (0)