如何在 Javascript 中获取音频的分贝级别
How do you get the decibel level of an audio in Javascript
我目前正在使用JavaScript,HTML和CSS制作分贝计可视化工具。
我已经浏览了几个 Web 音频 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 ));
};
相关文章:
- 如何通过给定的url获取youtube音频数据
- 获取 HTML 5 音频控制文件位置并在不使用 id 选择器的情况下更新 src
- 使用Cordova和JavaScript(无需getUserMedia)从android麦克风获取音频数据
- 从PHP脚本获取音频文件时,更改JavaScript音频对象的当前时间
- 如何获取音频录制的src
- html5音频.如何获取比特率
- Node.js:获取一个mp4文件的视频和音频格式信息
- 如何在 Javascript 中获取音频的分贝级别
- 从音频元素获取音频样本
- HTML5 音频 - 获取声音对象的播放时间(howler.js)
- 尝试通过javascript从音频文件中获取分贝级别
- 可以't获取音频文件
- 如何使用AudioContext HTML5从麦克风获取音频数据
- "获取音频时出错“;使用Recorder.js录制音频
- 如何从视频元素中获取频率数据,并实时输入音频API
- 获取要使用getdocumenttag播放的音频
- 从麦克风获取音频数据作为数组
- 使用Web audio API立即从音频文件中获取频率数据
- 通过Ajax获取音频文件
- 通过获取.attr,使用JavaScript / jquery播放/暂停音频文件