admin管理员组文章数量:1384952
I'm trying to find the intensity of a moment of audio with the Web Audio API. The only things which connect to intensity which I found in the specification are the:
analyser.minDecibels
analyser.maxDecibels
Is there a way to do this?
I'm trying to find the intensity of a moment of audio with the Web Audio API. The only things which connect to intensity which I found in the specification are the:
analyser.minDecibels
analyser.maxDecibels
Is there a way to do this?
Share Improve this question edited Mar 5, 2014 at 21:05 JohnB 1,2211 gold badge18 silver badges33 bronze badges asked Feb 28, 2014 at 19:21 bhc11bhc11 1691 gold badge2 silver badges14 bronze badges2 Answers
Reset to default 8If I understand correctly, you want a number that is high when the sound is loud, and low when the sound is quiet. You can use the "Sound Pressure Level" for that.
Getting this number from the Web Audio API is rather straightforward, and you had guessed correctly that we will use the AnalyserNode to achieve this. Here is a example code that shows you how to do it:
var ac = new AudioContext();
/* create the Web Audio graph, let's assume we have sound ing 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);
I wanted to thank you for this answer some 4 years later. I just did a quick POC and got it to work with the following code. I hope it might help somebody else too.
In this example, I am taking the live audio from my Mic and logging the results to the console - in my case, under the chrome dev tools.
<html>
<head>
<title>Intensity test</title>
</head>
<body>
<script>
var ac = new AudioContext();
var an = ac.createAnalyser();
var source = "";
var buffer = new Uint8Array(an.fftSize);
var scriptProcessorNode = ac.createScriptProcessor(16384, 1, 1);
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia(
{audio:true},
function(stream) {
source = ac.createMediaStreamSource(stream);
source.connect(an);
requestAnimationFrame(f);
},
function(e) {
alert('Error capturing audio.');
}
);
}
function f() {
an.getByteTimeDomainData(buffer);
var rms = 0;
for (var i = 0; i < buffer.length; i++)
rms += buffer[i] * buffer[i];
rms /= buffer.length;
rms = Math.sqrt(rms);
requestAnimationFrame(f);
console.log(rms);
}
</script>
</body>
</html>
本文标签: javascriptExporting intensity of audio in Web Audio APIStack Overflow
版权声明:本文标题:javascript - Exporting intensity of audio in Web Audio API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744538831a2611483.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论