Skip to content

Commit 02732d9

Browse files
authored
Merge pull request #1787 from jerch/fix_audiocontext
hold single AudioContext instance for all terminal instances
2 parents bcd3a4d + 9286ea3 commit 02732d9

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/SoundManager.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,36 @@ import { ITerminal, ISoundManager } from './Types';
1212
export const DEFAULT_BELL_SOUND = 'data:audio/wav;base64,UklGRigBAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQQBAADpAFgCwAMlBZoG/wdmCcoKRAypDQ8PbRDBEQQTOxRtFYcWlBePGIUZXhoiG88bcBz7HHIdzh0WHlMeZx51HmkeUx4WHs8dah0AHXwc3hs9G4saxRnyGBIYGBcQFv8U4RPAEoYRQBACD70NWwwHC6gJOwjWBloF7gOBAhABkf8b/qv8R/ve+Xf4Ife79W/0JfPZ8Z/wde9N7ijtE+wU6xvqM+lb6H7nw+YX5mrlxuQz5Mzje+Ma49fioeKD4nXiYeJy4pHitOL04j/jn+MN5IPkFOWs5U3mDefM55/ogOl36m7rdOyE7abuyu8D8Unyj/Pg9D/2qfcb+Yn6/vuK/Qj/lAAlAg==';
1313

1414
export class SoundManager implements ISoundManager {
15-
private _audioContext: AudioContext;
15+
private static _audioContext: AudioContext;
16+
17+
static get audioContext(): AudioContext | null {
18+
if (!SoundManager._audioContext) {
19+
const audioContextCtor: typeof AudioContext = (<any>window).AudioContext || (<any>window).webkitAudioContext;
20+
if (!audioContextCtor) {
21+
console.warn('Web Audio API is not supported by this browser. Consider upgrading to the latest version');
22+
return null;
23+
}
24+
SoundManager._audioContext = new audioContextCtor();
25+
}
26+
return SoundManager._audioContext;
27+
}
1628

1729
constructor(
1830
private _terminal: ITerminal
1931
) {
2032
}
2133

2234
public playBellSound(): void {
23-
const audioContextCtor: typeof AudioContext = (<any>window).AudioContext || (<any>window).webkitAudioContext;
24-
if (!this._audioContext && audioContextCtor) {
25-
this._audioContext = new audioContextCtor();
26-
}
27-
28-
if (this._audioContext) {
29-
const bellAudioSource = this._audioContext.createBufferSource();
30-
const context = this._audioContext;
31-
this._audioContext.decodeAudioData(this._base64ToArrayBuffer(this._removeMimeType(this._terminal.options.bellSound)), (buffer) => {
32-
bellAudioSource.buffer = buffer;
33-
bellAudioSource.connect(context.destination);
34-
bellAudioSource.start(0);
35-
});
36-
} else {
37-
console.warn('Sorry, but the Web Audio API is not supported by your browser. Please, consider upgrading to the latest version');
35+
const ctx = SoundManager.audioContext;
36+
if (!ctx) {
37+
return;
3838
}
39+
const bellAudioSource = ctx.createBufferSource();
40+
ctx.decodeAudioData(this._base64ToArrayBuffer(this._removeMimeType(this._terminal.options.bellSound)), (buffer) => {
41+
bellAudioSource.buffer = buffer;
42+
bellAudioSource.connect(ctx.destination);
43+
bellAudioSource.start(0);
44+
});
3945
}
4046

4147
private _base64ToArrayBuffer(base64: string): ArrayBuffer {

0 commit comments

Comments
 (0)