gpt4 book ai didi

javascript - 你如何在 Javascript 中获取音频的分贝级别

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:43:32 29 4
gpt4 key购买 nike

我目前正在使用 JavaScript、HTML 和 CSS 制作一个分贝计可视化工具。

我已经阅读了多个 Web Audio API 教程,但其中没有任何内容接近于我想做的事情。

这是我目前所拥有的:

window.onload = init;

function init() {

var ctx = new webkitAudioContext()
, url = 'https://dl.dropboxusercontent.com/u/86176287/pbjt.mp3'
, audio = new Audio(url)
// 2048 sample buffer, 1 channel in, 1 channel out
, 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);

// loop through PCM data and calculate average
// volume for a given 2048 sample buffer
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 ) + '%';
};

}

谁能解释一下我需要做什么,或者指出正确的方向,因为这似乎不起作用?

最佳答案

固定

所以我把它改成了:

var audioCtx = new AudioContext();
var url = 'https://dl.dropboxusercontent.com/u/86176287/pbjt.mp3';
var audio = new Audio(url);
var processor = audioCtx.createScriptProcessor(2048, 1, 1);
var meter = document.getElementById('meter');
var source;

audio.addEventListener('canplaythrough', function(){
source = audioCtx.createMediaElementSource(audio);
source.connect(processor);
source.connect(audioCtx.destination);
processor.connect(audioCtx.destination);
//audio.play();
}, false);

// loop through PCM data and calculate average
// volume for a given 2048 sample buffer
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 );
//console.log(( rms * 100 ));
};

关于javascript - 你如何在 Javascript 中获取音频的分贝级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123525/

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