Skip to content

Commit 8be749b

Browse files
committed
Throw correct error in ChannelSplitter/Merger for wrong no. of channels
1 parent 8254c0e commit 8be749b

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/node/channel_merger.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@ use crate::context::{AudioContextRegistration, BaseAudioContext};
44
use crate::render::{
55
AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope,
66
};
7+
use crate::MAX_CHANNELS;
78

89
use super::{
910
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
1011
};
1112

13+
/// Assert that the given number of channels is valid for a ChannelMergerNode
14+
///
15+
/// # Panics
16+
///
17+
/// This function will panic if:
18+
/// - the given number of channels is outside the [1, 32] range,
19+
/// 32 being defined by the MAX_CHANNELS constant.
20+
///
21+
#[track_caller]
22+
#[inline(always)]
23+
pub(crate) fn assert_valid_number_of_channels(number_of_channels: usize) {
24+
assert!(
25+
number_of_channels > 0 && number_of_channels <= MAX_CHANNELS,
26+
"IndexSizeError - Invalid number of channels: {:?} is outside range [1, {:?}]",
27+
number_of_channels,
28+
MAX_CHANNELS
29+
);
30+
}
31+
1232
/// Assert that the channel count is valid for the ChannelMergerNode
1333
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
1434
///
@@ -104,7 +124,7 @@ impl AudioNode for ChannelMergerNode {
104124
impl ChannelMergerNode {
105125
pub fn new<C: BaseAudioContext>(context: &C, options: ChannelMergerOptions) -> Self {
106126
context.base().register(move |registration| {
107-
crate::assert_valid_number_of_channels(options.number_of_inputs);
127+
assert_valid_number_of_channels(options.number_of_inputs);
108128

109129
assert_valid_channel_count(options.channel_config.count);
110130
assert_valid_channel_count_mode(options.channel_config.count_mode);

src/node/channel_splitter.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@ use crate::context::{AudioContextRegistration, BaseAudioContext};
44
use crate::render::{
55
AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope,
66
};
7+
use crate::MAX_CHANNELS;
78

89
use super::{
910
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
1011
};
1112

13+
/// Assert that the given number of channels is valid for a ChannelMergerNode
14+
///
15+
/// # Panics
16+
///
17+
/// This function will panic if:
18+
/// - the given number of channels is outside the [1, 32] range,
19+
/// 32 being defined by the MAX_CHANNELS constant.
20+
///
21+
#[track_caller]
22+
#[inline(always)]
23+
pub(crate) fn assert_valid_number_of_channels(number_of_channels: usize) {
24+
assert!(
25+
number_of_channels > 0 && number_of_channels <= MAX_CHANNELS,
26+
"IndexSizeError - Invalid number of channels: {:?} is outside range [1, {:?}]",
27+
number_of_channels,
28+
MAX_CHANNELS
29+
);
30+
}
31+
1232
/// Assert that the channel count mode is valid for the ChannelSplitterNode
1333
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
1434
///
@@ -112,7 +132,7 @@ impl AudioNode for ChannelSplitterNode {
112132
impl ChannelSplitterNode {
113133
pub fn new<C: BaseAudioContext>(context: &C, mut options: ChannelSplitterOptions) -> Self {
114134
context.base().register(move |registration| {
115-
crate::assert_valid_number_of_channels(options.number_of_outputs);
135+
assert_valid_number_of_channels(options.number_of_outputs);
116136
options.channel_config.count = options.number_of_outputs;
117137

118138
assert_valid_channel_count_mode(options.channel_config.count_mode);

0 commit comments

Comments
 (0)