Skip to content

Commit dbe3556

Browse files
authored
Merge pull request #473 from orottier/bugfix/wpt-fixes
WPT fixes: channel splitter/merger error msg, offline ctx channel count mode
2 parents 8254c0e + 40c6d9e commit dbe3556

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
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);

src/node/destination.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,24 @@ impl AudioNode for AudioDestinationNode {
5555
}
5656

5757
fn set_channel_count(&self, v: usize) {
58+
// <https://webaudio.github.io/web-audio-api/#AudioDestinationNode>
59+
// numberOfChannels is the number of channels specified when constructing the
60+
// OfflineAudioContext. This value may not be changed; a NotSupportedError exception MUST
61+
// be thrown if channelCount is changed to a different value.
62+
//
63+
// <https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount>
64+
// The channel count cannot be changed. An InvalidStateError exception MUST be thrown for
65+
// any attempt to change the value.
66+
//
67+
// TODO spec issue: NotSupportedError or InvalidStateError?
5868
assert!(
5969
!self.registration.context().offline() || v == self.max_channel_count(),
6070
"NotSupportedError - not allowed to change OfflineAudioContext destination channel count"
6171
);
6272

73+
// <https://webaudio.github.io/web-audio-api/#dom-audionode-channelcount>
74+
// The channel count MUST be between 1 and maxChannelCount. An IndexSizeError exception
75+
// MUST be thrown for any attempt to set the count outside this range.
6376
assert!(
6477
v <= self.max_channel_count(),
6578
"IndexSizeError - channel count cannot be greater than maxChannelCount ({})",
@@ -70,11 +83,15 @@ impl AudioNode for AudioDestinationNode {
7083
}
7184

7285
fn set_channel_count_mode(&self, v: ChannelCountMode) {
73-
// [spec] If the AudioDestinationNode is the destination node of an
86+
// <https://webaudio.github.io/web-audio-api/#AudioDestinationNode>
87+
// For an OfflineAudioContext, the defaults are [..] channelCountMode: "explicit"
88+
//
89+
// <https://webaudio.github.io/web-audio-api/#dom-audionode-channelcountmode>
90+
// If the AudioDestinationNode is the destination node of an
7491
// OfflineAudioContext, then the channel count mode cannot be changed.
7592
// An InvalidStateError exception MUST be thrown for any attempt to change the value.
7693
assert!(
77-
!self.registration.context().offline(),
94+
!self.registration.context().offline() || v == ChannelCountMode::Explicit,
7895
"InvalidStateError - AudioDestinationNode has channel count mode constraints",
7996
);
8097

0 commit comments

Comments
 (0)