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 import android.util.Log;
23 
24 // TODO - This functionality probably exists in the framework function. Remove this and
25 //    use that instead.
26 public class AudioUtils {
27     @SuppressWarnings("unused")
28     private static final String TAG = "AudioUtils";
29 
countIndexChannels(int chanConfig)30     public static int countIndexChannels(int chanConfig) {
31         return Integer.bitCount(chanConfig & ~0x80000000);
32     }
33 
countToIndexMask(int chanCount)34     public static int countToIndexMask(int chanCount) {
35         return (1 << chanCount) - 1;
36     }
37 
countToOutPositionMask(int channelCount)38     public static int countToOutPositionMask(int channelCount) {
39         switch (channelCount) {
40             case 1:
41                 return AudioFormat.CHANNEL_OUT_MONO;
42 
43             case 2:
44                 return AudioFormat.CHANNEL_OUT_STEREO;
45 
46             case 4:
47                 return AudioFormat.CHANNEL_OUT_QUAD;
48 
49             default:
50                 return AudioTrack.ERROR_BAD_VALUE;
51         }
52     }
53 
countToInPositionMask(int channelCount)54     public static int countToInPositionMask(int channelCount) {
55         switch (channelCount) {
56             case 1:
57                 return AudioFormat.CHANNEL_IN_MONO;
58 
59             case 2:
60                 return AudioFormat.CHANNEL_IN_STEREO;
61 
62             default:
63                 return AudioRecord.ERROR_BAD_VALUE;
64         }
65     }
66 
67     // Encodings
sampleSizeInBytes(int encoding)68     public static int sampleSizeInBytes(int encoding) {
69         switch (encoding) {
70             case AudioFormat.ENCODING_PCM_16BIT:
71                 return 2;
72 
73             case AudioFormat.ENCODING_PCM_FLOAT:
74                 return 4;
75 
76             default:
77                 return 0;
78         }
79     }
80 
calcFrameSizeInBytes(int encoding, int numChannels)81     public static int calcFrameSizeInBytes(int encoding, int numChannels) {
82         return sampleSizeInBytes(encoding) * numChannels;
83     }
84 
isMMapSupported()85     public static native boolean isMMapSupported();
isMMapExclusiveSupported()86     public static native boolean isMMapExclusiveSupported();
87 }