Oboe 1.5 A library for creating real-time audio apps on Android

StabilizedCallback.h
1 /*
2 * Copyright 2018 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_STABILIZEDCALLBACK_H
18 #define OBOE_STABILIZEDCALLBACK_H
19
20 #include <cstdint>
21 #include "oboe/AudioStream.h"
22
23 namespace oboe {
24
25 class StabilizedCallback : public AudioStreamCallback {
26
27 public:
28 explicit StabilizedCallback(AudioStreamCallback *callback);
29
30 DataCallbackResult
31 onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override;
32
33 void onErrorBeforeClose(AudioStream *oboeStream, Result error) override {
34 return mCallback->onErrorBeforeClose(oboeStream, error);
35 }
36
37 void onErrorAfterClose(AudioStream *oboeStream, Result error) override {
38
39 // Reset all fields now that the stream has been closed
40 mFrameCount = 0;
41 mEpochTimeNanos = 0;
42 mOpsPerNano = 1;
43 return mCallback->onErrorAfterClose(oboeStream, error);
44 }
45
46 private:
47
48 AudioStreamCallback *mCallback = nullptr;
49 int64_t mFrameCount = 0;
50 int64_t mEpochTimeNanos = 0;
51 double mOpsPerNano = 1;
52
53 void generateLoad(int64_t durationNanos);
54 };
55
60 #if defined(__i386__) || defined(__x86_64__)
61 #define cpu_relax() asm volatile("rep; nop" ::: "memory");
62
63 #elif defined(__arm__) || defined(__mips__)
64 #define cpu_relax() asm volatile("":::"memory")
65
66 #elif defined(__aarch64__)
67 #define cpu_relax() asm volatile("yield" ::: "memory")
68
69 #else
70 #error "cpu_relax is not defined for this architecture"
71 #endif
72
73 }
74
75 #endif //OBOE_STABILIZEDCALLBACK_H
oboe::StabilizedCallback::onAudioReady
DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override
oboe::AudioStreamCallback
Definition: AudioStreamCallback.h:181
oboe::DataCallbackResult
DataCallbackResult
Definition: Definitions.h:119
oboe::AudioStreamErrorCallback::onErrorAfterClose
virtual void onErrorAfterClose(AudioStream *, Result)
Definition: AudioStreamCallback.h:163
oboe::AudioStream
Definition: AudioStream.h:44
oboe::Result
Result
Definition: Definitions.h:131
oboe::StabilizedCallback
Definition: StabilizedCallback.h:25
oboe
Definition: AudioStream.h:31
oboe::StabilizedCallback::onErrorAfterClose
void onErrorAfterClose(AudioStream *oboeStream, Result error) override
Definition: StabilizedCallback.h:37
oboe::StabilizedCallback::onErrorBeforeClose
void onErrorBeforeClose(AudioStream *oboeStream, Result error) override
Definition: StabilizedCallback.h:33
oboe::AudioStreamErrorCallback::onErrorBeforeClose
virtual void onErrorBeforeClose(AudioStream *, Result)
Definition: AudioStreamCallback.h:147