@@ -8,7 +8,7 @@ use crate::render::{
8
8
} ;
9
9
use crate :: { AtomicF32 , RENDER_QUANTUM_SIZE } ;
10
10
11
- use super :: { AudioNode , AudioNodeOptions , ChannelConfig } ;
11
+ use super :: { AudioNode , AudioNodeOptions , ChannelConfig , ChannelCountMode , ChannelInterpretation } ;
12
12
13
13
// Converting a value 𝑣 in decibels to linear gain unit means returning 10𝑣/20.
14
14
fn db_to_lin ( val : f32 ) -> f32 {
@@ -53,11 +53,48 @@ impl Default for DynamicsCompressorOptions {
53
53
ratio : 12. , // unit less
54
54
release : 0.25 , // seconds
55
55
threshold : -24. , // dB
56
- audio_node_options : AudioNodeOptions :: default ( ) ,
56
+ audio_node_options : AudioNodeOptions {
57
+ channel_count : 2 ,
58
+ channel_count_mode : ChannelCountMode :: ClampedMax ,
59
+ channel_interpretation : ChannelInterpretation :: Speakers ,
60
+ } ,
57
61
}
58
62
}
59
63
}
60
64
65
+ /// Assert that the channel count is valid for the DynamicsCompressorNode
66
+ /// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
67
+ ///
68
+ /// # Panics
69
+ ///
70
+ /// This function panics if given count is greater than 2
71
+ ///
72
+ #[ track_caller]
73
+ #[ inline( always) ]
74
+ fn assert_valid_channel_count ( count : usize ) {
75
+ assert ! (
76
+ count <= 2 ,
77
+ "NotSupportedError - DynamicsCompressorNode channel count cannot be greater than two"
78
+ ) ;
79
+ }
80
+
81
+ /// Assert that the channel count is valid for the DynamicsCompressorNode
82
+ /// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
83
+ ///
84
+ /// # Panics
85
+ ///
86
+ /// This function panics if given count mode is [`ChannelCountMode::Max`]
87
+ ///
88
+ #[ track_caller]
89
+ #[ inline( always) ]
90
+ fn assert_valid_channel_count_mode ( mode : ChannelCountMode ) {
91
+ assert_ne ! (
92
+ mode,
93
+ ChannelCountMode :: Max ,
94
+ "NotSupportedError - DynamicsCompressorNode channel count mode cannot be set to max"
95
+ ) ;
96
+ }
97
+
61
98
/// `DynamicsCompressorNode` provides a compression effect.
62
99
///
63
100
/// It lowers the volume of the loudest parts of the signal and raises the volume
@@ -126,11 +163,27 @@ impl AudioNode for DynamicsCompressorNode {
126
163
fn number_of_outputs ( & self ) -> usize {
127
164
1
128
165
}
166
+
167
+ // see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
168
+ fn set_channel_count ( & self , count : usize ) {
169
+ assert_valid_channel_count ( count) ;
170
+ self . channel_config . set_count ( count, self . registration ( ) ) ;
171
+ }
172
+
173
+ // see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
174
+ fn set_channel_count_mode ( & self , mode : ChannelCountMode ) {
175
+ assert_valid_channel_count_mode ( mode) ;
176
+ self . channel_config
177
+ . set_count_mode ( mode, self . registration ( ) ) ;
178
+ }
129
179
}
130
180
131
181
impl DynamicsCompressorNode {
132
182
pub fn new < C : BaseAudioContext > ( context : & C , options : DynamicsCompressorOptions ) -> Self {
133
183
context. base ( ) . register ( move |registration| {
184
+ assert_valid_channel_count ( options. audio_node_options . channel_count ) ;
185
+ assert_valid_channel_count_mode ( options. audio_node_options . channel_count_mode ) ;
186
+
134
187
// attack, knee, ratio, release and threshold have automation rate constraints
135
188
// https://webaudio.github.io/web-audio-api/#audioparam-automation-rate-constraints
136
189
let attack_param_opts = AudioParamDescriptor {
0 commit comments