blob: 156c7beabae731761d5859c2ebc52bce739281a5 [file] [log] [blame]
Eric Laurent629afae2017-05-25 18:25:51 -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_UTILS_H
18#define AAUDIO_EXAMPLE_UTILS_H
19
Dan Albertcb2e7fb2017-10-11 12:43:41 -070020#include <errno.h>
Eric Laurent629afae2017-05-25 18:25:51 -070021#include <sched.h>
Dan Albertcb2e7fb2017-10-11 12:43:41 -070022#include <string.h>
23#include <unistd.h>
24
Eric Laurent629afae2017-05-25 18:25:51 -070025#include <aaudio/AAudio.h>
26
27#define NANOS_PER_MICROSECOND ((int64_t)1000)
28#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
29#define NANOS_PER_SECOND (NANOS_PER_MILLISECOND * 1000)
30
Chih-Hung Hsiehabf85fc2017-11-09 10:18:04 -080031template <class T = aaudio_sharing_mode_t>
32const char *getSharingModeText(aaudio_sharing_mode_t mode) {
Eric Laurent629afae2017-05-25 18:25:51 -070033 const char *modeText = "unknown";
34 switch (mode) {
35 case AAUDIO_SHARING_MODE_EXCLUSIVE:
36 modeText = "EXCLUSIVE";
37 break;
38 case AAUDIO_SHARING_MODE_SHARED:
39 modeText = "SHARED";
40 break;
41 default:
42 break;
43 }
44 return modeText;
45}
46
Chih-Hung Hsiehabf85fc2017-11-09 10:18:04 -080047template <class T = clockid_t>
48int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
Eric Laurent629afae2017-05-25 18:25:51 -070049 struct timespec time;
50 int result = clock_gettime(clockId, &time);
51 if (result < 0) {
52 return -errno;
53 }
54 return (time.tv_sec * NANOS_PER_SECOND) + time.tv_nsec;
55}
56
57void displayPeakLevel(float peakLevel) {
58 printf("%5.3f ", peakLevel);
59 const int maxStars = 50; // arbitrary, fits on one line
60 int numStars = (int) (peakLevel * maxStars);
61 for (int i = 0; i < numStars; i++) {
62 printf("*");
63 }
64 printf("\n");
65}
66
67#endif // AAUDIO_EXAMPLE_UTILS_H