15

I am currently making a decibel meter visualizer 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 = 'https://dl.dropboxusercontent.com/u/86176287/pbjt.mp3'  
    , 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, since this doesn't seem to be working?

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
ipalibowhyte
  • 1,553
  • 2
  • 22
  • 34
  • 1
    What isn't working? Is your value `rms` what you'd expect (between -1 and 1) or do you just need the formula to convert to dB? – jaket Jan 26 '15 at 04:24
  • Hi, does the code work properly now? If so, please post it as an answer so that the question can be marked as resolved. Thanks! – Qantas 94 Heavy Jan 27 '15 at 13:42
  • 2
    @ipalibowhyte you can add your own answer separate from the question, as well as mark it as the accepted answer. – CTS_AE May 14 '19 at 16:29
  • 1
    @CTS_AE It seems the OP is aware - they have done it before. Also, OP has been inactive for 1.5 years – ReinstateMonica3167040 May 14 '19 at 23:54

1 Answers1

3

Fixed

so this is what I changed it to:

var audioCtx = new AudioContext();
var url = 'https://dl.dropboxusercontent.com/u/86176287/pbjt.mp3';
var audio = new Audio(url);
var processor = audioCtx.createScriptProcessor(2048, 1, 1);
var meter = document.getElementById('meter');
var source;

audio.addEventListener('canplaythrough', function(){
source = audioCtx.createMediaElementSource(audio);
source.connect(processor);
source.connect(audioCtx.destination);
processor.connect(audioCtx.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 );
//console.log(( rms * 100 ));
};
Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24