Oboe
A library for creating real-time audio apps on Android
Loading...
Searching...
No Matches
AudioClock.h
1/*
2 * Copyright (C) 2016 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 OBOE_AUDIO_CLOCK_H
18#define OBOE_AUDIO_CLOCK_H
19
20#include <sys/types.h>
21#include <ctime>
22#include "oboe/Definitions.h"
23
24namespace oboe {
25
27public:
28 static int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC) {
29 struct timespec time;
30 int result = clock_gettime(clockId, &time);
31 if (result < 0) {
32 return result;
33 }
34 return (time.tv_sec * kNanosPerSecond) + time.tv_nsec;
35 }
36
45 static int sleepUntilNanoTime(int64_t nanoTime, clockid_t clockId = CLOCK_MONOTONIC) {
46 struct timespec time;
47 time.tv_sec = nanoTime / kNanosPerSecond;
48 time.tv_nsec = nanoTime - (time.tv_sec * kNanosPerSecond);
49 return 0 - clock_nanosleep(clockId, TIMER_ABSTIME, &time, NULL);
50 }
51
61 static int sleepForNanos(int64_t nanoseconds, clockid_t clockId = CLOCK_REALTIME) {
62 if (nanoseconds > 0) {
63 struct timespec time;
64 time.tv_sec = nanoseconds / kNanosPerSecond;
65 time.tv_nsec = nanoseconds - (time.tv_sec * kNanosPerSecond);
66 return 0 - clock_nanosleep(clockId, 0, &time, NULL);
67 }
68 return 0;
69 }
70};
71
72} // namespace oboe
73
74#endif //OBOE_AUDIO_CLOCK_H
Definition AudioClock.h:26
static int sleepUntilNanoTime(int64_t nanoTime, clockid_t clockId=CLOCK_MONOTONIC)
Definition AudioClock.h:45
static int sleepForNanos(int64_t nanoseconds, clockid_t clockId=CLOCK_REALTIME)
Definition AudioClock.h:61
Definition AudioClock.h:24
constexpr int64_t kNanosPerSecond
Definition Definitions.h:52