@@ -137,20 +137,26 @@ impl ScriptProcessorNode {
137137 /// the inputBuffer attribute. The audio data which is the result of the processing (or the
138138 /// synthesized data if there are no inputs) is then placed into the outputBuffer.
139139 ///
140+ /// The output buffer is shipped back to the render thread when the AudioProcessingEvent goes
141+ /// out of scope, so be sure not to store it somewhere.
142+ ///
140143 /// Only a single event handler is active at any time. Calling this method multiple times will
141144 /// override the previous event handler.
142- pub fn set_onaudioprocess < F : FnMut ( & mut AudioProcessingEvent ) + Send + ' static > (
145+ pub fn set_onaudioprocess < F : FnMut ( AudioProcessingEvent ) + Send + ' static > (
143146 & self ,
144147 mut callback : F ,
145148 ) {
146- let registration = self . registration . clone ( ) ;
149+ // We need these fields to ship the output buffer to the render thread
150+ let base = self . registration ( ) . context ( ) . clone ( ) ;
151+ let id = self . registration ( ) . id ( ) ;
152+
147153 let callback = move |v| {
148154 let mut payload = match v {
149155 EventPayload :: AudioProcessing ( v) => v,
150156 _ => unreachable ! ( ) ,
151157 } ;
152- callback ( & mut payload ) ;
153- registration . post_message ( payload. output_buffer ) ;
158+ payload . registration = Some ( ( base . clone ( ) , id ) ) ;
159+ callback ( payload) ;
154160 } ;
155161
156162 self . context ( ) . set_event_handler (
@@ -305,7 +311,7 @@ mod tests {
305311
306312 let node = context. create_script_processor ( BUFFER_SIZE , 0 , 1 ) ;
307313 node. connect ( & context. destination ( ) ) ;
308- node. set_onaudioprocess ( |e| {
314+ node. set_onaudioprocess ( |mut e| {
309315 e. output_buffer . get_channel_data_mut ( 0 ) . fill ( 1. ) ; // set all samples to 1.
310316 } ) ;
311317
@@ -336,7 +342,7 @@ mod tests {
336342 // 2 input channels, 2 output channels
337343 let node = context. create_script_processor ( BUFFER_SIZE , 2 , 2 ) ;
338344 node. connect ( & context. destination ( ) ) ;
339- node. set_onaudioprocess ( |e| {
345+ node. set_onaudioprocess ( |mut e| {
340346 // left output buffer is left input * 2
341347 e. output_buffer
342348 . get_channel_data_mut ( 0 )
0 commit comments