2014-02-28 86 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);