1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Arrays;
27 import java.util.Objects;
28 
29 /**
30  * The {@link AudioFormat} class is used to access a number of audio format and
31  * channel configuration constants. They are for instance used
32  * in {@link AudioTrack} and {@link AudioRecord}, as valid values in individual parameters of
33  * constructors like {@link AudioTrack#AudioTrack(int, int, int, int, int, int)}, where the fourth
34  * parameter is one of the <code>AudioFormat.ENCODING_*</code> constants.
35  * The <code>AudioFormat</code> constants are also used in {@link MediaFormat} to specify
36  * audio related values commonly used in media, such as for {@link MediaFormat#KEY_CHANNEL_MASK}.
37  * <p>The {@link AudioFormat.Builder} class can be used to create instances of
38  * the <code>AudioFormat</code> format class.
39  * Refer to
40  * {@link AudioFormat.Builder} for documentation on the mechanics of the configuration and building
41  * of such instances. Here we describe the main concepts that the <code>AudioFormat</code> class
42  * allow you to convey in each instance, they are:
43  * <ol>
44  * <li><a href="#sampleRate">sample rate</a>
45  * <li><a href="#encoding">encoding</a>
46  * <li><a href="#channelMask">channel masks</a>
47  * </ol>
48  * <p>Closely associated with the <code>AudioFormat</code> is the notion of an
49  * <a href="#audioFrame">audio frame</a>, which is used throughout the documentation
50  * to represent the minimum size complete unit of audio data.
51  *
52  * <h4 id="sampleRate">Sample rate</h4>
53  * <p>Expressed in Hz, the sample rate in an <code>AudioFormat</code> instance expresses the number
54  * of audio samples for each channel per second in the content you are playing or recording. It is
55  * not the sample rate
56  * at which content is rendered or produced. For instance a sound at a media sample rate of 8000Hz
57  * can be played on a device operating at a sample rate of 48000Hz; the sample rate conversion is
58  * automatically handled by the platform, it will not play at 6x speed.
59  *
60  * <p>As of API {@link android.os.Build.VERSION_CODES#M},
61  * sample rates up to 192kHz are supported
62  * for <code>AudioRecord</code> and <code>AudioTrack</code>, with sample rate conversion
63  * performed as needed.
64  * To improve efficiency and avoid lossy conversions, it is recommended to match the sample rate
65  * for <code>AudioRecord</code> and <code>AudioTrack</code> to the endpoint device
66  * sample rate, and limit the sample rate to no more than 48kHz unless there are special
67  * device capabilities that warrant a higher rate.
68  *
69  * <h4 id="encoding">Encoding</h4>
70  * <p>Audio encoding is used to describe the bit representation of audio data, which can be
71  * either linear PCM or compressed audio, such as AC3 or DTS.
72  * <p>For linear PCM, the audio encoding describes the sample size, 8 bits, 16 bits, or 32 bits,
73  * and the sample representation, integer or float.
74  * <ul>
75  * <li> {@link #ENCODING_PCM_8BIT}: The audio sample is a 8 bit unsigned integer in the
76  * range [0, 255], with a 128 offset for zero. This is typically stored as a Java byte in a
77  * byte array or ByteBuffer. Since the Java byte is <em>signed</em>,
78  * be careful with math operations and conversions as the most significant bit is inverted.
79  * </li>
80  * <li> {@link #ENCODING_PCM_16BIT}: The audio sample is a 16 bit signed integer
81  * typically stored as a Java short in a short array, but when the short
82  * is stored in a ByteBuffer, it is native endian (as compared to the default Java big endian).
83  * The short has full range from [-32768, 32767],
84  * and is sometimes interpreted as fixed point Q.15 data.
85  * </li>
86  * <li> {@link #ENCODING_PCM_FLOAT}: Introduced in
87  * API {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this encoding specifies that
88  * the audio sample is a 32 bit IEEE single precision float. The sample can be
89  * manipulated as a Java float in a float array, though within a ByteBuffer
90  * it is stored in native endian byte order.
91  * The nominal range of <code>ENCODING_PCM_FLOAT</code> audio data is [-1.0, 1.0].
92  * It is implementation dependent whether the positive maximum of 1.0 is included
93  * in the interval. Values outside of the nominal range are clamped before
94  * sending to the endpoint device. Beware that
95  * the handling of NaN is undefined; subnormals may be treated as zero; and
96  * infinities are generally clamped just like other values for <code>AudioTrack</code>
97  * &ndash; try to avoid infinities because they can easily generate a NaN.
98  * <br>
99  * To achieve higher audio bit depth than a signed 16 bit integer short,
100  * it is recommended to use <code>ENCODING_PCM_FLOAT</code> for audio capture, processing,
101  * and playback.
102  * Floats are efficiently manipulated by modern CPUs,
103  * have greater precision than 24 bit signed integers,
104  * and have greater dynamic range than 32 bit signed integers.
105  * <code>AudioRecord</code> as of API {@link android.os.Build.VERSION_CODES#M} and
106  * <code>AudioTrack</code> as of API {@link android.os.Build.VERSION_CODES#LOLLIPOP}
107  * support <code>ENCODING_PCM_FLOAT</code>.
108  * </li>
109  * </ul>
110  * <p>For compressed audio, the encoding specifies the method of compression,
111  * for example {@link #ENCODING_AC3} and {@link #ENCODING_DTS}. The compressed
112  * audio data is typically stored as bytes in
113  * a byte array or ByteBuffer. When a compressed audio encoding is specified
114  * for an <code>AudioTrack</code>, it creates a direct (non-mixed) track
115  * for output to an endpoint (such as HDMI) capable of decoding the compressed audio.
116  * For (most) other endpoints, which are not capable of decoding such compressed audio,
117  * you will need to decode the data first, typically by creating a {@link MediaCodec}.
118  * Alternatively, one may use {@link MediaPlayer} for playback of compressed
119  * audio files or streams.
120  * <p>When compressed audio is sent out through a direct <code>AudioTrack</code>,
121  * it need not be written in exact multiples of the audio access unit;
122  * this differs from <code>MediaCodec</code> input buffers.
123  *
124  * <h4 id="channelMask">Channel mask</h4>
125  * <p>Channel masks are used in <code>AudioTrack</code> and <code>AudioRecord</code> to describe
126  * the samples and their arrangement in the audio frame. They are also used in the endpoint (e.g.
127  * a USB audio interface, a DAC connected to headphones) to specify allowable configurations of a
128  * particular device.
129  * <br>As of API {@link android.os.Build.VERSION_CODES#M}, there are two types of channel masks:
130  * channel position masks and channel index masks.
131  *
132  * <h5 id="channelPositionMask">Channel position masks</h5>
133  * Channel position masks are the original Android channel masks, and are used since API
134  * {@link android.os.Build.VERSION_CODES#BASE}.
135  * For input and output, they imply a positional nature - the location of a speaker or a microphone
136  * for recording or playback.
137  * <br>For a channel position mask, each allowed channel position corresponds to a bit in the
138  * channel mask. If that channel position is present in the audio frame, that bit is set,
139  * otherwise it is zero. The order of the bits (from lsb to msb) corresponds to the order of that
140  * position's sample in the audio frame.
141  * <br>The canonical channel position masks by channel count are as follows:
142  * <br><table>
143  * <tr><td>channel count</td><td>channel position mask</td></tr>
144  * <tr><td>1</td><td>{@link #CHANNEL_OUT_MONO}</td></tr>
145  * <tr><td>2</td><td>{@link #CHANNEL_OUT_STEREO}</td></tr>
146  * <tr><td>3</td><td>{@link #CHANNEL_OUT_STEREO} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
147  * <tr><td>4</td><td>{@link #CHANNEL_OUT_QUAD}</td></tr>
148  * <tr><td>5</td><td>{@link #CHANNEL_OUT_QUAD} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
149  * <tr><td>6</td><td>{@link #CHANNEL_OUT_5POINT1}</td></tr>
150  * <tr><td>7</td><td>{@link #CHANNEL_OUT_5POINT1} | {@link #CHANNEL_OUT_BACK_CENTER}</td></tr>
151  * <tr><td>8</td><td>{@link #CHANNEL_OUT_7POINT1_SURROUND}</td></tr>
152  * </table>
153  * <br>These masks are an ORed composite of individual channel masks. For example
154  * {@link #CHANNEL_OUT_STEREO} is composed of {@link #CHANNEL_OUT_FRONT_LEFT} and
155  * {@link #CHANNEL_OUT_FRONT_RIGHT}.
156  *
157  * <h5 id="channelIndexMask">Channel index masks</h5>
158  * Channel index masks are introduced in API {@link android.os.Build.VERSION_CODES#M}. They allow
159  * the selection of a particular channel from the source or sink endpoint by number, i.e. the first
160  * channel, the second channel, and so forth. This avoids problems with artificially assigning
161  * positions to channels of an endpoint, or figuring what the i<sup>th</sup> position bit is within
162  * an endpoint's channel position mask etc.
163  * <br>Here's an example where channel index masks address this confusion: dealing with a 4 channel
164  * USB device. Using a position mask, and based on the channel count, this would be a
165  * {@link #CHANNEL_OUT_QUAD} device, but really one is only interested in channel 0
166  * through channel 3. The USB device would then have the following individual bit channel masks:
167  * {@link #CHANNEL_OUT_FRONT_LEFT},
168  * {@link #CHANNEL_OUT_FRONT_RIGHT}, {@link #CHANNEL_OUT_BACK_LEFT}
169  * and {@link #CHANNEL_OUT_BACK_RIGHT}. But which is channel 0 and which is
170  * channel 3?
171  * <br>For a channel index mask, each channel number is represented as a bit in the mask, from the
172  * lsb (channel 0) upwards to the msb, numerically this bit value is
173  * <code>1 << channelNumber</code>.
174  * A set bit indicates that channel is present in the audio frame, otherwise it is cleared.
175  * The order of the bits also correspond to that channel number's sample order in the audio frame.
176  * <br>For the previous 4 channel USB device example, the device would have a channel index mask
177  * <code>0xF</code>. Suppose we wanted to select only the first and the third channels; this would
178  * correspond to a channel index mask <code>0x5</code> (the first and third bits set). If an
179  * <code>AudioTrack</code> uses this channel index mask, the audio frame would consist of two
180  * samples, the first sample of each frame routed to channel 0, and the second sample of each frame
181  * routed to channel 2.
182  * The canonical channel index masks by channel count are given by the formula
183  * <code>(1 << channelCount) - 1</code>.
184  *
185  * <h5>Use cases</h5>
186  * <ul>
187  * <li><i>Channel position mask for an endpoint:</i> <code>CHANNEL_OUT_FRONT_LEFT</code>,
188  *  <code>CHANNEL_OUT_FRONT_CENTER</code>, etc. for HDMI home theater purposes.
189  * <li><i>Channel position mask for an audio stream:</i> Creating an <code>AudioTrack</code>
190  *  to output movie content, where 5.1 multichannel output is to be written.
191  * <li><i>Channel index mask for an endpoint:</i> USB devices for which input and output do not
192  *  correspond to left or right speaker or microphone.
193  * <li><i>Channel index mask for an audio stream:</i> An <code>AudioRecord</code> may only want the
194  *  third and fourth audio channels of the endpoint (i.e. the second channel pair), and not care the
195  *  about position it corresponds to, in which case the channel index mask is <code>0xC</code>.
196  *  Multichannel <code>AudioRecord</code> sessions should use channel index masks.
197  * </ul>
198  * <h4 id="audioFrame">Audio Frame</h4>
199  * <p>For linear PCM, an audio frame consists of a set of samples captured at the same time,
200  * whose count and
201  * channel association are given by the <a href="#channelMask">channel mask</a>,
202  * and whose sample contents are specified by the <a href="#encoding">encoding</a>.
203  * For example, a stereo 16 bit PCM frame consists of
204  * two 16 bit linear PCM samples, with a frame size of 4 bytes.
205  * For compressed audio, an audio frame may alternately
206  * refer to an access unit of compressed data bytes that is logically grouped together for
207  * decoding and bitstream access (e.g. {@link MediaCodec}),
208  * or a single byte of compressed data (e.g. {@link AudioTrack#getBufferSizeInFrames()
209  * AudioTrack.getBufferSizeInFrames()}),
210  * or the linear PCM frame result from decoding the compressed data
211  * (e.g.{@link AudioTrack#getPlaybackHeadPosition()
212  * AudioTrack.getPlaybackHeadPosition()}),
213  * depending on the context where audio frame is used.
214  */
215 public final class AudioFormat implements Parcelable {
216 
217     //---------------------------------------------------------
218     // Constants
219     //--------------------
220     /** Invalid audio data format */
221     public static final int ENCODING_INVALID = 0;
222     /** Default audio data format */
223     public static final int ENCODING_DEFAULT = 1;
224 
225     // These values must be kept in sync with core/jni/android_media_AudioFormat.h
226     // Also sync av/services/audiopolicy/managerdefault/ConfigParsingUtils.h
227     /** Audio data format: PCM 16 bit per sample. Guaranteed to be supported by devices. */
228     public static final int ENCODING_PCM_16BIT = 2;
229     /** Audio data format: PCM 8 bit per sample. Not guaranteed to be supported by devices. */
230     public static final int ENCODING_PCM_8BIT = 3;
231     /** Audio data format: single-precision floating-point per sample */
232     public static final int ENCODING_PCM_FLOAT = 4;
233     /** Audio data format: AC-3 compressed */
234     public static final int ENCODING_AC3 = 5;
235     /** Audio data format: E-AC-3 compressed */
236     public static final int ENCODING_E_AC3 = 6;
237     /** Audio data format: DTS compressed */
238     public static final int ENCODING_DTS = 7;
239     /** Audio data format: DTS HD compressed */
240     public static final int ENCODING_DTS_HD = 8;
241     /** Audio data format: MP3 compressed
242      * @hide
243      * */
244     public static final int ENCODING_MP3 = 9;
245     /** Audio data format: AAC LC compressed
246      * @hide
247      * */
248     public static final int ENCODING_AAC_LC = 10;
249     /** Audio data format: AAC HE V1 compressed
250      * @hide
251      * */
252     public static final int ENCODING_AAC_HE_V1 = 11;
253     /** Audio data format: AAC HE V2 compressed
254      * @hide
255      * */
256     public static final int ENCODING_AAC_HE_V2 = 12;
257     /** Audio data format: compressed audio wrapped in PCM for HDMI
258      * or S/PDIF passthrough.
259      * IEC61937 uses a stereo stream of 16-bit samples as the wrapper.
260      * So the channel mask for the track must be {@link #CHANNEL_OUT_STEREO}.
261      * Data should be written to the stream in a short[] array.
262      * If the data is written in a byte[] array then there may be endian problems
263      * on some platforms when converting to short internally.
264      */
265     public static final int ENCODING_IEC61937 = 13;
266 
267     /** Invalid audio channel configuration */
268     /** @deprecated Use {@link #CHANNEL_INVALID} instead.  */
269     @Deprecated    public static final int CHANNEL_CONFIGURATION_INVALID   = 0;
270     /** Default audio channel configuration */
271     /** @deprecated Use {@link #CHANNEL_OUT_DEFAULT} or {@link #CHANNEL_IN_DEFAULT} instead.  */
272     @Deprecated    public static final int CHANNEL_CONFIGURATION_DEFAULT   = 1;
273     /** Mono audio configuration */
274     /** @deprecated Use {@link #CHANNEL_OUT_MONO} or {@link #CHANNEL_IN_MONO} instead.  */
275     @Deprecated    public static final int CHANNEL_CONFIGURATION_MONO      = 2;
276     /** Stereo (2 channel) audio configuration */
277     /** @deprecated Use {@link #CHANNEL_OUT_STEREO} or {@link #CHANNEL_IN_STEREO} instead.  */
278     @Deprecated    public static final int CHANNEL_CONFIGURATION_STEREO    = 3;
279 
280     /** Invalid audio channel mask */
281     public static final int CHANNEL_INVALID = 0;
282     /** Default audio channel mask */
283     public static final int CHANNEL_OUT_DEFAULT = 1;
284 
285     // Output channel mask definitions below are translated to the native values defined in
286     //  in /system/media/audio/include/system/audio.h in the JNI code of AudioTrack
287     public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;
288     public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;
289     public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;
290     public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;
291     public static final int CHANNEL_OUT_BACK_LEFT = 0x40;
292     public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;
293     public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;
294     public static final int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200;
295     public static final int CHANNEL_OUT_BACK_CENTER = 0x400;
296     public static final int CHANNEL_OUT_SIDE_LEFT =         0x800;
297     public static final int CHANNEL_OUT_SIDE_RIGHT =       0x1000;
298     /** @hide */
299     public static final int CHANNEL_OUT_TOP_CENTER =       0x2000;
300     /** @hide */
301     public static final int CHANNEL_OUT_TOP_FRONT_LEFT =   0x4000;
302     /** @hide */
303     public static final int CHANNEL_OUT_TOP_FRONT_CENTER = 0x8000;
304     /** @hide */
305     public static final int CHANNEL_OUT_TOP_FRONT_RIGHT = 0x10000;
306     /** @hide */
307     public static final int CHANNEL_OUT_TOP_BACK_LEFT =   0x20000;
308     /** @hide */
309     public static final int CHANNEL_OUT_TOP_BACK_CENTER = 0x40000;
310     /** @hide */
311     public static final int CHANNEL_OUT_TOP_BACK_RIGHT =  0x80000;
312 
313     public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
314     public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
315     // aka QUAD_BACK
316     public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
317             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
318     /** @hide */
319     public static final int CHANNEL_OUT_QUAD_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
320             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
321     public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
322             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
323     // aka 5POINT1_BACK
324     public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
325             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
326     /** @hide */
327     public static final int CHANNEL_OUT_5POINT1_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
328             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY |
329             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
330     // different from AUDIO_CHANNEL_OUT_7POINT1 used internally, and not accepted by AudioRecord.
331     /** @deprecated Not the typical 7.1 surround configuration. Use {@link #CHANNEL_OUT_7POINT1_SURROUND} instead. */
332     @Deprecated    public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
333             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
334             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
335     // matches AUDIO_CHANNEL_OUT_7POINT1
336     public static final int CHANNEL_OUT_7POINT1_SURROUND = (
337             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
338             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
339             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
340             CHANNEL_OUT_LOW_FREQUENCY);
341     // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL
342 
343     /** Minimum value for sample rate,
344      *  assuming AudioTrack and AudioRecord share the same limitations.
345      * @hide
346      */
347     // never unhide
348     public static final int SAMPLE_RATE_HZ_MIN = 4000;
349     /** Maximum value for sample rate,
350      *  assuming AudioTrack and AudioRecord share the same limitations.
351      * @hide
352      */
353     // never unhide
354     public static final int SAMPLE_RATE_HZ_MAX = 192000;
355     /** Sample rate will be a route-dependent value.
356      * For AudioTrack, it is usually the sink sample rate,
357      * and for AudioRecord it is usually the source sample rate.
358      */
359     public static final int SAMPLE_RATE_UNSPECIFIED = 0;
360 
361     /**
362      * @hide
363      * Return the input channel mask corresponding to an output channel mask.
364      * This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
365      * @param outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
366      * @return a combination of CHANNEL_IN_* definitions matching an output channel mask
367      * @throws IllegalArgumentException
368      */
inChannelMaskFromOutChannelMask(int outMask)369     public static int inChannelMaskFromOutChannelMask(int outMask) throws IllegalArgumentException {
370         if (outMask == CHANNEL_OUT_DEFAULT) {
371             throw new IllegalArgumentException(
372                     "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
373         }
374         switch (channelCountFromOutChannelMask(outMask)) {
375             case 1:
376                 return CHANNEL_IN_MONO;
377             case 2:
378                 return CHANNEL_IN_STEREO;
379             default:
380                 throw new IllegalArgumentException("Unsupported channel configuration for input.");
381         }
382     }
383 
384     /**
385      * @hide
386      * Return the number of channels from an input channel mask
387      * @param mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
388      * @return number of channels for the mask
389      */
channelCountFromInChannelMask(int mask)390     public static int channelCountFromInChannelMask(int mask) {
391         return Integer.bitCount(mask);
392     }
393     /**
394      * @hide
395      * Return the number of channels from an output channel mask
396      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
397      * @return number of channels for the mask
398      */
channelCountFromOutChannelMask(int mask)399     public static int channelCountFromOutChannelMask(int mask) {
400         return Integer.bitCount(mask);
401     }
402     /**
403      * @hide
404      * Return a channel mask ready to be used by native code
405      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
406      * @return a native channel mask
407      */
convertChannelOutMaskToNativeMask(int javaMask)408     public static int convertChannelOutMaskToNativeMask(int javaMask) {
409         return (javaMask >> 2);
410     }
411 
412     /**
413      * @hide
414      * Return a java output channel mask
415      * @param mask a native channel mask
416      * @return a combination of the CHANNEL_OUT_* definitions
417      */
convertNativeChannelMaskToOutMask(int nativeMask)418     public static int convertNativeChannelMaskToOutMask(int nativeMask) {
419         return (nativeMask << 2);
420     }
421 
422     public static final int CHANNEL_IN_DEFAULT = 1;
423     // These directly match native
424     public static final int CHANNEL_IN_LEFT = 0x4;
425     public static final int CHANNEL_IN_RIGHT = 0x8;
426     public static final int CHANNEL_IN_FRONT = 0x10;
427     public static final int CHANNEL_IN_BACK = 0x20;
428     public static final int CHANNEL_IN_LEFT_PROCESSED = 0x40;
429     public static final int CHANNEL_IN_RIGHT_PROCESSED = 0x80;
430     public static final int CHANNEL_IN_FRONT_PROCESSED = 0x100;
431     public static final int CHANNEL_IN_BACK_PROCESSED = 0x200;
432     public static final int CHANNEL_IN_PRESSURE = 0x400;
433     public static final int CHANNEL_IN_X_AXIS = 0x800;
434     public static final int CHANNEL_IN_Y_AXIS = 0x1000;
435     public static final int CHANNEL_IN_Z_AXIS = 0x2000;
436     public static final int CHANNEL_IN_VOICE_UPLINK = 0x4000;
437     public static final int CHANNEL_IN_VOICE_DNLINK = 0x8000;
438     public static final int CHANNEL_IN_MONO = CHANNEL_IN_FRONT;
439     public static final int CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT);
440     /** @hide */
441     public static final int CHANNEL_IN_FRONT_BACK = CHANNEL_IN_FRONT | CHANNEL_IN_BACK;
442     // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL
443 
444     /** @hide */
getBytesPerSample(int audioFormat)445     public static int getBytesPerSample(int audioFormat)
446     {
447         switch (audioFormat) {
448         case ENCODING_PCM_8BIT:
449             return 1;
450         case ENCODING_PCM_16BIT:
451         case ENCODING_IEC61937:
452         case ENCODING_DEFAULT:
453             return 2;
454         case ENCODING_PCM_FLOAT:
455             return 4;
456         case ENCODING_INVALID:
457         default:
458             throw new IllegalArgumentException("Bad audio format " + audioFormat);
459         }
460     }
461 
462     /** @hide */
isValidEncoding(int audioFormat)463     public static boolean isValidEncoding(int audioFormat)
464     {
465         switch (audioFormat) {
466         case ENCODING_PCM_8BIT:
467         case ENCODING_PCM_16BIT:
468         case ENCODING_PCM_FLOAT:
469         case ENCODING_AC3:
470         case ENCODING_E_AC3:
471         case ENCODING_DTS:
472         case ENCODING_DTS_HD:
473         case ENCODING_MP3:
474         case ENCODING_AAC_LC:
475         case ENCODING_AAC_HE_V1:
476         case ENCODING_AAC_HE_V2:
477         case ENCODING_IEC61937:
478             return true;
479         default:
480             return false;
481         }
482     }
483 
484     /** @hide */
isPublicEncoding(int audioFormat)485     public static boolean isPublicEncoding(int audioFormat)
486     {
487         switch (audioFormat) {
488         case ENCODING_PCM_8BIT:
489         case ENCODING_PCM_16BIT:
490         case ENCODING_PCM_FLOAT:
491         case ENCODING_AC3:
492         case ENCODING_E_AC3:
493         case ENCODING_DTS:
494         case ENCODING_DTS_HD:
495         case ENCODING_IEC61937:
496             return true;
497         default:
498             return false;
499         }
500     }
501 
502     /** @hide */
isEncodingLinearPcm(int audioFormat)503     public static boolean isEncodingLinearPcm(int audioFormat)
504     {
505         switch (audioFormat) {
506         case ENCODING_PCM_8BIT:
507         case ENCODING_PCM_16BIT:
508         case ENCODING_PCM_FLOAT:
509         case ENCODING_DEFAULT:
510             return true;
511         case ENCODING_AC3:
512         case ENCODING_E_AC3:
513         case ENCODING_DTS:
514         case ENCODING_DTS_HD:
515         case ENCODING_MP3:
516         case ENCODING_AAC_LC:
517         case ENCODING_AAC_HE_V1:
518         case ENCODING_AAC_HE_V2:
519         case ENCODING_IEC61937: // wrapped in PCM but compressed
520             return false;
521         case ENCODING_INVALID:
522         default:
523             throw new IllegalArgumentException("Bad audio format " + audioFormat);
524         }
525     }
526 
527     /** @hide */
isEncodingLinearFrames(int audioFormat)528     public static boolean isEncodingLinearFrames(int audioFormat)
529     {
530         switch (audioFormat) {
531         case ENCODING_PCM_8BIT:
532         case ENCODING_PCM_16BIT:
533         case ENCODING_PCM_FLOAT:
534         case ENCODING_IEC61937: // same size as stereo PCM
535         case ENCODING_DEFAULT:
536             return true;
537         case ENCODING_AC3:
538         case ENCODING_E_AC3:
539         case ENCODING_DTS:
540         case ENCODING_DTS_HD:
541         case ENCODING_MP3:
542         case ENCODING_AAC_LC:
543         case ENCODING_AAC_HE_V1:
544         case ENCODING_AAC_HE_V2:
545             return false;
546         case ENCODING_INVALID:
547         default:
548             throw new IllegalArgumentException("Bad audio format " + audioFormat);
549         }
550     }
551     /**
552      * Returns an array of public encoding values extracted from an array of
553      * encoding values.
554      * @hide
555      */
filterPublicFormats(int[] formats)556     public static int[] filterPublicFormats(int[] formats) {
557         if (formats == null) {
558             return null;
559         }
560         int[] myCopy = Arrays.copyOf(formats, formats.length);
561         int size = 0;
562         for (int i = 0; i < myCopy.length; i++) {
563             if (isPublicEncoding(myCopy[i])) {
564                 if (size != i) {
565                     myCopy[size] = myCopy[i];
566                 }
567                 size++;
568             }
569         }
570         return Arrays.copyOf(myCopy, size);
571     }
572 
573     /** @removed */
AudioFormat()574     public AudioFormat()
575     {
576         throw new UnsupportedOperationException("There is no valid usage of this constructor");
577     }
578 
579     /**
580      * Private constructor with an ignored argument to differentiate from the removed default ctor
581      * @param ignoredArgument
582      */
AudioFormat(int ignoredArgument)583     private AudioFormat(int ignoredArgument) {
584     }
585 
586     /**
587      * Constructor used by the JNI.  Parameters are not checked for validity.
588      */
589     // Update sound trigger JNI in core/jni/android_hardware_SoundTrigger.cpp when modifying this
590     // constructor
AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask)591     private AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask) {
592         mEncoding = encoding;
593         mSampleRate = sampleRate;
594         mChannelMask = channelMask;
595         mChannelIndexMask = channelIndexMask;
596         mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_ENCODING |
597                 AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE |
598                 AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK |
599                 AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
600     }
601 
602     /** @hide */
603     public final static int AUDIO_FORMAT_HAS_PROPERTY_NONE = 0x0;
604     /** @hide */
605     public final static int AUDIO_FORMAT_HAS_PROPERTY_ENCODING = 0x1 << 0;
606     /** @hide */
607     public final static int AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE = 0x1 << 1;
608     /** @hide */
609     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK = 0x1 << 2;
610     /** @hide */
611     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK = 0x1 << 3;
612 
613     private int mEncoding;
614     private int mSampleRate;
615     private int mChannelMask;
616     private int mChannelIndexMask;
617     private int mPropertySetMask;
618 
619     /**
620      * Return the encoding.
621      * See the section on <a href="#encoding">encodings</a> for more information about the different
622      * types of supported audio encoding.
623      * @return one of the values that can be set in {@link Builder#setEncoding(int)} or
624      * {@link AudioFormat#ENCODING_INVALID} if not set.
625      */
getEncoding()626     public int getEncoding() {
627         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) == 0) {
628             return ENCODING_INVALID;
629         }
630         return mEncoding;
631     }
632 
633     /**
634      * Return the sample rate.
635      * @return one of the values that can be set in {@link Builder#setSampleRate(int)} or
636      * {@link #SAMPLE_RATE_UNSPECIFIED} if not set.
637      */
getSampleRate()638     public int getSampleRate() {
639         return mSampleRate;
640     }
641 
642     /**
643      * Return the channel mask.
644      * See the section on <a href="#channelMask">channel masks</a> for more information about
645      * the difference between index-based masks(as returned by {@link #getChannelIndexMask()}) and
646      * the position-based mask returned by this function.
647      * @return one of the values that can be set in {@link Builder#setChannelMask(int)} or
648      * {@link AudioFormat#CHANNEL_INVALID} if not set.
649      */
getChannelMask()650     public int getChannelMask() {
651         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) == 0) {
652             return CHANNEL_INVALID;
653         }
654         return mChannelMask;
655     }
656 
657     /**
658      * Return the channel index mask.
659      * See the section on <a href="#channelMask">channel masks</a> for more information about
660      * the difference between index-based masks, and position-based masks (as returned
661      * by {@link #getChannelMask()}).
662      * @return one of the values that can be set in {@link Builder#setChannelIndexMask(int)} or
663      * {@link AudioFormat#CHANNEL_INVALID} if not set or an invalid mask was used.
664      */
getChannelIndexMask()665     public int getChannelIndexMask() {
666         if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) == 0) {
667             return CHANNEL_INVALID;
668         }
669         return mChannelIndexMask;
670     }
671 
672     /**
673      * Return the channel count.
674      * @return the channel count derived from the channel position mask or the channel index mask.
675      * Zero is returned if both the channel position mask and the channel index mask are not set.
676      */
getChannelCount()677     public int getChannelCount() {
678         final int channelIndexCount = Integer.bitCount(getChannelIndexMask());
679         int channelCount = channelCountFromOutChannelMask(getChannelMask());
680         if (channelCount == 0) {
681             channelCount = channelIndexCount;
682         } else if (channelCount != channelIndexCount && channelIndexCount != 0) {
683             channelCount = 0; // position and index channel count mismatch
684         }
685         return channelCount;
686     }
687 
688     /** @hide */
getPropertySetMask()689     public int getPropertySetMask() {
690         return mPropertySetMask;
691     }
692 
693     /**
694      * Builder class for {@link AudioFormat} objects.
695      * Use this class to configure and create an AudioFormat instance. By setting format
696      * characteristics such as audio encoding, channel mask or sample rate, you indicate which
697      * of those are to vary from the default behavior on this device wherever this audio format
698      * is used. See {@link AudioFormat} for a complete description of the different parameters that
699      * can be used to configure an <code>AudioFormat</code> instance.
700      * <p>{@link AudioFormat} is for instance used in
701      * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. In this
702      * constructor, every format characteristic set on the <code>Builder</code> (e.g. with
703      * {@link #setSampleRate(int)}) will alter the default values used by an
704      * <code>AudioTrack</code>. In this case for audio playback with <code>AudioTrack</code>, the
705      * sample rate set in the <code>Builder</code> would override the platform output sample rate
706      * which would otherwise be selected by default.
707      */
708     public static class Builder {
709         private int mEncoding = ENCODING_INVALID;
710         private int mSampleRate = SAMPLE_RATE_UNSPECIFIED;
711         private int mChannelMask = CHANNEL_INVALID;
712         private int mChannelIndexMask = 0;
713         private int mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_NONE;
714 
715         /**
716          * Constructs a new Builder with none of the format characteristics set.
717          */
Builder()718         public Builder() {
719         }
720 
721         /**
722          * Constructs a new Builder from a given {@link AudioFormat}.
723          * @param af the {@link AudioFormat} object whose data will be reused in the new Builder.
724          */
Builder(AudioFormat af)725         public Builder(AudioFormat af) {
726             mEncoding = af.mEncoding;
727             mSampleRate = af.mSampleRate;
728             mChannelMask = af.mChannelMask;
729             mChannelIndexMask = af.mChannelIndexMask;
730             mPropertySetMask = af.mPropertySetMask;
731         }
732 
733         /**
734          * Combines all of the format characteristics that have been set and return a new
735          * {@link AudioFormat} object.
736          * @return a new {@link AudioFormat} object
737          */
build()738         public AudioFormat build() {
739             AudioFormat af = new AudioFormat(1980/*ignored*/);
740             af.mEncoding = mEncoding;
741             // not calling setSampleRate is equivalent to calling
742             // setSampleRate(SAMPLE_RATE_UNSPECIFIED)
743             af.mSampleRate = mSampleRate;
744             af.mChannelMask = mChannelMask;
745             af.mChannelIndexMask = mChannelIndexMask;
746             af.mPropertySetMask = mPropertySetMask;
747             return af;
748         }
749 
750         /**
751          * Sets the data encoding format.
752          * @param encoding one of {@link AudioFormat#ENCODING_DEFAULT},
753          *     {@link AudioFormat#ENCODING_PCM_8BIT},
754          *     {@link AudioFormat#ENCODING_PCM_16BIT},
755          *     {@link AudioFormat#ENCODING_PCM_FLOAT},
756          *     {@link AudioFormat#ENCODING_AC3},
757          *     {@link AudioFormat#ENCODING_E_AC3}.
758          *     {@link AudioFormat#ENCODING_DTS},
759          *     {@link AudioFormat#ENCODING_DTS_HD}.
760          * @return the same Builder instance.
761          * @throws java.lang.IllegalArgumentException
762          */
setEncoding(@ncoding int encoding)763         public Builder setEncoding(@Encoding int encoding) throws IllegalArgumentException {
764             switch (encoding) {
765                 case ENCODING_DEFAULT:
766                     mEncoding = ENCODING_PCM_16BIT;
767                     break;
768                 case ENCODING_PCM_8BIT:
769                 case ENCODING_PCM_16BIT:
770                 case ENCODING_PCM_FLOAT:
771                 case ENCODING_AC3:
772                 case ENCODING_E_AC3:
773                 case ENCODING_DTS:
774                 case ENCODING_DTS_HD:
775                 case ENCODING_IEC61937:
776                     mEncoding = encoding;
777                     break;
778                 case ENCODING_INVALID:
779                 default:
780                     throw new IllegalArgumentException("Invalid encoding " + encoding);
781             }
782             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_ENCODING;
783             return this;
784         }
785 
786         /**
787          * Sets the channel position mask.
788          * The channel position mask specifies the association between audio samples in a frame
789          * with named endpoint channels. The samples in the frame correspond to the
790          * named set bits in the channel position mask, in ascending bit order.
791          * See {@link #setChannelIndexMask(int)} to specify channels
792          * based on endpoint numbered channels. This <a href="#channelPositionMask>description of
793          * channel position masks</a> covers the concept in more details.
794          * @param channelMask describes the configuration of the audio channels.
795          *    <p> For output, the channelMask can be an OR-ed combination of
796          *    channel position masks, e.g.
797          *    {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT},
798          *    {@link AudioFormat#CHANNEL_OUT_FRONT_RIGHT},
799          *    {@link AudioFormat#CHANNEL_OUT_FRONT_CENTER},
800          *    {@link AudioFormat#CHANNEL_OUT_LOW_FREQUENCY}
801          *    {@link AudioFormat#CHANNEL_OUT_BACK_LEFT},
802          *    {@link AudioFormat#CHANNEL_OUT_BACK_RIGHT},
803          *    {@link AudioFormat#CHANNEL_OUT_BACK_CENTER},
804          *    {@link AudioFormat#CHANNEL_OUT_SIDE_LEFT},
805          *    {@link AudioFormat#CHANNEL_OUT_SIDE_RIGHT}.
806          *    <p> For a valid {@link AudioTrack} channel position mask,
807          *    the following conditions apply:
808          *    <br> (1) at most eight channel positions may be used;
809          *    <br> (2) right/left pairs should be matched.
810          *    <p> For input or {@link AudioRecord}, the mask should be
811          *    {@link AudioFormat#CHANNEL_IN_MONO} or
812          *    {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is
813          *    guaranteed to work on all devices.
814          * @return the same <code>Builder</code> instance.
815          * @throws IllegalArgumentException if the channel mask is invalid or
816          *    if both channel index mask and channel position mask
817          *    are specified but do not have the same channel count.
818          */
setChannelMask(int channelMask)819         public @NonNull Builder setChannelMask(int channelMask) {
820             if (channelMask == CHANNEL_INVALID) {
821                 throw new IllegalArgumentException("Invalid zero channel mask");
822             } else if (/* channelMask != 0 && */ mChannelIndexMask != 0 &&
823                     Integer.bitCount(channelMask) != Integer.bitCount(mChannelIndexMask)) {
824                 throw new IllegalArgumentException("Mismatched channel count for mask " +
825                         Integer.toHexString(channelMask).toUpperCase());
826             }
827             mChannelMask = channelMask;
828             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
829             return this;
830         }
831 
832         /**
833          * Sets the channel index mask.
834          * A channel index mask specifies the association of audio samples in the frame
835          * with numbered endpoint channels. The i-th bit in the channel index
836          * mask corresponds to the i-th endpoint channel.
837          * For example, an endpoint with four channels is represented
838          * as index mask bits 0 through 3. This <a href="#channelIndexMask>description of channel
839          * index masks</a> covers the concept in more details.
840          * See {@link #setChannelMask(int)} for a positional mask interpretation.
841          * <p> Both {@link AudioTrack} and {@link AudioRecord} support
842          * a channel index mask.
843          * If a channel index mask is specified it is used,
844          * otherwise the channel position mask specified
845          * by <code>setChannelMask</code> is used.
846          * For <code>AudioTrack</code> and <code>AudioRecord</code>,
847          * a channel position mask is not required if a channel index mask is specified.
848          *
849          * @param channelIndexMask describes the configuration of the audio channels.
850          *    <p> For output, the <code>channelIndexMask</code> is an OR-ed combination of
851          *    bits representing the mapping of <code>AudioTrack</code> write samples
852          *    to output sink channels.
853          *    For example, a mask of <code>0xa</code>, or binary <code>1010</code>,
854          *    means the <code>AudioTrack</code> write frame consists of two samples,
855          *    which are routed to the second and the fourth channels of the output sink.
856          *    Unmatched output sink channels are zero filled and unmatched
857          *    <code>AudioTrack</code> write samples are dropped.
858          *    <p> For input, the <code>channelIndexMask</code> is an OR-ed combination of
859          *    bits representing the mapping of input source channels to
860          *    <code>AudioRecord</code> read samples.
861          *    For example, a mask of <code>0x5</code>, or binary
862          *    <code>101</code>, will read from the first and third channel of the input
863          *    source device and store them in the first and second sample of the
864          *    <code>AudioRecord</code> read frame.
865          *    Unmatched input source channels are dropped and
866          *    unmatched <code>AudioRecord</code> read samples are zero filled.
867          * @return the same <code>Builder</code> instance.
868          * @throws IllegalArgumentException if the channel index mask is invalid or
869          *    if both channel index mask and channel position mask
870          *    are specified but do not have the same channel count.
871          */
setChannelIndexMask(int channelIndexMask)872         public @NonNull Builder setChannelIndexMask(int channelIndexMask) {
873             if (channelIndexMask == 0) {
874                 throw new IllegalArgumentException("Invalid zero channel index mask");
875             } else if (/* channelIndexMask != 0 && */ mChannelMask != 0 &&
876                     Integer.bitCount(channelIndexMask) != Integer.bitCount(mChannelMask)) {
877                 throw new IllegalArgumentException("Mismatched channel count for index mask " +
878                         Integer.toHexString(channelIndexMask).toUpperCase());
879             }
880             mChannelIndexMask = channelIndexMask;
881             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
882             return this;
883         }
884 
885         /**
886          * Sets the sample rate.
887          * @param sampleRate the sample rate expressed in Hz
888          * @return the same Builder instance.
889          * @throws java.lang.IllegalArgumentException
890          */
setSampleRate(int sampleRate)891         public Builder setSampleRate(int sampleRate) throws IllegalArgumentException {
892             // TODO Consider whether to keep the MIN and MAX range checks here.
893             // It is not necessary and poses the problem of defining the limits independently from
894             // native implementation or platform capabilities.
895             if (((sampleRate < SAMPLE_RATE_HZ_MIN) || (sampleRate > SAMPLE_RATE_HZ_MAX)) &&
896                     sampleRate != SAMPLE_RATE_UNSPECIFIED) {
897                 throw new IllegalArgumentException("Invalid sample rate " + sampleRate);
898             }
899             mSampleRate = sampleRate;
900             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE;
901             return this;
902         }
903     }
904 
905     @Override
equals(Object o)906     public boolean equals(Object o) {
907         if (this == o) return true;
908         if (o == null || getClass() != o.getClass()) return false;
909 
910         AudioFormat that = (AudioFormat) o;
911 
912         if (mPropertySetMask != that.mPropertySetMask) return false;
913 
914         // return false if any of the properties is set and the values differ
915         return !((((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
916                             && (mEncoding != that.mEncoding))
917                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
918                             && (mSampleRate != that.mSampleRate))
919                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0)
920                             && (mChannelMask != that.mChannelMask))
921                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0)
922                             && (mChannelIndexMask != that.mChannelIndexMask)));
923     }
924 
925     @Override
hashCode()926     public int hashCode() {
927         return Objects.hash(mPropertySetMask, mSampleRate, mEncoding, mChannelMask,
928                 mChannelIndexMask);
929     }
930 
931     @Override
describeContents()932     public int describeContents() {
933         return 0;
934     }
935 
936     @Override
writeToParcel(Parcel dest, int flags)937     public void writeToParcel(Parcel dest, int flags) {
938         dest.writeInt(mPropertySetMask);
939         dest.writeInt(mEncoding);
940         dest.writeInt(mSampleRate);
941         dest.writeInt(mChannelMask);
942         dest.writeInt(mChannelIndexMask);
943     }
944 
AudioFormat(Parcel in)945     private AudioFormat(Parcel in) {
946         mPropertySetMask = in.readInt();
947         mEncoding = in.readInt();
948         mSampleRate = in.readInt();
949         mChannelMask = in.readInt();
950         mChannelIndexMask = in.readInt();
951     }
952 
953     public static final Parcelable.Creator<AudioFormat> CREATOR =
954             new Parcelable.Creator<AudioFormat>() {
955         public AudioFormat createFromParcel(Parcel p) {
956             return new AudioFormat(p);
957         }
958         public AudioFormat[] newArray(int size) {
959             return new AudioFormat[size];
960         }
961     };
962 
963     @Override
toString()964     public String toString () {
965         return new String("AudioFormat:"
966                 + " props=" + mPropertySetMask
967                 + " enc=" + mEncoding
968                 + " chan=0x" + Integer.toHexString(mChannelMask).toUpperCase()
969                 + " chan_index=0x" + Integer.toHexString(mChannelIndexMask).toUpperCase()
970                 + " rate=" + mSampleRate);
971     }
972 
973     /** @hide */
974     @IntDef({
975         ENCODING_DEFAULT,
976         ENCODING_PCM_8BIT,
977         ENCODING_PCM_16BIT,
978         ENCODING_PCM_FLOAT,
979         ENCODING_AC3,
980         ENCODING_E_AC3,
981         ENCODING_DTS,
982         ENCODING_DTS_HD,
983         ENCODING_IEC61937
984     })
985     @Retention(RetentionPolicy.SOURCE)
986     public @interface Encoding {}
987 
988 }
989