Skip to content

Commit

Permalink
Merge pull request #2289 from Tyriar/sound_browser
Browse files Browse the repository at this point in the history
Move sound into browser
  • Loading branch information
Tyriar authored Jul 4, 2019
2 parents fb6ec7f + 49b5eda commit f889796
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { SelectionService } from './browser/services/SelectionService';
import * as Browser from 'common/Platform';
import { addDisposableDomListener } from 'browser/Lifecycle';
import * as Strings from './browser/LocalizableStrings';
import { SoundManager } from './SoundManager';
import { SoundService } from 'browser/services/SoundService';
import { MouseZoneManager } from './MouseZoneManager';
import { AccessibilityManager } from './AccessibilityManager';
import { ITheme, IMarker, IDisposable, ISelectionPosition } from 'xterm';
Expand All @@ -49,7 +49,7 @@ import { ColorManager } from 'browser/ColorManager';
import { RenderService } from 'browser/services/RenderService';
import { IOptionsService, IBufferService, ICoreService } from 'common/services/Services';
import { OptionsService } from 'common/services/OptionsService';
import { ICharSizeService, IRenderService, IMouseService, ISelectionService } from 'browser/services/Services';
import { ICharSizeService, IRenderService, IMouseService, ISelectionService, ISoundService } from 'browser/services/Services';
import { CharSizeService } from 'browser/services/CharSizeService';
import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService';
import { Disposable } from 'common/Lifecycle';
Expand Down Expand Up @@ -116,6 +116,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
private _mouseService: IMouseService;
private _renderService: IRenderService;
private _selectionService: ISelectionService;
private _soundService: ISoundService;

// modes
public applicationKeypad: boolean;
Expand Down Expand Up @@ -174,7 +175,6 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
private _userScrolling: boolean;

private _inputHandler: InputHandler;
public soundManager: SoundManager;
public linkifier: ILinkifier;
public viewport: IViewport;
private _compositionHelper: ICompositionHelper;
Expand Down Expand Up @@ -304,7 +304,6 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
this._selectionService = this._selectionService || null;
this.linkifier = this.linkifier || new Linkifier(this);
this._mouseZoneManager = this._mouseZoneManager || null;
this.soundManager = this.soundManager || new SoundManager(this);

if (this.options.windowsMode) {
this._windowsMode = applyWindowsMode(this);
Expand Down Expand Up @@ -619,6 +618,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
this._renderService.onRender(e => this._onRender.fire(e));
this.onResize(e => this._renderService.resize(e.cols, e.rows));

this._soundService = new SoundService(this.optionsService);
this._mouseService = new MouseService(this._renderService, this._charSizeService);

this._mouseZoneManager = new MouseZoneManager(this, this._mouseService);
Expand Down Expand Up @@ -1676,7 +1676,7 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp
*/
public bell(): void {
if (this._soundBell()) {
this.soundManager.playBellSound();
this._soundService.playBellSound();
}

if (this._visualBell()) {
Expand Down
4 changes: 0 additions & 4 deletions src/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,6 @@ export interface IBrowser {
isWindows: boolean;
}

export interface ISoundManager {
playBellSound(): void;
}

export interface IMouseZoneManager extends IDisposable {
add(zone: IMouseZone): void;
clearAll(start?: number, end?: number): void;
Expand Down
4 changes: 4 additions & 0 deletions src/browser/services/Services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ export interface ISelectionService {
refresh(isLinuxMouseSelection?: boolean): void;
onMouseDown(event: MouseEvent): void;
}

export interface ISoundService {
playBellSound(): void;
}
17 changes: 9 additions & 8 deletions src/SoundManager.ts → src/browser/services/SoundService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@
* @license MIT
*/

import { ITerminal, ISoundManager } from './Types';
import { IOptionsService } from 'common/services/Services';
import { ISoundService } from 'browser/services/Services';

export class SoundManager implements ISoundManager {
export class SoundService implements ISoundService {
private static _audioContext: AudioContext;

static get audioContext(): AudioContext | null {
if (!SoundManager._audioContext) {
if (!SoundService._audioContext) {
const audioContextCtor: typeof AudioContext = (<any>window).AudioContext || (<any>window).webkitAudioContext;
if (!audioContextCtor) {
console.warn('Web Audio API is not supported by this browser. Consider upgrading to the latest version');
return null;
}
SoundManager._audioContext = new audioContextCtor();
SoundService._audioContext = new audioContextCtor();
}
return SoundManager._audioContext;
return SoundService._audioContext;
}

constructor(
private _terminal: ITerminal
private _optionsService: IOptionsService
) {
}

public playBellSound(): void {
const ctx = SoundManager.audioContext;
const ctx = SoundService.audioContext;
if (!ctx) {
return;
}
const bellAudioSource = ctx.createBufferSource();
ctx.decodeAudioData(this._base64ToArrayBuffer(this._removeMimeType(this._terminal.options.bellSound)), (buffer) => {
ctx.decodeAudioData(this._base64ToArrayBuffer(this._removeMimeType(this._optionsService.options.bellSound)), (buffer) => {
bellAudioSource.buffer = buffer;
bellAudioSource.connect(ctx.destination);
bellAudioSource.start(0);
Expand Down

0 comments on commit f889796

Please sign in to comment.