【问题标题】:Web Audio OfflineAudioContext syntax error when I follow the API当我遵循 API 时,Web Audio OfflineAudioContext 语法错误
【发布时间】:2013-06-23 10:27:22
【问题描述】:

当我尝试执行以下 javascript 行时,Chrome 27 beta 会引发 SyntatxError:

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

这符合 W3C 建议,经过数小时的网络搜索后,我看到了几十个显然适用于这种确切形式的示例。我知道 API 可能会发生变化,但我找不到任何提示。我哪里错了?

奇怪的是,当我在 jsfiddle 中尝试时它并没有失败。我什至尝试在 jsfiddle 中运行这个 sn-p 存在的整个函数,删除几个项目。下面是我试图运行的功能。当函数执行时,会出现“SyntaxError Dom Exception 12”。

我有一个可以工作的音频应用程序,我只是想添加后台处理,以便以后使用 OfflineAudioContext 进行渲染。如果我在我的 init 函数中定义 OfflineAudoiContext,我不会在那里得到语法错误。但是我需要在 createRenderData() 函数中定义它,因为我需要为每个媒体源设置长度和采样率,如果我不带参数调用 OfflineAudioContext 构造函数,我会得到一个 TypeError。此外,我在 API 中看不到任何方法来设置现有 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");
    }
};

【问题讨论】:

  • 更正:当我在 init 函数中调用构造函数时,我也会收到 SyntaxError。以前一定错过了。
  • 您是否有机会使用 Windows 计算机?它适用于我的 Mac。也许他们还没有为 Win/Chrome 实现它。
  • 它在 OSX 上失败了,但在 CentOs(Chrome 的 Fedora 发行版)上没有。但是,我发现了问题所在。事实证明,OfflineAudioContext 的采样率必须与常规 AudioContext 的设置相同。

标签: javascript web-audio-api


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    只是让您知道,将此段输入 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 有效,但是the w3c specification

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2021-09-06
      • 2017-08-10
      • 2018-09-17
      • 2012-05-26
      相关资源
      最近更新 更多