blob: eeea0851232e2521bd266b3c9d8b30abf49947f6 [file] [log] [blame]
Phil Burk7386a832019-03-21 16:07:27 -07001/*
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
20oboe::Result FullDuplexAnalyzer::start() {
Phil Burke87667d2019-04-30 14:01:42 -070021 getLoopbackProcessor()->setSampleRate(getOutputStream()->getSampleRate());
Phil Burkbbde5bb2019-09-02 16:00:18 -070022 getLoopbackProcessor()->onStartTest();
Phil Burk7386a832019-03-21 16:07:27 -070023 return FullDuplexStream::start();
24}
25
26oboe::DataCallbackResult FullDuplexAnalyzer::onBothStreamsReady(
27 const void *inputData,
28 int numInputFrames,
29 void *outputData,
30 int numOutputFrames) {
Phil Burk81ca4742019-04-24 07:53:36 -070031
32 int32_t inputStride = getInputStream()->getChannelCount();
33 int32_t outputStride = getOutputStream()->getChannelCount();
Phil Burka556a322019-10-24 15:43:18 -070034 float *inputFloat = (float *) inputData;
35 float *outputFloat = (float *) outputData;
Phil Burk81ca4742019-04-24 07:53:36 -070036
Phil Burka556a322019-10-24 15:43:18 -070037 (void) getLoopbackProcessor()->process(inputFloat, inputStride, numInputFrames,
38 outputFloat, outputStride, numOutputFrames);
Phil Burk7386a832019-03-21 16:07:27 -070039
Phil Burka556a322019-10-24 15:43:18 -070040 // write the first channel of output and input to the stereo recorder
Phil Burk81ca4742019-04-24 07:53:36 -070041 if (mRecording != nullptr) {
42 float buffer[2];
Phil Burka556a322019-10-24 15:43:18 -070043 int numBoth = std::min(numInputFrames, numOutputFrames);
44 for (int i = 0; i < numBoth; i++) {
Phil Burk81ca4742019-04-24 07:53:36 -070045 buffer[0] = *outputFloat;
46 outputFloat += outputStride;
Phil Burka556a322019-10-24 15:43:18 -070047 buffer[1] = *inputFloat;
48 inputFloat += inputStride;
49 mRecording->write(buffer, 1);
50 }
51 for (int i = numBoth; i < numInputFrames; i++) {
52 buffer[0] = 0.0f; // gap
53 buffer[1] = *inputFloat;
54 inputFloat += inputStride;
55 mRecording->write(buffer, 1);
56 }
57 for (int i = numBoth; i < numOutputFrames; i++) {
58 buffer[0] = *outputFloat;
59 outputFloat += outputStride;
60 buffer[1] = 0.0f; // gap
Phil Burk81ca4742019-04-24 07:53:36 -070061 mRecording->write(buffer, 1);
62 }
63 }
Phil Burk7386a832019-03-21 16:07:27 -070064 return oboe::DataCallbackResult::Continue;
65};