X-Git-Url: https://git.josue.xyz/?p=VSoRC%2F.git;a=blobdiff_plain;f=node_modules%2Fxterm%2Fsrc%2Fbrowser%2Fservices%2FSoundService.ts;fp=node_modules%2Fxterm%2Fsrc%2Fbrowser%2Fservices%2FSoundService.ts;h=1772c7504e2b052bf088f1d45c8465157f951bac;hp=0000000000000000000000000000000000000000;hb=4339da12467b75fb8b6ca831f4bf0081c485ed2c;hpb=af450fde25a9ccf4767b29254c463ffb8ef25237 diff --git a/node_modules/xterm/src/browser/services/SoundService.ts b/node_modules/xterm/src/browser/services/SoundService.ts new file mode 100644 index 0000000..1772c75 --- /dev/null +++ b/node_modules/xterm/src/browser/services/SoundService.ts @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2018 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { IOptionsService } from 'common/services/Services'; +import { ISoundService } from 'browser/services/Services'; + +export class SoundService implements ISoundService { + serviceBrand: any; + + private static _audioContext: AudioContext; + + static get audioContext(): AudioContext | null { + if (!SoundService._audioContext) { + const audioContextCtor: typeof AudioContext = (window).AudioContext || (window).webkitAudioContext; + if (!audioContextCtor) { + console.warn('Web Audio API is not supported by this browser. Consider upgrading to the latest version'); + return null; + } + SoundService._audioContext = new audioContextCtor(); + } + return SoundService._audioContext; + } + + constructor( + @IOptionsService private _optionsService: IOptionsService + ) { + } + + public playBellSound(): void { + const ctx = SoundService.audioContext; + if (!ctx) { + return; + } + const bellAudioSource = ctx.createBufferSource(); + ctx.decodeAudioData(this._base64ToArrayBuffer(this._removeMimeType(this._optionsService.options.bellSound)), (buffer) => { + bellAudioSource.buffer = buffer; + bellAudioSource.connect(ctx.destination); + bellAudioSource.start(0); + }); + } + + private _base64ToArrayBuffer(base64: string): ArrayBuffer { + const binaryString = window.atob(base64); + const len = binaryString.length; + const bytes = new Uint8Array(len); + + for (let i = 0; i < len; i++) { + bytes[i] = binaryString.charCodeAt(i); + } + + return bytes.buffer; + } + + private _removeMimeType(dataURI: string): string { + // Split the input to get the mime-type and the data itself + const splitUri = dataURI.split(','); + + // Return only the data + return splitUri[1]; + } +}