Skip to content

Commit

Permalink
Make compatible with browsers that don't support Web Audio (shader only)
Browse files Browse the repository at this point in the history
  • Loading branch information
notlion committed Apr 10, 2012
1 parent 68cc33c commit 921b246
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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){
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 921b246

Please sign in to comment.