Changes

Jump to: navigation, search

Audio Data API

26 bytes added, 23:16, 23 September 2010
Writing Audio
<pre>
// Get current audible position of the underlying audio stream, measured in samples available.
var currentSampleOffset = audioOutput.mozCurrentSampleOffset();
</pre>
function audioAvailable(event) {
// Write the current framebuffer
var frameBuffer = event.mozFrameBufferframeBuffer;
writeAudio(frameBuffer);
}
</pre>
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 the sample offset of that is currently being played by the 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, such that the sound is played without interruptions, and lag between writing and playback is minimal. To do this start writing the data in small portions and wait for the value returned by '''mozCurrentSampleOffset()''' to be more than 0.
50
edits

Navigation menu