Skip to content

Commit c82d706

Browse files
committed
ScriptProcessorNode: fix placement of output buffers
1 parent 2293237 commit c82d706

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/node/script_processor.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/render/processor.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,14 @@ impl AudioWorkletGlobalScope {
6363
output_buffer: AudioBuffer,
6464
playback_time: f64,
6565
) {
66-
if let Some(sender) = self.event_sender.as_ref() {
67-
// sending could fail if the channel is saturated or the main thread is shutting down
68-
let event = AudioProcessingEvent {
69-
input_buffer,
70-
output_buffer,
71-
playback_time,
72-
};
73-
let dispatch = EventDispatch::audio_processing(self.node_id.get(), event);
74-
let _ = sender.try_send(dispatch);
75-
}
66+
// sending could fail if the channel is saturated or the main thread is shutting down
67+
let event = AudioProcessingEvent {
68+
input_buffer,
69+
output_buffer,
70+
playback_time,
71+
};
72+
let dispatch = EventDispatch::audio_processing(self.node_id.get(), event);
73+
let _ = self.event_sender.try_send(dispatch);
7674
}
7775

7876
pub(crate) fn report_error(&self, error: Box<dyn Any + Send>) {

0 commit comments

Comments
 (0)