Skip to content

Commit

Permalink
Degrade gracefully when WebAudio API isn't supported (older browsers,…
Browse files Browse the repository at this point in the history
… WebWorkers, etc)
  • Loading branch information
jbaicoianu committed Oct 1, 2014
1 parent a7faee2 commit d927166
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions templates/default/webaudio.js
Expand Up @@ -13,7 +13,7 @@ var sampleScale = 32766;
var prebufferDuration = 100 / 1000;

function lazy_init () {
if (context)
if (context || typeof AudioContext == 'undefined')
return;

context = new AudioContext();
Expand All @@ -28,6 +28,7 @@ function set_mastervolume (
attenuation_in_decibels
) {
lazy_init();
if (!context) return;

// http://stackoverflow.com/questions/22604500/web-audio-api-working-with-decibels
// seemingly incorrect/broken. figures. welcome to Web Audio
Expand All @@ -50,6 +51,7 @@ function update_audio_stream (
samples_this_frame // int. number of samples at pBuffer address.
) {
lazy_init();
if (!context) return;

var buffer = context.createBuffer(
numChannels, samples_this_frame,
Expand Down Expand Up @@ -133,13 +135,17 @@ function tick () {
buffer_insert_point = now;
}
};
function get_context() {
return context;
};

return {
set_mastervolume: set_mastervolume,
update_audio_stream: update_audio_stream
update_audio_stream: update_audio_stream,
get_context: get_context
};

})();

jsmess_set_mastervolume = jsmess_web_audio.set_mastervolume;
jsmess_update_audio_stream = jsmess_web_audio.update_audio_stream;
jsmess_update_audio_stream = jsmess_web_audio.update_audio_stream;

0 comments on commit d927166

Please sign in to comment.