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