@@ -15,7 +15,6 @@ use std::any::Any;
15
15
pub struct ScriptProcessorNode {
16
16
registration : AudioContextRegistration ,
17
17
channel_config : ChannelConfig ,
18
- // bufferSize MUST be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384
19
18
buffer_size : usize ,
20
19
}
21
20
@@ -36,7 +35,24 @@ impl AudioNode for ScriptProcessorNode {
36
35
1
37
36
}
38
37
39
- // TODO channel config constraints
38
+ fn set_channel_count_mode ( & self , mode : ChannelCountMode ) {
39
+ assert_eq ! (
40
+ mode,
41
+ ChannelCountMode :: Explicit ,
42
+ "NotSupportedError - ScriptProcessorNode channel count mode must be 'explicit'" ,
43
+ ) ;
44
+ self . channel_config
45
+ . set_count_mode ( mode, self . registration ( ) ) ;
46
+ }
47
+
48
+ fn set_channel_count ( & self , count : usize ) {
49
+ assert_eq ! (
50
+ count,
51
+ self . channel_config. count( ) ,
52
+ "NotSupportedError - ScriptProcessorNode channel count must equal numberOfInputChannels"
53
+ ) ;
54
+ self . channel_config . set_count ( count, self . registration ( ) ) ;
55
+ }
40
56
}
41
57
42
58
impl ScriptProcessorNode {
@@ -46,7 +62,19 @@ impl ScriptProcessorNode {
46
62
number_of_input_channels : usize ,
47
63
number_of_output_channels : usize ,
48
64
) -> Self {
49
- // TODO assert valid arguments
65
+ assert ! (
66
+ ( buffer_size / 256 ) . is_power_of_two( ) && buffer_size <= 16384 ,
67
+ "IndexSizeError - bufferSize must be one of: 256, 512, 1024, 2048, 4096, 8192, 16384" ,
68
+ ) ;
69
+
70
+ match ( number_of_input_channels, number_of_output_channels) {
71
+ ( 0 , 0 ) => panic ! ( "IndexSizeError - numberOfInputChannels and numberOfOutputChannels cannot both be zero" ) ,
72
+ ( 0 , c) | ( c, 0 ) => crate :: assert_valid_number_of_channels ( c) ,
73
+ ( c, d) => {
74
+ crate :: assert_valid_number_of_channels ( c) ;
75
+ crate :: assert_valid_number_of_channels ( d) ;
76
+ }
77
+ } ;
50
78
51
79
context. base ( ) . register ( move |registration| {
52
80
let render = ScriptProcessorRenderer {
@@ -209,10 +237,15 @@ impl AudioProcessor for ScriptProcessorRenderer {
209
237
mod tests {
210
238
use super :: * ;
211
239
use crate :: context:: OfflineAudioContext ;
212
- use float_eq:: assert_float_eq;
213
240
214
241
#[ test]
215
- fn test ( ) {
216
- // TODO how to test?
242
+ fn test_constructor ( ) {
243
+ let mut context = OfflineAudioContext :: new ( 2 , 1024 , 48000. ) ;
244
+ let node = context. create_script_processor ( 512 , 1 , 1 ) ;
245
+ node. set_channel_count ( 1 ) ;
246
+ node. set_channel_count_mode ( ChannelCountMode :: Explicit ) ;
247
+ node. connect ( & context. destination ( ) ) ;
248
+ let _ = context. start_rendering_sync ( ) ;
249
+ // TODO - does not work with OfflineAudioContext due to lack of event loop
217
250
}
218
251
}
0 commit comments