2014-11-21 62 views
0

从音频文件获得分贝的水平,我一直在这里看着的信息:试图通过JavaScript

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) + '%'; 
    }; 
}; 

我不确定它在做什么。任何人都可以给我更多的信息?

+0

我不追一轮互联网寻找你的代码。请查看[问] – 2014-11-21 21:41:05

+0

单击一个链接并不真正构成追逐互联网......但我会编辑这个问题。 – Sonofblip 2014-11-21 21:52:33

+0

你只是要求某人解释代码正在做什么,或者你是否期望它应该做一些不是的事情?你的问题不是很清楚。 – jaket 2014-11-21 22:07:31

回答

3

您的代码不会产生RMS输出,一旦你更换解散的API调用

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