@@ -6,7 +6,7 @@ use crate::render::{
66} ;
77use crate :: { AudioBuffer , RENDER_QUANTUM_SIZE } ;
88
9- use super :: { AudioNode , ChannelConfig , ChannelConfigOptions } ;
9+ use super :: { AudioNode , AudioNodeOptions , ChannelConfig } ;
1010
1111use std:: any:: Any ;
1212
@@ -85,15 +85,20 @@ impl ScriptProcessorNode {
8585 number_of_output_channels,
8686 } ;
8787
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 ,
9297 } ;
9398
9499 let node = ScriptProcessorNode {
95100 registration,
96- channel_config : channel_config . into ( ) ,
101+ channel_config : audio_node_options . into ( ) ,
97102 buffer_size,
98103 } ;
99104
@@ -237,6 +242,7 @@ impl AudioProcessor for ScriptProcessorRenderer {
237242mod tests {
238243 use super :: * ;
239244 use crate :: context:: OfflineAudioContext ;
245+ use float_eq:: assert_float_eq;
240246
241247 #[ test]
242248 fn test_constructor ( ) {
@@ -248,4 +254,46 @@ mod tests {
248254 let _ = context. start_rendering_sync ( ) ;
249255 // TODO - does not work with OfflineAudioContext due to lack of event loop
250256 }
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+ }
251299}
0 commit comments