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 "FullDuplexAnalyzer.h"
19 
start()20 oboe::Result  FullDuplexAnalyzer::start() {
21     getLoopbackProcessor()->setSampleRate(getOutputStream()->getSampleRate());
22     getLoopbackProcessor()->prepareToTest();
23     return FullDuplexStream::start();
24 }
25 
onBothStreamsReady(const void * inputData,int numInputFrames,void * outputData,int numOutputFrames)26 oboe::DataCallbackResult FullDuplexAnalyzer::onBothStreamsReady(
27         const void *inputData,
28         int   numInputFrames,
29         void *outputData,
30         int   numOutputFrames) {
31 
32     int32_t inputStride = getInputStream()->getChannelCount();
33     int32_t outputStride = getOutputStream()->getChannelCount();
34     float *inputFloat = (float *) inputData;
35     float *outputFloat = (float *) outputData;
36 
37     (void) getLoopbackProcessor()->process(inputFloat, inputStride, numInputFrames,
38                                    outputFloat, outputStride, numOutputFrames);
39 
40     // write the first channel of output and input to the stereo recorder
41     if (mRecording != nullptr) {
42         float buffer[2];
43         int numBoth = std::min(numInputFrames, numOutputFrames);
44         for (int i = 0; i < numBoth; i++) {
45             buffer[0] = *outputFloat;
46             outputFloat += outputStride;
47             buffer[1] = *inputFloat;
48             inputFloat += inputStride;
49             mRecording->write(buffer, 1);
50         }
51         // Handle mismatch in in numFrames.
52         buffer[0] = 0.0f; // gap in output
53         for (int i = numBoth; i < numInputFrames; i++) {
54             buffer[1] = *inputFloat;
55             inputFloat += inputStride;
56             mRecording->write(buffer, 1);
57         }
58         buffer[1] = 0.0f; // gap in input
59         for (int i = numBoth; i < numOutputFrames; i++) {
60             buffer[0] = *outputFloat;
61             outputFloat += outputStride;
62             mRecording->write(buffer, 1);
63         }
64     }
65     return oboe::DataCallbackResult::Continue;
66 };
67