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 mWriteReadDeltaValid = false;
24 return FullDuplexStreamWithConversion::start();
25 }
26
onBothStreamsReadyFloat(const float * inputData,int numInputFrames,float * outputData,int numOutputFrames)27 oboe::DataCallbackResult FullDuplexAnalyzer::onBothStreamsReadyFloat(
28 const float *inputData,
29 int numInputFrames,
30 float *outputData,
31 int numOutputFrames) {
32
33 int32_t inputStride = getInputStream()->getChannelCount();
34 int32_t outputStride = getOutputStream()->getChannelCount();
35 auto *inputFloat = static_cast<const float *>(inputData);
36 float *outputFloat = outputData;
37
38 // Get atomic snapshot of the relative frame positions so they
39 // can be used to calculate timestamp latency.
40 int64_t framesRead = getInputStream()->getFramesRead();
41 int64_t framesWritten = getOutputStream()->getFramesWritten();
42 mWriteReadDelta = framesWritten - framesRead;
43 mWriteReadDeltaValid = true;
44
45 (void) getLoopbackProcessor()->process(inputFloat, inputStride, numInputFrames,
46 outputFloat, outputStride, numOutputFrames);
47
48 // write the first channel of output and input to the stereo recorder
49 if (mRecording != nullptr) {
50 float buffer[2];
51 int numBoth = std::min(numInputFrames, numOutputFrames);
52 for (int i = 0; i < numBoth; i++) {
53 buffer[0] = *outputFloat;
54 outputFloat += outputStride;
55 buffer[1] = *inputFloat;
56 inputFloat += inputStride;
57 mRecording->write(buffer, 1);
58 }
59 // Handle mismatch in numFrames.
60 buffer[0] = 0.0f; // gap in output
61 for (int i = numBoth; i < numInputFrames; i++) {
62 buffer[1] = *inputFloat;
63 inputFloat += inputStride;
64 mRecording->write(buffer, 1);
65 }
66 buffer[1] = 0.0f; // gap in input
67 for (int i = numBoth; i < numOutputFrames; i++) {
68 buffer[0] = *outputFloat;
69 outputFloat += outputStride;
70 mRecording->write(buffer, 1);
71 }
72 }
73 return oboe::DataCallbackResult::Continue;
74 };
75