Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Audio OfflineAudioContext syntax error when I follow the API

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");
    }
};
like image 910
MidnightJava Avatar asked May 09 '13 04:05

MidnightJava


2 Answers

So the problem was that the sample rate for the OfflineAudioContext needs to be set to the same value as that of the regular AudioContext. Chris Rogers is going to look into fixing this, which occurred for me with OSX Chrome (beta and stable channels), but not for the Fedora Chrome running on CentOS (tested stable channel only). But with the two audio contexts set to the same sample rate, everything is working fine now.

like image 84
MidnightJava Avatar answered Oct 07 '22 01:10

MidnightJava


Just to let you know, entering this segment into the debug console in Chrome (preferably while on about:blank so as not to lose anything) will output all of the valid whole number values for bitrate.

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");

It seems that only 44100 is valid right now, but the w3c specification says that

The sampleRate parameter describes the sample-rate of the linear PCM audio data in the buffer in sample-frames per second. An implementation must support sample-rates in at least the range 22050 to 96000.

I wonder if this has been submitted to Webkit/Chrome as a big bad bug...

like image 21
user2350838 Avatar answered Oct 07 '22 01:10

user2350838