-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Use web audio api for bell sound #1200
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b75f527
Use Web audio API
bmf-ribeiro 1972bf4
Merge remote-tracking branch 'ups/master' into 1181
Tyriar db8ba17
Check audio context ctor before constructing
Tyriar 45c9763
Fix indentation
Tyriar af5b3f7
Fix lint
Tyriar a91e0e2
Use const everywhere
Tyriar ad7d226
Undo package-lock changes
Tyriar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2018 The xterm.js authors. All rights reserved. | ||
* @license MIT | ||
*/ | ||
|
||
import { ITerminal, ISoundManager } from './Types'; | ||
|
||
// Source: https://freesound.org/people/altemark/sounds/45759/ | ||
// This sound is released under the Creative Commons Attribution 3.0 Unported | ||
// (CC BY 3.0) license. It was created by 'altemark'. No modifications have been | ||
// made, apart from the conversion to base64. | ||
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=='; | ||
|
||
export class SoundManager implements ISoundManager { | ||
private _audioContext: AudioContext; | ||
|
||
constructor( | ||
private _terminal: ITerminal | ||
) { | ||
} | ||
|
||
public playBellSound(): void { | ||
const audioContextCtor: typeof AudioContext = (<any>window).AudioContext || (<any>window).webkitAudioContext; | ||
if (!this._audioContext && audioContextCtor) { | ||
this._audioContext = new audioContextCtor(); | ||
} | ||
|
||
if (this._audioContext) { | ||
const bellAudioSource = this._audioContext.createBufferSource(); | ||
const context = this._audioContext; | ||
this._audioContext.decodeAudioData(this.base64ToArrayBuffer(this.removeMimeType(this._terminal.options.bellSound)), (buffer) => { | ||
bellAudioSource.buffer = buffer; | ||
bellAudioSource.connect(context.destination); | ||
bellAudioSource.start(0); | ||
}); | ||
} else { | ||
console.warn('Sorry, but the Web Audio API is not supported by your browser. Please, consider upgrading to the latest version'); | ||
} | ||
} | ||
|
||
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a good idea to move all the sound-related code (including
base64ToArrayBuffer
) into it's own file now that the DOM interactions are gone.