1 /* 2 * Copyright 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.google.sample.oboe.manualtest; 18 19 /** 20 * Container for the properties of a Stream. 21 * 22 * This can be used to build a stream, or as a base class for a Stream, 23 * or as a way to report the properties of a Stream. 24 */ 25 26 public class StreamConfiguration { 27 public static final int UNSPECIFIED = 0; 28 29 // These must match order in Spinner and in native code and in AAudio.h 30 public static final int NATIVE_API_UNSPECIFIED = 0; 31 public static final int NATIVE_API_OPENSLES = 1; 32 public static final int NATIVE_API_AAUDIO = 2; 33 34 public static final int SHARING_MODE_EXCLUSIVE = 0; // must match AAUDIO 35 public static final int SHARING_MODE_SHARED = 1; // must match AAUDIO 36 37 public static final int AUDIO_FORMAT_PCM_16 = 1; // must match AAUDIO 38 public static final int AUDIO_FORMAT_PCM_FLOAT = 2; // must match AAUDIO 39 40 public static final int DIRECTION_OUTPUT = 0; // must match AAUDIO 41 public static final int DIRECTION_INPUT = 1; // must match AAUDIO 42 43 public static final int SESSION_ID_NONE = -1; // must match AAUDIO 44 public static final int SESSION_ID_ALLOCATE = 0; // must match AAUDIO 45 46 public static final int PERFORMANCE_MODE_NONE = 10; // must match AAUDIO 47 public static final int PERFORMANCE_MODE_POWER_SAVING = 11; // must match AAUDIO 48 public static final int PERFORMANCE_MODE_LOW_LATENCY = 12; // must match AAUDIO 49 50 public static final int RATE_CONVERSION_QUALITY_NONE = 0; // must match Oboe 51 public static final int RATE_CONVERSION_QUALITY_FASTEST = 1; // must match Oboe 52 public static final int RATE_CONVERSION_QUALITY_LOW = 2; // must match Oboe 53 public static final int RATE_CONVERSION_QUALITY_MEDIUM = 3; // must match Oboe 54 public static final int RATE_CONVERSION_QUALITY_HIGH = 4; // must match Oboe 55 public static final int RATE_CONVERSION_QUALITY_BEST = 5; // must match Oboe 56 57 public static final int STREAM_STATE_STARTING = 3; // must match Oboe 58 public static final int STREAM_STATE_STARTED = 4; // must match Oboe 59 60 public static final int INPUT_PRESET_GENERIC = 1; // must match Oboe 61 public static final int INPUT_PRESET_CAMCORDER = 5; // must match Oboe 62 public static final int INPUT_PRESET_VOICE_RECOGNITION = 6; // must match Oboe 63 public static final int INPUT_PRESET_VOICE_COMMUNICATION = 7; // must match Oboe 64 public static final int INPUT_PRESET_UNPROCESSED = 9; // must match Oboe 65 public static final int INPUT_PRESET_VOICE_PERFORMANCE = 10; // must match Oboe 66 67 private int mNativeApi; 68 private int mBufferCapacityInFrames; 69 private int mChannelCount; 70 private int mDeviceId; 71 private int mSessionId; 72 private int mDirection; // does not get reset 73 private int mFormat; 74 private int mSampleRate; 75 private int mSharingMode; 76 private int mPerformanceMode; 77 private boolean mFormatConversionAllowed; 78 private boolean mChannelConversionAllowed; 79 private int mRateConversionQuality; 80 private int mInputPreset; 81 82 private int mFramesPerBurst = 0; 83 84 private boolean mMMap = false; 85 StreamConfiguration()86 public StreamConfiguration() { 87 reset(); 88 } 89 reset()90 public void reset() { 91 mNativeApi = NATIVE_API_UNSPECIFIED; 92 mBufferCapacityInFrames = UNSPECIFIED; 93 mChannelCount = UNSPECIFIED; 94 mDeviceId = UNSPECIFIED; 95 mSessionId = -1; 96 mFormat = AUDIO_FORMAT_PCM_FLOAT; 97 mSampleRate = UNSPECIFIED; 98 mSharingMode = SHARING_MODE_EXCLUSIVE; 99 mPerformanceMode = PERFORMANCE_MODE_LOW_LATENCY; 100 mInputPreset = INPUT_PRESET_VOICE_RECOGNITION; 101 mFormatConversionAllowed = false; 102 mChannelConversionAllowed = false; 103 mRateConversionQuality = RATE_CONVERSION_QUALITY_NONE; 104 mMMap = NativeEngine.isMMapSupported(); 105 } 106 getFramesPerBurst()107 public int getFramesPerBurst() { 108 return mFramesPerBurst; 109 } 110 setFramesPerBurst(int framesPerBurst)111 public void setFramesPerBurst(int framesPerBurst) { 112 this.mFramesPerBurst = framesPerBurst; 113 } 114 getBufferCapacityInFrames()115 public int getBufferCapacityInFrames() { 116 return mBufferCapacityInFrames; 117 } 118 setBufferCapacityInFrames(int bufferCapacityInFrames)119 public void setBufferCapacityInFrames(int bufferCapacityInFrames) { 120 this.mBufferCapacityInFrames = bufferCapacityInFrames; 121 } 122 getFormat()123 public int getFormat() { 124 return mFormat; 125 } 126 setFormat(int format)127 public void setFormat(int format) { 128 this.mFormat = format; 129 } 130 getDirection()131 public int getDirection() { 132 return mDirection; 133 } 134 setDirection(int direction)135 public void setDirection(int direction) { 136 this.mDirection = direction; 137 } 138 getPerformanceMode()139 public int getPerformanceMode() { 140 return mPerformanceMode; 141 } 142 setPerformanceMode(int performanceMode)143 public void setPerformanceMode(int performanceMode) { 144 this.mPerformanceMode = performanceMode; 145 } 146 getInputPreset()147 public int getInputPreset() { 148 return mInputPreset; 149 } setInputPreset(int inputPreset)150 public void setInputPreset(int inputPreset) { 151 this.mInputPreset = inputPreset; 152 } 153 convertPerformanceModeToText(int performanceMode)154 static String convertPerformanceModeToText(int performanceMode) { 155 switch(performanceMode) { 156 case PERFORMANCE_MODE_NONE: 157 return "NONE"; 158 case PERFORMANCE_MODE_POWER_SAVING: 159 return "PWRSAV"; 160 case PERFORMANCE_MODE_LOW_LATENCY: 161 return "LOWLAT"; 162 default: 163 return "INVALID"; 164 } 165 } 166 getSharingMode()167 public int getSharingMode() { 168 return mSharingMode; 169 } 170 setSharingMode(int sharingMode)171 public void setSharingMode(int sharingMode) { 172 this.mSharingMode = sharingMode; 173 } 174 convertSharingModeToText(int sharingMode)175 static String convertSharingModeToText(int sharingMode) { 176 switch(sharingMode) { 177 case SHARING_MODE_SHARED: 178 return "SHARED"; 179 case SHARING_MODE_EXCLUSIVE: 180 return "EXCLUSIVE"; 181 default: 182 return "INVALID"; 183 } 184 } 185 convertFormatToText(int format)186 public static String convertFormatToText(int format) { 187 switch(format) { 188 case UNSPECIFIED: 189 return "Unspecified"; 190 case AUDIO_FORMAT_PCM_16: 191 return "I16"; 192 case AUDIO_FORMAT_PCM_FLOAT: 193 return "Float"; 194 default: 195 return "Invalid"; 196 } 197 } 198 convertNativeApiToText(int api)199 public static String convertNativeApiToText(int api) { 200 switch(api) { 201 case NATIVE_API_UNSPECIFIED: 202 return "Unspec"; 203 case NATIVE_API_AAUDIO: 204 return "AAudio"; 205 case NATIVE_API_OPENSLES: 206 return "OpenSL"; 207 default: 208 return "Invalid"; 209 } 210 } 211 212 dump()213 public String dump() { 214 String prefix = (getDirection() == DIRECTION_INPUT) ? "in" : "out"; 215 StringBuffer message = new StringBuffer(); 216 message.append(String.format("%s.channels = %d\n", prefix, mChannelCount)); 217 message.append(String.format("%s.perf = %s\n", prefix, 218 convertPerformanceModeToText(mPerformanceMode).toLowerCase())); 219 if (getDirection() == DIRECTION_INPUT) { 220 message.append(String.format("%s.preset = %s\n", prefix, 221 convertInputPresetToText(mInputPreset).toLowerCase())); 222 } 223 message.append(String.format("%s.sharing = %s\n", prefix, 224 convertSharingModeToText(mSharingMode).toLowerCase())); 225 message.append(String.format("%s.api = %s\n", prefix, 226 convertNativeApiToText(getNativeApi()).toLowerCase())); 227 message.append(String.format("%s.rate = %d\n", prefix, mSampleRate)); 228 message.append(String.format("%s.device = %d\n", prefix, mDeviceId)); 229 message.append(String.format("%s.mmap = %s\n", prefix, isMMap() ? "yes" : "no")); 230 message.append(String.format("%s.rate.conversion.quality = %d\n", prefix, mRateConversionQuality)); 231 return message.toString(); 232 } 233 234 // text must match menu values 235 public static final String NAME_INPUT_PRESET_GENERIC = "Generic"; 236 public static final String NAME_INPUT_PRESET_CAMCORDER = "Camcorder"; 237 public static final String NAME_INPUT_PRESET_VOICE_RECOGNITION = "VoiceRec"; 238 public static final String NAME_INPUT_PRESET_VOICE_COMMUNICATION = "VoiceComm"; 239 public static final String NAME_INPUT_PRESET_UNPROCESSED = "Unprocessed"; 240 public static final String NAME_INPUT_PRESET_VOICE_PERFORMANCE = "Performance"; 241 convertInputPresetToText(int inputPreset)242 public static String convertInputPresetToText(int inputPreset) { 243 switch(inputPreset) { 244 case INPUT_PRESET_GENERIC: 245 return NAME_INPUT_PRESET_GENERIC; 246 case INPUT_PRESET_CAMCORDER: 247 return NAME_INPUT_PRESET_CAMCORDER; 248 case INPUT_PRESET_VOICE_RECOGNITION: 249 return NAME_INPUT_PRESET_VOICE_RECOGNITION; 250 case INPUT_PRESET_VOICE_COMMUNICATION: 251 return NAME_INPUT_PRESET_VOICE_COMMUNICATION; 252 case INPUT_PRESET_UNPROCESSED: 253 return NAME_INPUT_PRESET_UNPROCESSED; 254 case INPUT_PRESET_VOICE_PERFORMANCE: 255 return NAME_INPUT_PRESET_VOICE_PERFORMANCE; 256 default: 257 return "Invalid"; 258 } 259 } 260 matchInputPreset(String text, int preset)261 private static boolean matchInputPreset(String text, int preset) { 262 return convertInputPresetToText(preset).toLowerCase().equals(text); 263 } 264 265 /** 266 * Case insensitive. 267 * @param text 268 * @return inputPreset, eg. INPUT_PRESET_CAMCORDER 269 */ convertTextToInputPreset(String text)270 public static int convertTextToInputPreset(String text) { 271 text = text.toLowerCase(); 272 if (matchInputPreset(text, INPUT_PRESET_GENERIC)) { 273 return INPUT_PRESET_GENERIC; 274 } else if (matchInputPreset(text, INPUT_PRESET_CAMCORDER)) { 275 return INPUT_PRESET_CAMCORDER; 276 } else if (matchInputPreset(text, INPUT_PRESET_VOICE_RECOGNITION)) { 277 return INPUT_PRESET_VOICE_RECOGNITION; 278 } else if (matchInputPreset(text, INPUT_PRESET_VOICE_COMMUNICATION)) { 279 return INPUT_PRESET_VOICE_COMMUNICATION; 280 } else if (matchInputPreset(text, INPUT_PRESET_UNPROCESSED)) { 281 return INPUT_PRESET_UNPROCESSED; 282 } else if (matchInputPreset(text, INPUT_PRESET_VOICE_PERFORMANCE)) { 283 return INPUT_PRESET_VOICE_PERFORMANCE; 284 } 285 return -1; 286 } 287 getChannelCount()288 public int getChannelCount() { 289 return mChannelCount; 290 } 291 setChannelCount(int channelCount)292 public void setChannelCount(int channelCount) { 293 this.mChannelCount = channelCount; 294 } 295 getSampleRate()296 public int getSampleRate() { 297 return mSampleRate; 298 } 299 setSampleRate(int sampleRate)300 public void setSampleRate(int sampleRate) { 301 this.mSampleRate = sampleRate; 302 } 303 getDeviceId()304 public int getDeviceId() { 305 return mDeviceId; 306 } 307 setDeviceId(int deviceId)308 public void setDeviceId(int deviceId) { 309 this.mDeviceId = deviceId; 310 } 311 getSessionId()312 public int getSessionId() { 313 return mSessionId; 314 } 315 setSessionId(int sessionId)316 public void setSessionId(int sessionId) { 317 mSessionId = sessionId; 318 } 319 isMMap()320 public boolean isMMap() { 321 return mMMap; 322 } setMMap(boolean b)323 public void setMMap(boolean b) { 324 mMMap = b; 325 } 326 getNativeApi()327 public int getNativeApi() { 328 return mNativeApi; 329 } 330 setNativeApi(int nativeApi)331 public void setNativeApi(int nativeApi) { 332 mNativeApi = nativeApi; 333 } 334 setChannelConversionAllowed(boolean b)335 public void setChannelConversionAllowed(boolean b) { mChannelConversionAllowed = b; } 336 getChannelConversionAllowed()337 public boolean getChannelConversionAllowed() { 338 return mChannelConversionAllowed; 339 } 340 setFormatConversionAllowed(boolean b)341 public void setFormatConversionAllowed(boolean b) { 342 mFormatConversionAllowed = b; 343 } 344 getFormatConversionAllowed()345 public boolean getFormatConversionAllowed() { 346 return mFormatConversionAllowed; 347 } 348 setRateConversionQuality(int quality)349 public void setRateConversionQuality(int quality) { mRateConversionQuality = quality; } 350 getRateConversionQuality()351 public int getRateConversionQuality() { 352 return mRateConversionQuality; 353 } 354 355 } 356