Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix audio worklets in safari #652

Merged
merged 1 commit into from
Aug 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/platform/javascript/AlphaSynthAudioWorkletOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,31 @@ declare var AudioWorkletProcessor: {
new (options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
};

// Bug 646: Safari 14.1 is buggy regarding audio worklets
// globalThis cannot be used to access registerProcessor or samplerate
// we need to really use them as globals
/**
* @target web
*/
declare var registerProcessor: any;
/**
* @target web
*/
declare var sampleRate: number;

/**
* This class implements a HTML5 Web Audio API based audio output device
* for alphaSynth using the modern Audio Worklets.
* @target web
*/
export class AlphaSynthWebWorklet {
private static _isRegistered = false;
public static init() {
(Environment.globalThis as any).registerProcessor(
if(AlphaSynthWebWorklet._isRegistered) {
return;
}
AlphaSynthWebWorklet._isRegistered = true;
registerProcessor(
'alphatab',
class AlphaSynthWebWorkletProcessor extends AudioWorkletProcessor {
public static readonly BufferSize: number = 4096;
Expand All @@ -41,9 +58,11 @@ export class AlphaSynthWebWorklet {
constructor(...args: any[]) {
super(...args);

Logger.info('WebAudio', 'creating processor');

this._bufferCount = Math.floor(
(AlphaSynthWebWorkletProcessor.TotalBufferTimeInMilliseconds *
Environment.globalThis.sampleRate) /
sampleRate) /
1000 /
AlphaSynthWebWorkletProcessor.BufferSize
);
Expand Down Expand Up @@ -77,6 +96,7 @@ export class AlphaSynthWebWorklet {
if (outputs.length !== 1 && outputs[0].length !== 2) {
return false;
}

let left: Float32Array = outputs[0][0];
let right: Float32Array = outputs[0][1];

Expand Down Expand Up @@ -160,7 +180,7 @@ export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
this._worklet.connect(ctx!.destination);
},
reason => {
Logger.debug('WebAudio', `Audio Worklet creation failed: reason=${reason}`);
Logger.error('WebAudio', `Audio Worklet creation failed: reason=${reason}`);
}
);
}
Expand Down