如何在Javascript中获得音频的分贝级别

时间:2015-01-24 08:02:24

标签: javascript audio html5-audio decibel

我目前正在使用JavaScript,HTML和CSS制作分贝计可视化工具。

我已经阅读了几个Web Audio API教程,但没有任何内容与我想做的事情有关。

这是我到目前为止所做的:

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 ) + '%';
  };

}

有人可以解释我需要做什么,或者指出我正确的方向,因为这似乎不起作用?


修正

休息一天后,我发现我正在制作的一些API调用已被弃用,所以我将其更改为:

  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 ));
  };

2 个答案:

答案 0 :(得分:1)

已修复

所以这就是我将其更改为:

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 ));
};

答案 1 :(得分:0)

OP在问题中回答了这个问题。

  

休息一天后,我发现不赞成使用某些API调用,因此将其更改为: