samedi 24 janvier 2015

How do you get the decibel level of an audio in Javascript

I am currently making a dbmeter visuilaizer using javascript, html and css.


I have gone through several web audio api tutorials, but nothing on there is close to being specific to what i want to do.


This is what i have so far:



window.onload = init;

function init(){

var ctx = new webkitAudioContext()
, url = 'http://ift.tt/1Ed6cKO'
, audio = new Audio(url)
// 2048 sample buffer, 1 channel in, 1 channel out
, processor = ctx.createJavaScriptNode(2048, 1, 1)
, meter = document.getElementById('meter')
, source;

audio.addEventListener('canplaythrough', function(){
source = ctx.createMediaElementSource(audio);
source.connect(processor);
source.connect(ctx.destination);
processor.connect(ctx.destination);
audio.play();
}, false);

// loop through PCM data and calculate average
// volume for a given 2048 sample buffer
processor.onaudioprocess = function(evt){
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 );
meter.style.width = ( rms * 100 ) + '%';
};

}


can someone please explain what I need to do or point me in the right direction as this doesn't seem to be working.


Regards


Aucun commentaire:

Enregistrer un commentaire