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 30 public static int countIndexChannels(int chanConfig) { 31 return Integer.bitCount(chanConfig & ~0x80000000); 32 } 33 34 public static int countToIndexMask(int chanCount) { 35 // From the documentation for AudioFormat: 36 // The canonical channel index masks by channel count are given by the formula 37 // (1 << channelCount) - 1. 38 return (1 << chanCount) - 1; 39 } 40 41 public static int countToOutPositionMask(int channelCount) { 42 switch (channelCount) { 43 case 1: 44 return AudioFormat.CHANNEL_OUT_MONO; 45 46 case 2: 47 return AudioFormat.CHANNEL_OUT_STEREO; 48 49 case 4: 50 return AudioFormat.CHANNEL_OUT_QUAD; 51 52 default: 53 return AudioTrack.ERROR_BAD_VALUE; 54 } 55 } 56 57 public static int countToInPositionMask(int channelCount) { 58 switch (channelCount) { 59 case 1: 60 return AudioFormat.CHANNEL_IN_MONO; 61 62 case 2: 63 return AudioFormat.CHANNEL_IN_STEREO; 64 65 default: 66 return AudioRecord.ERROR_BAD_VALUE; 67 } 68 } 69 70 // Encodings 71 public static int sampleSizeInBytes(int encoding) { 72 switch (encoding) { 73 case AudioFormat.ENCODING_PCM_16BIT: 74 return 2; 75 76 case AudioFormat.ENCODING_PCM_FLOAT: 77 return 4; 78 79 default: 80 return 0; 81 } 82 } 83 84 public static int calcFrameSizeInBytes(int encoding, int numChannels) { 85 return sampleSizeInBytes(encoding) * numChannels; 86 } 87 }