19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.media;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Triviimport android.annotation.IntDef;
20a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hungimport android.annotation.NonNull;
215affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Triviimport android.os.Parcel;
225affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Triviimport android.os.Parcelable;
2304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung
24f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Triviimport java.lang.annotation.Retention;
25f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Triviimport java.lang.annotation.RetentionPolicy;
26cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurentimport java.util.Arrays;
275affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Triviimport java.util.Objects;
28f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
3004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * The {@link AudioFormat} class is used to access a number of audio format and
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * channel configuration constants. They are for instance used
324ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * in {@link AudioTrack} and {@link AudioRecord}, as valid values in individual parameters of
334ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * constructors like {@link AudioTrack#AudioTrack(int, int, int, int, int, int)}, where the fourth
344ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * parameter is one of the <code>AudioFormat.ENCODING_*</code> constants.
3504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * The <code>AudioFormat</code> constants are also used in {@link MediaFormat} to specify
3604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * audio related values commonly used in media, such as for {@link MediaFormat#KEY_CHANNEL_MASK}.
374ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <p>The {@link AudioFormat.Builder} class can be used to create instances of
384ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * the <code>AudioFormat</code> format class.
394ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * Refer to
404ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link AudioFormat.Builder} for documentation on the mechanics of the configuration and building
414ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * of such instances. Here we describe the main concepts that the <code>AudioFormat</code> class
424ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * allow you to convey in each instance, they are:
434ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <ol>
444ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><a href="#sampleRate">sample rate</a>
454ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><a href="#encoding">encoding</a>
464ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><a href="#channelMask">channel masks</a>
474ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * </ol>
4804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>Closely associated with the <code>AudioFormat</code> is the notion of an
4904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <a href="#audioFrame">audio frame</a>, which is used throughout the documentation
5004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * to represent the minimum size complete unit of audio data.
51ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent *
524ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h4 id="sampleRate">Sample rate</h4>
534ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <p>Expressed in Hz, the sample rate in an <code>AudioFormat</code> instance expresses the number
544ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * of audio samples for each channel per second in the content you are playing or recording. It is
554ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * not the sample rate
564ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * at which content is rendered or produced. For instance a sound at a media sample rate of 8000Hz
574ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * can be played on a device operating at a sample rate of 48000Hz; the sample rate conversion is
584ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * automatically handled by the platform, it will not play at 6x speed.
594ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *
600e3de6cacaffcfeda4d6353be61e2f1f9ed80705Dianne Hackborn * <p>As of API {@link android.os.Build.VERSION_CODES#M},
6104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * sample rates up to 192kHz are supported
6204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * for <code>AudioRecord</code> and <code>AudioTrack</code>, with sample rate conversion
6304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * performed as needed.
6404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * To improve efficiency and avoid lossy conversions, it is recommended to match the sample rate
6504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * for <code>AudioRecord</code> and <code>AudioTrack</code> to the endpoint device
6604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * sample rate, and limit the sample rate to no more than 48kHz unless there are special
6704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * device capabilities that warrant a higher rate.
6804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung *
694ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h4 id="encoding">Encoding</h4>
7004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>Audio encoding is used to describe the bit representation of audio data, which can be
7104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * either linear PCM or compressed audio, such as AC3 or DTS.
7204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>For linear PCM, the audio encoding describes the sample size, 8 bits, 16 bits, or 32 bits,
7304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * and the sample representation, integer or float.
7404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <ul>
7504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <li> {@link #ENCODING_PCM_8BIT}: The audio sample is a 8 bit unsigned integer in the
7604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * range [0, 255], with a 128 offset for zero. This is typically stored as a Java byte in a
7704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * byte array or ByteBuffer. Since the Java byte is <em>signed</em>,
7804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * be careful with math operations and conversions as the most significant bit is inverted.
7904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * </li>
8004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <li> {@link #ENCODING_PCM_16BIT}: The audio sample is a 16 bit signed integer
8104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * typically stored as a Java short in a short array, but when the short
8204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * is stored in a ByteBuffer, it is native endian (as compared to the default Java big endian).
8304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * The short has full range from [-32768, 32767],
8404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * and is sometimes interpreted as fixed point Q.15 data.
8504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * </li>
8604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <li> {@link #ENCODING_PCM_FLOAT}: Introduced in
8704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * API {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this encoding specifies that
8804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * the audio sample is a 32 bit IEEE single precision float. The sample can be
8904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * manipulated as a Java float in a float array, though within a ByteBuffer
9004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * it is stored in native endian byte order.
9104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * The nominal range of <code>ENCODING_PCM_FLOAT</code> audio data is [-1.0, 1.0].
9204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * It is implementation dependent whether the positive maximum of 1.0 is included
9304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * in the interval. Values outside of the nominal range are clamped before
9404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * sending to the endpoint device. Beware that
9504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * the handling of NaN is undefined; subnormals may be treated as zero; and
9604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * infinities are generally clamped just like other values for <code>AudioTrack</code>
9704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * &ndash; try to avoid infinities because they can easily generate a NaN.
9804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <br>
9904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * To achieve higher audio bit depth than a signed 16 bit integer short,
10004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * it is recommended to use <code>ENCODING_PCM_FLOAT</code> for audio capture, processing,
10104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * and playback.
10204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * Floats are efficiently manipulated by modern CPUs,
10304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * have greater precision than 24 bit signed integers,
10404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * and have greater dynamic range than 32 bit signed integers.
1050e3de6cacaffcfeda4d6353be61e2f1f9ed80705Dianne Hackborn * <code>AudioRecord</code> as of API {@link android.os.Build.VERSION_CODES#M} and
10604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <code>AudioTrack</code> as of API {@link android.os.Build.VERSION_CODES#LOLLIPOP}
10704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * support <code>ENCODING_PCM_FLOAT</code>.
10804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * </li>
10904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * </ul>
11004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>For compressed audio, the encoding specifies the method of compression,
11104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * for example {@link #ENCODING_AC3} and {@link #ENCODING_DTS}. The compressed
11204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * audio data is typically stored as bytes in
11304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * a byte array or ByteBuffer. When a compressed audio encoding is specified
11404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * for an <code>AudioTrack</code>, it creates a direct (non-mixed) track
11504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * for output to an endpoint (such as HDMI) capable of decoding the compressed audio.
11604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * For (most) other endpoints, which are not capable of decoding such compressed audio,
11704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * you will need to decode the data first, typically by creating a {@link MediaCodec}.
11804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * Alternatively, one may use {@link MediaPlayer} for playback of compressed
11904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * audio files or streams.
12004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>When compressed audio is sent out through a direct <code>AudioTrack</code>,
12104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * it need not be written in exact multiples of the audio access unit;
12204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * this differs from <code>MediaCodec</code> input buffers.
1234ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *
1244ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h4 id="channelMask">Channel mask</h4>
1254ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <p>Channel masks are used in <code>AudioTrack</code> and <code>AudioRecord</code> to describe
1264ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * the samples and their arrangement in the audio frame. They are also used in the endpoint (e.g.
1274ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * a USB audio interface, a DAC connected to headphones) to specify allowable configurations of a
1284ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * particular device.
1290e3de6cacaffcfeda4d6353be61e2f1f9ed80705Dianne Hackborn * <br>As of API {@link android.os.Build.VERSION_CODES#M}, there are two types of channel masks:
1304ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * channel position masks and channel index masks.
1314ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *
1324ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h5 id="channelPositionMask">Channel position masks</h5>
1334ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * Channel position masks are the original Android channel masks, and are used since API
1344ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link android.os.Build.VERSION_CODES#BASE}.
1354ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * For input and output, they imply a positional nature - the location of a speaker or a microphone
1364ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * for recording or playback.
1374ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>For a channel position mask, each allowed channel position corresponds to a bit in the
1384ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * channel mask. If that channel position is present in the audio frame, that bit is set,
1394ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * otherwise it is zero. The order of the bits (from lsb to msb) corresponds to the order of that
1404ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * position's sample in the audio frame.
1414ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>The canonical channel position masks by channel count are as follows:
1424ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br><table>
1434ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>channel count</td><td>channel position mask</td></tr>
1444ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>1</td><td>{@link #CHANNEL_OUT_MONO}</td></tr>
1454ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>2</td><td>{@link #CHANNEL_OUT_STEREO}</td></tr>
1464ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>3</td><td>{@link #CHANNEL_OUT_STEREO} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
1474ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>4</td><td>{@link #CHANNEL_OUT_QUAD}</td></tr>
1484ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>5</td><td>{@link #CHANNEL_OUT_QUAD} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
1494ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>6</td><td>{@link #CHANNEL_OUT_5POINT1}</td></tr>
1504ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>7</td><td>{@link #CHANNEL_OUT_5POINT1} | {@link #CHANNEL_OUT_BACK_CENTER}</td></tr>
1514ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <tr><td>8</td><td>{@link #CHANNEL_OUT_7POINT1_SURROUND}</td></tr>
1524ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * </table>
1534ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>These masks are an ORed composite of individual channel masks. For example
1544ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link #CHANNEL_OUT_STEREO} is composed of {@link #CHANNEL_OUT_FRONT_LEFT} and
1554ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link #CHANNEL_OUT_FRONT_RIGHT}.
1564ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *
1574ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h5 id="channelIndexMask">Channel index masks</h5>
1580e3de6cacaffcfeda4d6353be61e2f1f9ed80705Dianne Hackborn * Channel index masks are introduced in API {@link android.os.Build.VERSION_CODES#M}. They allow
1594ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * the selection of a particular channel from the source or sink endpoint by number, i.e. the first
1604ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * channel, the second channel, and so forth. This avoids problems with artificially assigning
1614ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * positions to channels of an endpoint, or figuring what the i<sup>th</sup> position bit is within
1624ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * an endpoint's channel position mask etc.
1634ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>Here's an example where channel index masks address this confusion: dealing with a 4 channel
1644ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * USB device. Using a position mask, and based on the channel count, this would be a
1654ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link #CHANNEL_OUT_QUAD} device, but really one is only interested in channel 0
1664ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * through channel 3. The USB device would then have the following individual bit channel masks:
1674ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link #CHANNEL_OUT_FRONT_LEFT},
1684ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * {@link #CHANNEL_OUT_FRONT_RIGHT}, {@link #CHANNEL_OUT_BACK_LEFT}
1694ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * and {@link #CHANNEL_OUT_BACK_RIGHT}. But which is channel 0 and which is
1704ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * channel 3?
1714ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>For a channel index mask, each channel number is represented as a bit in the mask, from the
1724ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * lsb (channel 0) upwards to the msb, numerically this bit value is
1734ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <code>1 << channelNumber</code>.
1744ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * A set bit indicates that channel is present in the audio frame, otherwise it is cleared.
1754ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * The order of the bits also correspond to that channel number's sample order in the audio frame.
1764ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <br>For the previous 4 channel USB device example, the device would have a channel index mask
1774ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <code>0xF</code>. Suppose we wanted to select only the first and the third channels; this would
1784ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * correspond to a channel index mask <code>0x5</code> (the first and third bits set). If an
1794ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <code>AudioTrack</code> uses this channel index mask, the audio frame would consist of two
1804ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * samples, the first sample of each frame routed to channel 0, and the second sample of each frame
1814ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * routed to channel 2.
1824ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * The canonical channel index masks by channel count are given by the formula
1834ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <code>(1 << channelCount) - 1</code>.
1844ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *
1854ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <h5>Use cases</h5>
1864ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <ul>
1874ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><i>Channel position mask for an endpoint:</i> <code>CHANNEL_OUT_FRONT_LEFT</code>,
1884ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  <code>CHANNEL_OUT_FRONT_CENTER</code>, etc. for HDMI home theater purposes.
1894ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><i>Channel position mask for an audio stream:</i> Creating an <code>AudioTrack</code>
1904ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  to output movie content, where 5.1 multichannel output is to be written.
1914ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><i>Channel index mask for an endpoint:</i> USB devices for which input and output do not
1924ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  correspond to left or right speaker or microphone.
1934ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * <li><i>Channel index mask for an audio stream:</i> An <code>AudioRecord</code> may only want the
1944ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  third and fourth audio channels of the endpoint (i.e. the second channel pair), and not care the
1954ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  about position it corresponds to, in which case the channel index mask is <code>0xC</code>.
1964ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi *  Multichannel <code>AudioRecord</code> sessions should use channel index masks.
1974ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi * </ul>
19804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <h4 id="audioFrame">Audio Frame</h4>
19904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * <p>For linear PCM, an audio frame consists of a set of samples captured at the same time,
20004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * whose count and
20104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * channel association are given by the <a href="#channelMask">channel mask</a>,
20204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * and whose sample contents are specified by the <a href="#encoding">encoding</a>.
20304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * For example, a stereo 16 bit PCM frame consists of
20404065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * two 16 bit linear PCM samples, with a frame size of 4 bytes.
20504065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * For compressed audio, an audio frame may alternately
20604065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * refer to an access unit of compressed data bytes that is logically grouped together for
20704065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * decoding and bitstream access (e.g. {@link MediaCodec}),
20804065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * or a single byte of compressed data (e.g. {@link AudioTrack#getBufferSizeInFrames()
20904065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * AudioTrack.getBufferSizeInFrames()}),
21004065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * or the linear PCM frame result from decoding the compressed data
21104065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * (e.g.{@link AudioTrack#getPlaybackHeadPosition()
21204065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * AudioTrack.getPlaybackHeadPosition()}),
21304065187d994bf4e10266cdccdffffd26288c2fdAndy Hung * depending on the context where audio frame is used.
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
21550d1c044b5ce4b6fef532dc6e083cef903f554b2Jeff Sharkeypublic final class AudioFormat implements Parcelable {
216ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Constants
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Invalid audio data format */
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int ENCODING_INVALID = 0;
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Default audio data format */
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int ENCODING_DEFAULT = 1;
224fe834d30f4f3f51b754d55fecb36f11279733948Glenn Kasten
225313f59887b1439fa2f39dbf3578407c52fcbf60dGlenn Kasten    // These values must be kept in sync with core/jni/android_media_AudioFormat.h
226e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk    // Also sync av/services/audiopolicy/managerdefault/ConfigParsingUtils.h
227635fefec06458224750170e7ce127bc2c8e4215bDan Tasse    /** Audio data format: PCM 16 bit per sample. Guaranteed to be supported by devices. */
2285c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten    public static final int ENCODING_PCM_16BIT = 2;
229635fefec06458224750170e7ce127bc2c8e4215bDan Tasse    /** Audio data format: PCM 8 bit per sample. Not guaranteed to be supported by devices. */
2305c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten    public static final int ENCODING_PCM_8BIT = 3;
2317d60bcd2d950e3571c00ce9f1c492c6bd58334c9Glenn Kasten    /** Audio data format: single-precision floating-point per sample */
232313f59887b1439fa2f39dbf3578407c52fcbf60dGlenn Kasten    public static final int ENCODING_PCM_FLOAT = 4;
233ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    /** Audio data format: AC-3 compressed */
234ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    public static final int ENCODING_AC3 = 5;
235ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    /** Audio data format: E-AC-3 compressed */
236ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    public static final int ENCODING_E_AC3 = 6;
237e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk    /** Audio data format: DTS compressed */
238e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk    public static final int ENCODING_DTS = 7;
239e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk    /** Audio data format: DTS HD compressed */
240e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk    public static final int ENCODING_DTS_HD = 8;
241cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /** Audio data format: MP3 compressed
242cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * @hide
243cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * */
244cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static final int ENCODING_MP3 = 9;
245cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /** Audio data format: AAC LC compressed
246cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * @hide
247cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * */
248cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static final int ENCODING_AAC_LC = 10;
249cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /** Audio data format: AAC HE V1 compressed
250cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * @hide
251cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * */
252cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static final int ENCODING_AAC_HE_V1 = 11;
253cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /** Audio data format: AAC HE V2 compressed
254cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * @hide
255cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * */
256cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static final int ENCODING_AAC_HE_V2 = 12;
2574ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    /** Audio data format: compressed audio wrapped in PCM for HDMI
2584ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk     * or S/PDIF passthrough.
259621dbc874047b7f8c227f12fe15f977618f1f2f8Phil Burk     * IEC61937 uses a stereo stream of 16-bit samples as the wrapper.
260621dbc874047b7f8c227f12fe15f977618f1f2f8Phil Burk     * So the channel mask for the track must be {@link #CHANNEL_OUT_STEREO}.
261621dbc874047b7f8c227f12fe15f977618f1f2f8Phil Burk     * Data should be written to the stream in a short[] array.
262621dbc874047b7f8c227f12fe15f977618f1f2f8Phil Burk     * If the data is written in a byte[] array then there may be endian problems
263621dbc874047b7f8c227f12fe15f977618f1f2f8Phil Burk     * on some platforms when converting to short internally.
2644ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk     */
2654ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    public static final int ENCODING_IEC61937 = 13;
266b4fae5ba3980df1690492c70deb134e31bef5b4dEric Laurent    /** Audio data format: DOLBY TRUEHD compressed
267b4fae5ba3980df1690492c70deb134e31bef5b4dEric Laurent     **/
268b4fae5ba3980df1690492c70deb134e31bef5b4dEric Laurent    public static final int ENCODING_DOLBY_TRUEHD = 14;
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Invalid audio channel configuration */
2717fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    /** @deprecated Use {@link #CHANNEL_INVALID} instead.  */
272a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated    public static final int CHANNEL_CONFIGURATION_INVALID   = 0;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Default audio channel configuration */
2747fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    /** @deprecated Use {@link #CHANNEL_OUT_DEFAULT} or {@link #CHANNEL_IN_DEFAULT} instead.  */
275a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated    public static final int CHANNEL_CONFIGURATION_DEFAULT   = 1;
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Mono audio configuration */
2777fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    /** @deprecated Use {@link #CHANNEL_OUT_MONO} or {@link #CHANNEL_IN_MONO} instead.  */
278a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated    public static final int CHANNEL_CONFIGURATION_MONO      = 2;
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Stereo (2 channel) audio configuration */
2807fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    /** @deprecated Use {@link #CHANNEL_OUT_STEREO} or {@link #CHANNEL_IN_STEREO} instead.  */
281a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated    public static final int CHANNEL_CONFIGURATION_STEREO    = 3;
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
283a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /** Invalid audio channel mask */
2843026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_INVALID = 0;
285a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /** Default audio channel mask */
2863026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_DEFAULT = 1;
2873026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent
288c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    // Output channel mask definitions below are translated to the native values defined in
289b2b292317482d00d067bc91669322b273be61926Rom Lemarchand    //  in /system/media/audio/include/system/audio.h in the JNI code of AudioTrack
2903026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;
2913026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;
2923026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;
2933026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;
2943026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_BACK_LEFT = 0x40;
2953026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;
2963026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;
2973026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200;
2983026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_OUT_BACK_CENTER = 0x400;
299ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_SIDE_LEFT =         0x800;
300ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_SIDE_RIGHT =       0x1000;
301ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
302ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_CENTER =       0x2000;
303ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
304ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_FRONT_LEFT =   0x4000;
305ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
306ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_FRONT_CENTER = 0x8000;
307ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
308ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_FRONT_RIGHT = 0x10000;
309ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
310ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_BACK_LEFT =   0x20000;
311ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
312ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_BACK_CENTER = 0x40000;
313ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    /** @hide */
314ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_TOP_BACK_RIGHT =  0x80000;
315ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi
316a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
317a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
318f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    // aka QUAD_BACK
319a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
320a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
321f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    /** @hide */
322f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    public static final int CHANNEL_OUT_QUAD_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
323f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten            CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
324a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
325a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
326f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    // aka 5POINT1_BACK
327a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
328a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
329f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    /** @hide */
330f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten    public static final int CHANNEL_OUT_5POINT1_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
331f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten            CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY |
332f8d5e7af7457ee0d935be63cb7e1479f147f7451Glenn Kasten            CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
3337fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    // different from AUDIO_CHANNEL_OUT_7POINT1 used internally, and not accepted by AudioRecord.
3347fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    /** @deprecated Not the typical 7.1 surround configuration. Use {@link #CHANNEL_OUT_7POINT1_SURROUND} instead. */
3357fdf51b18a3945441761fa2cd7b63c92c51d3e5fAndy Hung    @Deprecated    public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
336a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
337a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
338c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    // matches AUDIO_CHANNEL_OUT_7POINT1
339ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi    public static final int CHANNEL_OUT_7POINT1_SURROUND = (
340ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi            CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
341ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi            CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
342ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi            CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
343ff7656c9de4e6ca0ec13da2b99593be71dcfd796Jean-Michel Trivi            CHANNEL_OUT_LOW_FREQUENCY);
344c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3461cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    /** Minimum value for sample rate,
3471cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     *  assuming AudioTrack and AudioRecord share the same limitations.
3481cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * @hide
3491cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     */
3501cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    // never unhide
3511cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    public static final int SAMPLE_RATE_HZ_MIN = 4000;
3521cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    /** Maximum value for sample rate,
3531cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     *  assuming AudioTrack and AudioRecord share the same limitations.
3541cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * @hide
3551cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     */
3561cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    // never unhide
3571cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    public static final int SAMPLE_RATE_HZ_MAX = 192000;
3581cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    /** Sample rate will be a route-dependent value.
3591cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * For AudioTrack, it is usually the sink sample rate,
3601cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * and for AudioRecord it is usually the source sample rate.
3611cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     */
3621cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten    public static final int SAMPLE_RATE_UNSPECIFIED = 0;
3631cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten
364d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    /**
365d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @hide
3668dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * Return the input channel mask corresponding to an output channel mask.
3678dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
3688dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * @param outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
3698dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * @return a combination of CHANNEL_IN_* definitions matching an output channel mask
3708dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * @throws IllegalArgumentException
3718dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     */
3728dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi    public static int inChannelMaskFromOutChannelMask(int outMask) throws IllegalArgumentException {
3738dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi        if (outMask == CHANNEL_OUT_DEFAULT) {
3748dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi            throw new IllegalArgumentException(
3758dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi                    "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
3768dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi        }
3778dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi        switch (channelCountFromOutChannelMask(outMask)) {
3788dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi            case 1:
3798dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi                return CHANNEL_IN_MONO;
3808dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi            case 2:
3818dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi                return CHANNEL_IN_STEREO;
3828dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi            default:
3838dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi                throw new IllegalArgumentException("Unsupported channel configuration for input.");
3848dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi        }
3858dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi    }
3868dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi
3878dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi    /**
3888dc1031453377d2f9d9c7847027f276e6726a4c8Jean-Michel Trivi     * @hide
389701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi     * Return the number of channels from an input channel mask
390701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi     * @param mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
391701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi     * @return number of channels for the mask
392701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi     */
393701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi    public static int channelCountFromInChannelMask(int mask) {
394701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi        return Integer.bitCount(mask);
395701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi    }
396701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi    /**
397701d6ff12f36bf5e9de0dafdaced06744fd411ebJean-Michel Trivi     * @hide
398d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * Return the number of channels from an output channel mask
399d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
400d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @return number of channels for the mask
401d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     */
402d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    public static int channelCountFromOutChannelMask(int mask) {
403d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi        return Integer.bitCount(mask);
404d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    }
405d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    /**
406d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @hide
407d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * Return a channel mask ready to be used by native code
408d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
409d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @return a native channel mask
410d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     */
411d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    public static int convertChannelOutMaskToNativeMask(int javaMask) {
412d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi        return (javaMask >> 2);
413d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    }
414d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi
415d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    /**
416d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @hide
417d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * Return a java output channel mask
418d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @param mask a native channel mask
419d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     * @return a combination of the CHANNEL_OUT_* definitions
420d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi     */
421d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    public static int convertNativeChannelMaskToOutMask(int nativeMask) {
422d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi        return (nativeMask << 2);
423d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi    }
424d2bebb3ab86177c0d27664af86b30b7dce2c9bcbJean-Michel Trivi
4253026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_DEFAULT = 1;
426c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    // These directly match native
4273026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_LEFT = 0x4;
4283026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_RIGHT = 0x8;
4293026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_FRONT = 0x10;
4303026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_BACK = 0x20;
4313026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_LEFT_PROCESSED = 0x40;
4323026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_RIGHT_PROCESSED = 0x80;
4333026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_FRONT_PROCESSED = 0x100;
4343026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_BACK_PROCESSED = 0x200;
4353026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_PRESSURE = 0x400;
4363026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_X_AXIS = 0x800;
4373026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_Y_AXIS = 0x1000;
4383026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_Z_AXIS = 0x2000;
4393026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_VOICE_UPLINK = 0x4000;
4403026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    public static final int CHANNEL_IN_VOICE_DNLINK = 0x8000;
441a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_IN_MONO = CHANNEL_IN_FRONT;
442a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT);
443c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    /** @hide */
444c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    public static final int CHANNEL_IN_FRONT_BACK = CHANNEL_IN_FRONT | CHANNEL_IN_BACK;
445c806050a078151ed47876ae326654e7c7e94b9b0Glenn Kasten    // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
44734a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten    /** @hide */
44834a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten    public static int getBytesPerSample(int audioFormat)
44934a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten    {
45034a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        switch (audioFormat) {
45134a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        case ENCODING_PCM_8BIT:
45234a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten            return 1;
453ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_16BIT:
4544ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_IEC61937:
455ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_DEFAULT:
456ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            return 2;
4572854ed50254f06c9da21b8fe028b300b6ab267e2Andy Hung        case ENCODING_PCM_FLOAT:
4582854ed50254f06c9da21b8fe028b300b6ab267e2Andy Hung            return 4;
459ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_INVALID:
460ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        default:
461ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            throw new IllegalArgumentException("Bad audio format " + audioFormat);
462ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        }
463ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    }
464ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent
465ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    /** @hide */
466ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    public static boolean isValidEncoding(int audioFormat)
467ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    {
468ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        switch (audioFormat) {
469ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_8BIT:
47034a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        case ENCODING_PCM_16BIT:
471ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_FLOAT:
472ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_AC3:
473ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_E_AC3:
474e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        case ENCODING_DTS:
475e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        case ENCODING_DTS_HD:
476cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_MP3:
477cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_LC:
478cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_HE_V1:
479cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_HE_V2:
4804ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_IEC61937:
481cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent            return true;
482cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        default:
483cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent            return false;
484cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        }
485cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    }
486cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent
487cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /** @hide */
488cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static boolean isPublicEncoding(int audioFormat)
489cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    {
490cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        switch (audioFormat) {
491cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_PCM_8BIT:
492cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_PCM_16BIT:
493cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_PCM_FLOAT:
494cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AC3:
495cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_E_AC3:
496cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_DTS:
497cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_DTS_HD:
4984ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_IEC61937:
499ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            return true;
500ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        default:
501ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            return false;
502ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        }
503ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    }
504ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent
505ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    /** @hide */
506ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    public static boolean isEncodingLinearPcm(int audioFormat)
507ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    {
508ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        switch (audioFormat) {
509ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_8BIT:
510ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_16BIT:
511ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_PCM_FLOAT:
51234a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        case ENCODING_DEFAULT:
513ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            return true;
514ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_AC3:
515ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        case ENCODING_E_AC3:
516e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        case ENCODING_DTS:
517e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        case ENCODING_DTS_HD:
518cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_MP3:
519cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_LC:
520cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_HE_V1:
521cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        case ENCODING_AAC_HE_V2:
5224ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_IEC61937: // wrapped in PCM but compressed
523ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent            return false;
52434a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        case ENCODING_INVALID:
52534a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        default:
52634a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten            throw new IllegalArgumentException("Bad audio format " + audioFormat);
52734a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten        }
52834a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten    }
52934a37bdebb3d606dac7c7d1dd7a0effdb59bd3d6Glenn Kasten
5304ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    /** @hide */
5314ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    public static boolean isEncodingLinearFrames(int audioFormat)
5324ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    {
5334ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        switch (audioFormat) {
5344ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_PCM_8BIT:
5354ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_PCM_16BIT:
5364ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_PCM_FLOAT:
5374ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_IEC61937: // same size as stereo PCM
5384ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_DEFAULT:
5394ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk            return true;
5404ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_AC3:
5414ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_E_AC3:
5424ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_DTS:
5434ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_DTS_HD:
5444ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_MP3:
5454ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_AAC_LC:
5464ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_AAC_HE_V1:
5474ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_AAC_HE_V2:
5484ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk            return false;
5494ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        case ENCODING_INVALID:
5504ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        default:
5514ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk            throw new IllegalArgumentException("Bad audio format " + audioFormat);
5524ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        }
5534ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk    }
554cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    /**
555cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * Returns an array of public encoding values extracted from an array of
556cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * encoding values.
557cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     * @hide
558cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent     */
559cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    public static int[] filterPublicFormats(int[] formats) {
560cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        if (formats == null) {
561cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent            return null;
562cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        }
563cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        int[] myCopy = Arrays.copyOf(formats, formats.length);
564cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        int size = 0;
565cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        for (int i = 0; i < myCopy.length; i++) {
566cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent            if (isPublicEncoding(myCopy[i])) {
567cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent                if (size != i) {
568cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent                    myCopy[size] = myCopy[i];
569cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent                }
570cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent                size++;
571cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent            }
572cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        }
573cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent        return Arrays.copyOf(myCopy, size);
574cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent    }
575cae346633321e237f824f3ca10d7e6fff2307b2fEric Laurent
5761aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten    /** @removed */
5771aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten    public AudioFormat()
5781aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten    {
5791aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten        throw new UnsupportedOperationException("There is no valid usage of this constructor");
5801aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten    }
5811aa74e4bc24cbe014f0c4217761a9d5e082111c6Glenn Kasten
582f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /**
583f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi     * Private constructor with an ignored argument to differentiate from the removed default ctor
584f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi     * @param ignoredArgument
585f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi     */
586f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    private AudioFormat(int ignoredArgument) {
587f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    }
588f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
589d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    /**
5901cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * Constructor used by the JNI.  Parameters are not checked for validity.
591d3b8223377b8046280e4c09e728edc600171f941Eric Laurent     */
592d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    // Update sound trigger JNI in core/jni/android_hardware_SoundTrigger.cpp when modifying this
593d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    // constructor
594a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    private AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask) {
595d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        mEncoding = encoding;
596d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        mSampleRate = sampleRate;
597d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        mChannelMask = channelMask;
598a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        mChannelIndexMask = channelIndexMask;
599d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_ENCODING |
600d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE |
601a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK |
602a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
603d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
604d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
605f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /** @hide */
606f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public final static int AUDIO_FORMAT_HAS_PROPERTY_NONE = 0x0;
607f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /** @hide */
608f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public final static int AUDIO_FORMAT_HAS_PROPERTY_ENCODING = 0x1 << 0;
609f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /** @hide */
610f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public final static int AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE = 0x1 << 1;
611f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /** @hide */
612f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK = 0x1 << 2;
613a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    /** @hide */
614a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK = 0x1 << 3;
615f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
616f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    private int mEncoding;
617f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    private int mSampleRate;
618f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    private int mChannelMask;
619a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    private int mChannelIndexMask;
620f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    private int mPropertySetMask;
621f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
6220498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent    /**
6230498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * Return the encoding.
6244ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * See the section on <a href="#encoding">encodings</a> for more information about the different
6254ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * types of supported audio encoding.
6260498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * @return one of the values that can be set in {@link Builder#setEncoding(int)} or
6270498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * {@link AudioFormat#ENCODING_INVALID} if not set.
6280498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     */
629a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    public int getEncoding() {
6300498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) == 0) {
6310498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent            return ENCODING_INVALID;
6320498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent        }
633a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi        return mEncoding;
634a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi    }
635a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi
6360498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent    /**
6370498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * Return the sample rate.
6380498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * @return one of the values that can be set in {@link Builder#setSampleRate(int)} or
6391cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten     * {@link #SAMPLE_RATE_UNSPECIFIED} if not set.
6400498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     */
641a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    public int getSampleRate() {
642a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi        return mSampleRate;
643a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi    }
644a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi
6450498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent    /**
6460498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * Return the channel mask.
6474ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * See the section on <a href="#channelMask">channel masks</a> for more information about
6484ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * the difference between index-based masks(as returned by {@link #getChannelIndexMask()}) and
6494ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * the position-based mask returned by this function.
6500498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * @return one of the values that can be set in {@link Builder#setChannelMask(int)} or
6510498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     * {@link AudioFormat#CHANNEL_INVALID} if not set.
6520498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent     */
653a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    public int getChannelMask() {
6540498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) == 0) {
6550498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent            return CHANNEL_INVALID;
6560498269c00e340a5cacf19c00d552c9a311cc604Eric Laurent        }
657a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi        return mChannelMask;
658a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi    }
659a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi
660a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    /**
661a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * Return the channel index mask.
6624ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * See the section on <a href="#channelMask">channel masks</a> for more information about
6634ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * the difference between index-based masks, and position-based masks (as returned
6644ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * by {@link #getChannelMask()}).
665a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * @return one of the values that can be set in {@link Builder#setChannelIndexMask(int)} or
666a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * {@link AudioFormat#CHANNEL_INVALID} if not set or an invalid mask was used.
667a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     */
668a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    public int getChannelIndexMask() {
669a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) == 0) {
670a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            return CHANNEL_INVALID;
671a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        }
672a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        return mChannelIndexMask;
673a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    }
674a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung
675a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    /**
676a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * Return the channel count.
677a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * @return the channel count derived from the channel position mask or the channel index mask.
678a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     * Zero is returned if both the channel position mask and the channel index mask are not set.
679a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung     */
680a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    public int getChannelCount() {
681a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        final int channelIndexCount = Integer.bitCount(getChannelIndexMask());
682a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        int channelCount = channelCountFromOutChannelMask(getChannelMask());
683a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        if (channelCount == 0) {
684a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            channelCount = channelIndexCount;
685a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        } else if (channelCount != channelIndexCount && channelIndexCount != 0) {
686a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            channelCount = 0; // position and index channel count mismatch
687a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        }
688a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        return channelCount;
689a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    }
690a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung
691a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    /** @hide */
692a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    public int getPropertySetMask() {
693a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi        return mPropertySetMask;
694a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi    }
695a1d80e3b1d210c60c6881a55ed39a4077ff66080Jean-Michel Trivi
696f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /**
697f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi     * Builder class for {@link AudioFormat} objects.
698aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * Use this class to configure and create an AudioFormat instance. By setting format
699aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * characteristics such as audio encoding, channel mask or sample rate, you indicate which
700aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * of those are to vary from the default behavior on this device wherever this audio format
7014ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * is used. See {@link AudioFormat} for a complete description of the different parameters that
7024ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi     * can be used to configure an <code>AudioFormat</code> instance.
703aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * <p>{@link AudioFormat} is for instance used in
704aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. In this
705aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * constructor, every format characteristic set on the <code>Builder</code> (e.g. with
706aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * {@link #setSampleRate(int)}) will alter the default values used by an
707aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * <code>AudioTrack</code>. In this case for audio playback with <code>AudioTrack</code>, the
708aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * sample rate set in the <code>Builder</code> would override the platform output sample rate
709aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi     * which would otherwise be selected by default.
710f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi     */
711f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public static class Builder {
712aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi        private int mEncoding = ENCODING_INVALID;
7131cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten        private int mSampleRate = SAMPLE_RATE_UNSPECIFIED;
714f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        private int mChannelMask = CHANNEL_INVALID;
715a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        private int mChannelIndexMask = 0;
716f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        private int mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_NONE;
717f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
718f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
719aea175e499b40cdb8d3b03ae8bdc9ff66ce786abJean-Michel Trivi         * Constructs a new Builder with none of the format characteristics set.
720f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
721f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        public Builder() {
722f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
723f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
724f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
725f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * Constructs a new Builder from a given {@link AudioFormat}.
726f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @param af the {@link AudioFormat} object whose data will be reused in the new Builder.
727f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
728f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        public Builder(AudioFormat af) {
729f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mEncoding = af.mEncoding;
730f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mSampleRate = af.mSampleRate;
731f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mChannelMask = af.mChannelMask;
732a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            mChannelIndexMask = af.mChannelIndexMask;
733f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mPropertySetMask = af.mPropertySetMask;
734f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
735f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
736f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
737f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * Combines all of the format characteristics that have been set and return a new
738f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * {@link AudioFormat} object.
739f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @return a new {@link AudioFormat} object
740f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
741f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        public AudioFormat build() {
742f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            AudioFormat af = new AudioFormat(1980/*ignored*/);
743f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            af.mEncoding = mEncoding;
7441cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            // not calling setSampleRate is equivalent to calling
7451cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            // setSampleRate(SAMPLE_RATE_UNSPECIFIED)
746f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            af.mSampleRate = mSampleRate;
747f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            af.mChannelMask = mChannelMask;
748a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            af.mChannelIndexMask = mChannelIndexMask;
749f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            af.mPropertySetMask = mPropertySetMask;
750f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            return af;
751f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
752f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
753f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
754f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * Sets the data encoding format.
755f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @param encoding one of {@link AudioFormat#ENCODING_DEFAULT},
756f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *     {@link AudioFormat#ENCODING_PCM_8BIT},
757f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *     {@link AudioFormat#ENCODING_PCM_16BIT},
758ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent         *     {@link AudioFormat#ENCODING_PCM_FLOAT},
759ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent         *     {@link AudioFormat#ENCODING_AC3},
760ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent         *     {@link AudioFormat#ENCODING_E_AC3}.
761e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk         *     {@link AudioFormat#ENCODING_DTS},
762e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk         *     {@link AudioFormat#ENCODING_DTS_HD}.
763f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @return the same Builder instance.
764f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @throws java.lang.IllegalArgumentException
765f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
766f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        public Builder setEncoding(@Encoding int encoding) throws IllegalArgumentException {
767f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            switch (encoding) {
768f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                case ENCODING_DEFAULT:
769f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                    mEncoding = ENCODING_PCM_16BIT;
770f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                    break;
771f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                case ENCODING_PCM_8BIT:
772f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                case ENCODING_PCM_16BIT:
773f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                case ENCODING_PCM_FLOAT:
774ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent                case ENCODING_AC3:
775ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent                case ENCODING_E_AC3:
776e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk                case ENCODING_DTS:
777e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk                case ENCODING_DTS_HD:
7784ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk                case ENCODING_IEC61937:
779f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                    mEncoding = encoding;
780f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                    break;
781f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                case ENCODING_INVALID:
782f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                default:
783f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                    throw new IllegalArgumentException("Invalid encoding " + encoding);
784f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            }
785f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_ENCODING;
786f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            return this;
787f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
788f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
789f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
790a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * Sets the channel position mask.
791a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * The channel position mask specifies the association between audio samples in a frame
792a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * with named endpoint channels. The samples in the frame correspond to the
793a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * named set bits in the channel position mask, in ascending bit order.
794a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * See {@link #setChannelIndexMask(int)} to specify channels
7954ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi         * based on endpoint numbered channels. This <a href="#channelPositionMask>description of
7964ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi         * channel position masks</a> covers the concept in more details.
797f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @param channelMask describes the configuration of the audio channels.
798a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <p> For output, the channelMask can be an OR-ed combination of
799a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    channel position masks, e.g.
800f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *    {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT},
801f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *    {@link AudioFormat#CHANNEL_OUT_FRONT_RIGHT},
802a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_FRONT_CENTER},
803a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_LOW_FREQUENCY}
804f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *    {@link AudioFormat#CHANNEL_OUT_BACK_LEFT},
805a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_BACK_RIGHT},
806a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_BACK_CENTER},
807a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_SIDE_LEFT},
808a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_OUT_SIDE_RIGHT}.
809a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <p> For a valid {@link AudioTrack} channel position mask,
810a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    the following conditions apply:
811a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <br> (1) at most eight channel positions may be used;
812a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <br> (2) right/left pairs should be matched.
813a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <p> For input or {@link AudioRecord}, the mask should be
814a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    {@link AudioFormat#CHANNEL_IN_MONO} or
815f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *    {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is
816f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         *    guaranteed to work on all devices.
817a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * @return the same <code>Builder</code> instance.
818a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * @throws IllegalArgumentException if the channel mask is invalid or
819a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    if both channel index mask and channel position mask
820a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    are specified but do not have the same channel count.
821f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
822e90a0a88f4eabdc27c3ef033f38c91ea23362a1eAndy Hung        public @NonNull Builder setChannelMask(int channelMask) {
8231cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            if (channelMask == CHANNEL_INVALID) {
824a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                throw new IllegalArgumentException("Invalid zero channel mask");
825a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            } else if (/* channelMask != 0 && */ mChannelIndexMask != 0 &&
826a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                    Integer.bitCount(channelMask) != Integer.bitCount(mChannelIndexMask)) {
827a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                throw new IllegalArgumentException("Mismatched channel count for mask " +
828a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                        Integer.toHexString(channelMask).toUpperCase());
829a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            }
830f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mChannelMask = channelMask;
831f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
832f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            return this;
833f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
834f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
835f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        /**
836a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * Sets the channel index mask.
837a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * A channel index mask specifies the association of audio samples in the frame
838a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * with numbered endpoint channels. The i-th bit in the channel index
839a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * mask corresponds to the i-th endpoint channel.
840a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * For example, an endpoint with four channels is represented
8414ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi         * as index mask bits 0 through 3. This <a href="#channelIndexMask>description of channel
8424ed06d2e303b48ff13b119964e507a959f36f5bcJean-Michel Trivi         * index masks</a> covers the concept in more details.
843a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * See {@link #setChannelMask(int)} for a positional mask interpretation.
844a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * <p> Both {@link AudioTrack} and {@link AudioRecord} support
845a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * a channel index mask.
846a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * If a channel index mask is specified it is used,
847a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * otherwise the channel position mask specified
848a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * by <code>setChannelMask</code> is used.
849a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * For <code>AudioTrack</code> and <code>AudioRecord</code>,
850a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * a channel position mask is not required if a channel index mask is specified.
851a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *
852a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * @param channelIndexMask describes the configuration of the audio channels.
853a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <p> For output, the <code>channelIndexMask</code> is an OR-ed combination of
854a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    bits representing the mapping of <code>AudioTrack</code> write samples
855a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    to output sink channels.
856a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    For example, a mask of <code>0xa</code>, or binary <code>1010</code>,
857a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    means the <code>AudioTrack</code> write frame consists of two samples,
858a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    which are routed to the second and the fourth channels of the output sink.
859a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    Unmatched output sink channels are zero filled and unmatched
860a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <code>AudioTrack</code> write samples are dropped.
861a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <p> For input, the <code>channelIndexMask</code> is an OR-ed combination of
862a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    bits representing the mapping of input source channels to
863a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <code>AudioRecord</code> read samples.
864a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    For example, a mask of <code>0x5</code>, or binary
865a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <code>101</code>, will read from the first and third channel of the input
866a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    source device and store them in the first and second sample of the
867a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    <code>AudioRecord</code> read frame.
868a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    Unmatched input source channels are dropped and
869a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    unmatched <code>AudioRecord</code> read samples are zero filled.
870a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * @return the same <code>Builder</code> instance.
871a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         * @throws IllegalArgumentException if the channel index mask is invalid or
872a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    if both channel index mask and channel position mask
873a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         *    are specified but do not have the same channel count.
874a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung         */
875e90a0a88f4eabdc27c3ef033f38c91ea23362a1eAndy Hung        public @NonNull Builder setChannelIndexMask(int channelIndexMask) {
876a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            if (channelIndexMask == 0) {
877a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                throw new IllegalArgumentException("Invalid zero channel index mask");
878a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            } else if (/* channelIndexMask != 0 && */ mChannelMask != 0 &&
879a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                    Integer.bitCount(channelIndexMask) != Integer.bitCount(mChannelMask)) {
880a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                throw new IllegalArgumentException("Mismatched channel count for index mask " +
881a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                        Integer.toHexString(channelIndexMask).toUpperCase());
882a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            }
883a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            mChannelIndexMask = channelIndexMask;
884a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
885a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung            return this;
886a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        }
887a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung
888a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung        /**
889f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * Sets the sample rate.
890f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @param sampleRate the sample rate expressed in Hz
891f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @return the same Builder instance.
892f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         * @throws java.lang.IllegalArgumentException
893f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi         */
894f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        public Builder setSampleRate(int sampleRate) throws IllegalArgumentException {
8951cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            // TODO Consider whether to keep the MIN and MAX range checks here.
8961cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            // It is not necessary and poses the problem of defining the limits independently from
8971cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            // native implementation or platform capabilities.
8981cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten            if (((sampleRate < SAMPLE_RATE_HZ_MIN) || (sampleRate > SAMPLE_RATE_HZ_MAX)) &&
8991cbf9b3741ec486c3ffce08f145501eb1ca73640Glenn Kasten                    sampleRate != SAMPLE_RATE_UNSPECIFIED) {
900f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                throw new IllegalArgumentException("Invalid sample rate " + sampleRate);
901f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            }
902f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mSampleRate = sampleRate;
903f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE;
904f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi            return this;
905f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        }
906f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    }
907f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
908f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    @Override
9098ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi    public boolean equals(Object o) {
9108ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        if (this == o) return true;
9118ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        if (o == null || getClass() != o.getClass()) return false;
9128ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi
9138ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        AudioFormat that = (AudioFormat) o;
9148ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi
9158ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        if (mPropertySetMask != that.mPropertySetMask) return false;
9168ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi
9178ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        // return false if any of the properties is set and the values differ
9188ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi        return !((((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
9198ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                            && (mEncoding != that.mEncoding))
9208ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                    || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
9218ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                            && (mSampleRate != that.mSampleRate))
9228ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                    || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0)
9238ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                            && (mChannelMask != that.mChannelMask))
9248ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                    || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0)
9258ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi                            && (mChannelIndexMask != that.mChannelIndexMask)));
9268ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi    }
9278ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi
9288ab728093eed85c176822d58a0d2ba1f4ebbb362Jean-Michel Trivi    @Override
9295affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    public int hashCode() {
9305affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        return Objects.hash(mPropertySetMask, mSampleRate, mEncoding, mChannelMask,
9315affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi                mChannelIndexMask);
9325affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    }
9335affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi
9345affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    @Override
9355affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    public int describeContents() {
9365affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        return 0;
9375affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    }
9385affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi
9395affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    @Override
9405affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    public void writeToParcel(Parcel dest, int flags) {
9415affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        dest.writeInt(mPropertySetMask);
9425affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        dest.writeInt(mEncoding);
9435affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        dest.writeInt(mSampleRate);
9445affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        dest.writeInt(mChannelMask);
9455affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        dest.writeInt(mChannelIndexMask);
9465affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    }
9475affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi
9485affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    private AudioFormat(Parcel in) {
9495affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        mPropertySetMask = in.readInt();
9505affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        mEncoding = in.readInt();
9515affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        mSampleRate = in.readInt();
9525affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        mChannelMask = in.readInt();
9535affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        mChannelIndexMask = in.readInt();
9545affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    }
9555affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi
9565affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    public static final Parcelable.Creator<AudioFormat> CREATOR =
9575affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi            new Parcelable.Creator<AudioFormat>() {
9585affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        public AudioFormat createFromParcel(Parcel p) {
9595affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi            return new AudioFormat(p);
9605affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        }
9615affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        public AudioFormat[] newArray(int size) {
9625affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi            return new AudioFormat[size];
9635affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi        }
9645affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    };
9655affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi
9665affc2b587323f55201d5db768b838505b5a4b72Jean-Michel Trivi    @Override
967f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public String toString () {
968f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        return new String("AudioFormat:"
969f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                + " props=" + mPropertySetMask
970f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                + " enc=" + mEncoding
971a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                + " chan=0x" + Integer.toHexString(mChannelMask).toUpperCase()
972a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung                + " chan_index=0x" + Integer.toHexString(mChannelIndexMask).toUpperCase()
973f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi                + " rate=" + mSampleRate);
974f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    }
975f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
976f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    /** @hide */
977f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    @IntDef({
978f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        ENCODING_DEFAULT,
979f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        ENCODING_PCM_8BIT,
980f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi        ENCODING_PCM_16BIT,
981ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        ENCODING_PCM_FLOAT,
982ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent        ENCODING_AC3,
983e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        ENCODING_E_AC3,
984e12189dee6504cf793d37f5689a7bbf0773293c7Phil Burk        ENCODING_DTS,
9854ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        ENCODING_DTS_HD,
9864ddbc0e3773a5028b5598f8828e565a878eee8fdPhil Burk        ENCODING_IEC61937
987f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    })
988f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    @Retention(RetentionPolicy.SOURCE)
989f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi    public @interface Encoding {}
990f723f8e9cf5b4a00ce84c8b1b4b9256cf7745e2cJean-Michel Trivi
991a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent}
992