为什么 decodeAudioData() 结果在不同 OS 上不同
Why are decodeAudioData() results different on different OS
更新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>
更新:提交了一个问题here。
我想解码一个音频文件并获取其原始数据,以便在接下来的步骤中进行处理。想法是使用 Web Audio API 的 decodeAudioData()。奇怪的问题是解码结果将取决于操作系统。有没有人遇到同样的问题,或者知道原因?
我做了一个快速 demo 并在 Mac OS (10.11) 和 Windows 7 上测试了它,浏览器都是 Chrome (v46 .0+),但他们的结果不同。要可视化结果:
每个人都可以 运行 这个 demo,只需单击 "LOAD" 按钮,结果就会记录在控制台中。
Windows 和 Chrome 应该没有区别。在 crbug.com/new.
提交错误
终于发现OfflineAudioContext可以为以后的decodeAudioData()
设置采样率了。要以 44.1kHz 采样率解码 30 秒长的立体声音频,请创建如下所示的 offlineAudioContext
,并且不要忘记 Safari 的 webkit
前缀。
window.OfflineAudioContext = window.offlineAudioContext || window.webkitOfflineAudioContext;
var offlineAudioContext = new OfflineAudioContext(2, 44100 * 30, 44100);
更新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>
更新:提交了一个问题here。
我想解码一个音频文件并获取其原始数据,以便在接下来的步骤中进行处理。想法是使用 Web Audio API 的 decodeAudioData()。奇怪的问题是解码结果将取决于操作系统。有没有人遇到同样的问题,或者知道原因?
我做了一个快速 demo 并在 Mac OS (10.11) 和 Windows 7 上测试了它,浏览器都是 Chrome (v46 .0+),但他们的结果不同。要可视化结果:
每个人都可以 运行 这个 demo,只需单击 "LOAD" 按钮,结果就会记录在控制台中。
Windows 和 Chrome 应该没有区别。在 crbug.com/new.
提交错误终于发现OfflineAudioContext可以为以后的decodeAudioData()
设置采样率了。要以 44.1kHz 采样率解码 30 秒长的立体声音频,请创建如下所示的 offlineAudioContext
,并且不要忘记 Safari 的 webkit
前缀。
window.OfflineAudioContext = window.offlineAudioContext || window.webkitOfflineAudioContext;
var offlineAudioContext = new OfflineAudioContext(2, 44100 * 30, 44100);