Skip to content

Commit de6c1c4

Browse files
committed
Fix: run assertions on ChannelMerger/Splitter ctor, not only when updating
1 parent c06804a commit de6c1c4

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

src/node/channel_merger.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ use super::{
77
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
88
};
99

10+
/// Assert that the channel count is valid for the ChannelMergerNode
11+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
12+
///
13+
/// # Panics
14+
///
15+
/// This function panics if given count is greater than 2
16+
///
17+
#[track_caller]
18+
#[inline(always)]
19+
fn assert_valid_channel_count(count: usize) {
20+
assert!(
21+
count == 1,
22+
"InvalidStateError - channel count of ChannelMergerNode must be equal to 1"
23+
);
24+
}
25+
26+
/// Assert that the channel count mode is valid for the ChannelMergerNode
27+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
28+
///
29+
/// # Panics
30+
///
31+
/// This function panics if the mode is not equal to Explicit
32+
///
33+
#[track_caller]
34+
#[inline(always)]
35+
fn assert_valid_channel_count_mode(mode: ChannelCountMode) {
36+
assert!(
37+
mode == ChannelCountMode::Explicit,
38+
"InvalidStateError - channel count of ChannelMergerNode must be set to Explicit"
39+
);
40+
}
41+
1042
/// Options for constructing a [`ChannelMergerNode`]
1143
// dictionary ChannelMergerOptions : AudioNodeOptions {
1244
// unsigned long numberOfInputs = 6;
@@ -45,12 +77,14 @@ impl AudioNode for ChannelMergerNode {
4577
&self.channel_config
4678
}
4779

48-
fn set_channel_count(&self, _v: usize) {
49-
panic!("InvalidStateError - Cannot edit channel count of ChannelMergerNode")
80+
fn set_channel_count(&self, count: usize) {
81+
assert_valid_channel_count(count);
82+
self.channel_config.set_count(count);
5083
}
5184

52-
fn set_channel_count_mode(&self, _v: ChannelCountMode) {
53-
panic!("InvalidStateError - Cannot edit channel count mode of ChannelMergerNode")
85+
fn set_channel_count_mode(&self, mode: ChannelCountMode) {
86+
assert_valid_channel_count_mode(mode);
87+
self.channel_config.set_count_mode(mode);
5488
}
5589

5690
fn number_of_inputs(&self) -> usize {
@@ -63,11 +97,14 @@ impl AudioNode for ChannelMergerNode {
6397
}
6498

6599
impl ChannelMergerNode {
66-
pub fn new<C: BaseAudioContext>(context: &C, mut options: ChannelMergerOptions) -> Self {
100+
pub fn new<C: BaseAudioContext>(context: &C, options: ChannelMergerOptions) -> Self {
67101
context.register(move |registration| {
68102
crate::assert_valid_number_of_channels(options.number_of_inputs);
69103
options.channel_config.count = options.number_of_inputs;
70104

105+
assert_valid_channel_count(options.channel_config.count);
106+
assert_valid_channel_count_mode(options.channel_config.count_mode);
107+
71108
let node = ChannelMergerNode {
72109
registration,
73110
channel_config: options.channel_config.into(),

src/node/channel_splitter.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ use super::{
77
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
88
};
99

10+
/// Assert that the channel count mode is valid for the ChannelSplitterNode
11+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
12+
///
13+
/// # Panics
14+
///
15+
/// This function panics if the mode is not equal to Explicit
16+
///
17+
#[track_caller]
18+
#[inline(always)]
19+
fn assert_valid_channel_count_mode(mode: ChannelCountMode) {
20+
assert!(
21+
mode == ChannelCountMode::Explicit,
22+
"InvalidStateError - channel count of ChannelSplitterNode must be set to Explicit"
23+
);
24+
}
25+
26+
/// Assert that the channel interpretation is valid for the ChannelSplitterNode
27+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelinterpretation-constraints>
28+
///
29+
/// # Panics
30+
///
31+
/// This function panics if the mode is not equal to Explicit
32+
///
33+
#[track_caller]
34+
#[inline(always)]
35+
fn assert_valid_channel_interpretation(interpretation: ChannelInterpretation) {
36+
assert!(
37+
interpretation == ChannelInterpretation::Discrete,
38+
"InvalidStateError - channel interpretation of ChannelSplitterNode must be set to Discrete"
39+
);
40+
}
41+
1042
/// Options for constructing a [`ChannelSplitterNode`]
1143
// dictionary ChannelSplitterOptions : AudioNodeOptions {
1244
// unsigned long numberOfOutputs = 6;
@@ -45,16 +77,22 @@ impl AudioNode for ChannelSplitterNode {
4577
&self.channel_config
4678
}
4779

48-
fn set_channel_count(&self, _v: usize) {
49-
panic!("InvalidStateError - Cannot edit channel count of ChannelSplitterNode")
80+
fn set_channel_count(&self, count: usize) {
81+
assert_eq!(
82+
count,
83+
self.channel_count(),
84+
"InvalidStateError - Cannot edit channel count of ChannelSplitterNode"
85+
);
5086
}
5187

52-
fn set_channel_count_mode(&self, _v: ChannelCountMode) {
53-
panic!("InvalidStateError - Cannot edit channel count mode of ChannelSplitterNode")
88+
fn set_channel_count_mode(&self, mode: ChannelCountMode) {
89+
assert_valid_channel_count_mode(mode);
90+
self.channel_config.set_count_mode(mode);
5491
}
5592

56-
fn set_channel_interpretation(&self, _v: ChannelInterpretation) {
57-
panic!("InvalidStateError - Cannot edit channel interpretation of ChannelSplitterNode")
93+
fn set_channel_interpretation(&self, interpretation: ChannelInterpretation) {
94+
assert_valid_channel_interpretation(interpretation);
95+
self.channel_config.set_interpretation(interpretation);
5896
}
5997

6098
fn number_of_inputs(&self) -> usize {
@@ -72,6 +110,9 @@ impl ChannelSplitterNode {
72110
crate::assert_valid_number_of_channels(options.number_of_outputs);
73111
options.channel_config.count = options.number_of_outputs;
74112

113+
assert_valid_channel_count_mode(options.channel_config.count_mode);
114+
assert_valid_channel_interpretation(options.channel_config.interpretation);
115+
75116
let node = ChannelSplitterNode {
76117
registration,
77118
channel_config: options.channel_config.into(),

0 commit comments

Comments
 (0)