@@ -203,6 +203,8 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
203203 private readonly _settings : Settings ;
204204 private _boundHandleMessage : ( e : MessageEvent < IAlphaSynthWorkerMessage > ) => void ;
205205
206+ private _pendingEvents ?: IAlphaSynthWorkerMessage [ ] ;
207+
206208 public constructor ( settings : Settings ) {
207209 super ( ) ;
208210 this . _settings = settings ;
@@ -234,6 +236,14 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
234236 this . source ! . connect ( this . _worklet ) ;
235237 this . source ! . start ( 0 ) ;
236238 this . _worklet . connect ( ctx ! . destination ) ;
239+
240+ const pending = this . _pendingEvents ;
241+ if ( pending ) {
242+ for ( const e of pending ) {
243+ this . _worklet . port . postMessage ( e ) ;
244+ }
245+ this . _pendingEvents = undefined ;
246+ }
237247 } ,
238248 ( reason : any ) => {
239249 Logger . error ( 'WebAudio' , `Audio Worklet creation failed: reason=${ reason } ` ) ;
@@ -264,17 +274,28 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
264274 this . _worklet . disconnect ( ) ;
265275 }
266276 this . _worklet = null ;
277+ this . _pendingEvents = undefined ;
278+ }
279+
280+ private _postWorkerMessage ( message : IAlphaSynthWorkerMessage ) {
281+ const worklet = this . _worklet ;
282+ if ( worklet ) {
283+ worklet . port . postMessage ( message ) ;
284+ } else {
285+ this . _pendingEvents ??= [ ] ;
286+ this . _pendingEvents . push ( message ) ;
287+ }
267288 }
268289
269290 public addSamples ( f : Float32Array ) : void {
270- this . _worklet ?. port . postMessage ( {
291+ this . _postWorkerMessage ( {
271292 cmd : 'alphaSynth.output.addSamples' ,
272293 samples : Environment . prepareForPostMessage ( f )
273294 } ) ;
274295 }
275296
276297 public resetSamples ( ) : void {
277- this . _worklet ?. port . postMessage ( {
298+ this . _postWorkerMessage ( {
278299 cmd : 'alphaSynth.output.resetSamples'
279300 } ) ;
280301 }
0 commit comments