2014-02-28 63 views
1

音频的强度,我试图找到与网络音频API音频的时刻的强度。唯一的东西连接强度,我在规格中发现是:导出在网络音频API

analyser.minDecibels 
analyser.maxDecibels 

有没有办法做到这一点?

回答

3

如果我理解正确的话,你想一个数字,高时声音洪亮,低时声音安静。你可以使用“声压级”。

充分利用网络音频API这个数量是相当简单的,你已经正确地猜到,我们将使用AnalyserNode实现这一目标。这里是一个示例代码,告诉您如何做到这一点:

var ac = new AudioContext(); 
/* create the Web Audio graph, let's assume we have sound coming out of the 
* node `source` */ 
var an = ac.createAnalyser(); 
source.connect(an); 
/* Get an array that will hold our values */ 
var buffer = new Uint8Array(an.fftSize); 

function f() { 
    /* note that getFloatTimeDomainData will be available in the near future, 
    * if needed. */ 
    an.getByteTimeDomainData(buffer); 
    /* RMS stands for Root Mean Square, basically the root square of the 
    * average of the square of each value. */ 
    var rms = 0; 
    for (var i = 0; i < buffer.length; i++) { 
    rms += buffer[i] * buffer[i]; 
    } 
    rms /= buffer.length; 
    rms = Math.sqrt(rms); 
    /* rms now has the value we want. */ 
    requestAnimationFrame(f); 
} 

requestAnimationFrame(f); 
/* start our hypothetical source. */ 
source.start(0);