Skip to content

Commit

Permalink
Fix unsupported AudioContext
Browse files Browse the repository at this point in the history
  • Loading branch information
williamngan committed Apr 7, 2019
1 parent 6efd83f commit d58a6dc
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/Play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ export class Sound {
*/
constructor( type:SoundType ) {
this._type = type;
this.ctx = new AudioContext();
// @ts-ignore
let _ctx = window.AudioContext || window.webkitAudioContext || false;
if (!_ctx) console.error("Your browser doesn't support Web Audio. (No AudioContext)");
this.ctx = (_ctx) ? new _ctx() : undefined;
}


Expand All @@ -186,6 +189,7 @@ export class Sound {
*/
static from( node:AudioNode, ctx:AudioContext, type:SoundType="gen", stream?:MediaStream ) {
let s = new Sound( type );
if (!s) return undefined;
s.node = node;
s.ctx = ctx;
if (stream) s.stream = stream;
Expand All @@ -199,12 +203,22 @@ export class Sound {
* @returns a `Sound` instance
* @example `Sound.load( '/path/to/file.mp3' )`
*/
static load( source:HTMLMediaElement|string ):Sound {
let s = new Sound("file");
s.source = (typeof source === 'string') ? new Audio(source) : source;
s.source.addEventListener("ended", () => s._playing = false );
s.node = s.ctx.createMediaElementSource( s.source );
return s;
static load( source:HTMLMediaElement|string ):Promise<Sound> {
return new Promise( (resolve, reject) => {
let s = new Sound("file");
if (!s) {
reject("Error when creating Sound object.");
return;
}
s.source = (typeof source === 'string') ? new Audio(source) : source;
s.source.autoplay = false;
s.source.addEventListener("ended", function () { s._playing = false; } );
s.source.addEventListener('error', function () { reject("Error loading sound"); });
s.source.addEventListener('canplaythrough', function () {
s.node = s.ctx.createMediaElementSource( s.source );
resolve( s );
});
});
}


Expand All @@ -216,7 +230,9 @@ export class Sound {
* @example `Sound.generate( 'sine', 120 )`
*/
static generate( type:OscillatorType, val:number|PeriodicWave ):Sound {
return new Sound("gen")._gen( type, val );
let s = new Sound("gen");
if (!s) return undefined;
return s._gen( type, val );
}


Expand All @@ -243,6 +259,7 @@ export class Sound {
static async input( constraint?:MediaStreamConstraints ):Promise<Sound> {
try {
let s = new Sound("input");
if (!s) return undefined;
const c = constraint ? constraint : { audio: true, video: false };
s.stream = await navigator.mediaDevices.getUserMedia( c );
s.node = s.ctx.createMediaStreamSource( s.stream );
Expand Down

0 comments on commit d58a6dc

Please sign in to comment.