5

Chrome 27 beta throws a SyntatxError when I try to execute the following javascript line:

offlineContext = new webkitOfflineAudioContext(2, 7*48000, 48000);

This is in conformance with the W3C Recommendation, and after hours of searching the web I've seen dozens of examples that apparently work with this exact form. I know the API can be subject to change, but I can't find any hint that it has. Where am I going wrong?

Strangely, it doesn't fail when I try it in jsfiddle. I even tried running in jsfiddle the entire function in which this snippet exists, stubbing out a couple items. Below is the function that I'm trying to run. A "SyntaxError Dom Exception 12" occurs where indicated when the function is executed.

I have a working audio app, and I'm just trying to add background processing for later rendering using an OfflineAudioContext. If I define the OfflineAudoiContext in my init function, I don't get the syntax error there. But I need to define it in the createRenderData() function because I need to set the length and sample rate for each media source, and if I call the OfflineAudioContext constructor with no arguments, I get a TypeError. Also I don't see any way in the API to set the length and sample rate on an existing OfflineAudioContext.

AudioUI.createRenderData = function() {
    var url = BGAudioUI.renderQueue.pop();
    if (url) {
        BGAudioUI.media = new Audio(url);
        BGAudioUI.media.addEventListener('durationchange',  function() {
            console.log("background duration: " + BGAudioUI.media.duration);
            BGAudioUI.duration = BGAudioUI.media.duration;
            //BGAudioUI.allData = new Float32Array(Math.floor((AudioUI.context.sampleRate.toFixed() / AudioUI.downsampleFactor.toFixed() * BGAudioUI.duration)) + 1);
            //console.log("calculated sample rate: " + Math.floor(AudioUI.context.sampleRate / AudioUI.downsampleFactor.toFixed()));
            //console.log("duration: " + BGAudioUI.duration);
            BGAudioUI.offlineContext = new webkitOfflineAudioContext(2, 7*48000, 48000); //SyntaxError DOM Exception 12 occurs here
            BGAudioUI.mediaSource = BGAudioUI.offlineContext.createMediaElementSource(BGAudioUI.media);
            BGAudioUI.offlineContext.oncomplete = function(buffer) {
                data = buffer.getChannelData(0);
                console.log(data.length + " records processed offline");
            };
            BGAudioUI.offlineContext.startRendering();
            BGAudioUI.media.play();
            console.log("created background media source for " + BGAudioUI.media.src);
        }, false);
        BGAudioUI.media.load();
        console.log("created background media object for " + BGAudioUI.media.src);
    } else {
        alert("Background rendering complete");
    }
};
4

2 回答 2

4

所以问题是 OfflineAudioContext 的采样率需要设置为与常规 AudioContext 相同的值。Chris Rogers 将着手解决这个问题,这发生在我使用 OSX Chrome(测试版和稳定通道)时,但不适用于在 CentOS 上运行的 Fedora Chrome(仅经过测试的稳定通道)。但是将两个音频上下文设置为相同的采样率,现在一切正常。

于 2013-05-10T03:46:06.353 回答
1

只是让您知道,将此段输入 Chrome 中的调试控制台(最好在 about:blank 上,以免丢失任何内容)将输出比特率的所有有效整数值。

document.querySelector('body').innerHTML = "";
var b, sampleRate;
for(sampleRate = 22050;
sampleRate <= 96000; //22050 to 96000 is the minimum support according to the w3c working draft
 ++sampleRate){
var b = true;
try{var l = new webkitOfflineAudioContext(1,sampleRate, sampleRate);}
catch(e){b = false}
if(b){document.writeln(sampleRate + " is a good sample rate.");}} console.log("Complete");

现在似乎只有 44100 有效,但是w3c 规范

sampleRate 参数描述了缓冲区中线性 PCM 音频数据的采样率,以每秒采样帧数为单位。实现必须支持至少 22050 到 96000 范围内的采样率。

我想知道这是否已作为一个大错误提交给 Webkit/Chrome...

于 2013-06-23T02:06:10.570 回答