@@ -77,10 +77,11 @@ impl ScriptProcessorNode {
7777 } ;
7878
7979 context. base ( ) . register ( move |registration| {
80+ let number_of_quanta = buffer_size / RENDER_QUANTUM_SIZE ;
8081 let render = ScriptProcessorRenderer {
81- input_buffer : Vec :: with_capacity ( buffer_size / RENDER_QUANTUM_SIZE ) ,
82- output_buffer : Vec :: with_capacity ( buffer_size / RENDER_QUANTUM_SIZE ) ,
83- next_output_buffer : Vec :: with_capacity ( buffer_size / RENDER_QUANTUM_SIZE ) ,
82+ input_buffer : Vec :: with_capacity ( number_of_quanta ) ,
83+ output_buffer : Vec :: with_capacity ( number_of_quanta ) ,
84+ next_output_buffer : Vec :: with_capacity ( number_of_quanta ) ,
8485 buffer_size,
8586 number_of_output_channels,
8687 } ;
@@ -171,11 +172,20 @@ impl AudioProcessor for ScriptProcessorRenderer {
171172 let input = & inputs[ 0 ] ;
172173 let output = & mut outputs[ 0 ] ;
173174
175+ // default to silent output
174176 output. make_silent ( ) ;
177+ let silence = output. clone ( ) ;
175178
176- let number_of_quanta = self . input_buffer . capacity ( ) ;
179+ // when there are output buffers lined up, emit the first one
180+ if !self . output_buffer . is_empty ( ) {
181+ * output = self . output_buffer . remove ( 0 ) ;
182+ }
177183
184+ // buffer inputs
185+ let number_of_quanta = self . input_buffer . capacity ( ) ;
178186 self . input_buffer . push ( input. clone ( ) ) ;
187+
188+ // check if we need to emit an event (input buffer is full)
179189 if self . input_buffer . len ( ) == number_of_quanta {
180190 // convert self.input_buffer to an AudioBuffer
181191 let number_of_input_channels = self
@@ -210,17 +220,16 @@ impl AudioProcessor for ScriptProcessorRenderer {
210220
211221 // move next output buffer into current output buffer
212222 std:: mem:: swap ( & mut self . output_buffer , & mut self . next_output_buffer ) ;
223+
213224 // fill next output buffer with silence
214225 self . next_output_buffer . clear ( ) ;
215- let silence = output. clone ( ) ;
216226 self . next_output_buffer . resize ( number_of_quanta, silence) ;
217227 }
218228
219- if !self . output_buffer . is_empty ( ) {
220- * output = self . output_buffer . remove ( 0 ) ;
221- }
222-
223- true // TODO - spec says false but that seems weird
229+ // TODO, spec says to return false but we actually need true because of 'actively
230+ // processing' requirements
231+ // <https://webaudio.github.io/web-audio-api/#AudioNode-actively-processing>
232+ true
224233 }
225234
226235 fn onmessage ( & mut self , msg : & mut dyn Any ) {
0 commit comments