我正在尝试使用 Web Audio API 查找音频时刻的强度。我在规范中找到的唯一与强度相关的是:
analyser.minDecibels
analyser.maxDecibels
有办法吗?
最佳答案
如果我没理解错的话,你想要一个在声音大的时候高,在声音小的时候低的数字。您可以为此使用“声压级”。
从 Web Audio 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);
关于javascript - 在 Web Audio API 中导出音频强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22104036/