Changes

Jump to: navigation, search

Audio Data API

6 bytes added, 14:24, 2 June 2010
nsIDOMHTMLAudioElement additions
unsigned long mozWriteAudio(array); // array is Array() or Float32Array()
void unsigned long long mozCurrentSampleOffset();
void mozSetFramebufferSize(size); // size is must be a power of 2 between 512 and 32768
</pre>
# '''volume''' - the initial volume to use (e.g., 1.0)
The choices made for '''channel''' and '''rate''' are significant, because they determine the frame size amount of data you must use when passing data pass to '''mozWriteAudio()'''. That is, you must pass either pass an array with 0 elements--similar to flushing the audio stream--or enough data for each channel specified in '''mozSetup()'''.
The '''mozSetup()''' method, if called more than once, will recreate a new audio stream (destroying an existing one if present) with each call. Thus it is safe to call this more than once, but unnecessary.
The '''mozWriteAudio()''' method can be called after '''mozSetup()'''. It allows audio data to be written directly from script. It takes one argument:
# '''array''' - this is a JS Array (i.e., new Array()) or a typed float array (i.e., new Float32Array()) containing the audio data (floats) you wish to write. It must be 0 or N (elements in length, where N % channels == 0) elements in length, otherwise a DOM error occurs.
The '''mozWriteAudio()''' method returns the number of samples that were just written, which may or may not be the same as the number in '''array'''. Only the number of samples that can be written without blocking the audio hardware will be written. It is the responsibility of the caller to deal with any samples that don't get written in the first pass (e.g., buffer and write in the next call).
The '''mozCurrentSampleOffset()''' method can be called after '''mozSetup()'''. It returns the current position (measured in samples) of the audio stream. This is useful when determining how much data to write with '''mozWriteAudio()'''.
The '''mozSetFrameBufferSize()''' is used to change the default framebuffer size for '''AudioWritten''' events. By default, this value will be 1024 * channels (e.g., 2048 for 2 channels). You can give another size if you need lower latency, or larger amounts of data, etc. The size you give '''must''' be a power of 2 between 512 and 32768. The following are all valid:  * 512, * 1024, * 2048, * 4096, * 8192, * 16384, and * 32768.  Using any other size will result in an exception being thrown. The best time to call '''mozSetFrameBufferSize()''' is in the '''loadedmetadata''' event, when the audio info is known, but before the audio has started or events begun firing.
All of '''mozWriteAudio()''', '''mozCurrentSampleOffset()''', and '''mozSetup()''' will throw exceptions if called out of order.
Confirm
656
edits

Navigation menu