xref: /aosp_15_r20/external/oboe/samples/LiveEffect/src/main/cpp/LiveEffectEngine.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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_LIVEEFFECTENGINE_H
18 #define OBOE_LIVEEFFECTENGINE_H
19 
20 #include <jni.h>
21 #include <oboe/Oboe.h>
22 #include <string>
23 #include <thread>
24 #include "FullDuplexPass.h"
25 
26 class LiveEffectEngine : public oboe::AudioStreamCallback {
27 public:
28     LiveEffectEngine();
29 
30     void setRecordingDeviceId(int32_t deviceId);
31     void setPlaybackDeviceId(int32_t deviceId);
32 
33     /**
34      * @param isOn
35      * @return true if it succeeds
36      */
37     bool setEffectOn(bool isOn);
38 
39     /*
40      * oboe::AudioStreamDataCallback interface implementation
41      */
42     oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream,
43                                           void *audioData, int32_t numFrames) override;
44 
45     /*
46      * oboe::AudioStreamErrorCallback interface implementation
47      */
48     void onErrorBeforeClose(oboe::AudioStream *oboeStream, oboe::Result error) override;
49     void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override;
50 
51     bool setAudioApi(oboe::AudioApi);
52     bool isAAudioRecommended(void);
53 
54 private:
55     FullDuplexPass    mFullDuplexPass;
56     bool              mIsEffectOn = false;
57     int32_t           mRecordingDeviceId = oboe::kUnspecified;
58     int32_t           mPlaybackDeviceId = oboe::kUnspecified;
59     const oboe::AudioFormat mFormat = oboe::AudioFormat::Float; // for easier processing
60     oboe::AudioApi    mAudioApi = oboe::AudioApi::AAudio;
61     int32_t           mSampleRate = oboe::kUnspecified;
62     const int32_t     mInputChannelCount = oboe::ChannelCount::Stereo;
63     const int32_t     mOutputChannelCount = oboe::ChannelCount::Stereo;
64 
65     std::shared_ptr<oboe::AudioStream> mRecordingStream;
66     std::shared_ptr<oboe::AudioStream> mPlayStream;
67 
68     oboe::Result openStreams();
69 
70     void closeStreams();
71 
72     void closeStream(std::shared_ptr<oboe::AudioStream> &stream);
73 
74     oboe::AudioStreamBuilder *setupCommonStreamParameters(
75         oboe::AudioStreamBuilder *builder);
76     oboe::AudioStreamBuilder *setupRecordingStreamParameters(
77         oboe::AudioStreamBuilder *builder, int32_t sampleRate);
78     oboe::AudioStreamBuilder *setupPlaybackStreamParameters(
79         oboe::AudioStreamBuilder *builder);
80     void warnIfNotLowLatency(std::shared_ptr<oboe::AudioStream> &stream);
81 };
82 
83 #endif  // OBOE_LIVEEFFECTENGINE_H
84