为什么不同操作系统上的decodeAudioData()结果不同

问题描述 投票:0回答:3

更新 2: 在此处运行脚本。单击“LOAD”按钮时会显示采样率。

function decode() {
    // If the default naming is not enabled, use the Chrome one
    if (!window.AudioContext) {
        if (!window.webkitAudioContext) {
            alert("No AudioContext found.");
        }
        window.AudioContext = window.webkitAudioContext;
    }
    var audioContext = new AudioContext();
    alert("sample rate: " + audioContext.sampleRate);
    var sourceNode;

    var BUFFER_SIZE = 1024;

    // Create buffer node and download file to buffer
    createAudioNodes();
    var fileURL = "http://plaay.in/decode-audio-data/decode-this-audio.wav";
    downloadAudioFromURL(fileURL);

    function createAudioNodes() {
        sourceNode = audioContext.createBufferSource();
        sourceNode.connect(audioContext.destination);
    }

    function downloadAudioFromURL(url) {
        var request = new XMLHttpRequest();
        request.open("GET", url, true);
        request.responseType = "arraybuffer";
        request.addEventListener("progress", updateProgress);

        request.onload = function() {
            audioContext.decodeAudioData(request.response, function(buffer) {
                setSourceBuffer(buffer);
            }, onError);
        };
        request.send();
    }

    function updateProgress(progress) {
        if (progress.lengthComputable) {
            var percentComplete = Math.round(progress.loaded / progress.total * 100);
            document.getElementById("progress").innerHTML = "Loading... " + percentComplete.toString() + "%";
        }
    }

    function setSourceBuffer(buffer) {
        var leftInBuffer = buffer.getChannelData(0);
        console.log("leftInBuffer length: " + leftInBuffer.length);
        for (var i = 0; i < leftInBuffer.length; i++) {
            console.log(leftInBuffer[i]);
        }
    }

    function onError(e) {
        console.log(e);
    }
}
<!doctype html>
<html lang="en-US">

<head>
    <meta charset="utf-8">
    <title>Decode Audio Data</title>
</head>

<body>
    <script src="decode.js"></script>
    <button id="playButton" type="button" style="height:40px;width:150px" onclick="decode()">LOAD</button>
    <p id="progress">File download progress</p>
</body>

</html>

更新:在此处提出问题。

我想解码音频文件并获取其原始数据以便在下一步中进行处理。这个想法是使用 Web Audio API 的decodeAudioData()。奇怪的问题是解码结果将取决于操作系统。有没有人遇到过同样的问题,或者知道原因吗?

我做了一个快速的demo并在Mac OS(10.11)和Windows 7上进行了测试,浏览器都是Chrome(v46.0+),但它们的结果是不同的。可视化结果:

每个人都可以运行这个demo,只需点击“LOAD”按钮,结果就会记录在控制台中。

javascript html google-chrome operating-system web-audio-api
3个回答
2
投票

Windows 和 Chrome 之间应该没有什么不同。在 crbug.com/new.

提交错误

1
投票

最后我发现OfflineAudioContext可以为我以后的

decodeAudioData()
设置采样率。要以 44.1kHz 采样率解码 30 秒长的立体声音频,请创建如下所示的
offlineAudioContext
,并且不要忘记 Safari 的
webkit
前缀。

window.OfflineAudioContext = window.offlineAudioContext || window.webkitOfflineAudioContext;
var offlineAudioContext = new OfflineAudioContext(2, 44100 * 30, 44100); 

0
投票

每个浏览器/操作系统组合都可以使用不同的解码器库,并且由于标准的模糊性,每个解码器的结果可能略有不同。这在过去也曾困扰过我,但我必须忍受。或者使用未压缩的格式!

© www.soinside.com 2019 - 2024. All rights reserved.