1629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent/*
2629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * Copyright (C) 2017 The Android Open Source Project
3629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent *
4629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * you may not use this file except in compliance with the License.
6629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * You may obtain a copy of the License at
7629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent *
8629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent *
10629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * Unless required by applicable law or agreed to in writing, software
11629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * See the License for the specific language governing permissions and
14629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent * limitations under the License.
15629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent */
16629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
17629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#ifndef AAUDIO_EXAMPLE_UTILS_H
18629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#define AAUDIO_EXAMPLE_UTILS_H
19629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
20629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#include <unistd.h>
21629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#include <sched.h>
22629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#include <aaudio/AAudio.h>
23629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
24629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#define NANOS_PER_MICROSECOND ((int64_t)1000)
25629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
26629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#define NANOS_PER_SECOND      (NANOS_PER_MILLISECOND * 1000)
27629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
28629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurentstatic const char *getSharingModeText(aaudio_sharing_mode_t mode) {
29629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    const char *modeText = "unknown";
30629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    switch (mode) {
31629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    case AAUDIO_SHARING_MODE_EXCLUSIVE:
32629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        modeText = "EXCLUSIVE";
33629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        break;
34629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    case AAUDIO_SHARING_MODE_SHARED:
35629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        modeText = "SHARED";
36629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        break;
37629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    default:
38629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        break;
39629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    }
40629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    return modeText;
41629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent}
42629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
43629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurentstatic int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
44629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    struct timespec time;
45629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    int result = clock_gettime(clockId, &time);
46629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    if (result < 0) {
47629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        return -errno;
48629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    }
49629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    return (time.tv_sec * NANOS_PER_SECOND) + time.tv_nsec;
50629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent}
51629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
52629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurentvoid displayPeakLevel(float peakLevel) {
53629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    printf("%5.3f ", peakLevel);
54629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    const int maxStars = 50; // arbitrary, fits on one line
55629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    int numStars = (int) (peakLevel * maxStars);
56629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    for (int i = 0; i < numStars; i++) {
57629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent        printf("*");
58629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    }
59629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent    printf("\n");
60629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent}
61629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent
62629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#endif // AAUDIO_EXAMPLE_UTILS_H
63