From 09ae3a9b443133e043091525dd6d35e00b18f4c6 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Wed, 27 Jul 2022 05:55:00 -0700 Subject: [PATCH] Remove sound service and simplify bell --- src/browser/Terminal.ts | 28 +------------ src/browser/services/Services.ts | 8 ---- src/browser/services/SoundService.ts | 63 ---------------------------- 3 files changed, 2 insertions(+), 97 deletions(-) delete mode 100644 src/browser/services/SoundService.ts diff --git a/src/browser/Terminal.ts b/src/browser/Terminal.ts index bd82325d1..4c65b1b66 100644 --- a/src/browser/Terminal.ts +++ b/src/browser/Terminal.ts @@ -34,7 +34,6 @@ import { SelectionService } from 'browser/services/SelectionService'; import * as Browser from 'common/Platform'; import { addDisposableDomListener } from 'browser/Lifecycle'; import * as Strings from 'browser/LocalizableStrings'; -import { SoundService } from 'browser/services/SoundService'; import { MouseZoneManager } from 'browser/MouseZoneManager'; import { AccessibilityManager } from './AccessibilityManager'; import { ITheme, IMarker, IDisposable, ISelectionPosition, ILinkProvider, IDecorationOptions, IDecoration } from 'xterm'; @@ -45,7 +44,7 @@ import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter'; import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { ColorManager } from 'browser/ColorManager'; import { RenderService } from 'browser/services/RenderService'; -import { ICharSizeService, IRenderService, IMouseService, ISelectionService, ISoundService, ICoreBrowserService, ICharacterJoinerService } from 'browser/services/Services'; +import { ICharSizeService, IRenderService, IMouseService, ISelectionService, ICoreBrowserService, ICharacterJoinerService } from 'browser/services/Services'; import { CharSizeService } from 'browser/services/CharSizeService'; import { IBuffer } from 'common/buffer/Types'; import { MouseService } from 'browser/services/MouseService'; @@ -89,7 +88,6 @@ export class Terminal extends CoreTerminal implements ITerminal { private _renderService: IRenderService | undefined; private _characterJoinerService: ICharacterJoinerService | undefined; private _selectionService: ISelectionService | undefined; - private _soundService: ISoundService | undefined; /** * Records whether the keydown event has already been handled and triggered a data event, if so @@ -174,7 +172,7 @@ export class Terminal extends CoreTerminal implements ITerminal { this._instantiationService.setService(IDecorationService, this._decorationService); // Setup InputHandler listeners - this.register(this._inputHandler.onRequestBell(() => this.bell())); + this.register(this._inputHandler.onRequestBell(() => this._onBell.fire())); this.register(this._inputHandler.onRequestRefreshRows((start, end) => this.refresh(start, end))); this.register(this._inputHandler.onRequestSendFocus(() => this._reportFocus())); this.register(this._inputHandler.onRequestReset(() => this.reset())); @@ -537,8 +535,6 @@ export class Terminal extends CoreTerminal implements ITerminal { // Performance: Add viewport and helper elements from the fragment this.element.appendChild(fragment); - this._soundService = this._instantiationService.createInstance(SoundService); - this._instantiationService.setService(ISoundService, this._soundService); this._mouseService = this._instantiationService.createInstance(MouseService); this._instantiationService.setService(IMouseService, this._mouseService); @@ -1285,26 +1281,6 @@ export class Terminal extends CoreTerminal implements ITerminal { return false; } - /** - * Ring the bell. - * Note: We could do sweet things with webaudio here - */ - public bell(): void { - if (this._soundBell()) { - this._soundService?.playBellSound(); - } - - this._onBell.fire(); - - // if (this._visualBell()) { - // this.element.classList.add('visual-bell-active'); - // clearTimeout(this._visualBellTimer); - // this._visualBellTimer = window.setTimeout(() => { - // this.element.classList.remove('visual-bell-active'); - // }, 200); - // } - } - /** * Resizes the terminal. * diff --git a/src/browser/services/Services.ts b/src/browser/services/Services.ts index e1fb5dbd5..9f2263388 100644 --- a/src/browser/services/Services.ts +++ b/src/browser/services/Services.ts @@ -105,14 +105,6 @@ export interface ISelectionService { isCellInSelection(x: number, y: number): boolean; } -export const ISoundService = createDecorator('SoundService'); -export interface ISoundService { - serviceBrand: undefined; - - playBellSound(): void; -} - - export const ICharacterJoinerService = createDecorator('CharacterJoinerService'); export interface ICharacterJoinerService { serviceBrand: undefined; diff --git a/src/browser/services/SoundService.ts b/src/browser/services/SoundService.ts deleted file mode 100644 index a3b6800d4..000000000 --- a/src/browser/services/SoundService.ts +++ /dev/null @@ -1,63 +0,0 @@ -/** - * 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 { - public serviceBrand: undefined; - - private static _audioContext: AudioContext; - - public static get audioContext(): AudioContext | null { - if (!SoundService._audioContext) { - const audioContextCtor: typeof AudioContext = (window as any).AudioContext || (window as any).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.rawOptions.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]; - } -}