@@ -77,10 +77,11 @@ impl ScriptProcessorNode {
77
77
} ;
78
78
79
79
context. base ( ) . register ( move |registration| {
80
+ let number_of_quanta = buffer_size / RENDER_QUANTUM_SIZE ;
80
81
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 ) ,
84
85
buffer_size,
85
86
number_of_output_channels,
86
87
} ;
@@ -171,11 +172,20 @@ impl AudioProcessor for ScriptProcessorRenderer {
171
172
let input = & inputs[ 0 ] ;
172
173
let output = & mut outputs[ 0 ] ;
173
174
175
+ // default to silent output
174
176
output. make_silent ( ) ;
177
+ let silence = output. clone ( ) ;
175
178
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
+ }
177
183
184
+ // buffer inputs
185
+ let number_of_quanta = self . input_buffer . capacity ( ) ;
178
186
self . input_buffer . push ( input. clone ( ) ) ;
187
+
188
+ // check if we need to emit an event (input buffer is full)
179
189
if self . input_buffer . len ( ) == number_of_quanta {
180
190
// convert self.input_buffer to an AudioBuffer
181
191
let number_of_input_channels = self
@@ -210,17 +220,16 @@ impl AudioProcessor for ScriptProcessorRenderer {
210
220
211
221
// move next output buffer into current output buffer
212
222
std:: mem:: swap ( & mut self . output_buffer , & mut self . next_output_buffer ) ;
223
+
213
224
// fill next output buffer with silence
214
225
self . next_output_buffer . clear ( ) ;
215
- let silence = output. clone ( ) ;
216
226
self . next_output_buffer . resize ( number_of_quanta, silence) ;
217
227
}
218
228
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
224
233
}
225
234
226
235
fn onmessage ( & mut self , msg : & mut dyn Any ) {
0 commit comments