xref: /aosp_15_r20/external/oboe/samples/LiveEffect/src/main/cpp/FullDuplexPass.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 SAMPLES_FULLDUPLEXPASS_H
18 #define SAMPLES_FULLDUPLEXPASS_H
19 
20 class FullDuplexPass : public oboe::FullDuplexStream {
21 public:
22     virtual oboe::DataCallbackResult
onBothStreamsReady(const void * inputData,int numInputFrames,void * outputData,int numOutputFrames)23     onBothStreamsReady(
24             const void *inputData,
25             int   numInputFrames,
26             void *outputData,
27             int   numOutputFrames) {
28         // Copy the input samples to the output with a little arbitrary gain change.
29 
30         // This code assumes the data format for both streams is Float.
31         const float *inputFloats = static_cast<const float *>(inputData);
32         float *outputFloats = static_cast<float *>(outputData);
33 
34         // It also assumes the channel count for each stream is the same.
35         int32_t samplesPerFrame = getOutputStream()->getChannelCount();
36         int32_t numInputSamples = numInputFrames * samplesPerFrame;
37         int32_t numOutputSamples = numOutputFrames * samplesPerFrame;
38 
39         // It is possible that there may be fewer input than output samples.
40         int32_t samplesToProcess = std::min(numInputSamples, numOutputSamples);
41         for (int32_t i = 0; i < samplesToProcess; i++) {
42             *outputFloats++ = *inputFloats++ * 0.95; // do some arbitrary processing
43         }
44 
45         // If there are fewer input samples then clear the rest of the buffer.
46         int32_t samplesLeft = numOutputSamples - numInputSamples;
47         for (int32_t i = 0; i < samplesLeft; i++) {
48             *outputFloats++ = 0.0; // silence
49         }
50 
51         return oboe::DataCallbackResult::Continue;
52     }
53 };
54 #endif //SAMPLES_FULLDUPLEXPASS_H
55