Skip to content

Instantly share code, notes, and snippets.

@iapain
Created March 13, 2015 12:22
Show Gist options
  • Save iapain/1fb52d7f0f29874cb0b0 to your computer and use it in GitHub Desktop.
Save iapain/1fb52d7f0f29874cb0b0 to your computer and use it in GitHub Desktop.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var duration = 0;
var silence = 0;
var processor = audioCtx.createScriptProcessor(0, 1, 1);
processor.onaudioprocess = function(evt){
duration += evt.inputBuffer.duration;
var input = evt.inputBuffer.getChannelData(0)
, len = input.length
, total = i = 0
, rms;
while ( i < len ) total += Math.abs( input[i++] );
rms = Math.sqrt( total / len ) * 100;
if (rms < 15) {
silence += evt.inputBuffer.duration;
};
if (silence > 2) { console.log(duration, silence, rms); silence =0}
if (duration > 10) {soundSource.stop();}
};
var analyser = audioCtx.createAnalyser();
var soundSource, concertHallBuffer;
ajaxRequest = new XMLHttpRequest();
ajaxRequest.open('GET', 'http://ia802309.us.archive.org/32/items/fabulas_esopo_01_librivox/fabula_01_001_esopo.mp3', true);
ajaxRequest.responseType = 'arraybuffer';
ajaxRequest.onload = function() {
var audioData = ajaxRequest.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
concertHallBuffer = buffer;
soundSource = audioCtx.createBufferSource();
soundSource.buffer = concertHallBuffer;
soundSource.connect(analyser);
console.log(soundSource.buffer);
soundSource.connect(analyser);
analyser.connect(processor);
}, function(e){"Error with decoding audio data" + e.err});
soundSource.start();
}
ajaxRequest.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment