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

Improve handling of audio context and related promise handling #768

Merged
merged 2 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
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
44 changes: 29 additions & 15 deletions src/platform/javascript/AlphaSynthWebAudioOutputBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export abstract class AlphaSynthWebAudioOutputBase implements ISynthOutput {
protected _buffer: AudioBuffer | null = null;
protected _source: AudioBufferSourceNode | null = null;

private _resumeHandler?: () => void;

public get sampleRate(): number {
return this._context ? this._context.sampleRate : AlphaSynthWebAudioOutputBase.PreferredSampleRate;
}

public activate(): void {
public activate(resumedCallback?: () => void): void {
if (!this._context) {
this._context = this.createAudioContext();
}
Expand All @@ -35,9 +37,12 @@ export abstract class AlphaSynthWebAudioOutputBase implements ISynthOutput {
'WebAudio',
`Audio Context resume success: state=${this._context?.state}, sampleRate:${this._context?.sampleRate}`
);
if (resumedCallback) {
resumedCallback();
}
},
reason => {
Logger.debug(
Logger.warning(
'WebAudio',
`Audio Context resume failed: state=${this._context?.state}, sampleRate:${this._context?.sampleRate}, reason=${reason}`
);
Expand All @@ -48,7 +53,7 @@ export abstract class AlphaSynthWebAudioOutputBase implements ISynthOutput {

private patchIosSampleRate(): void {
let ua: string = navigator.userAgent;
if (ua.indexOf('iPhone') !== -1 || ua.indexOf('iPad') !== 0) {
if (ua.indexOf('iPhone') !== -1 || ua.indexOf('iPad') !== -1) {
let context: AudioContext = this.createAudioContext();
let buffer: AudioBuffer = context.createBuffer(1, 1, AlphaSynthWebAudioOutputBase.PreferredSampleRate);
let dummy: AudioBufferSourceNode = context.createBufferSource();
Expand All @@ -73,20 +78,27 @@ export abstract class AlphaSynthWebAudioOutputBase implements ISynthOutput {
public open(): void {
this.patchIosSampleRate();
this._context = this.createAudioContext();
// possible fix for Web Audio in iOS 9 (issue #4)
let ctx: any = this._context;
if (ctx.state === 'suspended') {
let resume = () => {
ctx.resume();
Environment.globalThis.setTimeout(() => {
if (ctx.state === 'running') {
document.body.removeEventListener('touchend', resume, false);
document.body.removeEventListener('click', resume, false);
}
}, 0);
};
document.body.addEventListener('touchend', resume, false);
document.body.addEventListener('click', resume, false);
this.registerResumeHandler();
}
}

private registerResumeHandler() {
this._resumeHandler = (() => {
this.activate(() => {
this.unregisterResumeHandler();
});
}).bind(this);
document.body.addEventListener('touchend', this._resumeHandler, false);
document.body.addEventListener('click', this._resumeHandler, false);
}

private unregisterResumeHandler() {
const resumeHandler = this._resumeHandler;
if (resumeHandler) {
document.body.removeEventListener('touchend', resumeHandler, false);
document.body.removeEventListener('click', resumeHandler, false);
}
}

Expand All @@ -111,6 +123,8 @@ export abstract class AlphaSynthWebAudioOutputBase implements ISynthOutput {
public destroy(): void {
this.pause();
this._context?.close();
this._context = null;
this.unregisterResumeHandler();
}

public abstract addSamples(f: Float32Array): void;
Expand Down
1 change: 0 additions & 1 deletion src/platform/javascript/AlphaTabApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class AlphaTabApi extends AlphaTabApiBase<any|Settings> {
SettingsSerializer.fromJson(settings, additionalSettings);
let alphaTab: AlphaTabApi = new AlphaTabApi(a4, settings);
alphaTab.renderer.postRenderFinished.on(() => {
alphaTab.canvasElement.height = -1;
preview.print();
});
alphaTab.renderTracks(this.tracks);
Expand Down