blob: 46bc99ed98e3d975ae2cae8063808c5a3612fb92 [file] [log] [blame]
Phil Burk44795232017-06-30 16:27:38 -07001/*
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
30class AAudioParameters {
31public:
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
114private:
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
Phil Burkfcf9efd2017-07-14 08:25:08 -0700124 int32_t mNumberOfBursts = AAUDIO_UNSPECIFIED;
Phil Burk44795232017-06-30 16:27:38 -0700125};
126
127class AAudioArgsParser : public AAudioParameters {
128public:
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;
Phil Burkfcf9efd2017-07-14 08:25:08 -0700154 case 'm': {
155 aaudio_policy_t policy = AAUDIO_POLICY_AUTO;
156 if (strlen(arg) > 2) {
157 policy = atoi(&arg[2]);
158 }
159 AAudio_setMMapPolicy(policy);
160 } break;
Phil Burk44795232017-06-30 16:27:38 -0700161 case 'n':
162 setNumberOfBursts(atoi(&arg[2]));
163 break;
164 case 'p':
165 setPerformanceMode(parsePerformanceMode(arg[2]));
166 break;
167 case 'r':
168 setSampleRate(atoi(&arg[2]));
169 break;
170 case 'x':
171 setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
172 break;
173 default:
174 unrecognized = true;
175 break;
176 }
177 }
178 return unrecognized;
179 }
180
181 /**
182 *
183 * @param argc
184 * @param argv
185 * @return true if an unrecognized argument was passed
186 */
187 bool parseArgs(int argc, const char **argv) {
188 for (int i = 1; i < argc; i++) {
189 const char *arg = argv[i];
190 if (parseArg(arg)) {
191 usage();
192 return true;
193 }
194
195 }
196 return false;
197 }
198
199 static void usage() {
200 printf("-c{channels} -d{duration} -m -n{burstsPerBuffer} -p{perfMode} -r{rate} -x\n");
201 printf(" Default values are UNSPECIFIED unless otherwise stated.\n");
202 printf(" -b{bufferCapacity} frames\n");
203 printf(" -c{channels} for example 2 for stereo\n");
204 printf(" -d{duration} in seconds, default is %d\n", DEFAULT_DURATION_SECONDS);
Phil Burkfcf9efd2017-07-14 08:25:08 -0700205 printf(" -m{0|1|2|3} set MMAP policy\n");
206 printf(" 0 = _UNSPECIFIED, default\n");
207 printf(" 1 = _NEVER\n");
208 printf(" 2 = _AUTO, also if -m is used with no number\n");
209 printf(" 3 = _ALWAYS\n");
Phil Burk44795232017-06-30 16:27:38 -0700210 printf(" -n{numberOfBursts} for setBufferSize\n");
211 printf(" -p{performanceMode} set output AAUDIO_PERFORMANCE_MODE*, default NONE\n");
212 printf(" n for _NONE\n");
213 printf(" l for _LATENCY\n");
214 printf(" p for _POWER_SAVING;\n");
215 printf(" -r{sampleRate} for example 44100\n");
216 printf(" -x to use EXCLUSIVE mode\n");
217 }
218
219 static aaudio_performance_mode_t parsePerformanceMode(char c) {
220 aaudio_performance_mode_t mode = AAUDIO_PERFORMANCE_MODE_NONE;
221 switch (c) {
222 case 'n':
223 mode = AAUDIO_PERFORMANCE_MODE_NONE;
224 break;
225 case 'l':
226 mode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY;
227 break;
228 case 'p':
229 mode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING;
230 break;
231 default:
232 printf("ERROR invalid performance mode %c\n", c);
233 break;
234 }
235 return mode;
236 }
237
238 /**
239 * Print stream parameters in comparison with requested values.
240 * @param stream
241 */
242 void compareWithStream(AAudioStream *stream) {
243
244 printf(" DeviceId: requested = %d, actual = %d\n",
245 getDeviceId(), AAudioStream_getDeviceId(stream));
246
247 aaudio_stream_state_t state = AAudioStream_getState(stream);
248 printf(" State: %s\n", AAudio_convertStreamStateToText(state));
249
250 // Check to see what kind of stream we actually got.
251 printf(" SampleRate: requested = %d, actual = %d\n",
252 getSampleRate(), AAudioStream_getSampleRate(stream));
253
254 printf(" ChannelCount: requested = %d, actual = %d\n",
255 getChannelCount(), AAudioStream_getChannelCount(stream));
256
257 printf(" DataFormat: requested = %d, actual = %d\n",
258 getFormat(), AAudioStream_getFormat(stream));
259
260 int32_t framesPerBurst = AAudioStream_getFramesPerBurst(stream);
261 int32_t sizeFrames = AAudioStream_getBufferSizeInFrames(stream);
262 printf(" Buffer: burst = %d\n", framesPerBurst);
263 if (framesPerBurst > 0) {
264 printf(" Buffer: size = %d = (%d * %d) + %d\n",
265 sizeFrames,
266 (sizeFrames / framesPerBurst),
267 framesPerBurst,
268 (sizeFrames % framesPerBurst));
269 }
270 printf(" Capacity: requested = %d, actual = %d\n", getBufferCapacity(),
271 AAudioStream_getBufferCapacityInFrames(stream));
272
273 printf(" SharingMode: requested = %s, actual = %s\n",
274 getSharingModeText(getSharingMode()),
275 getSharingModeText(AAudioStream_getSharingMode(stream)));
276
277 printf(" PerformanceMode: requested = %d, actual = %d\n",
278 getPerformanceMode(), AAudioStream_getPerformanceMode(stream));
279 printf(" Is MMAP used? %s\n", AAudioStream_isMMapUsed(stream)
280 ? "yes" : "no");
281
282 }
283
284 int32_t getDurationSeconds() const {
285 return mDurationSeconds;
286 }
287
288 void setDurationSeconds(int32_t seconds) {
289 mDurationSeconds = seconds;
290 }
291
292private:
293 int32_t mDurationSeconds = DEFAULT_DURATION_SECONDS;
294};
295
296#endif // AAUDIO_EXAMPLE_ARGS_PARSER_H