@@ -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,18 @@ mod tests {
475
486
AudioBuffer :: from ( samples, sample_rate) ; // should panic
476
487
}
477
488
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
+
478
501
#[ test]
479
502
fn test_channel_data_get_set ( ) {
480
503
let options = AudioBufferOptions {
@@ -710,12 +733,8 @@ mod tests {
710
733
711
734
#[ test]
712
735
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. ) ;
719
738
buffer. resample ( 48000. ) ;
720
739
721
740
assert_eq ! ( buffer. length( ) , 0 ) ;
0 commit comments