blob: 530188b05aa69f034fc9cda0e4467aeb23805622 [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
31static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
32 const char *modeText = "unknown";
33 switch (mode) {
34 case AAUDIO_SHARING_MODE_EXCLUSIVE:
35 modeText = "EXCLUSIVE";
36 break;
37 case AAUDIO_SHARING_MODE_SHARED:
38 modeText = "SHARED";
39 break;
40 default:
41 break;
42 }
43 return modeText;
44}
45
46static int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
47 struct timespec time;
48 int result = clock_gettime(clockId, &time);
49 if (result < 0) {
50 return -errno;
51 }
52 return (time.tv_sec * NANOS_PER_SECOND) + time.tv_nsec;
53}
54
55void displayPeakLevel(float peakLevel) {
56 printf("%5.3f ", peakLevel);
57 const int maxStars = 50; // arbitrary, fits on one line
58 int numStars = (int) (peakLevel * maxStars);
59 for (int i = 0; i < numStars; i++) {
60 printf("*");
61 }
62 printf("\n");
63}
64
65#endif // AAUDIO_EXAMPLE_UTILS_H