xref: /aosp_15_r20/external/oboe/apps/OboeTester/app/src/main/cpp/FullDuplexEcho.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2019 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 #include "common/OboeDebug.h"
18 #include "FullDuplexEcho.h"
19 
start()20 oboe::Result  FullDuplexEcho::start() {
21     int32_t delayFrames = (int32_t) (kMaxDelayTimeSeconds * getOutputStream()->getSampleRate());
22     mDelayLine = std::make_unique<InterpolatingDelayLine>(delayFrames);
23     // Use peak detector for input streams
24     mNumChannels = getInputStream()->getChannelCount();
25     mPeakDetectors = std::make_unique<PeakDetector[]>(mNumChannels);
26     return FullDuplexStreamWithConversion::start();
27 }
28 
getPeakLevel(int index)29 double FullDuplexEcho::getPeakLevel(int index) {
30     if (mPeakDetectors == nullptr) {
31         LOGE("%s() called before setup()", __func__);
32         return -1.0;
33     } else if (index < 0 || index >= mNumChannels) {
34         LOGE("%s(), index out of range, 0 <= %d < %d", __func__, index, mNumChannels.load());
35         return -2.0;
36     }
37     return mPeakDetectors[index].getLevel();
38 }
39 
onBothStreamsReadyFloat(const float * inputData,int numInputFrames,float * outputData,int numOutputFrames)40 oboe::DataCallbackResult FullDuplexEcho::onBothStreamsReadyFloat(
41         const float *inputData,
42         int   numInputFrames,
43         float *outputData,
44         int   numOutputFrames) {
45     int32_t framesToEcho = std::min(numInputFrames, numOutputFrames);
46     auto *inputFloat = const_cast<float *>(inputData);
47     float *outputFloat = outputData;
48     // zero out entire output array
49     memset(outputFloat, 0, static_cast<size_t>(numOutputFrames)
50             * static_cast<size_t>(getOutputStream()->getBytesPerFrame()));
51 
52     int32_t inputStride = getInputStream()->getChannelCount();
53     int32_t outputStride = getOutputStream()->getChannelCount();
54     float delayFrames = mDelayTimeSeconds * getOutputStream()->getSampleRate();
55     while (framesToEcho-- > 0) {
56         *outputFloat = mDelayLine->process(delayFrames, *inputFloat); // mono delay
57 
58         for (int iChannel = 0; iChannel < inputStride; iChannel++) {
59             float sample = * (inputFloat + iChannel);
60             mPeakDetectors[iChannel].process(sample);
61         }
62 
63         inputFloat += inputStride;
64         outputFloat += outputStride;
65     }
66     return oboe::DataCallbackResult::Continue;
67 };
68