Skip to content

Commit 56edb74

Browse files
committed
fix(synth): buffer pending worklet messages
1 parent 6c3194f commit 56edb74

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

packages/alphatab/src/platform/javascript/AlphaSynthAudioWorkletOutput.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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}`);
@@ -266,15 +276,25 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
266276
this._worklet = null;
267277
}
268278

279+
private _postWorkerMessage(message: IAlphaSynthWorkerMessage) {
280+
const worklet = this._worklet;
281+
if (worklet) {
282+
worklet.port.postMessage(message);
283+
} else {
284+
this._pendingEvents ??= [];
285+
this._pendingEvents.push(message);
286+
}
287+
}
288+
269289
public addSamples(f: Float32Array): void {
270-
this._worklet?.port.postMessage({
290+
this._postWorkerMessage({
271291
cmd: 'alphaSynth.output.addSamples',
272292
samples: Environment.prepareForPostMessage(f)
273293
});
274294
}
275295

276296
public resetSamples(): void {
277-
this._worklet?.port.postMessage({
297+
this._postWorkerMessage({
278298
cmd: 'alphaSynth.output.resetSamples'
279299
});
280300
}

0 commit comments

Comments
 (0)