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

标签 javascript json audio html5-audio web-audio-api

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

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/

相关文章:

javascript - 如何将事件附加到 Bootstrap 折叠面板的选择?

javascript - React 中未定义渲染 json

php - MySQL JSON 数据类型是否不利于数据检索的性能?

android - 带有额外变量的 gson 数组反序列化

javascript - React hooks map 不是一个函数

java - ArrayList 排序时出现 NullPointer

audio - .wav信号的音频分离

audio - ffmpeg 音频编码器帧大小

c - 使用 libswresample 将音频从 48000 重新采样到 44100

javascript - 使用 Array.reduce() 从获取 promise 中提取 JSON 数据