@@ -11,7 +11,7 @@ use crate::render::{
1111} ;
1212use crate :: RENDER_QUANTUM_SIZE ;
1313
14- use super :: { AudioNode , AudioNodeOptions , ChannelConfig , ChannelInterpretation } ;
14+ use super :: { AudioNode , AudioNodeOptions , ChannelConfig , ChannelCountMode , ChannelInterpretation } ;
1515
1616/// Scale buffer by an equal-power normalization
1717// see - <https://webaudio.github.io/web-audio-api/#dom-convolvernode-normalize>
@@ -59,7 +59,7 @@ fn normalize_buffer(buffer: &AudioBuffer) -> f32 {
5959// AudioBuffer? buffer;
6060// boolean disableNormalization = false;
6161//};
62- #[ derive( Clone , Debug , Default ) ]
62+ #[ derive( Clone , Debug ) ]
6363pub struct ConvolverOptions {
6464 /// The desired buffer for the ConvolverNode
6565 pub buffer : Option < AudioBuffer > ,
@@ -69,6 +69,53 @@ pub struct ConvolverOptions {
6969 pub audio_node_options : AudioNodeOptions ,
7070}
7171
72+ impl Default for ConvolverOptions {
73+ fn default ( ) -> Self {
74+ Self {
75+ buffer : Default :: default ( ) ,
76+ disable_normalization : Default :: default ( ) ,
77+ audio_node_options : AudioNodeOptions {
78+ channel_count : 2 ,
79+ channel_count_mode : ChannelCountMode :: ClampedMax ,
80+ channel_interpretation : ChannelInterpretation :: Speakers ,
81+ } ,
82+ }
83+ }
84+ }
85+
86+ /// Assert that the channel count is valid for the ConvolverNode
87+ /// see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
88+ ///
89+ /// # Panics
90+ ///
91+ /// This function panics if given count is greater than 2
92+ ///
93+ #[ track_caller]
94+ #[ inline( always) ]
95+ fn assert_valid_channel_count ( count : usize ) {
96+ assert ! (
97+ count <= 2 ,
98+ "NotSupportedError - ConvolverNode channel count cannot be greater than two"
99+ ) ;
100+ }
101+
102+ /// Assert that the channel count mode is valid for the ConvolverNode
103+ /// see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
104+ ///
105+ /// # Panics
106+ ///
107+ /// This function panics if given count mode is [`ChannelCountMode::Max`]
108+ ///
109+ #[ track_caller]
110+ #[ inline( always) ]
111+ fn assert_valid_channel_count_mode ( mode : ChannelCountMode ) {
112+ assert_ne ! (
113+ mode,
114+ ChannelCountMode :: Max ,
115+ "NotSupportedError - ConvolverNode channel count mode cannot be set to max"
116+ ) ;
117+ }
118+
72119/// Processing node which applies a linear convolution effect given an impulse response.
73120///
74121/// - MDN documentation: <https://developer.mozilla.org/en-US/docs/Web/API/ConvolverNode>
@@ -137,6 +184,19 @@ impl AudioNode for ConvolverNode {
137184 fn number_of_outputs ( & self ) -> usize {
138185 1
139186 }
187+
188+ // see <https://webaudio.github.io/web-audio-api/#audionode-channelcount-constraints>
189+ fn set_channel_count ( & self , count : usize ) {
190+ assert_valid_channel_count ( count) ;
191+ self . channel_config . set_count ( count, self . registration ( ) ) ;
192+ }
193+
194+ // see <https://webaudio.github.io/web-audio-api/#audionode-channelcountmode-constraints>
195+ fn set_channel_count_mode ( & self , mode : ChannelCountMode ) {
196+ assert_valid_channel_count_mode ( mode) ;
197+ self . channel_config
198+ . set_count_mode ( mode, self . registration ( ) ) ;
199+ }
140200}
141201
142202impl ConvolverNode {
@@ -155,15 +215,18 @@ impl ConvolverNode {
155215 let ConvolverOptions {
156216 buffer,
157217 disable_normalization,
158- audio_node_options : channel_config ,
218+ audio_node_options,
159219 } = options;
160220
221+ assert_valid_channel_count ( audio_node_options. channel_count ) ;
222+ assert_valid_channel_count_mode ( audio_node_options. channel_count_mode ) ;
223+
161224 let mut node = context. base ( ) . register ( move |registration| {
162225 let renderer = ConvolverRenderer { inner : None } ;
163226
164227 let node = Self {
165228 registration,
166- channel_config : channel_config . into ( ) ,
229+ channel_config : audio_node_options . into ( ) ,
167230 normalize : !disable_normalization,
168231 buffer : None ,
169232 } ;
@@ -273,18 +336,18 @@ impl AudioProcessor for ConvolverRenderer {
273336 Some ( convolver) => convolver,
274337 } ;
275338
276- // handle tail time
277- // if input.is_silent() {
278- // return convolver.tail(output);
279- // }
280-
281339 let mut mono = input. clone ( ) ;
282340 mono. mix ( 1 , ChannelInterpretation :: Speakers ) ;
283341 let input = & mono. channel_data ( 0 ) [ ..] ;
284342 let output = & mut output. channel_data_mut ( 0 ) [ ..] ;
285343
286344 let _ = convolver. process ( input, output) ;
287345
346+ // handle tail time
347+ // if input.is_silent() {
348+ // return convolver.tail(output);
349+ // }
350+
288351 true
289352 }
290353
0 commit comments