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 
499     public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
500     public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
501     // aka QUAD_BACK
502     public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
503             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
504     /** @hide */
505     public static final int CHANNEL_OUT_QUAD_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
506             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
507     public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
508             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
509     // aka 5POINT1_BACK
510     /** Output channel mask for 5.1 */
511     public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
512             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
513     /** @hide */
514     public static final int CHANNEL_OUT_5POINT1_SIDE = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
515             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY |
516             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT);
517     // different from AUDIO_CHANNEL_OUT_7POINT1 used internally, and not accepted by AudioRecord.
518     /** @deprecated Not the typical 7.1 surround configuration. Use {@link #CHANNEL_OUT_7POINT1_SURROUND} instead. */
519     @Deprecated    public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
520             CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
521             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
522     /** Output channel mask for 7.1 */
523     // matches AUDIO_CHANNEL_OUT_7POINT1
524     public static final int CHANNEL_OUT_7POINT1_SURROUND = (
525             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
526             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
527             CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
528             CHANNEL_OUT_LOW_FREQUENCY);
529     /** Output channel mask for 5.1.2
530      *  Same as 5.1 with the addition of left and right top channels */
531     public static final int CHANNEL_OUT_5POINT1POINT2 = (CHANNEL_OUT_5POINT1 |
532             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
533     /** Output channel mask for 5.1.4
534      * Same as 5.1 with the addition of four top channels */
535     public static final int CHANNEL_OUT_5POINT1POINT4 = (CHANNEL_OUT_5POINT1 |
536             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_RIGHT |
537             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT);
538     /** Output channel mask for 7.1.2
539      * Same as 7.1 with the addition of left and right top channels*/
540     public static final int CHANNEL_OUT_7POINT1POINT2 = (CHANNEL_OUT_7POINT1_SURROUND |
541             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
542     /** Output channel mask for 7.1.4
543      *  Same as 7.1 with the addition of four top channels */
544     public static final int CHANNEL_OUT_7POINT1POINT4 = (CHANNEL_OUT_7POINT1_SURROUND |
545             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_RIGHT |
546             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT);
547     /** Output channel mask for 9.1.4
548      * Same as 7.1.4 with the addition of left and right front wide channels */
549     public static final int CHANNEL_OUT_9POINT1POINT4 = (CHANNEL_OUT_7POINT1POINT4
550             | CHANNEL_OUT_FRONT_WIDE_LEFT | CHANNEL_OUT_FRONT_WIDE_RIGHT);
551     /** Output channel mask for 9.1.6
552      * Same as 9.1.4 with the addition of left and right top side channels */
553     public static final int CHANNEL_OUT_9POINT1POINT6 = (CHANNEL_OUT_9POINT1POINT4
554             | CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT);
555     /** @hide */
556     public static final int CHANNEL_OUT_13POINT_360RA = (
557             CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_FRONT_RIGHT |
558             CHANNEL_OUT_SIDE_LEFT | CHANNEL_OUT_SIDE_RIGHT |
559             CHANNEL_OUT_TOP_FRONT_LEFT | CHANNEL_OUT_TOP_FRONT_CENTER |
560             CHANNEL_OUT_TOP_FRONT_RIGHT |
561             CHANNEL_OUT_TOP_BACK_LEFT | CHANNEL_OUT_TOP_BACK_RIGHT |
562             CHANNEL_OUT_BOTTOM_FRONT_LEFT | CHANNEL_OUT_BOTTOM_FRONT_CENTER |
563             CHANNEL_OUT_BOTTOM_FRONT_RIGHT);
564     /** @hide */
565     public static final int CHANNEL_OUT_22POINT2 = (CHANNEL_OUT_7POINT1POINT4 |
566             CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
567             CHANNEL_OUT_BACK_CENTER | CHANNEL_OUT_TOP_CENTER |
568             CHANNEL_OUT_TOP_FRONT_CENTER | CHANNEL_OUT_TOP_BACK_CENTER |
569             CHANNEL_OUT_TOP_SIDE_LEFT | CHANNEL_OUT_TOP_SIDE_RIGHT |
570             CHANNEL_OUT_BOTTOM_FRONT_LEFT | CHANNEL_OUT_BOTTOM_FRONT_RIGHT |
571             CHANNEL_OUT_BOTTOM_FRONT_CENTER |
572             CHANNEL_OUT_LOW_FREQUENCY_2);
573     // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL
574 
575     /** Minimum value for sample rate,
576      *  assuming AudioTrack and AudioRecord share the same limitations.
577      * @hide
578      */
579     // never unhide
580     public static final int SAMPLE_RATE_HZ_MIN = AudioSystem.SAMPLE_RATE_HZ_MIN;
581     /** Maximum 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_MAX = AudioSystem.SAMPLE_RATE_HZ_MAX;
587     /** Sample rate will be a route-dependent value.
588      * For AudioTrack, it is usually the sink sample rate,
589      * and for AudioRecord it is usually the source sample rate.
590      */
591     public static final int SAMPLE_RATE_UNSPECIFIED = 0;
592 
593     /**
594      * @hide
595      * Return the input channel mask corresponding to an output channel mask.
596      * This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
597      * @param outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
598      * @return a combination of CHANNEL_IN_* definitions matching an output channel mask
599      * @throws IllegalArgumentException
600      */
inChannelMaskFromOutChannelMask(int outMask)601     public static int inChannelMaskFromOutChannelMask(int outMask) throws IllegalArgumentException {
602         if (outMask == CHANNEL_OUT_DEFAULT) {
603             throw new IllegalArgumentException(
604                     "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
605         }
606         switch (channelCountFromOutChannelMask(outMask)) {
607             case 1:
608                 return CHANNEL_IN_MONO;
609             case 2:
610                 return CHANNEL_IN_STEREO;
611             default:
612                 throw new IllegalArgumentException("Unsupported channel configuration for input.");
613         }
614     }
615 
616     /**
617      * @hide
618      * Return the number of channels from an input channel mask
619      * @param mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
620      * @return number of channels for the mask
621      */
622     @TestApi
channelCountFromInChannelMask(int mask)623     public static int channelCountFromInChannelMask(int mask) {
624         return Integer.bitCount(mask);
625     }
626     /**
627      * @hide
628      * Return the number of channels from an output channel mask
629      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
630      * @return number of channels for the mask
631      */
632     @TestApi
channelCountFromOutChannelMask(int mask)633     public static int channelCountFromOutChannelMask(int mask) {
634         return Integer.bitCount(mask);
635     }
636     /**
637      * @hide
638      * Return a channel mask ready to be used by native code
639      * @param mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
640      * @return a native channel mask
641      */
convertChannelOutMaskToNativeMask(int javaMask)642     public static int convertChannelOutMaskToNativeMask(int javaMask) {
643         return (javaMask >> 2);
644     }
645 
646     /**
647      * @hide
648      * Return a java output channel mask
649      * @param mask a native channel mask
650      * @return a combination of the CHANNEL_OUT_* definitions
651      */
convertNativeChannelMaskToOutMask(int nativeMask)652     public static int convertNativeChannelMaskToOutMask(int nativeMask) {
653         return (nativeMask << 2);
654     }
655 
656     public static final int CHANNEL_IN_DEFAULT = 1;
657     // These directly match native
658     public static final int CHANNEL_IN_LEFT = 0x4;
659     public static final int CHANNEL_IN_RIGHT = 0x8;
660     public static final int CHANNEL_IN_FRONT = 0x10;
661     public static final int CHANNEL_IN_BACK = 0x20;
662     public static final int CHANNEL_IN_LEFT_PROCESSED = 0x40;
663     public static final int CHANNEL_IN_RIGHT_PROCESSED = 0x80;
664     public static final int CHANNEL_IN_FRONT_PROCESSED = 0x100;
665     public static final int CHANNEL_IN_BACK_PROCESSED = 0x200;
666     public static final int CHANNEL_IN_PRESSURE = 0x400;
667     public static final int CHANNEL_IN_X_AXIS = 0x800;
668     public static final int CHANNEL_IN_Y_AXIS = 0x1000;
669     public static final int CHANNEL_IN_Z_AXIS = 0x2000;
670     public static final int CHANNEL_IN_VOICE_UPLINK = 0x4000;
671     public static final int CHANNEL_IN_VOICE_DNLINK = 0x8000;
672     public static final int CHANNEL_IN_MONO = CHANNEL_IN_FRONT;
673     public static final int CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT);
674     /** @hide */
675     public static final int CHANNEL_IN_FRONT_BACK = CHANNEL_IN_FRONT | CHANNEL_IN_BACK;
676     // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL
677 
678     /** @hide */
679     @TestApi
getBytesPerSample(int audioFormat)680     public static int getBytesPerSample(int audioFormat)
681     {
682         switch (audioFormat) {
683             case ENCODING_PCM_8BIT:
684                 return 1;
685             case ENCODING_PCM_16BIT:
686             case ENCODING_IEC61937:
687             case ENCODING_DEFAULT:
688                 return 2;
689             case ENCODING_PCM_24BIT_PACKED:
690                 return 3;
691             case ENCODING_PCM_FLOAT:
692             case ENCODING_PCM_32BIT:
693                 return 4;
694             case ENCODING_INVALID:
695             default:
696                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
697         }
698     }
699 
700     /** @hide */
isValidEncoding(int audioFormat)701     public static boolean isValidEncoding(int audioFormat)
702     {
703         switch (audioFormat) {
704             case ENCODING_PCM_16BIT:
705             case ENCODING_PCM_8BIT:
706             case ENCODING_PCM_FLOAT:
707             case ENCODING_AC3:
708             case ENCODING_E_AC3:
709             case ENCODING_DTS:
710             case ENCODING_DTS_HD:
711             case ENCODING_MP3:
712             case ENCODING_AAC_LC:
713             case ENCODING_AAC_HE_V1:
714             case ENCODING_AAC_HE_V2:
715             case ENCODING_IEC61937:
716             case ENCODING_DOLBY_TRUEHD:
717             case ENCODING_AAC_ELD:
718             case ENCODING_AAC_XHE:
719             case ENCODING_AC4:
720             case ENCODING_E_AC3_JOC:
721             case ENCODING_DOLBY_MAT:
722             case ENCODING_OPUS:
723             case ENCODING_PCM_24BIT_PACKED:
724             case ENCODING_PCM_32BIT:
725             case ENCODING_MPEGH_BL_L3:
726             case ENCODING_MPEGH_BL_L4:
727             case ENCODING_MPEGH_LC_L3:
728             case ENCODING_MPEGH_LC_L4:
729             case ENCODING_DTS_UHD:
730             case ENCODING_DRA:
731                 return true;
732             default:
733                 return false;
734         }
735     }
736 
737     /** @hide */
isPublicEncoding(int audioFormat)738     public static boolean isPublicEncoding(int audioFormat)
739     {
740         switch (audioFormat) {
741             case ENCODING_PCM_16BIT:
742             case ENCODING_PCM_8BIT:
743             case ENCODING_PCM_FLOAT:
744             case ENCODING_AC3:
745             case ENCODING_E_AC3:
746             case ENCODING_DTS:
747             case ENCODING_DTS_HD:
748             case ENCODING_MP3:
749             case ENCODING_AAC_LC:
750             case ENCODING_AAC_HE_V1:
751             case ENCODING_AAC_HE_V2:
752             case ENCODING_IEC61937:
753             case ENCODING_DOLBY_TRUEHD:
754             case ENCODING_AAC_ELD:
755             case ENCODING_AAC_XHE:
756             case ENCODING_AC4:
757             case ENCODING_E_AC3_JOC:
758             case ENCODING_DOLBY_MAT:
759             case ENCODING_OPUS:
760             case ENCODING_PCM_24BIT_PACKED:
761             case ENCODING_PCM_32BIT:
762             case ENCODING_MPEGH_BL_L3:
763             case ENCODING_MPEGH_BL_L4:
764             case ENCODING_MPEGH_LC_L3:
765             case ENCODING_MPEGH_LC_L4:
766             case ENCODING_DTS_UHD:
767             case ENCODING_DRA:
768                 return true;
769             default:
770                 return false;
771         }
772     }
773 
774     /** @hide */
775     @TestApi
isEncodingLinearPcm(int audioFormat)776     public static boolean isEncodingLinearPcm(int audioFormat)
777     {
778         switch (audioFormat) {
779             case ENCODING_PCM_16BIT:
780             case ENCODING_PCM_8BIT:
781             case ENCODING_PCM_FLOAT:
782             case ENCODING_PCM_24BIT_PACKED:
783             case ENCODING_PCM_32BIT:
784             case ENCODING_DEFAULT:
785                 return true;
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: // wrapped in PCM but compressed
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_MPEGH_BL_L3:
803             case ENCODING_MPEGH_BL_L4:
804             case ENCODING_MPEGH_LC_L3:
805             case ENCODING_MPEGH_LC_L4:
806             case ENCODING_DTS_UHD:
807             case ENCODING_DRA:
808                 return false;
809             case ENCODING_INVALID:
810             default:
811                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
812         }
813     }
814 
815     /** @hide */
isEncodingLinearFrames(int audioFormat)816     public static boolean isEncodingLinearFrames(int audioFormat)
817     {
818         switch (audioFormat) {
819             case ENCODING_PCM_16BIT:
820             case ENCODING_PCM_8BIT:
821             case ENCODING_PCM_FLOAT:
822             case ENCODING_IEC61937: // same size as stereo PCM
823             case ENCODING_PCM_24BIT_PACKED:
824             case ENCODING_PCM_32BIT:
825             case ENCODING_DEFAULT:
826                 return true;
827             case ENCODING_AC3:
828             case ENCODING_E_AC3:
829             case ENCODING_DTS:
830             case ENCODING_DTS_HD:
831             case ENCODING_MP3:
832             case ENCODING_AAC_LC:
833             case ENCODING_AAC_HE_V1:
834             case ENCODING_AAC_HE_V2:
835             case ENCODING_DOLBY_TRUEHD:
836             case ENCODING_AAC_ELD:
837             case ENCODING_AAC_XHE:
838             case ENCODING_AC4:
839             case ENCODING_E_AC3_JOC:
840             case ENCODING_DOLBY_MAT:
841             case ENCODING_OPUS:
842             case ENCODING_MPEGH_BL_L3:
843             case ENCODING_MPEGH_BL_L4:
844             case ENCODING_MPEGH_LC_L3:
845             case ENCODING_MPEGH_LC_L4:
846             case ENCODING_DTS_UHD:
847             case ENCODING_DRA:
848                 return false;
849             case ENCODING_INVALID:
850             default:
851                 throw new IllegalArgumentException("Bad audio format " + audioFormat);
852         }
853     }
854     /**
855      * Returns an array of public encoding values extracted from an array of
856      * encoding values.
857      * @hide
858      */
filterPublicFormats(int[] formats)859     public static int[] filterPublicFormats(int[] formats) {
860         if (formats == null) {
861             return null;
862         }
863         int[] myCopy = Arrays.copyOf(formats, formats.length);
864         int size = 0;
865         for (int i = 0; i < myCopy.length; i++) {
866             if (isPublicEncoding(myCopy[i])) {
867                 if (size != i) {
868                     myCopy[size] = myCopy[i];
869                 }
870                 size++;
871             }
872         }
873         return Arrays.copyOf(myCopy, size);
874     }
875 
876     /** @removed */
AudioFormat()877     public AudioFormat()
878     {
879         throw new UnsupportedOperationException("There is no valid usage of this constructor");
880     }
881 
882     /**
883      * Constructor used by the JNI.  Parameters are not checked for validity.
884      */
885     // Update sound trigger JNI in core/jni/android_hardware_SoundTrigger.cpp when modifying this
886     // constructor
887     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask)888     private AudioFormat(int encoding, int sampleRate, int channelMask, int channelIndexMask) {
889         this(
890              AUDIO_FORMAT_HAS_PROPERTY_ENCODING
891              | AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE
892              | AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK
893              | AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK,
894              encoding, sampleRate, channelMask, channelIndexMask
895              );
896     }
897 
AudioFormat(int propertySetMask, int encoding, int sampleRate, int channelMask, int channelIndexMask)898     private AudioFormat(int propertySetMask,
899             int encoding, int sampleRate, int channelMask, int channelIndexMask) {
900         mPropertySetMask = propertySetMask;
901         mEncoding = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0
902                 ? encoding : ENCODING_INVALID;
903         mSampleRate = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0
904                 ? sampleRate : SAMPLE_RATE_UNSPECIFIED;
905         mChannelMask = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0
906                 ? channelMask : CHANNEL_INVALID;
907         mChannelIndexMask = (propertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0
908                 ? channelIndexMask : CHANNEL_INVALID;
909 
910         // Compute derived values.
911 
912         final int channelIndexCount = Integer.bitCount(getChannelIndexMask());
913         int channelCount = channelCountFromOutChannelMask(getChannelMask());
914         if (channelCount == 0) {
915             channelCount = channelIndexCount;
916         } else if (channelCount != channelIndexCount && channelIndexCount != 0) {
917             channelCount = 0; // position and index channel count mismatch
918         }
919         mChannelCount = channelCount;
920 
921         int frameSizeInBytes = 1;
922         try {
923             frameSizeInBytes = getBytesPerSample(mEncoding) * channelCount;
924         } catch (IllegalArgumentException iae) {
925             // ignored
926         }
927         // it is possible that channel count is 0, so ensure we return 1 for
928         // mFrameSizeInBytes for consistency.
929         mFrameSizeInBytes = frameSizeInBytes != 0 ? frameSizeInBytes : 1;
930     }
931 
932     /** @hide */
933     public final static int AUDIO_FORMAT_HAS_PROPERTY_NONE = 0x0;
934     /** @hide */
935     public final static int AUDIO_FORMAT_HAS_PROPERTY_ENCODING = 0x1 << 0;
936     /** @hide */
937     public final static int AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE = 0x1 << 1;
938     /** @hide */
939     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK = 0x1 << 2;
940     /** @hide */
941     public final static int AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK = 0x1 << 3;
942 
943     // This is an immutable class, all member variables are final.
944 
945     // Essential values.
946     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
947     private final int mEncoding;
948     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
949     private final int mSampleRate;
950     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
951     private final int mChannelMask;
952     private final int mChannelIndexMask;
953     private final int mPropertySetMask;
954 
955     // Derived values computed in the constructor, cached here.
956     private final int mChannelCount;
957     private final int mFrameSizeInBytes;
958 
959     /**
960      * Return the encoding.
961      * See the section on <a href="#encoding">encodings</a> for more information about the different
962      * types of supported audio encoding.
963      * @return one of the values that can be set in {@link Builder#setEncoding(int)} or
964      * {@link AudioFormat#ENCODING_INVALID} if not set.
965      */
getEncoding()966     public int getEncoding() {
967         return mEncoding;
968     }
969 
970     /**
971      * Return the sample rate.
972      * @return one of the values that can be set in {@link Builder#setSampleRate(int)} or
973      * {@link #SAMPLE_RATE_UNSPECIFIED} if not set.
974      */
getSampleRate()975     public int getSampleRate() {
976         return mSampleRate;
977     }
978 
979     /**
980      * Return the channel mask.
981      * See the section on <a href="#channelMask">channel masks</a> for more information about
982      * the difference between index-based masks(as returned by {@link #getChannelIndexMask()}) and
983      * the position-based mask returned by this function.
984      * @return one of the values that can be set in {@link Builder#setChannelMask(int)} or
985      * {@link AudioFormat#CHANNEL_INVALID} if not set.
986      */
getChannelMask()987     public int getChannelMask() {
988         return mChannelMask;
989     }
990 
991     /**
992      * Return the channel index mask.
993      * See the section on <a href="#channelMask">channel masks</a> for more information about
994      * the difference between index-based masks, and position-based masks (as returned
995      * by {@link #getChannelMask()}).
996      * @return one of the values that can be set in {@link Builder#setChannelIndexMask(int)} or
997      * {@link AudioFormat#CHANNEL_INVALID} if not set or an invalid mask was used.
998      */
getChannelIndexMask()999     public int getChannelIndexMask() {
1000         return mChannelIndexMask;
1001     }
1002 
1003     /**
1004      * Return the channel count.
1005      * @return the channel count derived from the channel position mask or the channel index mask.
1006      * Zero is returned if both the channel position mask and the channel index mask are not set.
1007      */
getChannelCount()1008     public int getChannelCount() {
1009         return mChannelCount;
1010     }
1011 
1012     /**
1013      * Return the frame size in bytes.
1014      *
1015      * For PCM or PCM packed compressed data this is the size of a sample multiplied
1016      * by the channel count. For all other cases, including invalid/unset channel masks,
1017      * this will return 1 byte.
1018      * As an example, a stereo 16-bit PCM format would have a frame size of 4 bytes,
1019      * an 8 channel float PCM format would have a frame size of 32 bytes,
1020      * and a compressed data format (not packed in PCM) would have a frame size of 1 byte.
1021      *
1022      * Both {@link AudioRecord} or {@link AudioTrack} process data in multiples of
1023      * this frame size.
1024      *
1025      * @return The audio frame size in bytes corresponding to the encoding and the channel mask.
1026      */
getFrameSizeInBytes()1027     public @IntRange(from = 1) int getFrameSizeInBytes() {
1028         return mFrameSizeInBytes;
1029     }
1030 
1031     /** @hide */
getPropertySetMask()1032     public int getPropertySetMask() {
1033         return mPropertySetMask;
1034     }
1035 
1036     /** @hide */
toLogFriendlyString()1037     public String toLogFriendlyString() {
1038         return String.format("%dch %dHz %s",
1039                 mChannelCount, mSampleRate, toLogFriendlyEncoding(mEncoding));
1040     }
1041 
1042     /**
1043      * Builder class for {@link AudioFormat} objects.
1044      * Use this class to configure and create an AudioFormat instance. By setting format
1045      * characteristics such as audio encoding, channel mask or sample rate, you indicate which
1046      * of those are to vary from the default behavior on this device wherever this audio format
1047      * is used. See {@link AudioFormat} for a complete description of the different parameters that
1048      * can be used to configure an <code>AudioFormat</code> instance.
1049      * <p>{@link AudioFormat} is for instance used in
1050      * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. In this
1051      * constructor, every format characteristic set on the <code>Builder</code> (e.g. with
1052      * {@link #setSampleRate(int)}) will alter the default values used by an
1053      * <code>AudioTrack</code>. In this case for audio playback with <code>AudioTrack</code>, the
1054      * sample rate set in the <code>Builder</code> would override the platform output sample rate
1055      * which would otherwise be selected by default.
1056      */
1057     public static class Builder {
1058         private int mEncoding = ENCODING_INVALID;
1059         private int mSampleRate = SAMPLE_RATE_UNSPECIFIED;
1060         private int mChannelMask = CHANNEL_INVALID;
1061         private int mChannelIndexMask = 0;
1062         private int mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_NONE;
1063 
1064         /**
1065          * Constructs a new Builder with none of the format characteristics set.
1066          */
Builder()1067         public Builder() {
1068         }
1069 
1070         /**
1071          * Constructs a new Builder from a given {@link AudioFormat}.
1072          * @param af the {@link AudioFormat} object whose data will be reused in the new Builder.
1073          */
Builder(AudioFormat af)1074         public Builder(AudioFormat af) {
1075             mEncoding = af.mEncoding;
1076             mSampleRate = af.mSampleRate;
1077             mChannelMask = af.mChannelMask;
1078             mChannelIndexMask = af.mChannelIndexMask;
1079             mPropertySetMask = af.mPropertySetMask;
1080         }
1081 
1082         /**
1083          * Combines all of the format characteristics that have been set and return a new
1084          * {@link AudioFormat} object.
1085          * @return a new {@link AudioFormat} object
1086          */
build()1087         public AudioFormat build() {
1088             AudioFormat af = new AudioFormat(
1089                     mPropertySetMask,
1090                     mEncoding,
1091                     mSampleRate,
1092                     mChannelMask,
1093                     mChannelIndexMask
1094                     );
1095             return af;
1096         }
1097 
1098         /**
1099          * Sets the data encoding format.
1100          * @param encoding the specified encoding or default.
1101          * @return the same Builder instance.
1102          * @throws java.lang.IllegalArgumentException
1103          */
setEncoding(@ncoding int encoding)1104         public Builder setEncoding(@Encoding int encoding) throws IllegalArgumentException {
1105             switch (encoding) {
1106                 case ENCODING_DEFAULT:
1107                     mEncoding = ENCODING_PCM_16BIT;
1108                     break;
1109                 case ENCODING_PCM_16BIT:
1110                 case ENCODING_PCM_8BIT:
1111                 case ENCODING_PCM_FLOAT:
1112                 case ENCODING_AC3:
1113                 case ENCODING_E_AC3:
1114                 case ENCODING_DTS:
1115                 case ENCODING_DTS_HD:
1116                 case ENCODING_MP3:
1117                 case ENCODING_AAC_LC:
1118                 case ENCODING_AAC_HE_V1:
1119                 case ENCODING_AAC_HE_V2:
1120                 case ENCODING_IEC61937:
1121                 case ENCODING_DOLBY_TRUEHD:
1122                 case ENCODING_AAC_ELD:
1123                 case ENCODING_AAC_XHE:
1124                 case ENCODING_AC4:
1125                 case ENCODING_E_AC3_JOC:
1126                 case ENCODING_DOLBY_MAT:
1127                 case ENCODING_OPUS:
1128                 case ENCODING_PCM_24BIT_PACKED:
1129                 case ENCODING_PCM_32BIT:
1130                 case ENCODING_MPEGH_BL_L3:
1131                 case ENCODING_MPEGH_BL_L4:
1132                 case ENCODING_MPEGH_LC_L3:
1133                 case ENCODING_MPEGH_LC_L4:
1134                 case ENCODING_DTS_UHD:
1135                 case ENCODING_DRA:
1136                     mEncoding = encoding;
1137                     break;
1138                 case ENCODING_INVALID:
1139                 default:
1140                     throw new IllegalArgumentException("Invalid encoding " + encoding);
1141             }
1142             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_ENCODING;
1143             return this;
1144         }
1145 
1146         /**
1147          * Sets the channel position mask.
1148          * The channel position mask specifies the association between audio samples in a frame
1149          * with named endpoint channels. The samples in the frame correspond to the
1150          * named set bits in the channel position mask, in ascending bit order.
1151          * See {@link #setChannelIndexMask(int)} to specify channels
1152          * based on endpoint numbered channels. This <a href="#channelPositionMask">description of
1153          * channel position masks</a> covers the concept in more details.
1154          * @param channelMask describes the configuration of the audio channels.
1155          *    <p> For output, the channelMask can be an OR-ed combination of
1156          *    channel position masks, e.g.
1157          *    {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT},
1158          *    {@link AudioFormat#CHANNEL_OUT_FRONT_RIGHT},
1159          *    {@link AudioFormat#CHANNEL_OUT_FRONT_CENTER},
1160          *    {@link AudioFormat#CHANNEL_OUT_LOW_FREQUENCY}
1161          *    {@link AudioFormat#CHANNEL_OUT_BACK_LEFT},
1162          *    {@link AudioFormat#CHANNEL_OUT_BACK_RIGHT},
1163          *    {@link AudioFormat#CHANNEL_OUT_BACK_CENTER},
1164          *    {@link AudioFormat#CHANNEL_OUT_SIDE_LEFT},
1165          *    {@link AudioFormat#CHANNEL_OUT_SIDE_RIGHT}.
1166          *    <p> For a valid {@link AudioTrack} channel position mask,
1167          *    the following conditions apply:
1168          *    <br> (1) at most eight channel positions may be used;
1169          *    <br> (2) right/left pairs should be matched.
1170          *    <p> For input or {@link AudioRecord}, the mask should be
1171          *    {@link AudioFormat#CHANNEL_IN_MONO} or
1172          *    {@link AudioFormat#CHANNEL_IN_STEREO}.  {@link AudioFormat#CHANNEL_IN_MONO} is
1173          *    guaranteed to work on all devices.
1174          * @return the same <code>Builder</code> instance.
1175          * @throws IllegalArgumentException if the channel mask is invalid or
1176          *    if both channel index mask and channel position mask
1177          *    are specified but do not have the same channel count.
1178          */
setChannelMask(int channelMask)1179         public @NonNull Builder setChannelMask(int channelMask) {
1180             if (channelMask == CHANNEL_INVALID) {
1181                 throw new IllegalArgumentException("Invalid zero channel mask");
1182             } else if (/* channelMask != 0 && */ mChannelIndexMask != 0 &&
1183                     Integer.bitCount(channelMask) != Integer.bitCount(mChannelIndexMask)) {
1184                 throw new IllegalArgumentException("Mismatched channel count for mask " +
1185                         Integer.toHexString(channelMask).toUpperCase());
1186             }
1187             mChannelMask = channelMask;
1188             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
1189             return this;
1190         }
1191 
1192         /**
1193          * Sets the channel index mask.
1194          * A channel index mask specifies the association of audio samples in the frame
1195          * with numbered endpoint channels. The i-th bit in the channel index
1196          * mask corresponds to the i-th endpoint channel.
1197          * For example, an endpoint with four channels is represented
1198          * as index mask bits 0 through 3. This <a href="#channelIndexMask>description of channel
1199          * index masks</a> covers the concept in more details.
1200          * See {@link #setChannelMask(int)} for a positional mask interpretation.
1201          * <p> Both {@link AudioTrack} and {@link AudioRecord} support
1202          * a channel index mask.
1203          * If a channel index mask is specified it is used,
1204          * otherwise the channel position mask specified
1205          * by <code>setChannelMask</code> is used.
1206          * For <code>AudioTrack</code> and <code>AudioRecord</code>,
1207          * a channel position mask is not required if a channel index mask is specified.
1208          *
1209          * @param channelIndexMask describes the configuration of the audio channels.
1210          *    <p> For output, the <code>channelIndexMask</code> is an OR-ed combination of
1211          *    bits representing the mapping of <code>AudioTrack</code> write samples
1212          *    to output sink channels.
1213          *    For example, a mask of <code>0xa</code>, or binary <code>1010</code>,
1214          *    means the <code>AudioTrack</code> write frame consists of two samples,
1215          *    which are routed to the second and the fourth channels of the output sink.
1216          *    Unmatched output sink channels are zero filled and unmatched
1217          *    <code>AudioTrack</code> write samples are dropped.
1218          *    <p> For input, the <code>channelIndexMask</code> is an OR-ed combination of
1219          *    bits representing the mapping of input source channels to
1220          *    <code>AudioRecord</code> read samples.
1221          *    For example, a mask of <code>0x5</code>, or binary
1222          *    <code>101</code>, will read from the first and third channel of the input
1223          *    source device and store them in the first and second sample of the
1224          *    <code>AudioRecord</code> read frame.
1225          *    Unmatched input source channels are dropped and
1226          *    unmatched <code>AudioRecord</code> read samples are zero filled.
1227          * @return the same <code>Builder</code> instance.
1228          * @throws IllegalArgumentException if the channel index mask is invalid or
1229          *    if both channel index mask and channel position mask
1230          *    are specified but do not have the same channel count.
1231          */
setChannelIndexMask(int channelIndexMask)1232         public @NonNull Builder setChannelIndexMask(int channelIndexMask) {
1233             if (channelIndexMask == 0) {
1234                 throw new IllegalArgumentException("Invalid zero channel index mask");
1235             } else if (/* channelIndexMask != 0 && */ mChannelMask != 0 &&
1236                     Integer.bitCount(channelIndexMask) != Integer.bitCount(mChannelMask)) {
1237                 throw new IllegalArgumentException("Mismatched channel count for index mask " +
1238                         Integer.toHexString(channelIndexMask).toUpperCase());
1239             }
1240             mChannelIndexMask = channelIndexMask;
1241             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK;
1242             return this;
1243         }
1244 
1245         /**
1246          * Sets the sample rate.
1247          * @param sampleRate the sample rate expressed in Hz
1248          * @return the same Builder instance.
1249          * @throws java.lang.IllegalArgumentException
1250          */
setSampleRate(int sampleRate)1251         public Builder setSampleRate(int sampleRate) throws IllegalArgumentException {
1252             // TODO Consider whether to keep the MIN and MAX range checks here.
1253             // It is not necessary and poses the problem of defining the limits independently from
1254             // native implementation or platform capabilities.
1255             if (((sampleRate < SAMPLE_RATE_HZ_MIN) || (sampleRate > SAMPLE_RATE_HZ_MAX)) &&
1256                     sampleRate != SAMPLE_RATE_UNSPECIFIED) {
1257                 throw new IllegalArgumentException("Invalid sample rate " + sampleRate);
1258             }
1259             mSampleRate = sampleRate;
1260             mPropertySetMask |= AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE;
1261             return this;
1262         }
1263     }
1264 
1265     @Override
equals(Object o)1266     public boolean equals(Object o) {
1267         if (this == o) return true;
1268         if (o == null || getClass() != o.getClass()) return false;
1269 
1270         AudioFormat that = (AudioFormat) o;
1271 
1272         if (mPropertySetMask != that.mPropertySetMask) return false;
1273 
1274         // return false if any of the properties is set and the values differ
1275         return !((((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) != 0)
1276                             && (mEncoding != that.mEncoding))
1277                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) != 0)
1278                             && (mSampleRate != that.mSampleRate))
1279                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) != 0)
1280                             && (mChannelMask != that.mChannelMask))
1281                     || (((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_INDEX_MASK) != 0)
1282                             && (mChannelIndexMask != that.mChannelIndexMask)));
1283     }
1284 
1285     @Override
hashCode()1286     public int hashCode() {
1287         return Objects.hash(mPropertySetMask, mSampleRate, mEncoding, mChannelMask,
1288                 mChannelIndexMask);
1289     }
1290 
1291     @Override
describeContents()1292     public int describeContents() {
1293         return 0;
1294     }
1295 
1296     @Override
writeToParcel(Parcel dest, int flags)1297     public void writeToParcel(Parcel dest, int flags) {
1298         dest.writeInt(mPropertySetMask);
1299         dest.writeInt(mEncoding);
1300         dest.writeInt(mSampleRate);
1301         dest.writeInt(mChannelMask);
1302         dest.writeInt(mChannelIndexMask);
1303     }
1304 
AudioFormat(Parcel in)1305     private AudioFormat(Parcel in) {
1306         this(
1307              in.readInt(), // propertySetMask
1308              in.readInt(), // encoding
1309              in.readInt(), // sampleRate
1310              in.readInt(), // channelMask
1311              in.readInt()  // channelIndexMask
1312             );
1313     }
1314 
1315     public static final @android.annotation.NonNull Parcelable.Creator<AudioFormat> CREATOR =
1316             new Parcelable.Creator<AudioFormat>() {
1317         public AudioFormat createFromParcel(Parcel p) {
1318             return new AudioFormat(p);
1319         }
1320         public AudioFormat[] newArray(int size) {
1321             return new AudioFormat[size];
1322         }
1323     };
1324 
1325     @Override
toString()1326     public String toString () {
1327         return new String("AudioFormat:"
1328                 + " props=" + mPropertySetMask
1329                 + " enc=" + mEncoding
1330                 + " chan=0x" + Integer.toHexString(mChannelMask).toUpperCase()
1331                 + " chan_index=0x" + Integer.toHexString(mChannelIndexMask).toUpperCase()
1332                 + " rate=" + mSampleRate);
1333     }
1334 
1335     /** @hide */
1336     @IntDef(flag = false, prefix = "ENCODING", value = {
1337         ENCODING_DEFAULT,
1338         ENCODING_PCM_16BIT,
1339         ENCODING_PCM_8BIT,
1340         ENCODING_PCM_FLOAT,
1341         ENCODING_AC3,
1342         ENCODING_E_AC3,
1343         ENCODING_DTS,
1344         ENCODING_DTS_HD,
1345         ENCODING_MP3,
1346         ENCODING_AAC_LC,
1347         ENCODING_AAC_HE_V1,
1348         ENCODING_AAC_HE_V2,
1349         ENCODING_IEC61937,
1350         ENCODING_DOLBY_TRUEHD,
1351         ENCODING_AAC_ELD,
1352         ENCODING_AAC_XHE,
1353         ENCODING_AC4,
1354         ENCODING_E_AC3_JOC,
1355         ENCODING_DOLBY_MAT,
1356         ENCODING_OPUS,
1357         ENCODING_PCM_24BIT_PACKED,
1358         ENCODING_PCM_32BIT,
1359         ENCODING_MPEGH_BL_L3,
1360         ENCODING_MPEGH_BL_L4,
1361         ENCODING_MPEGH_LC_L3,
1362         ENCODING_MPEGH_LC_L4,
1363         ENCODING_DTS_UHD,
1364         ENCODING_DRA }
1365     )
1366     @Retention(RetentionPolicy.SOURCE)
1367     public @interface Encoding {}
1368 
1369     /** @hide */
1370     public static final int[] SURROUND_SOUND_ENCODING = {
1371             ENCODING_AC3,
1372             ENCODING_E_AC3,
1373             ENCODING_DTS,
1374             ENCODING_DTS_HD,
1375             ENCODING_AAC_LC,
1376             ENCODING_DOLBY_TRUEHD,
1377             ENCODING_AC4,
1378             ENCODING_E_AC3_JOC,
1379             ENCODING_DOLBY_MAT,
1380             ENCODING_MPEGH_BL_L3,
1381             ENCODING_MPEGH_BL_L4,
1382             ENCODING_MPEGH_LC_L3,
1383             ENCODING_MPEGH_LC_L4,
1384             ENCODING_DTS_UHD,
1385             ENCODING_DRA
1386     };
1387 
1388     /** @hide */
1389     @IntDef(flag = false, prefix = "ENCODING", value = {
1390             ENCODING_AC3,
1391             ENCODING_E_AC3,
1392             ENCODING_DTS,
1393             ENCODING_DTS_HD,
1394             ENCODING_AAC_LC,
1395             ENCODING_DOLBY_TRUEHD,
1396             ENCODING_AC4,
1397             ENCODING_E_AC3_JOC,
1398             ENCODING_DOLBY_MAT,
1399             ENCODING_MPEGH_BL_L3,
1400             ENCODING_MPEGH_BL_L4,
1401             ENCODING_MPEGH_LC_L3,
1402             ENCODING_MPEGH_LC_L4,
1403             ENCODING_DTS_UHD,
1404             ENCODING_DRA }
1405     )
1406     @Retention(RetentionPolicy.SOURCE)
1407     public @interface SurroundSoundEncoding {}
1408 
1409     /**
1410      * @hide
1411      *
1412      * Return default name for a surround format. This is not an International name.
1413      * It is just a default to use if an international name is not available.
1414      *
1415      * @param audioFormat a surround format
1416      * @return short default name for the format.
1417      */
toDisplayName(@urroundSoundEncoding int audioFormat)1418     public static String toDisplayName(@SurroundSoundEncoding int audioFormat) {
1419         switch (audioFormat) {
1420             case ENCODING_AC3:
1421                 return "Dolby Digital";
1422             case ENCODING_E_AC3:
1423                 return "Dolby Digital Plus";
1424             case ENCODING_DTS:
1425                 return "DTS";
1426             case ENCODING_DTS_HD:
1427                 return "DTS HD";
1428             case ENCODING_AAC_LC:
1429                 return "AAC";
1430             case ENCODING_DOLBY_TRUEHD:
1431                 return "Dolby TrueHD";
1432             case ENCODING_AC4:
1433                 return "Dolby AC-4";
1434             case ENCODING_E_AC3_JOC:
1435                 return "Dolby Atmos in Dolby Digital Plus";
1436             case ENCODING_DOLBY_MAT:
1437                 return "Dolby MAT";
1438             case ENCODING_MPEGH_BL_L3:
1439                 return "MPEG-H 3D Audio baseline profile level 3";
1440             case ENCODING_MPEGH_BL_L4:
1441                 return "MPEG-H 3D Audio baseline profile level 4";
1442             case ENCODING_MPEGH_LC_L3:
1443                 return "MPEG-H 3D Audio low complexity profile level 3";
1444             case ENCODING_MPEGH_LC_L4:
1445                 return "MPEG-H 3D Audio low complexity profile level 4";
1446             case ENCODING_DTS_UHD:
1447                 return "DTS UHD";
1448             case ENCODING_DRA:
1449                 return "DRA";
1450             default:
1451                 return "Unknown surround sound format";
1452         }
1453     }
1454 
1455 }
1456