Skip to content

Commit

Permalink
don't throw web audio api error on load
Browse files Browse the repository at this point in the history
- since errors stop execution in a js file, if woscope was concatenated with other files for deployment, it could break an app. this is fixed now.
- error on woscope invocation instead
- send web audio API errors to error callback
  • Loading branch information
cvn committed Jan 24, 2017
1 parent 2f12aa3 commit d9d99d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 1 addition & 5 deletions .jshintrc
Expand Up @@ -2,9 +2,5 @@
"esnext": true,
"browser": true,
"browserify": true,
"devel": true,
"globals": {
"AudioContext": true,
"webkitAudioContext": true
}
"devel": true
}
24 changes: 15 additions & 9 deletions index.js
Expand Up @@ -14,15 +14,6 @@ let shadersDict = {
};

let audioCtx;
try {
try {
audioCtx = new AudioContext();
} catch(e) {
audioCtx = new webkitAudioContext();
}
} catch(e) {
throw new Error('Web Audio API is not supported in this browser');
}

function axhr(url, callback, progress) {
let request = new XMLHttpRequest();
Expand All @@ -39,6 +30,8 @@ function axhr(url, callback, progress) {

module.exports = woscope;
function woscope(config) {
audioCtx = audioCtx || initAudioCtx(config.error);

let canvas = config.canvas,
gl = initGl(canvas, config.error),
audio = config.audio,
Expand Down Expand Up @@ -95,6 +88,19 @@ function woscope(config) {
});
}

function initAudioCtx(errorCallback) {
try {
let AudioCtx = window.AudioContext || window.webkitAudioContext;
return new AudioCtx();
} catch(e) {
let message = 'Web Audio API is not supported in this browser';
if (errorCallback) {
errorCallback(message);
}
throw new Error(message);
}
}

function initGl(canvas, errorCallback) {
let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
if (!gl) {
Expand Down

0 comments on commit d9d99d8

Please sign in to comment.