Skip to content

Commit 208dff9

Browse files
committed
ScriptProcessorNode: enforce constraints in constructor
1 parent 063bd5e commit 208dff9

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/node/script_processor.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::any::Any;
1515
pub struct ScriptProcessorNode {
1616
registration: AudioContextRegistration,
1717
channel_config: ChannelConfig,
18-
// bufferSize MUST be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384
1918
buffer_size: usize,
2019
}
2120

@@ -36,7 +35,24 @@ impl AudioNode for ScriptProcessorNode {
3635
1
3736
}
3837

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+
}
4056
}
4157

4258
impl ScriptProcessorNode {
@@ -46,7 +62,19 @@ impl ScriptProcessorNode {
4662
number_of_input_channels: usize,
4763
number_of_output_channels: usize,
4864
) -> 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+
};
5078

5179
context.base().register(move |registration| {
5280
let render = ScriptProcessorRenderer {
@@ -209,10 +237,15 @@ impl AudioProcessor for ScriptProcessorRenderer {
209237
mod tests {
210238
use super::*;
211239
use crate::context::OfflineAudioContext;
212-
use float_eq::assert_float_eq;
213240

214241
#[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
217250
}
218251
}

0 commit comments

Comments
 (0)