@@ -6,7 +6,7 @@ use crate::render::{
6
6
} ;
7
7
use crate :: { AudioBuffer , RENDER_QUANTUM_SIZE } ;
8
8
9
- use super :: { AudioNode , ChannelConfig , ChannelConfigOptions } ;
9
+ use super :: { AudioNode , AudioNodeOptions , ChannelConfig } ;
10
10
11
11
use std:: any:: Any ;
12
12
@@ -85,15 +85,20 @@ impl ScriptProcessorNode {
85
85
number_of_output_channels,
86
86
} ;
87
87
88
- let channel_config = ChannelConfigOptions {
89
- count : number_of_input_channels,
90
- count_mode : ChannelCountMode :: Explicit ,
91
- interpretation : ChannelInterpretation :: Speakers ,
88
+ let upmix_input_channels = if number_of_input_channels == 0 {
89
+ 1 // any value will do, because upmixing is not performed
90
+ } else {
91
+ number_of_input_channels
92
+ } ;
93
+ let audio_node_options = AudioNodeOptions {
94
+ channel_count : upmix_input_channels,
95
+ channel_count_mode : ChannelCountMode :: Explicit ,
96
+ channel_interpretation : ChannelInterpretation :: Speakers ,
92
97
} ;
93
98
94
99
let node = ScriptProcessorNode {
95
100
registration,
96
- channel_config : channel_config . into ( ) ,
101
+ channel_config : audio_node_options . into ( ) ,
97
102
buffer_size,
98
103
} ;
99
104
@@ -237,6 +242,7 @@ impl AudioProcessor for ScriptProcessorRenderer {
237
242
mod tests {
238
243
use super :: * ;
239
244
use crate :: context:: OfflineAudioContext ;
245
+ use float_eq:: assert_float_eq;
240
246
241
247
#[ test]
242
248
fn test_constructor ( ) {
@@ -248,4 +254,46 @@ mod tests {
248
254
let _ = context. start_rendering_sync ( ) ;
249
255
// TODO - does not work with OfflineAudioContext due to lack of event loop
250
256
}
257
+
258
+ #[ test]
259
+ fn test_constructor_zero_inputs ( ) {
260
+ let context = OfflineAudioContext :: new ( 2 , 1024 , 48000. ) ;
261
+ let _ = context. create_script_processor ( 512 , 0 , 1 ) ; // should not panic
262
+ }
263
+
264
+ #[ test]
265
+ fn test_constructor_zero_outputs ( ) {
266
+ let context = OfflineAudioContext :: new ( 2 , 1024 , 48000. ) ;
267
+ let _ = context. create_script_processor ( 512 , 1 , 0 ) ; // should not panic
268
+ }
269
+
270
+ #[ test]
271
+ fn test_rendering ( ) {
272
+ const BUFFER_SIZE : usize = 256 ;
273
+
274
+ let mut context = OfflineAudioContext :: new ( 1 , BUFFER_SIZE * 3 , 48000. ) ;
275
+
276
+ let node = context. create_script_processor ( BUFFER_SIZE , 0 , 1 ) ;
277
+ node. connect ( & context. destination ( ) ) ;
278
+ node. set_onaudioprocess ( |e| {
279
+ e. output_buffer . get_channel_data_mut ( 0 ) . fill ( 1. ) ; // set all samples to 1.
280
+ } ) ;
281
+
282
+ let result = context. start_rendering_sync ( ) ;
283
+ let channel = result. get_channel_data ( 0 ) ;
284
+
285
+ // first `2 * BUFFER_SIZE` samples should be silent due to buffering
286
+ assert_float_eq ! (
287
+ channel[ ..2 * BUFFER_SIZE ] ,
288
+ & [ 0. ; 2 * BUFFER_SIZE ] [ ..] ,
289
+ abs_all <= 0.
290
+ ) ;
291
+
292
+ // rest of the samples should be 1.
293
+ assert_float_eq ! (
294
+ channel[ 2 * BUFFER_SIZE ..] ,
295
+ & [ 1. ; BUFFER_SIZE ] [ ..] ,
296
+ abs_all <= 0.
297
+ ) ;
298
+ }
251
299
}
0 commit comments