1 /*
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 
20 #include <unistd.h>
21 #include <sched.h>
22 #include <aaudio/AAudio.h>
23 
24 #define NANOS_PER_MICROSECOND ((int64_t)1000)
25 #define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
26 #define NANOS_PER_SECOND      (NANOS_PER_MILLISECOND * 1000)
27 
getSharingModeText(aaudio_sharing_mode_t mode)28 static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
29     const char *modeText = "unknown";
30     switch (mode) {
31     case AAUDIO_SHARING_MODE_EXCLUSIVE:
32         modeText = "EXCLUSIVE";
33         break;
34     case AAUDIO_SHARING_MODE_SHARED:
35         modeText = "SHARED";
36         break;
37     default:
38         break;
39     }
40     return modeText;
41 }
42 
43 static int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
44     struct timespec time;
45     int result = clock_gettime(clockId, &time);
46     if (result < 0) {
47         return -errno;
48     }
49     return (time.tv_sec * NANOS_PER_SECOND) + time.tv_nsec;
50 }
51 
displayPeakLevel(float peakLevel)52 void displayPeakLevel(float peakLevel) {
53     printf("%5.3f ", peakLevel);
54     const int maxStars = 50; // arbitrary, fits on one line
55     int numStars = (int) (peakLevel * maxStars);
56     for (int i = 0; i < numStars; i++) {
57         printf("*");
58     }
59     printf("\n");
60 }
61 
62 #endif // AAUDIO_EXAMPLE_UTILS_H
63