• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.cts.verifier.audio.audiolib;
18 
19 import android.media.AudioFormat;
20 import android.media.AudioRecord;
21 import android.media.AudioTrack;
22 
23 import java.util.HashMap;
24 
25 // TODO - This functionality probably exists in the framework function. Remove this and
26 //    use that instead.
27 public class AudioUtils {
28     @SuppressWarnings("unused")
29     private static final String TAG = "AudioUtils";
30 
countIndexChannels(int chanConfig)31     public static int countIndexChannels(int chanConfig) {
32         return Integer.bitCount(chanConfig & ~0x80000000);
33     }
34 
countToIndexMask(int chanCount)35     public static int countToIndexMask(int chanCount) {
36         return (1 << chanCount) - 1;
37     }
38 
countToOutPositionMask(int channelCount)39     public static int countToOutPositionMask(int channelCount) {
40         switch (channelCount) {
41             case 1:
42                 return AudioFormat.CHANNEL_OUT_MONO;
43 
44             case 2:
45                 return AudioFormat.CHANNEL_OUT_STEREO;
46 
47             case 4:
48                 return AudioFormat.CHANNEL_OUT_QUAD;
49 
50             default:
51                 return AudioTrack.ERROR_BAD_VALUE;
52         }
53     }
54 
countToInPositionMask(int channelCount)55     public static int countToInPositionMask(int channelCount) {
56         switch (channelCount) {
57             case 1:
58                 return AudioFormat.CHANNEL_IN_MONO;
59 
60             case 2:
61                 return AudioFormat.CHANNEL_IN_STEREO;
62 
63             default:
64                 return AudioRecord.ERROR_BAD_VALUE;
65         }
66     }
67 
68     // Encodings
sampleSizeInBytes(int encoding)69     public static int sampleSizeInBytes(int encoding) {
70         switch (encoding) {
71             case AudioFormat.ENCODING_PCM_16BIT:
72                 return 2;
73 
74             case AudioFormat.ENCODING_PCM_FLOAT:
75                 return 4;
76 
77             default:
78                 return 0;
79         }
80     }
81 
calcFrameSizeInBytes(int encoding, int numChannels)82     public static int calcFrameSizeInBytes(int encoding, int numChannels) {
83         return sampleSizeInBytes(encoding) * numChannels;
84     }
85 
isMMapSupported()86     public static native boolean isMMapSupported();
isMMapExclusiveSupported()87     public static native boolean isMMapExclusiveSupported();
88 
89     /*
90      * Channel Mask Utilities
91      */
92     private static final HashMap<Integer, String> sEncodingStrings =
93             new HashMap<Integer, String>();
94     /**
95      * A table of strings corresponding to output channel position masks
96      */
97     private static final HashMap<Integer, String> sOutChanPosStrings =
98             new HashMap<Integer, String>();
99 
100     /**
101      * A table of strings corresponding to output channel position masks
102      */
103     private static final HashMap<Integer, String> sInChanPosStrings =
104             new HashMap<Integer, String>();
105 
initOutChanPositionStrings()106     static void initOutChanPositionStrings() {
107         sOutChanPosStrings.put(AudioFormat.CHANNEL_INVALID, "CHANNEL_INVALID");
108         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_DEFAULT, "CHANNEL_OUT_DEFAULT");
109         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_FRONT_LEFT,
110                 "CHANNEL_OUT_MONO"/*"CHANNEL_OUT_FRONT_LEFT"*/);
111         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_FRONT_RIGHT, "CHANNEL_OUT_FRONT_RIGHT");
112         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_FRONT_CENTER, "CHANNEL_OUT_FRONT_CENTER");
113         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_LOW_FREQUENCY, "CHANNEL_OUT_LOW_FREQUENCY");
114         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_BACK_LEFT, "CHANNEL_OUT_BACK_LEFT");
115         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_BACK_RIGHT, "CHANNEL_OUT_BACK_RIGHT");
116         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_FRONT_LEFT_OF_CENTER,
117                 "CHANNEL_OUT_FRONT_LEFT_OF_CENTER");
118         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_FRONT_RIGHT_OF_CENTER,
119                 "CHANNEL_OUT_FRONT_RIGHT_OF_CENTER");
120         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_BACK_CENTER, "CHANNEL_OUT_BACK_CENTER");
121         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_SIDE_LEFT, "CHANNEL_OUT_SIDE_LEFT");
122         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_SIDE_RIGHT, "CHANNEL_OUT_SIDE_RIGHT");
123         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO");
124         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_QUAD, "CHANNEL_OUT_QUAD");
125         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_SURROUND, "CHANNEL_OUT_SURROUND");
126         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_5POINT1, "CHANNEL_OUT_5POINT1");
127         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_7POINT1, "CHANNEL_OUT_7POINT1");
128         sOutChanPosStrings.put(AudioFormat.CHANNEL_OUT_7POINT1_SURROUND,
129                 "CHANNEL_OUT_7POINT1_SURROUND");
130     }
131 
initInChanPositionStrings()132     static void initInChanPositionStrings() {
133         sInChanPosStrings.put(AudioFormat.CHANNEL_INVALID, "CHANNEL_INVALID");
134         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_DEFAULT, "CHANNEL_IN_DEFAULT");
135         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_LEFT, "CHANNEL_IN_LEFT");
136         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_RIGHT, "CHANNEL_IN_RIGHT");
137         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_MONO, "CHANNEL_IN_MONO"/*CHANNEL_IN_FRONT*/);
138         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_BACK, "CHANNEL_IN_BACK");
139         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_LEFT_PROCESSED, "CHANNEL_IN_LEFT_PROCESSED");
140         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_RIGHT_PROCESSED, "CHANNEL_IN_RIGHT_PROCESSED");
141         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_FRONT_PROCESSED, "CHANNEL_IN_FRONT_PROCESSED");
142         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_BACK_PROCESSED, "CHANNEL_IN_BACK_PROCESSED");
143         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_PRESSURE, "CHANNEL_IN_PRESSURE");
144         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_X_AXIS, "CHANNEL_IN_X_AXIS");
145         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_Y_AXIS, "CHANNEL_IN_Y_AXIS");
146         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_Z_AXIS, "CHANNEL_IN_Z_AXIS");
147         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_VOICE_UPLINK, "CHANNEL_IN_VOICE_UPLINK");
148         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_VOICE_DNLINK, "CHANNEL_IN_VOICE_DNLINK");
149         sInChanPosStrings.put(AudioFormat.CHANNEL_IN_STEREO, "CHANNEL_IN_STEREO");
150     }
151 
initEncodingStrings()152     static void initEncodingStrings() {
153         sEncodingStrings.put(AudioFormat.ENCODING_INVALID, "ENCODING_INVALID");
154         sEncodingStrings.put(AudioFormat.ENCODING_DEFAULT, "ENCODING_DEFAULT");
155         sEncodingStrings.put(AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT");
156         sEncodingStrings.put(AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT");
157         sEncodingStrings.put(AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT");
158         sEncodingStrings.put(AudioFormat.ENCODING_AC3, "ENCODING_AC3");
159         sEncodingStrings.put(AudioFormat.ENCODING_E_AC3, "ENCODING_E_AC3");
160         sEncodingStrings.put(AudioFormat.ENCODING_DTS, "ENCODING_DTS");
161         sEncodingStrings.put(AudioFormat.ENCODING_DTS_HD, "ENCODING_DTS_HD");
162         sEncodingStrings.put(AudioFormat.ENCODING_MP3, "ENCODING_MP3");
163         sEncodingStrings.put(AudioFormat.ENCODING_AAC_LC, "ENCODING_AAC_LC");
164         sEncodingStrings.put(AudioFormat.ENCODING_AAC_HE_V1, "ENCODING_AAC_HE_V1");
165         sEncodingStrings.put(AudioFormat.ENCODING_AAC_HE_V2, "ENCODING_AAC_HE_V2");
166         sEncodingStrings.put(AudioFormat.ENCODING_IEC61937, "ENCODING_IEC61937");
167         sEncodingStrings.put(AudioFormat.ENCODING_DOLBY_TRUEHD, "ENCODING_DOLBY_TRUEHD");
168         sEncodingStrings.put(AudioFormat.ENCODING_AAC_ELD, "ENCODING_AAC_ELD");
169         sEncodingStrings.put(AudioFormat.ENCODING_AAC_XHE, "ENCODING_AAC_XHE");
170         sEncodingStrings.put(AudioFormat.ENCODING_AC4, "ENCODING_AC4");
171         sEncodingStrings.put(AudioFormat.ENCODING_E_AC3_JOC, "ENCODING_E_AC3_JOC");
172         sEncodingStrings.put(AudioFormat.ENCODING_DOLBY_MAT, "ENCODING_DOLBY_MAT");
173         sEncodingStrings.put(AudioFormat.ENCODING_OPUS, "ENCODING_OPUS");
174         sEncodingStrings.put(AudioFormat.ENCODING_PCM_24BIT_PACKED, "ENCODING_PCM_24BIT_PACKED");
175         sEncodingStrings.put(AudioFormat.ENCODING_PCM_32BIT, "ENCODING_PCM_32BIT");
176         sEncodingStrings.put(AudioFormat.ENCODING_MPEGH_BL_L3, "ENCODING_MPEGH_BL_L3");
177         sEncodingStrings.put(AudioFormat.ENCODING_MPEGH_BL_L4, "ENCODING_MPEGH_BL_L4");
178         sEncodingStrings.put(AudioFormat.ENCODING_MPEGH_LC_L3, "ENCODING_MPEGH_LC_L3");
179         sEncodingStrings.put(AudioFormat.ENCODING_MPEGH_LC_L4, "ENCODING_MPEGH_LC_L4");
180         sEncodingStrings.put(AudioFormat.ENCODING_DTS_UHD, "ENCODING_DTS_UHD");
181         sEncodingStrings.put(AudioFormat.ENCODING_DRA, "ENCODING_DRA");
182     }
183 
184     static {
initOutChanPositionStrings()185         initOutChanPositionStrings();
initInChanPositionStrings()186         initInChanPositionStrings();
initEncodingStrings()187         initEncodingStrings();
188     }
189 
190     /**
191      * @param channelMask An OUTPUT Positional Channel Mask
192      * @return A human-readable string corresponding to the specified channel mask
193      */
channelOutPositionMaskToString(int channelMask)194     public static String channelOutPositionMaskToString(int channelMask) {
195         String maskString = sOutChanPosStrings.get(channelMask);
196         return maskString != null ? maskString : ("0x" + Integer.toHexString(channelMask));
197     }
198 
199     /**
200      * @param channelMask An INPUT Positional Channel Mask
201      * @return A human-readable string corresponding to the specified channel mask
202      */
channelInPositionMaskToString(int channelMask)203     public static String channelInPositionMaskToString(int channelMask) {
204         String maskString = sInChanPosStrings.get(channelMask);
205         return maskString != null ? maskString : ("0x" + Integer.toHexString(channelMask));
206     }
207 
208     /**
209      * @param channelMask An INDEX Channel Mask
210      * @return A human-readable string corresponding to the specified channel mask
211      */
channelIndexMaskToString(int channelMask)212     public static String channelIndexMaskToString(int channelMask) {
213         return "0x" + Integer.toHexString(channelMask);
214     }
215 
216     /**
217      * @param encoding An audio encoding constant
218      * @return A human-readable string corresponding to the specified encoding value
219      */
encodingToString(int encoding)220     public static String encodingToString(int encoding) {
221         String encodingString = sEncodingStrings.get(encoding);
222         return encodingString != null ? encodingString : ("0x" + Integer.toHexString(encoding));
223     }
224 }