Skip to content

Commit 639c56f

Browse files
authored
Fix audio worklets in safari (#652)
1 parent 2a9da20 commit 639c56f

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/platform/javascript/AlphaSynthAudioWorkletOutput.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,31 @@ declare var AudioWorkletProcessor: {
2020
new (options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
2121
};
2222

23+
// Bug 646: Safari 14.1 is buggy regarding audio worklets
24+
// globalThis cannot be used to access registerProcessor or samplerate
25+
// we need to really use them as globals
26+
/**
27+
* @target web
28+
*/
29+
declare var registerProcessor: any;
30+
/**
31+
* @target web
32+
*/
33+
declare var sampleRate: number;
34+
2335
/**
2436
* This class implements a HTML5 Web Audio API based audio output device
2537
* for alphaSynth using the modern Audio Worklets.
2638
* @target web
2739
*/
2840
export class AlphaSynthWebWorklet {
41+
private static _isRegistered = false;
2942
public static init() {
30-
(Environment.globalThis as any).registerProcessor(
43+
if(AlphaSynthWebWorklet._isRegistered) {
44+
return;
45+
}
46+
AlphaSynthWebWorklet._isRegistered = true;
47+
registerProcessor(
3148
'alphatab',
3249
class AlphaSynthWebWorkletProcessor extends AudioWorkletProcessor {
3350
public static readonly BufferSize: number = 4096;
@@ -41,9 +58,11 @@ export class AlphaSynthWebWorklet {
4158
constructor(...args: any[]) {
4259
super(...args);
4360

61+
Logger.info('WebAudio', 'creating processor');
62+
4463
this._bufferCount = Math.floor(
4564
(AlphaSynthWebWorkletProcessor.TotalBufferTimeInMilliseconds *
46-
Environment.globalThis.sampleRate) /
65+
sampleRate) /
4766
1000 /
4867
AlphaSynthWebWorkletProcessor.BufferSize
4968
);
@@ -77,6 +96,7 @@ export class AlphaSynthWebWorklet {
7796
if (outputs.length !== 1 && outputs[0].length !== 2) {
7897
return false;
7998
}
99+
80100
let left: Float32Array = outputs[0][0];
81101
let right: Float32Array = outputs[0][1];
82102

@@ -160,7 +180,7 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
160180
this._worklet.connect(ctx!.destination);
161181
},
162182
reason => {
163-
Logger.debug('WebAudio', `Audio Worklet creation failed: reason=${reason}`);
183+
Logger.error('WebAudio', `Audio Worklet creation failed: reason=${reason}`);
164184
}
165185
);
166186
}

0 commit comments

Comments
 (0)