Phil Burk | 4479523 | 2017-06-30 16:27:38 -0700 | [diff] [blame^] | 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 | #ifndef AAUDIO_EXAMPLE_ARGS_PARSER_H |
| 18 | #define AAUDIO_EXAMPLE_ARGS_PARSER_H |
| 19 | |
| 20 | #include <cctype> |
| 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
| 25 | #include <aaudio/AAudio.h> |
| 26 | #include <aaudio/AAudioTesting.h> |
| 27 | #include <AAudioExampleUtils.h> |
| 28 | |
| 29 | // TODO use this as a base class within AAudio |
| 30 | class AAudioParameters { |
| 31 | public: |
| 32 | |
| 33 | /** |
| 34 | * This is also known as samplesPerFrame. |
| 35 | */ |
| 36 | int32_t getChannelCount() const { |
| 37 | return mChannelCount; |
| 38 | } |
| 39 | |
| 40 | void setChannelCount(int32_t channelCount) { |
| 41 | mChannelCount = channelCount; |
| 42 | } |
| 43 | |
| 44 | int32_t getSampleRate() const { |
| 45 | return mSampleRate; |
| 46 | } |
| 47 | |
| 48 | void setSampleRate(int32_t sampleRate) { |
| 49 | mSampleRate = sampleRate; |
| 50 | } |
| 51 | |
| 52 | aaudio_format_t getFormat() const { |
| 53 | return mFormat; |
| 54 | } |
| 55 | |
| 56 | void setFormat(aaudio_format_t format) { |
| 57 | mFormat = format; |
| 58 | } |
| 59 | |
| 60 | aaudio_sharing_mode_t getSharingMode() const { |
| 61 | return mSharingMode; |
| 62 | } |
| 63 | |
| 64 | void setSharingMode(aaudio_sharing_mode_t sharingMode) { |
| 65 | mSharingMode = sharingMode; |
| 66 | } |
| 67 | |
| 68 | int32_t getBufferCapacity() const { |
| 69 | return mBufferCapacity; |
| 70 | } |
| 71 | |
| 72 | void setBufferCapacity(int32_t frames) { |
| 73 | mBufferCapacity = frames; |
| 74 | } |
| 75 | |
| 76 | int32_t getPerformanceMode() const { |
| 77 | return mPerformanceMode; |
| 78 | } |
| 79 | |
| 80 | void setPerformanceMode(aaudio_performance_mode_t performanceMode) { |
| 81 | mPerformanceMode = performanceMode; |
| 82 | } |
| 83 | |
| 84 | int32_t getDeviceId() const { |
| 85 | return mDeviceId; |
| 86 | } |
| 87 | |
| 88 | void setDeviceId(int32_t deviceId) { |
| 89 | mDeviceId = deviceId; |
| 90 | } |
| 91 | |
| 92 | int32_t getNumberOfBursts() const { |
| 93 | return mNumberOfBursts; |
| 94 | } |
| 95 | |
| 96 | void setNumberOfBursts(int32_t numBursts) { |
| 97 | mNumberOfBursts = numBursts; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Apply these parameters to a stream builder. |
| 102 | * @param builder |
| 103 | */ |
| 104 | void applyParameters(AAudioStreamBuilder *builder) const { |
| 105 | AAudioStreamBuilder_setChannelCount(builder, mChannelCount); |
| 106 | AAudioStreamBuilder_setFormat(builder, mFormat); |
| 107 | AAudioStreamBuilder_setSampleRate(builder, mSampleRate); |
| 108 | AAudioStreamBuilder_setBufferCapacityInFrames(builder, mBufferCapacity); |
| 109 | AAudioStreamBuilder_setDeviceId(builder, mDeviceId); |
| 110 | AAudioStreamBuilder_setSharingMode(builder, mSharingMode); |
| 111 | AAudioStreamBuilder_setPerformanceMode(builder, mPerformanceMode); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | int32_t mChannelCount = AAUDIO_UNSPECIFIED; |
| 116 | aaudio_format_t mFormat = AAUDIO_FORMAT_UNSPECIFIED; |
| 117 | int32_t mSampleRate = AAUDIO_UNSPECIFIED; |
| 118 | |
| 119 | int32_t mBufferCapacity = AAUDIO_UNSPECIFIED; |
| 120 | int32_t mDeviceId = AAUDIO_UNSPECIFIED; |
| 121 | aaudio_sharing_mode_t mSharingMode = AAUDIO_SHARING_MODE_SHARED; |
| 122 | aaudio_performance_mode_t mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; |
| 123 | |
| 124 | int32_t mNumberOfBursts = AAUDIO_UNSPECIFIED; |
| 125 | }; |
| 126 | |
| 127 | class AAudioArgsParser : public AAudioParameters { |
| 128 | public: |
| 129 | AAudioArgsParser() = default; |
| 130 | ~AAudioArgsParser() = default; |
| 131 | |
| 132 | enum { |
| 133 | DEFAULT_DURATION_SECONDS = 5 |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * @param arg |
| 138 | * @return true if the argument was not handled |
| 139 | */ |
| 140 | bool parseArg(const char *arg) { |
| 141 | bool unrecognized = false; |
| 142 | if (arg[0] == '-') { |
| 143 | char option = arg[1]; |
| 144 | switch (option) { |
| 145 | case 'b': |
| 146 | setBufferCapacity(atoi(&arg[2])); |
| 147 | break; |
| 148 | case 'c': |
| 149 | setChannelCount(atoi(&arg[2])); |
| 150 | break; |
| 151 | case 'd': |
| 152 | mDurationSeconds = atoi(&arg[2]); |
| 153 | break; |
| 154 | case 'm': |
| 155 | AAudio_setMMapPolicy(AAUDIO_POLICY_AUTO); |
| 156 | break; |
| 157 | case 'n': |
| 158 | setNumberOfBursts(atoi(&arg[2])); |
| 159 | break; |
| 160 | case 'p': |
| 161 | setPerformanceMode(parsePerformanceMode(arg[2])); |
| 162 | break; |
| 163 | case 'r': |
| 164 | setSampleRate(atoi(&arg[2])); |
| 165 | break; |
| 166 | case 'x': |
| 167 | setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 168 | break; |
| 169 | default: |
| 170 | unrecognized = true; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | return unrecognized; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * |
| 179 | * @param argc |
| 180 | * @param argv |
| 181 | * @return true if an unrecognized argument was passed |
| 182 | */ |
| 183 | bool parseArgs(int argc, const char **argv) { |
| 184 | for (int i = 1; i < argc; i++) { |
| 185 | const char *arg = argv[i]; |
| 186 | if (parseArg(arg)) { |
| 187 | usage(); |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | static void usage() { |
| 196 | printf("-c{channels} -d{duration} -m -n{burstsPerBuffer} -p{perfMode} -r{rate} -x\n"); |
| 197 | printf(" Default values are UNSPECIFIED unless otherwise stated.\n"); |
| 198 | printf(" -b{bufferCapacity} frames\n"); |
| 199 | printf(" -c{channels} for example 2 for stereo\n"); |
| 200 | printf(" -d{duration} in seconds, default is %d\n", DEFAULT_DURATION_SECONDS); |
| 201 | printf(" -m enable MMAP\n"); |
| 202 | printf(" -n{numberOfBursts} for setBufferSize\n"); |
| 203 | printf(" -p{performanceMode} set output AAUDIO_PERFORMANCE_MODE*, default NONE\n"); |
| 204 | printf(" n for _NONE\n"); |
| 205 | printf(" l for _LATENCY\n"); |
| 206 | printf(" p for _POWER_SAVING;\n"); |
| 207 | printf(" -r{sampleRate} for example 44100\n"); |
| 208 | printf(" -x to use EXCLUSIVE mode\n"); |
| 209 | } |
| 210 | |
| 211 | static aaudio_performance_mode_t parsePerformanceMode(char c) { |
| 212 | aaudio_performance_mode_t mode = AAUDIO_PERFORMANCE_MODE_NONE; |
| 213 | switch (c) { |
| 214 | case 'n': |
| 215 | mode = AAUDIO_PERFORMANCE_MODE_NONE; |
| 216 | break; |
| 217 | case 'l': |
| 218 | mode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; |
| 219 | break; |
| 220 | case 'p': |
| 221 | mode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; |
| 222 | break; |
| 223 | default: |
| 224 | printf("ERROR invalid performance mode %c\n", c); |
| 225 | break; |
| 226 | } |
| 227 | return mode; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Print stream parameters in comparison with requested values. |
| 232 | * @param stream |
| 233 | */ |
| 234 | void compareWithStream(AAudioStream *stream) { |
| 235 | |
| 236 | printf(" DeviceId: requested = %d, actual = %d\n", |
| 237 | getDeviceId(), AAudioStream_getDeviceId(stream)); |
| 238 | |
| 239 | aaudio_stream_state_t state = AAudioStream_getState(stream); |
| 240 | printf(" State: %s\n", AAudio_convertStreamStateToText(state)); |
| 241 | |
| 242 | // Check to see what kind of stream we actually got. |
| 243 | printf(" SampleRate: requested = %d, actual = %d\n", |
| 244 | getSampleRate(), AAudioStream_getSampleRate(stream)); |
| 245 | |
| 246 | printf(" ChannelCount: requested = %d, actual = %d\n", |
| 247 | getChannelCount(), AAudioStream_getChannelCount(stream)); |
| 248 | |
| 249 | printf(" DataFormat: requested = %d, actual = %d\n", |
| 250 | getFormat(), AAudioStream_getFormat(stream)); |
| 251 | |
| 252 | int32_t framesPerBurst = AAudioStream_getFramesPerBurst(stream); |
| 253 | int32_t sizeFrames = AAudioStream_getBufferSizeInFrames(stream); |
| 254 | printf(" Buffer: burst = %d\n", framesPerBurst); |
| 255 | if (framesPerBurst > 0) { |
| 256 | printf(" Buffer: size = %d = (%d * %d) + %d\n", |
| 257 | sizeFrames, |
| 258 | (sizeFrames / framesPerBurst), |
| 259 | framesPerBurst, |
| 260 | (sizeFrames % framesPerBurst)); |
| 261 | } |
| 262 | printf(" Capacity: requested = %d, actual = %d\n", getBufferCapacity(), |
| 263 | AAudioStream_getBufferCapacityInFrames(stream)); |
| 264 | |
| 265 | printf(" SharingMode: requested = %s, actual = %s\n", |
| 266 | getSharingModeText(getSharingMode()), |
| 267 | getSharingModeText(AAudioStream_getSharingMode(stream))); |
| 268 | |
| 269 | printf(" PerformanceMode: requested = %d, actual = %d\n", |
| 270 | getPerformanceMode(), AAudioStream_getPerformanceMode(stream)); |
| 271 | printf(" Is MMAP used? %s\n", AAudioStream_isMMapUsed(stream) |
| 272 | ? "yes" : "no"); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | int32_t getDurationSeconds() const { |
| 277 | return mDurationSeconds; |
| 278 | } |
| 279 | |
| 280 | void setDurationSeconds(int32_t seconds) { |
| 281 | mDurationSeconds = seconds; |
| 282 | } |
| 283 | |
| 284 | private: |
| 285 | int32_t mDurationSeconds = DEFAULT_DURATION_SECONDS; |
| 286 | }; |
| 287 | |
| 288 | #endif // AAUDIO_EXAMPLE_ARGS_PARSER_H |