Skip to content

Commit 2637ccb

Browse files
authored
Merge pull request #479 from b-ma/fix/compressor-options
Fix: handle channel constraints for DynamicsCompressorNode
2 parents dbe3556 + a293725 commit 2637ccb

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/node/dynamics_compressor.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use crate::render::{
88
};
99
use crate::{AtomicF32, RENDER_QUANTUM_SIZE};
1010

11-
use super::{AudioNode, ChannelConfig, ChannelConfigOptions};
11+
use super::{
12+
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
13+
};
1214

1315
// Converting a value 𝑣 in decibels to linear gain unit means returning 10𝑣/20.
1416
fn db_to_lin(val: f32) -> f32 {
@@ -53,11 +55,48 @@ impl Default for DynamicsCompressorOptions {
5355
ratio: 12., // unit less
5456
release: 0.25, // seconds
5557
threshold: -24., // dB
56-
channel_config: ChannelConfigOptions::default(),
58+
channel_config: ChannelConfigOptions {
59+
count: 2,
60+
count_mode: ChannelCountMode::ClampedMax,
61+
interpretation: ChannelInterpretation::Speakers,
62+
},
5763
}
5864
}
5965
}
6066

67+
/// Assert that the channel count is valid for the DynamicsCompressorNode
68+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
69+
///
70+
/// # Panics
71+
///
72+
/// This function panics if given count is greater than 2
73+
///
74+
#[track_caller]
75+
#[inline(always)]
76+
fn assert_valid_channel_count(count: usize) {
77+
assert!(
78+
count <= 2,
79+
"NotSupportedError - DynamicsCompressorNode channel count cannot be greater than two"
80+
);
81+
}
82+
83+
/// Assert that the channel count is valid for the DynamicsCompressorNode
84+
/// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
85+
///
86+
/// # Panics
87+
///
88+
/// This function panics if given count mode is [`ChannelCountMode::Max`]
89+
///
90+
#[track_caller]
91+
#[inline(always)]
92+
fn assert_valid_channel_count_mode(mode: ChannelCountMode) {
93+
assert_ne!(
94+
mode,
95+
ChannelCountMode::Max,
96+
"NotSupportedError - DynamicsCompressorNode channel count mode cannot be set to max"
97+
);
98+
}
99+
61100
/// `DynamicsCompressorNode` provides a compression effect.
62101
///
63102
/// It lowers the volume of the loudest parts of the signal and raises the volume
@@ -126,11 +165,27 @@ impl AudioNode for DynamicsCompressorNode {
126165
fn number_of_outputs(&self) -> usize {
127166
1
128167
}
168+
169+
// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
170+
fn set_channel_count(&self, count: usize) {
171+
assert_valid_channel_count(count);
172+
self.channel_config.set_count(count, self.registration());
173+
}
174+
175+
// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
176+
fn set_channel_count_mode(&self, mode: ChannelCountMode) {
177+
assert_valid_channel_count_mode(mode);
178+
self.channel_config
179+
.set_count_mode(mode, self.registration());
180+
}
129181
}
130182

131183
impl DynamicsCompressorNode {
132184
pub fn new<C: BaseAudioContext>(context: &C, options: DynamicsCompressorOptions) -> Self {
133185
context.base().register(move |registration| {
186+
assert_valid_channel_count(options.channel_config.count);
187+
assert_valid_channel_count_mode(options.channel_config.count_mode);
188+
134189
// attack, knee, ratio, release and threshold have automation rate constraints
135190
// https://webaudio.github.io/web-audio-api/#audioparam-automation-rate-constraints
136191
let attack_param_opts = AudioParamDescriptor {

0 commit comments

Comments
 (0)