• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.IntRange;
21 import android.annotation.NonNull;
22 import android.annotation.TestApi;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.os.Build;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Arrays;
31 import java.util.Objects;
32 
33 /**
34  * The {@link AudioFormat} class is used to access a number of audio format and
35  * channel configuration constants. They are for instance used
36  * in {@link AudioTrack} and {@link AudioRecord}, as valid values in individual parameters of
37  * constructors like {@link AudioTrack#AudioTrack(int, int, int, int, int, int)}, where the fourth
38  * parameter is one of the <code>AudioFormat.ENCODING_*</code> constants.
39  * The <code>AudioFormat</code> constants are also used in {@link MediaFormat} to specify
40  * audio related values commonly used in media, such as for {@link MediaFormat#KEY_CHANNEL_MASK}.
41  * <p>The {@link AudioFormat.Builder} class can be used to create instances of
42  * the <code>AudioFormat</code> format class.
43  * Refer to
44  * {@link AudioFormat.Builder} for documentation on the mechanics of the configuration and building
45  * of such instances. Here we describe the main concepts that the <code>AudioFormat</code> class
46  * allow you to convey in each instance, they are:
47  * <ol>
48  * <li><a href="#sampleRate">sample rate</a>
49  * <li><a href="#encoding">encoding</a>
50  * <li><a href="#channelMask">channel masks</a>
51  * </ol>
52  * <p>Closely associated with the <code>AudioFormat</code> is the notion of an
53  * <a href="#audioFrame">audio frame</a>, which is used throughout the documentation
54  * to represent the minimum size complete unit of audio data.
55  *
56  * <h4 id="sampleRate">Sample rate</h4>
57  * <p>Expressed in Hz, the sample rate in an <code>AudioFormat</code> instance expresses the number
58  * of audio samples for each channel per second in the content you are playing or recording. It is
59  * not the sample rate
60  * at which content is rendered or produced. For instance a sound at a media sample rate of 8000Hz
61  * can be played on a device operating at a sample rate of 48000Hz; the sample rate conversion is
62  * automatically handled by the platform, it will not play at 6x speed.
63  *
64  * <p>As of API {@link android.os.Build.VERSION_CODES#M},
65  * sample rates up to 192kHz are supported
66  * for <code>AudioRecord</code> and <code>AudioTrack</code>, with sample rate conversion
67  * performed as needed.
68  * To improve efficiency and avoid lossy conversions, it is recommended to match the sample rate
69  * for <code>AudioRecord</code> and <code>AudioTrack</code> to the endpoint device
70  * sample rate, and limit the sample rate to no more than 48kHz unless there are special
71  * device capabilities that warrant a higher rate.
72  *
73  * <h4 id="encoding">Encoding</h4>
74  * <p>Audio encoding is used to describe the bit representation of audio data, which can be
75  * either linear PCM or compressed audio, such as AC3 or DTS.
76  * <p>For linear PCM, the audio encoding describes the sample size, 8 bits, 16 bits, or 32 bits,
77  * and the sample representation, integer or float.
78  * <ul>
79  * <li> {@link #ENCODING_PCM_8BIT}: The audio sample is a 8 bit unsigned integer in the
80  * range [0, 255], with a 128 offset for zero. This is typically stored as a Java byte in a
81  * byte array or ByteBuffer. Since the Java byte is <em>signed</em>,
82  * be careful with math operations and conversions as the most significant bit is inverted.
83  * </li>
84  * <li> {@link #ENCODING_PCM_16BIT}: The audio sample is a 16 bit signed integer
85  * typically stored as a Java short in a short array, but when the short
86  * is stored in a ByteBuffer, it is native endian (as compared to the default Java big endian).
87  * The short has full range from [-32768, 32767],
88  * and is sometimes interpreted as fixed point Q.15 data.
89  * </li>
90  * <li> {@link #ENCODING_PCM_FLOAT}: Introduced in
91  * API {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this encoding specifies that
92  * the audio sample is a 32 bit IEEE single precision float. The sample can be
93  * manipulated as a Java float in a float array, though within a ByteBuffer
94  * it is stored in native endian byte order.
95  * The nominal range of <code>ENCODING_PCM_FLOAT</code> audio data is [-1.0, 1.0].
96  * It is implementation dependent whether the positive maximum of 1.0 is included
97  * in the interval. Values outside of the nominal range are clamped before
98  * sending to the endpoint device. Beware that
99  * the handling of NaN is undefined; subnormals may be treated as zero; and
100  * infinities are generally clamped just like other values for <code>AudioTrack</code>
101  * &ndash; try to avoid infinities because they can easily generate a NaN.
102  * <br>
103  * To achieve higher audio bit depth than a signed 16 bit integer short,
104  * it is recommended to use <code>ENCODING_PCM_FLOAT</code> for audio capture, processing,
105  * and playback.
106  * Floats are efficiently manipulated by modern CPUs,
107  * have greater precision than 24 bit signed integers,
108  * and have greater dynamic range than 32 bit signed integers.
109  * <code>AudioRecord</code> as of API {@link android.os.Build.VERSION_CODES#M} and
110  * <code>AudioTrack</code> as of API {@link android.os.Build.VERSION_CODES#LOLLIPOP}
111  * support <code>ENCODING_PCM_FLOAT</code>.
112  * </li>
113  * <li> {@link #ENCODING_PCM_24BIT_PACKED}: Introduced in
114  * API {@link android.os.Build.VERSION_CODES#S},
115  * this encoding specifies the audio sample is an
116  * extended precision 24 bit signed integer
117  * stored as a 3 Java bytes in a {@code ByteBuffer} or byte array in native endian
118  * (see {@link java.nio.ByteOrder#nativeOrder()}).
119  * Each sample has full range from [-8388608, 8388607],
120  * and can be interpreted as fixed point Q.23 data.
121  * </li>
122  * <li> {@link #ENCODING_PCM_32BIT}: Introduced in
123  * API {@link android.os.Build.VERSION_CODES#S},
124  * this encoding specifies the audio sample is an
125  * extended precision 32 bit signed integer
126  * stored as a 4 Java bytes in a {@code ByteBuffer} or byte array in native endian
127  * (see {@link java.nio.ByteOrder#nativeOrder()}).
128  * Each sample has full range from [-2147483648, 2147483647],
129  * and can be interpreted as fixed point Q.31 data.
130  * </li>
131  * </ul>
132  * <p>For compressed audio, the encoding specifies the method of compression,
133  * for example {@link #ENCODING_AC3} and {@link #ENCODING_DTS}. The compressed
134  * audio data is typically stored as bytes in
135  * a byte array or ByteBuffer. When a compressed audio encoding is specified
136  * for an <code>AudioTrack</code>, it creates a direct (non-mixed) track
137  * for output to an endpoint (such as HDMI) capable of decoding the compressed audio.
138  * For (most) other endpoints, which are not capable of decoding such compressed audio,
139  * you will need to decode the data first, typically by creating a {@link MediaCodec}.
140  * Alternatively, one may use {@link MediaPlayer} for playback of compressed
141  * audio files or streams.
142  * <p>When compressed audio is sent out through a direct <code>AudioTrack</code>,
143  * it need not be written in exact multiples of the audio access unit;
144  * this differs from <code>MediaCodec</code> input buffers.
145  *
146  * <h4 id="channelMask">Channel mask</h4>
147  * <p>Channel masks are used in <code>AudioTrack</code> and <code>AudioRecord</code> to describe
148  * the samples and their arrangement in the audio frame. They are also used in the endpoint (e.g.
149  * a USB audio interface, a DAC connected to headphones) to specify allowable configurations of a
150  * particular device.
151  * <br>As of API {@link android.os.Build.VERSION_CODES#M}, there are two types of channel masks:
152  * channel position masks and channel index masks.
153  *
154  * <h5 id="channelPositionMask">Channel position masks</h5>
155  * Channel position masks are the original Android channel masks, and are used since API
156  * {@link android.os.Build.VERSION_CODES#BASE}.
157  * For input and output, they imply a positional nature - the location of a speaker or a microphone
158  * for recording or playback.
159  * <br>For a channel position mask, each allowed channel position corresponds to a bit in the
160  * channel mask. If that channel position is present in the audio frame, that bit is set,
161  * otherwise it is zero. The order of the bits (from lsb to msb) corresponds to the order of that
162  * position's sample in the audio frame.
163  * <br>The canonical channel position masks by channel count are as follows:
164  * <br><table>
165  * <tr><td>channel count</td><td>channel position mask</td></tr>
166  * <tr><td>1</td><td>{@link #CHANNEL_OUT_MONO}</td></tr>
167  * <tr><td>2</td><td>{@link #CHANNEL_OUT_STEREO}</td></tr>
168  * <tr><td>3</td><td>{@link #CHANNEL_OUT_STEREO} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
169  * <tr><td>4</td><td>{@link #CHANNEL_OUT_QUAD}</td></tr>
170  * <tr><td>5</td><td>{@link #CHANNEL_OUT_QUAD} | {@link #CHANNEL_OUT_FRONT_CENTER}</td></tr>
171  * <tr><td>6</td><td>{@link #CHANNEL_OUT_5POINT1}</td></tr>
172  * <tr><td>7</td><td>{@link #CHANNEL_OUT_5POINT1} | {@link #CHANNEL_OUT_BACK_CENTER}</td></tr>
173  * <tr><td>8</td><td>{@link #CHANNEL_OUT_7POINT1_SURROUND}</td></tr>
174  * </table>
175  * <br>These masks are an ORed composite of individual channel masks. For example
176  * {@link #CHANNEL_OUT_STEREO} is composed of {@link #CHANNEL_OUT_FRONT_LEFT} and
177  * {@link #CHANNEL_OUT_FRONT_RIGHT}.
178  * <p>
179  * The following diagram represents the layout of the output channels, as seen from above
180  * the listener (in the center at the "lis" position, facing the front-center channel).
181  * <pre>
182  *       TFL ----- TFC ----- TFR     T is Top
183  *       |  \       |       /  |
184  *       |   FL --- FC --- FR  |     F is Front
185  *       |   |\     |     /|   |
186  *       |   | BFL-BFC-BFR |   |     BF is Bottom Front
187  *       |   |             |   |
188  *       |   FWL   lis   FWR   |     W is Wide
189  *       |   |             |   |
190  *      TSL  SL    TC     SR  TSR    S is Side
191  *       |   |             |   |
192  *       |   BL --- BC -- BR   |     B is Back
193  *       |  /               \  |
194  *       TBL ----- TBC ----- TBR     C is Center, L/R is Left/Right
195  * </pre>
196  * All "T" (top) channels are above the listener, all "BF" (bottom-front) channels are below the
197  * listener, all others are in the listener's horizontal plane. When used in conjunction, LFE1 and
198  * LFE2 are below the listener, when used alone, LFE plane is undefined.
199  * See the channel definitions for the abbreviations
200  *
201  * <h5 id="channelIndexMask">Channel index masks</h5>
202  * Channel index masks are introduced in API {@link android.os.Build.VERSION_CODES#M}. They allow
203  * the selection of a particular channel from the source or sink endpoint by number, i.e. the first
204  * channel, the second channel, and so forth. This avoids problems with artificially assigning
205  * positions to channels of an endpoint, or figuring what the i<sup>th</sup> position bit is within
206  * an endpoint's channel position mask etc.
207  * <br>Here's an example where channel index masks address this confusion: dealing with a 4 channel
208  * USB device. Using a position mask, and based on the channel count, this would be a
209  * {@link #CHANNEL_OUT_QUAD} device, but really one is only interested in channel 0
210  * through channel 3. The USB device would then have the following individual bit channel masks:
211  * {@link #CHANNEL_OUT_FRONT_LEFT},
212  * {@link #CHANNEL_OUT_FRONT_RIGHT}, {@link #CHANNEL_OUT_BACK_LEFT}
213  * and {@link #CHANNEL_OUT_BACK_RIGHT}. But which is channel 0 and which is
214  * channel 3?
215  * <br>For a channel index mask, each channel number is represented as a bit in the mask, from the
216  * lsb (channel 0) upwards to the msb, numerically this bit value is
217  * <code>1 << channelNumber</code>.
218  * A set bit indicates that channel is present in the audio frame, otherwise it is cleared.
219  * The order of the bits also correspond to that channel number's sample order in the audio frame.
220  * <br>For the previous 4 channel USB device example, the device would have a channel index mask
221  * <code>0xF</code>. Suppose we wanted to select only the first and the third channels; this would
222  * correspond to a channel index mask <code>0x5</code> (the first and third bits set). If an
223  * <code>AudioTrack</code> uses this channel index mask, the audio frame would consist of two
224  * samples, the first sample of each frame routed to channel 0, and the second sample of each frame
225  * routed to channel 2.
226  * The canonical channel index masks by channel count are given by the formula
227  * <code>(1 << channelCount) - 1</code>.
228  *
229  * <h5>Use cases</h5>
230  * <ul>
231  * <li><i>Channel position mask for an endpoint:</i> <code>CHANNEL_OUT_FRONT_LEFT</code>,
232  *  <code>CHANNEL_OUT_FRONT_CENTER</code>, etc. for HDMI home theater purposes.
233  * <li><i>Channel position mask for an audio stream:</i> Creating an <code>AudioTrack</code>
234  *  to output movie content, where 5.1 multichannel output is to be written.
235  * <li><i>Channel index mask for an endpoint:</i> USB devices for which input and output do not
236  *  correspond to left or right speaker or microphone.
237  * <li><i>Channel index mask for an audio stream:</i> An <code>AudioRecord</code> may only want the
238  *  third and fourth audio channels of the endpoint (i.e. the second channel pair), and not care the
239  *  about position it corresponds to, in which case the channel index mask is <code>0xC</code>.
240  *  Multichannel <code>AudioRecord</code> sessions should use channel index masks.
241  * </ul>
242  * <h4 id="audioFrame">Audio Frame</h4>
243  * <p>For linear PCM, an audio frame consists of a set of samples captured at the same time,
244  * whose count and
245  * channel association are given by the <a href="#channelMask">channel mask</a>,
246  * and whose sample contents are specified by the <a href="#encoding">encoding</a>.
247  * For example, a stereo 16 bit PCM frame consists of
248  * two 16 bit linear PCM samples, with a frame size of 4 bytes.
249  * For compressed audio, an audio frame may alternately
250  * refer to an access unit of compressed data bytes that is logically grouped together for
251  * decoding and bitstream access (e.g. {@link MediaCodec}),
252  * or a single byte of compressed data (e.g. {@link AudioTrack#getBufferSizeInFrames()
253  * AudioTrack.getBufferSizeInFrames()}),
254  * or the linear PCM frame result from decoding the compressed data
255  * (e.g.{@link AudioTrack#getPlaybackHeadPosition()
256  * AudioTrack.getPlaybackHeadPosition()}),
257  * depending on the context where audio frame is used.
258  * For the purposes of {@link AudioFormat#getFrameSizeInBytes()}, a compressed data format
259  * returns a frame size of 1 byte.
260  */
261 public final class AudioFormat implements Parcelable {
262 
263     //---------------------------------------------------------
264     // Constants
265     //--------------------
266     /** Invalid audio data format */
267     public static final int ENCODING_INVALID = 0;
268     /** Default audio data format */
269     public static final int ENCODING_DEFAULT = 1;
270 
271     // These values must be kept in sync with core/jni/android_media_AudioFormat.h
272     // Also sync av/services/audiopolicy/managerdefault/ConfigParsingUtils.h
273     /** Audio data format: PCM 16 bit per sample. Guaranteed to be supported by devices. */
274     public static final int ENCODING_PCM_16BIT = 2;
275     /** Audio data format: PCM 8 bit per sample. Not guaranteed to be supported by devices. */
276     public static final int ENCODING_PCM_8BIT = 3;
277     /** Audio data format: single-precision floating-point per sample */
278     public static final int ENCODING_PCM_FLOAT = 4;
279     /** Audio data format: AC-3 compressed, also known as Dolby Digital */
280     public static final int ENCODING_AC3 = 5;
281     /** Audio data format: E-AC-3 compressed, also known as Dolby Digital Plus or DD+ */
282     public static final int ENCODING_E_AC3 = 6;
283     /** Audio data format: DTS compressed */
284     public static final int ENCODING_DTS = 7;
285     /** Audio data format: DTS HD compressed */
286     public static final int ENCODING_DTS_HD = 8;
287     /** Audio data format: MP3 compressed */
288     public static final int ENCODING_MP3 = 9;
289     /** Audio data format: AAC LC compressed */
290     public static final int ENCODING_AAC_LC = 10;
291     /** Audio data format: AAC HE V1 compressed */
292     public static final int ENCODING_AAC_HE_V1 = 11;
293     /** Audio data format: AAC HE V2 compressed */
294     public static final int ENCODING_AAC_HE_V2 = 12;
295 
296     /** Audio data format: compressed audio wrapped in PCM for HDMI
297      * or S/PDIF passthrough.
298      * For devices whose SDK version is less than {@link android.os.Build.VERSION_CODES#S}, the
299      * channel mask of IEC61937 track must be {@link #CHANNEL_OUT_STEREO}.
300      * Data should be written to the stream in a short[] array.
301      * If the data is written in a byte[] array then there may be endian problems
302      * on some platforms when converting to short internally.
303      */
304     public static final int ENCODING_IEC61937 = 13;
305     /** Audio data format: DOLBY TRUEHD compressed
306      **/
307     public static final int ENCODING_DOLBY_TRUEHD = 14;
308     /** Audio data format: AAC ELD compressed */
309     public static final int ENCODING_AAC_ELD = 15;
310     /** Audio data format: AAC xHE compressed */
311     public static final int ENCODING_AAC_XHE = 16;
312     /** Audio data format: AC-4 sync frame transport format */
313     public static final int ENCODING_AC4 = 17;
314     /** Audio data format: E-AC-3-JOC compressed
315      * E-AC-3-JOC streams can be decoded by downstream devices supporting {@link #ENCODING_E_AC3}.
316      * Use {@link #ENCODING_E_AC3} as the AudioTrack encoding when the downstream device
317      * supports {@link #ENCODING_E_AC3} but not {@link #ENCODING_E_AC3_JOC}.
318      **/
319     public static final int ENCODING_E_AC3_JOC = 18;
320     /** Audio data format: Dolby MAT (Metadata-enhanced Audio Transmission)
321      * Dolby MAT bitstreams are used to transmit Dolby TrueHD, channel-based PCM, or PCM with
322      * metadata (object audio) over HDMI (e.g. Dolby Atmos content).
323      **/
324     public static final int ENCODING_DOLBY_MAT = 19;
325     /** Audio data format: OPUS compressed. */
326     public static final int ENCODING_OPUS = 20;
327 
328     /** @hide
329      * We do not permit legacy short array reads or writes for encodings
330      * introduced after this threshold.
331      */
332     public static final int ENCODING_LEGACY_SHORT_ARRAY_THRESHOLD = ENCODING_OPUS;
333 
334     /** Audio data format: PCM 24 bit per sample packed as 3 bytes.
335      *
336      * The bytes are in little-endian order, so the least significant byte
337      * comes first in the byte array.
338      *
339      * Not guaranteed to be supported by devices, may be emulated if not supported. */
340     public static final int ENCODING_PCM_24BIT_PACKED = 21;
341     /** Audio data format: PCM 32 bit per sample.
342      * Not guaranteed to be supported by devices, may be emulated if not supported. */
343     public static final int ENCODING_PCM_32BIT = 22;
344 
345     /** Audio data format: MPEG-H baseline profile, level 3 */
346     public static final int ENCODING_MPEGH_BL_L3 = 23;
347     /** Audio data format: MPEG-H baseline profile, level 4 */
348     public static final int ENCODING_MPEGH_BL_L4 = 24;
349     /** Audio data format: MPEG-H low complexity profile, level 3 */
350     public static final int ENCODING_MPEGH_LC_L3 = 25;
351     /** Audio data format: MPEG-H low complexity profile, level 4 */
352     public static final int ENCODING_MPEGH_LC_L4 = 26;
353     /** Audio data format: DTS UHD compressed */
354     public static final int ENCODING_DTS_UHD = 27;
355     /** Audio data format: DRA compressed */
356     public static final int ENCODING_DRA = 28;
357 
358     /** @hide */
toLogFriendlyEncoding(int enc)359     public static String toLogFriendlyEncoding(int enc) {
360         switch(enc) {
361             case ENCODING_INVALID:
362                 return "ENCODING_INVALID";
363             case ENCODING_PCM_16BIT:
364                 return "ENCODING_PCM_16BIT";
365             case ENCODING_PCM_8BIT:
366                 return "ENCODING_PCM_8BIT";
367             case ENCODING_PCM_FLOAT:
368                 return "ENCODING_PCM_FLOAT";
369             case ENCODING_AC3:
370                 return "ENCODING_AC3";
371             case ENCODING_E_AC3:
372                 return "ENCODING_E_AC3";
373             case ENCODING_DTS:
374                 return "ENCODING_DTS";
375             case ENCODING_DTS_HD:
376                 return "ENCODING_DTS_HD";
377             case ENCODING_MP3:
378                 return "ENCODING_MP3";
379             case ENCODING_AAC_LC:
380                 return "ENCODING_AAC_LC";
381             case ENCODING_AAC_HE_V1:
382                 return "ENCODING_AAC_HE_V1";
383             case ENCODING_AAC_HE_V2:
384                 return "ENCODING_AAC_HE_V2";
385             case ENCODING_IEC61937:
386                 return "ENCODING_IEC61937";
387             case ENCODING_DOLBY_TRUEHD:
388                 return "ENCODING_DOLBY_TRUEHD";
389             case ENCODING_AAC_ELD:
390                 return "ENCODING_AAC_ELD";
391             case ENCODING_AAC_XHE:
392                 return "ENCODING_AAC_XHE";
393             case ENCODING_AC4:
394                 return "ENCODING_AC4";
395             case ENCODING_E_AC3_JOC:
396                 return "ENCODING_E_AC3_JOC";
397             case ENCODING_DOLBY_MAT:
398                 return "ENCODING_DOLBY_MAT";
399             case ENCODING_OPUS:
400                 return "ENCODING_OPUS";
401             case ENCODING_PCM_24BIT_PACKED:
402                 return "ENCODING_PCM_24BIT_PACKED";
403             case ENCODING_PCM_32BIT:
404                 return "ENCODING_PCM_32BIT";
405             case ENCODING_MPEGH_BL_L3:
406                 return "ENCODING_MPEGH_BL_L3";
407             case ENCODING_MPEGH_BL_L4:
408                 return "ENCODING_MPEGH_BL_L4";
409             case ENCODING_MPEGH_LC_L3:
410                 return "ENCODING_MPEGH_LC_L3";
411             case ENCODING_MPEGH_LC_L4:
412                 return "ENCODING_MPEGH_LC_L4";
413             case ENCODING_DTS_UHD:
414                 return "ENCODING_DTS_UHD";
415             case ENCODING_DRA:
416                 return "ENCODING_DRA";
417             default :
418                 return "invalid encoding " + enc;
419         }
420     }
421 
422     /** Invalid audio channel configuration */
423     /** @deprecated Use {@link #CHANNEL_INVALID} instead.  */
424     @Deprecated    public static final int CHANNEL_CONFIGURATION_INVALID   = 0;
425     /** Default audio channel configuration */
426     /** @deprecated Use {@link #CHANNEL_OUT_DEFAULT} or {@link #CHANNEL_IN_DEFAULT} instead.  */
427     @Deprecated    public static final int CHANNEL_CONFIGURATION_DEFAULT   = 1;
428     /** Mono audio configuration */
429     /** @deprecated Use {@link #CHANNEL_OUT_MONO} or {@link #CHANNEL_IN_MONO} instead.  */
430     @Deprecated    public static final int CHANNEL_CONFIGURATION_MONO      = 2;
431     /** Stereo (2 channel) audio configuration */
432     /** @deprecated Use {@link #CHANNEL_OUT_STEREO} or {@link #CHANNEL_IN_STEREO} instead.  */
433     @Deprecated    public static final int CHANNEL_CONFIGURATION_STEREO    = 3;
434 
435     /** Invalid audio channel mask */
436     public static final int CHANNEL_INVALID = 0;
437     /** Default audio channel mask */
438     public static final int CHANNEL_OUT_DEFAULT = 1;
439 
440     // Output channel mask definitions below are translated to the native values defined in
441     //  in /system/media/audio/include/system/audio.h in the JNI code of AudioTrack
442     /** Front left output channel (see FL in channel diagram) */
443     public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;
444     /** Front right output channel (see FR in channel diagram) */
445     public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;
446     /** Front center output channel (see FC in channel diagram) */
447     public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;
448     /** LFE "low frequency effect" channel
449      * When used in conjunction with {@link #CHANNEL_OUT_LOW_FREQUENCY_2}, it is intended
450      * to contain the left low-frequency effect signal, also referred to as "LFE1"
451      * in ITU-R BS.2159-8 */
452     public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;
453     /** Back left output channel (see BL in channel diagram) */
454     public static final int CHANNEL_OUT_BACK_LEFT = 0x40;
455     /** Back right output channel (see BR in channel diagram) */
456     public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;
457     public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;
458     public static final int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200;
459     /** Back center output channel (see BC in channel diagram) */
460     public static final int CHANNEL_OUT_BACK_CENTER = 0x400;
461     /** Side left output channel (see SL in channel diagram) */
462     public static final int CHANNEL_OUT_SIDE_LEFT =         0x800;
463     /** Side right output channel (see SR in channel diagram) */
464     public static final int CHANNEL_OUT_SIDE_RIGHT =       0x1000;
465     /** Top center (above listener) output channel (see TC in channel diagram) */
466     public static final int CHANNEL_OUT_TOP_CENTER =       0x2000;
467     /** Top front left output channel (see TFL in channel diagram above FL) */
468     public static final int CHANNEL_OUT_TOP_FRONT_LEFT =   0x4000;
469     /** Top front center output channel (see TFC in channel diagram above FC) */
470     public static final int CHANNEL_OUT_TOP_FRONT_CENTER = 0x8000;
471     /** Top front right output channel (see TFR in channel diagram above FR) */
472     public static final int CHANNEL_OUT_TOP_FRONT_RIGHT = 0x10000;
473     /** Top back left output channel (see TBL in channel diagram above BL) */
474     public static final int CHANNEL_OUT_TOP_BACK_LEFT =   0x20000;
475     /** Top back center output channel (see TBC in channel diagram above BC) */
476     public static final int CHANNEL_OUT_TOP_BACK_CENTER = 0x40000;
477     /** Top back right output channel (see TBR in channel diagram above BR) */
478     public static final int CHANNEL_OUT_TOP_BACK_RIGHT =  0x80000;
479     /** Top side left output channel (see TSL in channel diagram above SL) */
480     public static final int CHANNEL_OUT_TOP_SIDE_LEFT = 0x100000;
481     /** Top side right output channel (see TSR in channel diagram above SR) */
482     public static final int CHANNEL_OUT_TOP_SIDE_RIGHT = 0x200000;
483     /** Bottom front left output channel (see BFL in channel diagram below FL) */
484     public static final int CHANNEL_OUT_BOTTOM_FRONT_LEFT = 0x400000;
485     /** Bottom front center output channel (see BFC in channel diagram below FC) */
486     public static final int CHANNEL_OUT_BOTTOM_FRONT_CENTER = 0x800000;
487     /** Bottom front right output channel (see BFR in channel diagram below FR) */
488     public static final int CHANNEL_OUT_BOTTOM_FRONT_RIGHT = 0x1000000;
489     /** The second LFE channel
490      * When used in conjunction with {@link #CHANNEL_OUT_LOW_FREQUENCY}, it is intended
491      * to contain the right low-frequency effect signal, also referred to as "LFE2"
492      * in ITU-R BS.2159-8 */
493     public static final int CHANNEL_OUT_LOW_FREQUENCY_2 = 0x2000000;
494     /** Front wide left output channel (see FWL in channel diagram) */
495     public static final int CHANNEL_OUT_FRONT_WIDE_LEFT = 0x4000000;
496     /** Front wide right output channel (see FWR in channel diagram) */
497     public static final int CHANNEL_OUT_FRONT_WIDE_RIGHT = 0x8000000;
498     /** @hide
499      * Haptic channels can be used by internal framework code. Use the same values as in native.
500      */
501     public static final int CHANNEL_OUT_HAPTIC_B = 0x10000000;
502     /** @hide */
503     public static final int CHANNEL_OUT_HAPTIC_A = 0x20000000;
504 
505     public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
506     public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
507     // aka QUAD_BACK
508     public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
509             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
510     /** @hide */
511     public static final int CHANNEL_OUT_QUAD_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
512             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
513     public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
514             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
515     // aka 5POINT1_BACK
516     /** Output channel mask for 5.1 */
517     public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
518             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
519     /** @hide */
520     public static final int CHANNEL_OUT_5POINT1_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
521             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY |
522             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
523     // different from AUDIO_CHANNEL_OUT_7POINT1 used internally, and not accepted by AudioRecord.
524     /** @deprecated Not the typical 7.1 surround configuration. Use {@link #CHANNEL_OUT_7POINT1_SURROUND} instead. */
525     @Deprecated    public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
526             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
527             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
528     /** Output channel mask for 7.1 */
529     // matches AUDIO_CHANNEL_OUT_7POINT1
530     public static final int CHANNEL_OUT_7POINT1_SURROUND = (
531             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
532             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
533             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
534             CHANNEL_OUT_LOW_FREQUENCY);
535     /** Output channel mask for 5.1.2
536      *  Same as 5.1 with the addition of left and right top channels */
537     public static final int CHANNEL_OUT_5POINT1POINT2 = (CHANNEL_OUT_5POINT1 |
538             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
539     /** Output channel mask for 5.1.4
540      * Same as 5.1 with the addition of four top channels */
541     public static final int CHANNEL_OUT_5POINT1POINT4 = (CHANNEL_OUT_5POINT1 |
542             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_RIGHT |
543             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT);
544     /** Output channel mask for 7.1.2
545      * Same as 7.1 with the addition of left and right top channels*/
546     public static final int CHANNEL_OUT_7POINT1POINT2 = (CHANNEL_OUT_7POINT1_SURROUND |
547             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
548     /** Output channel mask for 7.1.4
549      *  Same as 7.1 with the addition of four top channels */
550     public static final int CHANNEL_OUT_7POINT1POINT4 = (CHANNEL_OUT_7POINT1_SURROUND |
551             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_RIGHT |
552             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT);
553     /** Output channel mask for 9.1.4
554      * Same as 7.1.4 with the addition of left and right front wide channels */
555     public static final int CHANNEL_OUT_9POINT1POINT4 = (CHANNEL_OUT_7POINT1POINT4
556             | CHANNEL_OUT_FRONT_WIDE_LEFT | CHANNEL_OUT_FRONT_WIDE_RIGHT);
557     /** Output channel mask for 9.1.6
558      * Same as 9.1.4 with the addition of left and right top side channels */
559     public static final int CHANNEL_OUT_9POINT1POINT6 = (CHANNEL_OUT_9POINT1POINT4
560             | CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
561     /** @hide */
562     public static final int CHANNEL_OUT_13POINT_360RA = (
563             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
564             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
565             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_CENTER |
566             CHANNEL_OUT_TOP_FRONT_RIGHT |
567             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT |
568             CHANNEL_OUT_BOTTOM_FRONT_LEFT | CHANNEL_OUT_BOTTOM_FRONT_CENTER |
569             CHANNEL_OUT_BOTTOM_FRONT_RIGHT);
570     /** @hide */
571     public static final int CHANNEL_OUT_22POINT2 = (CHANNEL_OUT_7POINT1POINT4 |
572             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
573             CHANNEL_OUT_BACK_CENTER | CHANNEL_OUT_TOP_CENTER |
574             CHANNEL_OUT_TOP_FRONT_CENTER | CHANNEL_OUT_TOP_BACK_CENTER |
575             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT |
576             CHANNEL_OUT_BOTTOM_FRONT_LEFT | CHANNEL_OUT_BOTTOM_FRONT_RIGHT |
577             CHANNEL_OUT_BOTTOM_FRONT_CENTER |
578             CHANNEL_OUT_LOW_FREQUENCY_2);
579     // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL
580 
581     /** Minimum value for sample rate,
582      *  assuming AudioTrack and AudioRecord share the same limitations.
583      * @hide
584      */
585     // never unhide
586     public static final int SAMPLE_RATE_HZ_MIN = AudioSystem.SAMPLE_RATE_HZ_MIN;
587     /** Maximum value for sample rate,
588      *  assuming AudioTrack and AudioRecord share the same limitations.
589      * @hide
590      */
591     // never unhide
592     public static final int SAMPLE_RATE_HZ_MAX = AudioSystem.SAMPLE_RATE_HZ_MAX;
593     /** Sample rate will be a route-dependent value.
594      * For AudioTrack, it is usually the sink sample rate,
595      * and for AudioRecord it is usually the source sample rate.
596      */
597     public static final int SAMPLE_RATE_UNSPECIFIED = 0;
598 
599     /**
600      * @hide
601      * Return the input channel mask corresponding to an output channel mask.
602      * This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
603      * @param outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
604      * @return a combination of CHANNEL_IN_* definitions matching an output channel mask
605      * @throws IllegalArgumentException
606      */
inChannelMaskFromOutChannelMask(int outMask)607     public static int inChannelMaskFromOutChannelMask(int outMask) throws IllegalArgumentException {
608         if (outMask == CHANNEL_OUT_DEFAULT) {
609             throw new IllegalArgumentException(
610                     "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
611         }
612         switch (channelCountFromOutChannelMask(outMask)) {
613             case 1:
614                 return CHANNEL_IN_MONO;
615             case 2:
616                 return CHANNEL_IN_STEREO;
617             default:
618                 throw new IllegalArgumentException("Unsupported channel configuration for input.");
619         }
620     }
621 
622     /**
623      * @hide
624      * Return the number of channels from an input channel mask
625      * @param mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
626      * @return number of channels for the mask
627      */
628     @TestApi
channelCountFromInChannelMask(int mask)629     public static int channelCountFromInChannelMask(int mask) {
630         return Integer.bitCount(mask);
631     }
632     /**
633      * @hide
634      * Return the number of channels from an output channel mask
635      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
636      * @return number of channels for the mask
637      */
638     @TestApi
channelCountFromOutChannelMask(int mask)639     public static int channelCountFromOutChannelMask(int mask) {
640         return Integer.bitCount(mask);
641     }
642     /**
643      * @hide
644      * Return a channel mask ready to be used by native code
645      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
646      * @return a native channel mask
647      */
convertChannelOutMaskToNativeMask(int javaMask)648     public static int convertChannelOutMaskToNativeMask(int javaMask) {
649         return (javaMask >> 2);
650     }
651 
652     /**
653      * @hide
654      * Return a java output channel mask
655      * @param mask a native channel mask
656      * @return a combination of the CHANNEL_OUT_* definitions
657      */
convertNativeChannelMaskToOutMask(int nativeMask)658     public static int convertNativeChannelMaskToOutMask(int nativeMask) {
659         return (nativeMask << 2);
660     }
661 
662     public static final int CHANNEL_IN_DEFAULT = 1;
663     // These directly match native
664     public static final int CHANNEL_IN_LEFT = 0x4;
665     public static final int CHANNEL_IN_RIGHT = 0x8;
666     public static final int CHANNEL_IN_FRONT = 0x10;
667     public static final int CHANNEL_IN_BACK = 0x20;
668     public static final int CHANNEL_IN_LEFT_PROCESSED = 0x40;
669     public static final int CHANNEL_IN_RIGHT_PROCESSED = 0x80;
670     public static final int CHANNEL_IN_FRONT_PROCESSED = 0x100;
671     public static final int CHANNEL_IN_BACK_PROCESSED = 0x200;
672     public static final int CHANNEL_IN_PRESSURE = 0x400;
673     public static final int CHANNEL_IN_X_AXIS = 0x800;
674     public static final int CHANNEL_IN_Y_AXIS = 0x1000;
675     public static final int CHANNEL_IN_Z_AXIS = 0x2000;
676     public static final int CHANNEL_IN_VOICE_UPLINK = 0x4000;
677     public static final int CHANNEL_IN_VOICE_DNLINK = 0x8000;
678     // CHANNEL_IN_BACK_LEFT to TOP_RIGHT are not microphone positions
679     // but surround channels which are used when dealing with multi-channel inputs,
680     // e.g. via HDMI input on TV.
681     /** @hide */
682     public static final int CHANNEL_IN_BACK_LEFT = 0x10000;
683     /** @hide */
684     public static final int CHANNEL_IN_BACK_RIGHT = 0x20000;
685     /** @hide */
686     public static final int CHANNEL_IN_CENTER = 0x40000;
687     /** @hide */
688     public static final int CHANNEL_IN_LOW_FREQUENCY = 0x100000;
689     /** @hide */
690     public static final int CHANNEL_IN_TOP_LEFT = 0x200000;
691     /** @hide */
692     public static final int CHANNEL_IN_TOP_RIGHT = 0x400000;
693     public static final int CHANNEL_IN_MONO = CHANNEL_IN_FRONT;
694     public static final int CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT);
695     // Surround channel masks corresponding to output masks, used for
696     // surround sound inputs.
697     /** @hide */
698     public static final int CHANNEL_IN_2POINT0POINT2 = (
699             CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT | CHANNEL_IN_TOP_LEFT | CHANNEL_IN_TOP_RIGHT);
700     /** @hide */
701     public static final int CHANNEL_IN_2POINT1POINT2 = (
702             CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT | CHANNEL_IN_TOP_LEFT | CHANNEL_IN_TOP_RIGHT
703             | CHANNEL_IN_LOW_FREQUENCY);
704     /** @hide */
705     public static final int CHANNEL_IN_3POINT0POINT2 = (
706             CHANNEL_IN_LEFT | CHANNEL_IN_CENTER | CHANNEL_IN_RIGHT | CHANNEL_IN_TOP_LEFT
707             | CHANNEL_IN_TOP_RIGHT);
708     /** @hide */
709     public static final int CHANNEL_IN_3POINT1POINT2 = (
710             CHANNEL_IN_LEFT | CHANNEL_IN_CENTER | CHANNEL_IN_RIGHT | CHANNEL_IN_TOP_LEFT
711             | CHANNEL_IN_TOP_RIGHT | CHANNEL_IN_LOW_FREQUENCY);
712     /** @hide */
713     public static final int CHANNEL_IN_5POINT1 = (
714             CHANNEL_IN_LEFT | CHANNEL_IN_CENTER | CHANNEL_IN_RIGHT | CHANNEL_IN_BACK_LEFT
715             | CHANNEL_IN_BACK_RIGHT | CHANNEL_IN_LOW_FREQUENCY);
716     /** @hide */
717     public static final int CHANNEL_IN_FRONT_BACK = CHANNEL_IN_FRONT | CHANNEL_IN_BACK;
718     // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL
719 
720     /** @hide */
721     @TestApi
getBytesPerSample(int audioFormat)722     public static int getBytesPerSample(int audioFormat)
723     {
724         switch (audioFormat) {
725             case ENCODING_PCM_8BIT:
726                 return 1;
727             case ENCODING_PCM_16BIT:
728             case ENCODING_IEC61937:
729             case ENCODING_DEFAULT:
730                 return 2;
731             case ENCODING_PCM_24BIT_PACKED:
732                 return 3;
733             case ENCODING_PCM_FLOAT:
734             case ENCODING_PCM_32BIT:
735                 return 4;
736             case ENCODING_INVALID:
737             default:
738                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
739         }
740     }
741 
742     /** @hide */
isValidEncoding(int audioFormat)743     public static boolean isValidEncoding(int audioFormat)
744     {
745         switch (audioFormat) {
746             case ENCODING_PCM_16BIT:
747             case ENCODING_PCM_8BIT:
748             case ENCODING_PCM_FLOAT:
749             case ENCODING_AC3:
750             case ENCODING_E_AC3:
751             case ENCODING_DTS:
752             case ENCODING_DTS_HD:
753             case ENCODING_MP3:
754             case ENCODING_AAC_LC:
755             case ENCODING_AAC_HE_V1:
756             case ENCODING_AAC_HE_V2:
757             case ENCODING_IEC61937:
758             case ENCODING_DOLBY_TRUEHD:
759             case ENCODING_AAC_ELD:
760             case ENCODING_AAC_XHE:
761             case ENCODING_AC4:
762             case ENCODING_E_AC3_JOC:
763             case ENCODING_DOLBY_MAT:
764             case ENCODING_OPUS:
765             case ENCODING_PCM_24BIT_PACKED:
766             case ENCODING_PCM_32BIT:
767             case ENCODING_MPEGH_BL_L3:
768             case ENCODING_MPEGH_BL_L4:
769             case ENCODING_MPEGH_LC_L3:
770             case ENCODING_MPEGH_LC_L4:
771             case ENCODING_DTS_UHD:
772             case ENCODING_DRA:
773                 return true;
774             default:
775                 return false;
776         }
777     }
778 
779     /** @hide */
isPublicEncoding(int audioFormat)780     public static boolean isPublicEncoding(int audioFormat)
781     {
782         switch (audioFormat) {
783             case ENCODING_PCM_16BIT:
784             case ENCODING_PCM_8BIT:
785             case ENCODING_PCM_FLOAT:
786             case ENCODING_AC3:
787             case ENCODING_E_AC3:
788             case ENCODING_DTS:
789             case ENCODING_DTS_HD:
790             case ENCODING_MP3:
791             case ENCODING_AAC_LC:
792             case ENCODING_AAC_HE_V1:
793             case ENCODING_AAC_HE_V2:
794             case ENCODING_IEC61937:
795             case ENCODING_DOLBY_TRUEHD:
796             case ENCODING_AAC_ELD:
797             case ENCODING_AAC_XHE:
798             case ENCODING_AC4:
799             case ENCODING_E_AC3_JOC:
800             case ENCODING_DOLBY_MAT:
801             case ENCODING_OPUS:
802             case ENCODING_PCM_24BIT_PACKED:
803             case ENCODING_PCM_32BIT:
804             case ENCODING_MPEGH_BL_L3:
805             case ENCODING_MPEGH_BL_L4:
806             case ENCODING_MPEGH_LC_L3:
807             case ENCODING_MPEGH_LC_L4:
808             case ENCODING_DTS_UHD:
809             case ENCODING_DRA:
810                 return true;
811             default:
812                 return false;
813         }
814     }
815 
816     /** @hide */
817     @TestApi
isEncodingLinearPcm(int audioFormat)818     public static boolean isEncodingLinearPcm(int audioFormat)
819     {
820         switch (audioFormat) {
821             case ENCODING_PCM_16BIT:
822             case ENCODING_PCM_8BIT:
823             case ENCODING_PCM_FLOAT:
824             case ENCODING_PCM_24BIT_PACKED:
825             case ENCODING_PCM_32BIT:
826             case ENCODING_DEFAULT:
827                 return true;
828             case ENCODING_AC3:
829             case ENCODING_E_AC3:
830             case ENCODING_DTS:
831             case ENCODING_DTS_HD:
832             case ENCODING_MP3:
833             case ENCODING_AAC_LC:
834             case ENCODING_AAC_HE_V1:
835             case ENCODING_AAC_HE_V2:
836             case ENCODING_IEC61937: // wrapped in PCM but compressed
837             case ENCODING_DOLBY_TRUEHD:
838             case ENCODING_AAC_ELD:
839             case ENCODING_AAC_XHE:
840             case ENCODING_AC4:
841             case ENCODING_E_AC3_JOC:
842             case ENCODING_DOLBY_MAT:
843             case ENCODING_OPUS:
844             case ENCODING_MPEGH_BL_L3:
845             case ENCODING_MPEGH_BL_L4:
846             case ENCODING_MPEGH_LC_L3:
847             case ENCODING_MPEGH_LC_L4:
848             case ENCODING_DTS_UHD:
849             case ENCODING_DRA:
850                 return false;
851             case ENCODING_INVALID:
852             default:
853                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
854         }
855     }
856 
857     /** @hide */
isEncodingLinearFrames(int audioFormat)858     public static boolean isEncodingLinearFrames(int audioFormat)
859     {
860         switch (audioFormat) {
861             case ENCODING_PCM_16BIT:
862             case ENCODING_PCM_8BIT:
863             case ENCODING_PCM_FLOAT:
864             case ENCODING_IEC61937: // same size as stereo PCM
865             case ENCODING_PCM_24BIT_PACKED:
866             case ENCODING_PCM_32BIT:
867             case ENCODING_DEFAULT:
868                 return true;
869             case ENCODING_AC3:
870             case ENCODING_E_AC3:
871             case ENCODING_DTS:
872             case ENCODING_DTS_HD:
873             case ENCODING_MP3:
874             case ENCODING_AAC_LC:
875             case ENCODING_AAC_HE_V1:
876             case ENCODING_AAC_HE_V2:
877             case ENCODING_DOLBY_TRUEHD:
878             case ENCODING_AAC_ELD:
879             case ENCODING_AAC_XHE:
880             case ENCODING_AC4:
881             case ENCODING_E_AC3_JOC:
882             case ENCODING_DOLBY_MAT:
883             case ENCODING_OPUS:
884             case ENCODING_MPEGH_BL_L3:
885             case ENCODING_MPEGH_BL_L4:
886             case ENCODING_MPEGH_LC_L3:
887             case ENCODING_MPEGH_LC_L4:
888             case ENCODING_DTS_UHD:
889             case ENCODING_DRA:
890                 return false;
891             case ENCODING_INVALID:
892             default:
893                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
894         }
895     }
896     /**
897      * Returns an array of public encoding values extracted from an array of
898      * encoding values.
899      * @hide
900      */
filterPublicFormats(int[] formats)901     public static int[] filterPublicFormats(int[] formats) {
902         if (formats == null) {
903             return null;
904         }
905         int[] myCopy = Arrays.copyOf(formats, formats.length);
906         int size = 0;
907         for (int i = 0; i < myCopy.length; i++) {
908             if (isPublicEncoding(myCopy[i])) {
909                 if (size != i) {
910                     myCopy[size] = myCopy[i];
911                 }
912                 size++;
913             }
914         }
915         return Arrays.copyOf(myCopy, size);
916     }
917 
918     /** @removed */
AudioFormat()919     public AudioFormat()
920     {
921         throw new UnsupportedOperationException("There is no valid usage of this constructor");
922     }
923 
924     /**
925      * Constructor used by the JNI.  Parameters are not checked for validity.
926      */
927     // Update sound trigger JNI in core/jni/android_hardware_SoundTrigger.cpp when modifying this
928     // constructor
929     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask)930     private AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask) {
931         this(
932              AUDIO_FORMAT_HAS_PROPERTY_ENCODING
933              | AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE
934              | AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK
935              | AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK,
936              encoding, sampleRate, channelMask, channelIndexMask
937              );
938     }
939 
AudioFormat(int propertySetMask, int encoding, int sampleRate, int channelMask, int channelIndexMask)940     private AudioFormat(int propertySetMask,
941             int encoding, int sampleRate, int channelMask, int channelIndexMask) {
942         mPropertySetMask = propertySetMask;
943         mEncoding = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0
944                 ? encoding : ENCODING_INVALID;
945         mSampleRate = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0
946                 ? sampleRate : SAMPLE_RATE_UNSPECIFIED;
947         mChannelMask = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0
948                 ? channelMask : CHANNEL_INVALID;
949         mChannelIndexMask = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0
950                 ? channelIndexMask : CHANNEL_INVALID;
951 
952         // Compute derived values.
953 
954         final int channelIndexCount = Integer.bitCount(getChannelIndexMask());
955         int channelCount = channelCountFromOutChannelMask(getChannelMask());
956         if (channelCount == 0) {
957             channelCount = channelIndexCount;
958         } else if (channelCount != channelIndexCount && channelIndexCount != 0) {
959             channelCount = 0; // position and index channel count mismatch
960         }
961         mChannelCount = channelCount;
962 
963         int frameSizeInBytes = 1;
964         try {
965             frameSizeInBytes = getBytesPerSample(mEncoding) * channelCount;
966         } catch (IllegalArgumentException iae) {
967             // ignored
968         }
969         // it is possible that channel count is 0, so ensure we return 1 for
970         // mFrameSizeInBytes for consistency.
971         mFrameSizeInBytes = frameSizeInBytes != 0 ? frameSizeInBytes : 1;
972     }
973 
974     /** @hide */
975     public final static int AUDIO_FORMAT_HAS_PROPERTY_NONE = 0x0;
976     /** @hide */
977     public final static int AUDIO_FORMAT_HAS_PROPERTY_ENCODING = 0x1 << 0;
978     /** @hide */
979     public final static int AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE = 0x1 << 1;
980     /** @hide */
981     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK = 0x1 << 2;
982     /** @hide */
983     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK = 0x1 << 3;
984 
985     // This is an immutable class, all member variables are final.
986 
987     // Essential values.
988     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
989     private final int mEncoding;
990     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
991     private final int mSampleRate;
992     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
993     private final int mChannelMask;
994     private final int mChannelIndexMask;
995     private final int mPropertySetMask;
996 
997     // Derived values computed in the constructor, cached here.
998     private final int mChannelCount;
999     private final int mFrameSizeInBytes;
1000 
1001     /**
1002      * Return the encoding.
1003      * See the section on <a href="#encoding">encodings</a> for more information about the different
1004      * types of supported audio encoding.
1005      * @return one of the values that can be set in {@link Builder#setEncoding(int)} or
1006      * {@link AudioFormat#ENCODING_INVALID} if not set.
1007      */
getEncoding()1008     public int getEncoding() {
1009         return mEncoding;
1010     }
1011 
1012     /**
1013      * Return the sample rate.
1014      * @return one of the values that can be set in {@link Builder#setSampleRate(int)} or
1015      * {@link #SAMPLE_RATE_UNSPECIFIED} if not set.
1016      */
getSampleRate()1017     public int getSampleRate() {
1018         return mSampleRate;
1019     }
1020 
1021     /**
1022      * Return the channel mask.
1023      * See the section on <a href="#channelMask">channel masks</a> for more information about
1024      * the difference between index-based masks(as returned by {@link #getChannelIndexMask()}) and
1025      * the position-based mask returned by this function.
1026      * @return one of the values that can be set in {@link Builder#setChannelMask(int)} or
1027      * {@link AudioFormat#CHANNEL_INVALID} if not set.
1028      */
getChannelMask()1029     public int getChannelMask() {
1030         return mChannelMask;
1031     }
1032 
1033     /**
1034      * Return the channel index mask.
1035      * See the section on <a href="#channelMask">channel masks</a> for more information about
1036      * the difference between index-based masks, and position-based masks (as returned
1037      * by {@link #getChannelMask()}).
1038      * @return one of the values that can be set in {@link Builder#setChannelIndexMask(int)} or
1039      * {@link AudioFormat#CHANNEL_INVALID} if not set or an invalid mask was used.
1040      */
getChannelIndexMask()1041     public int getChannelIndexMask() {
1042         return mChannelIndexMask;
1043     }
1044 
1045     /**
1046      * Return the channel count.
1047      * @return the channel count derived from the channel position mask or the channel index mask.
1048      * Zero is returned if both the channel position mask and the channel index mask are not set.
1049      */
getChannelCount()1050     public int getChannelCount() {
1051         return mChannelCount;
1052     }
1053 
1054     /**
1055      * Return the frame size in bytes.
1056      *
1057      * For PCM or PCM packed compressed data this is the size of a sample multiplied
1058      * by the channel count. For all other cases, including invalid/unset channel masks,
1059      * this will return 1 byte.
1060      * As an example, a stereo 16-bit PCM format would have a frame size of 4 bytes,
1061      * an 8 channel float PCM format would have a frame size of 32 bytes,
1062      * and a compressed data format (not packed in PCM) would have a frame size of 1 byte.
1063      *
1064      * Both {@link AudioRecord} or {@link AudioTrack} process data in multiples of
1065      * this frame size.
1066      *
1067      * @return The audio frame size in bytes corresponding to the encoding and the channel mask.
1068      */
getFrameSizeInBytes()1069     public @IntRange(from = 1) int getFrameSizeInBytes() {
1070         return mFrameSizeInBytes;
1071     }
1072 
1073     /** @hide */
getPropertySetMask()1074     public int getPropertySetMask() {
1075         return mPropertySetMask;
1076     }
1077 
1078     /** @hide */
toLogFriendlyString()1079     public String toLogFriendlyString() {
1080         return String.format("%dch %dHz %s",
1081                 mChannelCount, mSampleRate, toLogFriendlyEncoding(mEncoding));
1082     }
1083 
1084     /**
1085      * Builder class for {@link AudioFormat} objects.
1086      * Use this class to configure and create an AudioFormat instance. By setting format
1087      * characteristics such as audio encoding, channel mask or sample rate, you indicate which
1088      * of those are to vary from the default behavior on this device wherever this audio format
1089      * is used. See {@link AudioFormat} for a complete description of the different parameters that
1090      * can be used to configure an <code>AudioFormat</code> instance.
1091      * <p>{@link AudioFormat} is for instance used in
1092      * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. In this
1093      * constructor, every format characteristic set on the <code>Builder</code> (e.g. with
1094      * {@link #setSampleRate(int)}) will alter the default values used by an
1095      * <code>AudioTrack</code>. In this case for audio playback with <code>AudioTrack</code>, the
1096      * sample rate set in the <code>Builder</code> would override the platform output sample rate
1097      * which would otherwise be selected by default.
1098      */
1099     public static class Builder {
1100         private int mEncoding = ENCODING_INVALID;
1101         private int mSampleRate = SAMPLE_RATE_UNSPECIFIED;
1102         private int mChannelMask = CHANNEL_INVALID;
1103         private int mChannelIndexMask = 0;
1104         private int mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_NONE;
1105 
1106         /**
1107          * Constructs a new Builder with none of the format characteristics set.
1108          */
Builder()1109         public Builder() {
1110         }
1111 
1112         /**
1113          * Constructs a new Builder from a given {@link AudioFormat}.
1114          * @param af the {@link AudioFormat} object whose data will be reused in the new Builder.
1115          */
Builder(AudioFormat af)1116         public Builder(AudioFormat af) {
1117             mEncoding = af.mEncoding;
1118             mSampleRate = af.mSampleRate;
1119             mChannelMask = af.mChannelMask;
1120             mChannelIndexMask = af.mChannelIndexMask;
1121             mPropertySetMask = af.mPropertySetMask;
1122         }
1123 
1124         /**
1125          * Combines all of the format characteristics that have been set and return a new
1126          * {@link AudioFormat} object.
1127          * @return a new {@link AudioFormat} object
1128          */
build()1129         public AudioFormat build() {
1130             AudioFormat af = new AudioFormat(
1131                     mPropertySetMask,
1132                     mEncoding,
1133                     mSampleRate,
1134                     mChannelMask,
1135                     mChannelIndexMask
1136                     );
1137             return af;
1138         }
1139 
1140         /**
1141          * Sets the data encoding format.
1142          * @param encoding the specified encoding or default.
1143          * @return the same Builder instance.
1144          * @throws java.lang.IllegalArgumentException
1145          */
setEncoding(@ncoding int encoding)1146         public Builder setEncoding(@Encoding int encoding) throws IllegalArgumentException {
1147             switch (encoding) {
1148                 case ENCODING_DEFAULT:
1149                     mEncoding = ENCODING_PCM_16BIT;
1150                     break;
1151                 case ENCODING_PCM_16BIT:
1152                 case ENCODING_PCM_8BIT:
1153                 case ENCODING_PCM_FLOAT:
1154                 case ENCODING_AC3:
1155                 case ENCODING_E_AC3:
1156                 case ENCODING_DTS:
1157                 case ENCODING_DTS_HD:
1158                 case ENCODING_MP3:
1159                 case ENCODING_AAC_LC:
1160                 case ENCODING_AAC_HE_V1:
1161                 case ENCODING_AAC_HE_V2:
1162                 case ENCODING_IEC61937:
1163                 case ENCODING_DOLBY_TRUEHD:
1164                 case ENCODING_AAC_ELD:
1165                 case ENCODING_AAC_XHE:
1166                 case ENCODING_AC4:
1167                 case ENCODING_E_AC3_JOC:
1168                 case ENCODING_DOLBY_MAT:
1169                 case ENCODING_OPUS:
1170                 case ENCODING_PCM_24BIT_PACKED:
1171                 case ENCODING_PCM_32BIT:
1172                 case ENCODING_MPEGH_BL_L3:
1173                 case ENCODING_MPEGH_BL_L4:
1174                 case ENCODING_MPEGH_LC_L3:
1175                 case ENCODING_MPEGH_LC_L4:
1176                 case ENCODING_DTS_UHD:
1177                 case ENCODING_DRA:
1178                     mEncoding = encoding;
1179                     break;
1180                 case ENCODING_INVALID:
1181                 default:
1182                     throw new IllegalArgumentException("Invalid encoding " + encoding);
1183             }
1184             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_ENCODING;
1185             return this;
1186         }
1187 
1188         /**
1189          * Sets the channel position mask.
1190          * The channel position mask specifies the association between audio samples in a frame
1191          * with named endpoint channels. The samples in the frame correspond to the
1192          * named set bits in the channel position mask, in ascending bit order.
1193          * See {@link #setChannelIndexMask(int)} to specify channels
1194          * based on endpoint numbered channels. This <a href="#channelPositionMask">description of
1195          * channel position masks</a> covers the concept in more details.
1196          * @param channelMask describes the configuration of the audio channels.
1197          *    <p> For output, the channelMask can be an OR-ed combination of
1198          *    channel position masks, e.g.
1199          *    {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT},
1200          *    {@link AudioFormat#CHANNEL_OUT_FRONT_RIGHT},
1201          *    {@link AudioFormat#CHANNEL_OUT_FRONT_CENTER},
1202          *    {@link AudioFormat#CHANNEL_OUT_LOW_FREQUENCY}
1203          *    {@link AudioFormat#CHANNEL_OUT_BACK_LEFT},
1204          *    {@link AudioFormat#CHANNEL_OUT_BACK_RIGHT},
1205          *    {@link AudioFormat#CHANNEL_OUT_BACK_CENTER},
1206          *    {@link AudioFormat#CHANNEL_OUT_SIDE_LEFT},
1207          *    {@link AudioFormat#CHANNEL_OUT_SIDE_RIGHT}.
1208          *    <p> For a valid {@link AudioTrack} channel position mask,
1209          *    the following conditions apply:
1210          *    <br> (1) at most eight channel positions may be used;
1211          *    <br> (2) right/left pairs should be matched.
1212          *    <p> For input or {@link AudioRecord}, the mask should be
1213          *    {@link AudioFormat#CHANNEL_IN_MONO} or
1214          *    {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is
1215          *    guaranteed to work on all devices.
1216          * @return the same <code>Builder</code> instance.
1217          * @throws IllegalArgumentException if the channel mask is invalid or
1218          *    if both channel index mask and channel position mask
1219          *    are specified but do not have the same channel count.
1220          */
setChannelMask(int channelMask)1221         public @NonNull Builder setChannelMask(int channelMask) {
1222             if (channelMask == CHANNEL_INVALID) {
1223                 throw new IllegalArgumentException("Invalid zero channel mask");
1224             } else if (/* channelMask != 0 && */ mChannelIndexMask != 0 &&
1225                     Integer.bitCount(channelMask) != Integer.bitCount(mChannelIndexMask)) {
1226                 throw new IllegalArgumentException("Mismatched channel count for mask " +
1227                         Integer.toHexString(channelMask).toUpperCase());
1228             }
1229             mChannelMask = channelMask;
1230             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
1231             return this;
1232         }
1233 
1234         /**
1235          * Sets the channel index mask.
1236          * A channel index mask specifies the association of audio samples in the frame
1237          * with numbered endpoint channels. The i-th bit in the channel index
1238          * mask corresponds to the i-th endpoint channel.
1239          * For example, an endpoint with four channels is represented
1240          * as index mask bits 0 through 3. This <a href="#channelIndexMask>description of channel
1241          * index masks</a> covers the concept in more details.
1242          * See {@link #setChannelMask(int)} for a positional mask interpretation.
1243          * <p> Both {@link AudioTrack} and {@link AudioRecord} support
1244          * a channel index mask.
1245          * If a channel index mask is specified it is used,
1246          * otherwise the channel position mask specified
1247          * by <code>setChannelMask</code> is used.
1248          * For <code>AudioTrack</code> and <code>AudioRecord</code>,
1249          * a channel position mask is not required if a channel index mask is specified.
1250          *
1251          * @param channelIndexMask describes the configuration of the audio channels.
1252          *    <p> For output, the <code>channelIndexMask</code> is an OR-ed combination of
1253          *    bits representing the mapping of <code>AudioTrack</code> write samples
1254          *    to output sink channels.
1255          *    For example, a mask of <code>0xa</code>, or binary <code>1010</code>,
1256          *    means the <code>AudioTrack</code> write frame consists of two samples,
1257          *    which are routed to the second and the fourth channels of the output sink.
1258          *    Unmatched output sink channels are zero filled and unmatched
1259          *    <code>AudioTrack</code> write samples are dropped.
1260          *    <p> For input, the <code>channelIndexMask</code> is an OR-ed combination of
1261          *    bits representing the mapping of input source channels to
1262          *    <code>AudioRecord</code> read samples.
1263          *    For example, a mask of <code>0x5</code>, or binary
1264          *    <code>101</code>, will read from the first and third channel of the input
1265          *    source device and store them in the first and second sample of the
1266          *    <code>AudioRecord</code> read frame.
1267          *    Unmatched input source channels are dropped and
1268          *    unmatched <code>AudioRecord</code> read samples are zero filled.
1269          * @return the same <code>Builder</code> instance.
1270          * @throws IllegalArgumentException if the channel index mask is invalid or
1271          *    if both channel index mask and channel position mask
1272          *    are specified but do not have the same channel count.
1273          */
setChannelIndexMask(int channelIndexMask)1274         public @NonNull Builder setChannelIndexMask(int channelIndexMask) {
1275             if (channelIndexMask == 0) {
1276                 throw new IllegalArgumentException("Invalid zero channel index mask");
1277             } else if (/* channelIndexMask != 0 && */ mChannelMask != 0 &&
1278                     Integer.bitCount(channelIndexMask) != Integer.bitCount(mChannelMask)) {
1279                 throw new IllegalArgumentException("Mismatched channel count for index mask " +
1280                         Integer.toHexString(channelIndexMask).toUpperCase());
1281             }
1282             mChannelIndexMask = channelIndexMask;
1283             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
1284             return this;
1285         }
1286 
1287         /**
1288          * Sets the sample rate.
1289          * @param sampleRate the sample rate expressed in Hz
1290          * @return the same Builder instance.
1291          * @throws java.lang.IllegalArgumentException
1292          */
setSampleRate(int sampleRate)1293         public Builder setSampleRate(int sampleRate) throws IllegalArgumentException {
1294             // TODO Consider whether to keep the MIN and MAX range checks here.
1295             // It is not necessary and poses the problem of defining the limits independently from
1296             // native implementation or platform capabilities.
1297             if (((sampleRate < SAMPLE_RATE_HZ_MIN) || (sampleRate > SAMPLE_RATE_HZ_MAX)) &&
1298                     sampleRate != SAMPLE_RATE_UNSPECIFIED) {
1299                 throw new IllegalArgumentException("Invalid sample rate " + sampleRate);
1300             }
1301             mSampleRate = sampleRate;
1302             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE;
1303             return this;
1304         }
1305     }
1306 
1307     @Override
equals(Object o)1308     public boolean equals(Object o) {
1309         if (this == o) return true;
1310         if (o == null || getClass() != o.getClass()) return false;
1311 
1312         AudioFormat that = (AudioFormat) o;
1313 
1314         if (mPropertySetMask != that.mPropertySetMask) return false;
1315 
1316         // return false if any of the properties is set and the values differ
1317         return !((((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
1318                             && (mEncoding != that.mEncoding))
1319                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
1320                             && (mSampleRate != that.mSampleRate))
1321                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0)
1322                             && (mChannelMask != that.mChannelMask))
1323                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0)
1324                             && (mChannelIndexMask != that.mChannelIndexMask)));
1325     }
1326 
1327     @Override
hashCode()1328     public int hashCode() {
1329         return Objects.hash(mPropertySetMask, mSampleRate, mEncoding, mChannelMask,
1330                 mChannelIndexMask);
1331     }
1332 
1333     @Override
describeContents()1334     public int describeContents() {
1335         return 0;
1336     }
1337 
1338     @Override
writeToParcel(Parcel dest, int flags)1339     public void writeToParcel(Parcel dest, int flags) {
1340         dest.writeInt(mPropertySetMask);
1341         dest.writeInt(mEncoding);
1342         dest.writeInt(mSampleRate);
1343         dest.writeInt(mChannelMask);
1344         dest.writeInt(mChannelIndexMask);
1345     }
1346 
AudioFormat(Parcel in)1347     private AudioFormat(Parcel in) {
1348         this(
1349              in.readInt(), // propertySetMask
1350              in.readInt(), // encoding
1351              in.readInt(), // sampleRate
1352              in.readInt(), // channelMask
1353              in.readInt()  // channelIndexMask
1354             );
1355     }
1356 
1357     public static final @android.annotation.NonNull Parcelable.Creator<AudioFormat> CREATOR =
1358             new Parcelable.Creator<AudioFormat>() {
1359         public AudioFormat createFromParcel(Parcel p) {
1360             return new AudioFormat(p);
1361         }
1362         public AudioFormat[] newArray(int size) {
1363             return new AudioFormat[size];
1364         }
1365     };
1366 
1367     @Override
toString()1368     public String toString () {
1369         return new String("AudioFormat:"
1370                 + " props=" + mPropertySetMask
1371                 + " enc=" + mEncoding
1372                 + " chan=0x" + Integer.toHexString(mChannelMask).toUpperCase()
1373                 + " chan_index=0x" + Integer.toHexString(mChannelIndexMask).toUpperCase()
1374                 + " rate=" + mSampleRate);
1375     }
1376 
1377     /** @hide */
1378     @IntDef(flag = false, prefix = "ENCODING", value = {
1379         ENCODING_DEFAULT,
1380         ENCODING_PCM_16BIT,
1381         ENCODING_PCM_8BIT,
1382         ENCODING_PCM_FLOAT,
1383         ENCODING_AC3,
1384         ENCODING_E_AC3,
1385         ENCODING_DTS,
1386         ENCODING_DTS_HD,
1387         ENCODING_MP3,
1388         ENCODING_AAC_LC,
1389         ENCODING_AAC_HE_V1,
1390         ENCODING_AAC_HE_V2,
1391         ENCODING_IEC61937,
1392         ENCODING_DOLBY_TRUEHD,
1393         ENCODING_AAC_ELD,
1394         ENCODING_AAC_XHE,
1395         ENCODING_AC4,
1396         ENCODING_E_AC3_JOC,
1397         ENCODING_DOLBY_MAT,
1398         ENCODING_OPUS,
1399         ENCODING_PCM_24BIT_PACKED,
1400         ENCODING_PCM_32BIT,
1401         ENCODING_MPEGH_BL_L3,
1402         ENCODING_MPEGH_BL_L4,
1403         ENCODING_MPEGH_LC_L3,
1404         ENCODING_MPEGH_LC_L4,
1405         ENCODING_DTS_UHD,
1406         ENCODING_DRA }
1407     )
1408     @Retention(RetentionPolicy.SOURCE)
1409     public @interface Encoding {}
1410 
1411     /** @hide */
1412     public static final int[] SURROUND_SOUND_ENCODING = {
1413             ENCODING_AC3,
1414             ENCODING_E_AC3,
1415             ENCODING_DTS,
1416             ENCODING_DTS_HD,
1417             ENCODING_AAC_LC,
1418             ENCODING_DOLBY_TRUEHD,
1419             ENCODING_AC4,
1420             ENCODING_E_AC3_JOC,
1421             ENCODING_DOLBY_MAT,
1422             ENCODING_MPEGH_BL_L3,
1423             ENCODING_MPEGH_BL_L4,
1424             ENCODING_MPEGH_LC_L3,
1425             ENCODING_MPEGH_LC_L4,
1426             ENCODING_DTS_UHD,
1427             ENCODING_DRA
1428     };
1429 
1430     /** @hide */
1431     @IntDef(flag = false, prefix = "ENCODING", value = {
1432             ENCODING_AC3,
1433             ENCODING_E_AC3,
1434             ENCODING_DTS,
1435             ENCODING_DTS_HD,
1436             ENCODING_AAC_LC,
1437             ENCODING_DOLBY_TRUEHD,
1438             ENCODING_AC4,
1439             ENCODING_E_AC3_JOC,
1440             ENCODING_DOLBY_MAT,
1441             ENCODING_MPEGH_BL_L3,
1442             ENCODING_MPEGH_BL_L4,
1443             ENCODING_MPEGH_LC_L3,
1444             ENCODING_MPEGH_LC_L4,
1445             ENCODING_DTS_UHD,
1446             ENCODING_DRA }
1447     )
1448     @Retention(RetentionPolicy.SOURCE)
1449     public @interface SurroundSoundEncoding {}
1450 
1451     /**
1452      * @hide
1453      *
1454      * Return default name for a surround format. This is not an International name.
1455      * It is just a default to use if an international name is not available.
1456      *
1457      * @param audioFormat a surround format
1458      * @return short default name for the format.
1459      */
toDisplayName(@urroundSoundEncoding int audioFormat)1460     public static String toDisplayName(@SurroundSoundEncoding int audioFormat) {
1461         switch (audioFormat) {
1462             case ENCODING_AC3:
1463                 return "Dolby Digital";
1464             case ENCODING_E_AC3:
1465                 return "Dolby Digital Plus";
1466             case ENCODING_DTS:
1467                 return "DTS";
1468             case ENCODING_DTS_HD:
1469                 return "DTS HD";
1470             case ENCODING_AAC_LC:
1471                 return "AAC";
1472             case ENCODING_DOLBY_TRUEHD:
1473                 return "Dolby TrueHD";
1474             case ENCODING_AC4:
1475                 return "Dolby AC-4";
1476             case ENCODING_E_AC3_JOC:
1477                 return "Dolby Atmos in Dolby Digital Plus";
1478             case ENCODING_DOLBY_MAT:
1479                 return "Dolby MAT";
1480             case ENCODING_MPEGH_BL_L3:
1481                 return "MPEG-H 3D Audio baseline profile level 3";
1482             case ENCODING_MPEGH_BL_L4:
1483                 return "MPEG-H 3D Audio baseline profile level 4";
1484             case ENCODING_MPEGH_LC_L3:
1485                 return "MPEG-H 3D Audio low complexity profile level 3";
1486             case ENCODING_MPEGH_LC_L4:
1487                 return "MPEG-H 3D Audio low complexity profile level 4";
1488             case ENCODING_DTS_UHD:
1489                 return "DTS UHD";
1490             case ENCODING_DRA:
1491                 return "DRA";
1492             default:
1493                 return "Unknown surround sound format";
1494         }
1495     }
1496 
1497 }
1498