@@ -5,6 +5,16 @@ use crate::{
5
5
assert_valid_channel_number, assert_valid_number_of_channels, assert_valid_sample_rate,
6
6
} ;
7
7
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
+
8
18
/// Options for constructing an [`AudioBuffer`]
9
19
// dictionary AudioBufferOptions {
10
20
// unsigned long numberOfChannels = 1;
@@ -81,6 +91,7 @@ impl AudioBuffer {
81
91
/// 32 being defined by the MAX_CHANNELS constant.
82
92
pub fn new ( options : AudioBufferOptions ) -> Self {
83
93
assert_valid_sample_rate ( options. sample_rate ) ;
94
+ assert_valid_length ( options. length ) ;
84
95
assert_valid_number_of_channels ( options. number_of_channels ) ;
85
96
86
97
let silence = ChannelData :: new ( options. length ) ;
@@ -475,6 +486,19 @@ mod tests {
475
486
AudioBuffer :: from ( samples, sample_rate) ; // should panic
476
487
}
477
488
489
+
490
+ #[ test]
491
+ #[ should_panic]
492
+ fn test_invalid_length ( ) {
493
+ let options = AudioBufferOptions {
494
+ number_of_channels : 1 ,
495
+ length : 0 ,
496
+ sample_rate : 48000. ,
497
+ } ;
498
+
499
+ AudioBuffer :: new ( options) ; // should panic
500
+ }
501
+
478
502
#[ test]
479
503
fn test_channel_data_get_set ( ) {
480
504
let options = AudioBufferOptions {
0 commit comments