@@ -8,7 +8,9 @@ use crate::render::{
8
8
} ;
9
9
use crate :: { AtomicF32 , RENDER_QUANTUM_SIZE } ;
10
10
11
- use super :: { AudioNode , ChannelConfig , ChannelConfigOptions } ;
11
+ use super :: {
12
+ AudioNode , ChannelConfig , ChannelConfigOptions , ChannelCountMode , ChannelInterpretation ,
13
+ } ;
12
14
13
15
// Converting a value 𝑣 in decibels to linear gain unit means returning 10𝑣/20.
14
16
fn db_to_lin ( val : f32 ) -> f32 {
@@ -53,11 +55,48 @@ impl Default for DynamicsCompressorOptions {
53
55
ratio : 12. , // unit less
54
56
release : 0.25 , // seconds
55
57
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
+ } ,
57
63
}
58
64
}
59
65
}
60
66
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
+
61
100
/// `DynamicsCompressorNode` provides a compression effect.
62
101
///
63
102
/// It lowers the volume of the loudest parts of the signal and raises the volume
@@ -126,11 +165,27 @@ impl AudioNode for DynamicsCompressorNode {
126
165
fn number_of_outputs ( & self ) -> usize {
127
166
1
128
167
}
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
+ }
129
181
}
130
182
131
183
impl DynamicsCompressorNode {
132
184
pub fn new < C : BaseAudioContext > ( context : & C , options : DynamicsCompressorOptions ) -> Self {
133
185
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
+
134
189
// attack, knee, ratio, release and threshold have automation rate constraints
135
190
// https://webaudio.github.io/web-audio-api/#audioparam-automation-rate-constraints
136
191
let attack_param_opts = AudioParamDescriptor {
0 commit comments