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

试图通过javascript从音频文件中获取分贝级别,javascript,json,audio,html5-audio,web-audio-api,Javascript,Json,Audio,Html5 Audio,Web Audio Api,我一直在看这里的信息: 但是当我尝试在这里运行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="t

我一直在看这里的信息:

但是当我尝试在这里运行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) + '%';
    };
};

获取音频数据
#仪表{
宽度:500px;
高度:15px;
边际:2px0;
背景:绿色;
-webkit转换;
宽度0.05s;
}
函数(){
var ctx=新的webkitAudioContext(),
url='test.mp3',
音频=新音频(url),
processor=ctx.createJavaScriptNode(2048,1,1),
meter=document.getElementById('meter'),
来源;
addEventListener('canplaythrough',function(){
source=ctx.createMediaElementSource(音频);
source.connect(处理器);
source.connect(ctx.destination);
处理器连接(ctx目的地);
音频播放();
},假);
processor.onaudioprocess=函数(evt){
var input=evt.inputBuffer.getChannelData(0),
len=输入长度,
总计=i=0,
rms;

而(i一旦替换了失效的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");
}


一旦替换了失效的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");
}


一旦替换了失效的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");
}


一旦替换了失效的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");
}


我不是在网上到处寻找你的代码。请看一看,点击一个链接并不真正构成在网上到处寻找…但我会编辑这个问题。你是在要求别人解释代码在做什么,还是你希望它应该做一些不应该做的事?你的问题不是很清楚。我不是在网上到处寻找你的代码。请看一看,点击一个链接并不真正构成在网上到处寻找…但我会编辑这个问题。你是在要求别人解释代码在做什么,还是你希望它应该做一些不该做的事?你的问题不是很清楚r、 我不是在网上到处寻找你的代码。请看一看,点击一个链接并不真正构成在网上到处寻找…但我会编辑这个问题。你是在要求别人解释代码在做什么,还是你希望它应该做一些不该做的事?你的问题不是很简单答:我不是在互联网上到处寻找你的代码。请看一下,点击一个链接并不真正构成在互联网上到处寻找……但我会编辑这个问题。你是在要求别人解释代码在做什么,还是你希望它应该做一些不应该做的事情?你的问题不是很清楚耳朵