Permalink
Browse files

Make compatible with browsers that don't support Web Audio (shader only)

  • Loading branch information...
1 parent 68cc33c commit 921b2462ae4c0a0647ddf6645475defe911e06e6 @notlion notlion committed Apr 10, 2012
Showing with 18 additions and 12 deletions.
  1. +18 −12 main.js
View
30 main.js
@@ -204,7 +204,8 @@ function(core, material, event, params, selector){
var context, source, analyser, freq_data, buffer_complete_cb;
function safeCreateAudioBuffer(buffer, callback){
- context.decodeAudioData(buffer, callback, onCreateAudioBufferError);
+ if(context)
+ context.decodeAudioData(buffer, callback, onCreateAudioBufferError);
}
function onCreateAudioBufferError(){
console.error("Error decoding audio buffer");
@@ -228,14 +229,16 @@ function(core, material, event, params, selector){
}
function initAudio(){
- context = new webkitAudioContext();
+ if(window.webkitAudioContext){
+ context = new webkitAudioContext();
- analyser = context.createAnalyser();
- analyser.fftSize = 512;
- analyser.smoothingTimeConstant = 0.5;
- analyser.connect(context.destination);
+ analyser = context.createAnalyser();
+ analyser.fftSize = 512;
+ analyser.smoothingTimeConstant = 0.5;
+ analyser.connect(context.destination);
- initFrequencyData();
+ initFrequencyData();
+ }
}
function playAudioBuffer(buffer){
@@ -304,10 +307,11 @@ function(core, material, event, params, selector){
parseShaderOutlets(shader_src_frag, {
"smoothing": function(value){
- analyser.smoothingTimeConstant = core.math.clamp(value, 0, 1);
+ if(analyser)
+ analyser.smoothingTimeConstant = core.math.clamp(value, 0, 1);
},
"num_bands": function(value){
- if(core.math.isPow2(value)){
+ if(analyser && core.math.isPow2(value)){
analyser.fftSize = value * 2;
initFrequencyData();
}
@@ -355,10 +359,12 @@ function(core, material, event, params, selector){
gl.viewport(0, 0, canvas.width, canvas.height);
window.requestAnimationFrame(render);
- analyser.getByteFrequencyData(freq_data);
- freq_texture.bind();
- freq_texture.updateData(freq_data);
+ if(analyser){
+ analyser.getByteFrequencyData(freq_data);
+ freq_texture.bind();
+ freq_texture.updateData(freq_data);
+ }
program.use({
u_frequencies: 0,

0 comments on commit 921b246

Please sign in to comment.