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