gpt4 book ai didi

javascript - 尝试通过javascript从音频文件中获取分贝级别

转载 作者:行者123 更新时间:2023-11-28 19:29:51 25 4
gpt4 key购买 nike

我一直在查看这里的信息:

Is there a way get something like decibel levels from an audio file and transform that information into a json array?

但是当我尝试在这里运行 JSBin 时:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Get Audio Data</title>
<link rel="stylesheet" type="text/css" href="index.css">
<script type="text/javascript" src="music.js"></script>
</head>
<body>
<div id=meter></div>
</body>
</html>

#meter {
width: 500px;
height: 15px;
margin: 2px 0;
background: green;
-webkit-transition;
width .05s;
}

function() {
var ctx = new webkitAudioContext(),
url = 'test.mp3',
audio = new Audio(url),
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);

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 调用,您的代码就会生成 RMS 输出

old    webkitAudioContext
new


try {

window.AudioContext = window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext;

ctx = new AudioContext();

console.log("cool audio context established ... ctx ");

} catch (e) {

alert("Web Audio API is not supported by this browser\n ... http://caniuse.com/#feat=audio-api");
}

old    createJavaScriptNode
new createScriptProcessor

关于javascript - 尝试通过javascript从音频文件中获取分贝级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27070807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com