Changes

Jump to: navigation, search

Audio Data API

646 bytes added, 01:55, 11 September 2010
Writing Audio
Audio data written using the '''mozWriteAudio()''' method needs to be written at a regular interval in equal portions, in order to keep a little ahead of the current sample offset (current sample offset of hardware can be obtained with '''mozCurrentSampleOffset()'''), where a little means something on the order of 500ms of samples. For example, if working with 2 channels at 44100 samples per second, a writing interval of 100ms, and a pre-buffer equal to 500ms, one would write an array of (2 * 44100 / 10) = 8820 samples, and a total of (currentSampleOffset + 2 * 44100 / 2).
 
It's also possible to auto detect the minimal duration of the pre-buffer, so the sound is playing without the "clicks" and lag between writing and playback is minimal. To do that, start writing the data in small portions and wait when the '''mozCurrentSampleOffset()''' will be more than 0.
 
<pre>
var prebufferSize = sampleRate * 0.020; // initial 20 ms
var autoLatency = true, started = new Date().valueOf();
...
// Auto latency detection
if (autoLatency) {
prebufferSize = Math.floor(sampleRate * (new Date().valueOf() - started) / 1000);
if (audio.mozCurrentSampleOffset()) { // play position moved
autoLatency = false;
}
}
</pre>
===== Complete Example: Creating a Web Based Tone Generator =====
50
edits

Navigation menu