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 // Save data for later analysis or for writing to a WAVE file.
49 if (mRecording != nullptr) {
50 float buffer[2];
51 int numBoth = std::min(numInputFrames, numOutputFrames);
52 // Offset to the selected channels that we are analyzing.
53 inputFloat += getLoopbackProcessor()->getInputChannel();
54 outputFloat += getLoopbackProcessor()->getOutputChannel();
55 for (int i = 0; i < numBoth; i++) {
56 buffer[0] = *outputFloat;
57 outputFloat += outputStride;
58 buffer[1] = *inputFloat;
59 inputFloat += inputStride;
60 mRecording->write(buffer, 1);
61 }
62 // Handle mismatch in numFrames.
63 const float gapMarker = -0.9f; // Recognizable value so we can tell underruns from DSP gaps.
64 buffer[0] = gapMarker; // gap in output
65 for (int i = numBoth; i < numInputFrames; i++) {
66 buffer[1] = *inputFloat;
67 inputFloat += inputStride;
68 mRecording->write(buffer, 1);
69 }
70 buffer[1] = gapMarker; // gap in input
71 for (int i = numBoth; i < numOutputFrames; i++) {
72 buffer[0] = *outputFloat;
73 outputFloat += outputStride;
74 mRecording->write(buffer, 1);
75 }
76 }
77 return oboe::DataCallbackResult::Continue;
78 };
79