Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 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 | |
| 20 | oboe::Result FullDuplexAnalyzer::start() { |
Phil Burk | e87667d | 2019-04-30 14:01:42 -0700 | [diff] [blame] | 21 | getLoopbackProcessor()->setSampleRate(getOutputStream()->getSampleRate()); |
Phil Burk | bbde5bb | 2019-09-02 16:00:18 -0700 | [diff] [blame] | 22 | getLoopbackProcessor()->onStartTest(); |
Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 23 | return FullDuplexStream::start(); |
| 24 | } |
| 25 | |
| 26 | oboe::DataCallbackResult FullDuplexAnalyzer::onBothStreamsReady( |
| 27 | const void *inputData, |
| 28 | int numInputFrames, |
| 29 | void *outputData, |
| 30 | int numOutputFrames) { |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 31 | |
| 32 | int32_t inputStride = getInputStream()->getChannelCount(); |
| 33 | int32_t outputStride = getOutputStream()->getChannelCount(); |
Phil Burk | a556a32 | 2019-10-24 15:43:18 -0700 | [diff] [blame^] | 34 | float *inputFloat = (float *) inputData; |
| 35 | float *outputFloat = (float *) outputData; |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 36 | |
Phil Burk | a556a32 | 2019-10-24 15:43:18 -0700 | [diff] [blame^] | 37 | (void) getLoopbackProcessor()->process(inputFloat, inputStride, numInputFrames, |
| 38 | outputFloat, outputStride, numOutputFrames); |
Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 39 | |
Phil Burk | a556a32 | 2019-10-24 15:43:18 -0700 | [diff] [blame^] | 40 | // write the first channel of output and input to the stereo recorder |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 41 | if (mRecording != nullptr) { |
| 42 | float buffer[2]; |
Phil Burk | a556a32 | 2019-10-24 15:43:18 -0700 | [diff] [blame^] | 43 | int numBoth = std::min(numInputFrames, numOutputFrames); |
| 44 | for (int i = 0; i < numBoth; i++) { |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 45 | buffer[0] = *outputFloat; |
| 46 | outputFloat += outputStride; |
Phil Burk | a556a32 | 2019-10-24 15:43:18 -0700 | [diff] [blame^] | 47 | 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 Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 61 | mRecording->write(buffer, 1); |
| 62 | } |
| 63 | } |
Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 64 | return oboe::DataCallbackResult::Continue; |
| 65 | }; |