Changes

Jump to: navigation, search

Audio Data API

1,041 bytes added, 15:41, 30 May 2010
no edit summary
* Changed '''mozWriteAudio()''' to only write as much as can be written to the audio hardware without blocking, and to return the number of samples written.
* Native array interfaces instead of using accessors and IDL array arguments.
* No zero padding of audio data occurs anymore. All frames are exactly 4096 channels * 1024 elements in length.
* Added '''mozCurrentSampleOffset()'''
* Removed undocumented position/buffer methods on audio element.
* Added '''mozChannels''', '''mozRatemozSampleRate''', '''mozFrameBufferLength''' to '''loadedmetadata' event.* Added '''mozSetFrameBufferSize()''' method.
Demos written for the previous version are '''not''' compatible, though can be made to be quite easily. See details below.
function audioInfo(event) {
channels = event.mozChannels;
rate = event.mozRatemozSampleRate;
frameBufferLength = event.mozFrameBufferLength;
}
function loadedMetadata(event) {
var channels = event.mozChannels,
rate = event.mozRatemozSampleRate,
frameBufferLength = event.mozFrameBufferLength;
a1.volume = 0;
// Setup a2 to be identical to a1, and play through there.
a2.mozSetup(event.mozChannels, event.mozRatemozSampleRate, 1);
}
{
readonly attribute unsigned long mozChannels;
readonly attribute unsigned long mozRatemozSampleRate;
readonly attribute unsigned long mozFrameBufferLength;
};
</pre>
The '''mozChannels''' attribute contains a the number of channels in this audio resource (e.g., 2). The '''mozRatemozSampleRate''' attribute contains the number of samples per second that will be played, for example 44100. The '''mozFrameBufferLength''' attribute contains the number of samples that will be returned in each '''AudioWritten''' event. This number is a total for all channels , and by default is set to be the number of channels * 1024 (e.g., 2 channels * 2048 1024 samples = 4096 2048 total). You can change this size using the '''mozSetFrameBufferSize()''' method to be another power of 2 between 512 and 32768 (see details below).
===== nsIDOMNotifyAudioWrittenEvent =====
</pre>
The '''mozFrameBuffer''' attribute contains a typed array ('''Float32Array''') and the raw audio data (float values) obtained from decoding a single frame of audio. This is of the form <nowiki>[left, right, left, right, ...]</nowiki>. All audio frames are normalized to a length of channels * 1024 by default, but could be any power of 2 between 512 and 32768 if the user has set a different size using '''4096'''. mozSetFrameBufferSize()''Note:'' this size may change in future versions of this API in order to more properly deal with sample rate and channel variations.
The '''mozTime''' attribute contains an unsigned integer representing the time in milliseconds since the start.
void mozCurrentSampleOffset();
 
void mozSetFramebufferSize(size); // size is power of 2 between 512 and 32768
</pre>
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