Re: Setting a sample rate in the web audio API

Hi Grant,

I should probably clarify the sample-rate behavior in my specification for
both the AudioContext and AudioBuffer / AudioBufferSourceNode.  I don't
really provide enough detail there and can see how it would be confusing.

For now I can describe here a little more about sample-rates.  First of all,
all AudioNodes run at the same sample rate which is the sample-rate of the
AudioContext (see the "sampleRate" attribute in AudioContext).  The
"sampleRate" attribute of an AudioBuffer represents the sample-rate for a
specific "clip" or "portion" of audio.  When a buffer is played back with
the noteOn() method of an AudioBufferSource then the buffer's sample-rate
will be factored into the overall pitch (resampled to the sample-rate of the
AudioContext).  But this does *not* change the sample-rate of any AudioNode.
 This, I think, is the misunderstanding.

Chris

On Sat, Feb 12, 2011 at 4:31 PM, Grant Galitz <grantgalitz@gmail.com> wrote:

>
>
> ---------- Forwarded message ----------
> From: Grant Galitz <grantgalitz@gmail.com>
> Date: Sat, Feb 12, 2011 at 7:28 PM
> Subject: Re: Setting a sample rate in the web audio API
> To: Chris Rogers <crogers@google.com <crogers@google..com>>
>
>
> I'd think that the buffer size in audioSource would not matter, because I
> do drop it, but the rate at which my event handler for my onaudioprocess
> does not sync up with the sample rate set for the audioSource's set sample
> rate, but I believe it should. Even if I was ignoring the "input", the false
> input should still be feed at the correct speed, of which the event handler
> is obviously called at the wrong rate for refilling (timed against the
> audioContext and not the false buffer I created, which seems wrong since I
> connected the false buffer to the javascript node.).
>
>
> On Sat, Feb 12, 2011 at 6:20 PM, Chris Rogers <crogers@google.com> wrote:
>
>> Hi Grant,
>>
>> With this line:
>> audioSource.buffer = audioContextHandle.createBuffer(1, 1, settings[14]); //Create
>> a zero'd input buffer for the input to be valid.
>>
>>
>> You're creating a buffer whose length is only 1 sample-frame (the second
>> argument) which is pretty short!  And from what I understand, you're
>> ignoring the "audioSource" input.  The sample-rate *is* taken into account
>> in the "audioSource", it's just that you're ignoring what it's giving you
>> and generating your own output stream.  The sample-rate of the AudioContext
>> itself is fixed and is usually going to be the sample-rate of the audio
>> hardware (such as 44.1KHz).
>>
>> Chris
>>
>> On Fri, Feb 11, 2011 at 12:41 PM, Grant Galitz <grantgalitz@gmail.com>wrote:
>>
>>> Hey, you probably already know my JS GameBoy Color emulator project
>>> https://github.com/grantgalitz/GameBoy-Online
>>> The thing is, I'm wondering why the web audio API refuses to allow me to
>>> set my own sample rate (I want 70khz). Right now I have to set the sample
>>> rate to that already found in the API itself:
>>>  audioSource = audioContextHandle.createBufferSource(); //We need to
>>> create a false input to get the chain started.
>>>  audioSource.loop = false; //Keep this alive forever (Event handler will
>>> know when to ouput.)
>>>  settings[14] = audioContextHandle.sampleRate;
>>>  audioSource.buffer = audioContextHandle.createBuffer(1, 1, settings[14
>>> ]); //Create a zero'd input buffer for the input to be valid.
>>>  audioNode = audioContextHandle.createJavaScriptNode(settings[18], 1, 2
>>> ); //Create 2 outputs and ignore the input buffer (Just copy buffer 1
>>> over if mono)
>>>  audioNode..onaudioprocess = audioOutputEvent; //Connect the audio
>>> processing event to a handling function so we can manipulate output
>>>  audioSource.connect(audioNode); //Send and chain the input to the audio
>>> manipulation.
>>>  audioNode..connect(audioContextHandle.destination); //Send and chain
>>> the output of the audio manipulation to the system audio output.
>>>  audioSource.noteOn(0);
>>>
>>> settings[14] being the sample rate 70000.
>>> I need to do this to get it to work:
>>> settings[14] = audioContextHandle.sampleRate;
>>> Which effectively forces me to use the API's arbitrary sample rate
>>> instead of one I want to use.
>>>
>>>
>>
>
>

Received on Sunday, 13 February 2011 00:46:26 UTC