Phil Burk | 7e601f3 | 2019-03-08 16:04:43 -0800 | [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 "FullDuplexEcho.h" |
| 19 | |
| 20 | oboe::Result FullDuplexEcho::start() { |
Phil Burk | 071bf2d | 2019-03-13 19:42:33 -0700 | [diff] [blame] | 21 | int32_t delayFrames = (int32_t) (kMaxDelayTimeSeconds * getOutputStream()->getSampleRate()); |
Phil Burk | 1d17a60 | 2019-03-12 15:37:56 -0700 | [diff] [blame] | 22 | mDelayLine = std::make_unique<InterpolatingDelayLine>(delayFrames); |
Phil Burk | 7e601f3 | 2019-03-08 16:04:43 -0800 | [diff] [blame] | 23 | return FullDuplexStream::start(); |
| 24 | } |
| 25 | |
| 26 | oboe::DataCallbackResult FullDuplexEcho::onBothStreamsReady( |
| 27 | const void *inputData, |
| 28 | int numInputFrames, |
| 29 | void *outputData, |
| 30 | int numOutputFrames) { |
| 31 | // FIXME only handles matching stream formats. |
Phil Burk | d715163 | 2019-03-09 09:37:24 -0800 | [diff] [blame] | 32 | // TODO Add delay node |
| 33 | // TODO use flowgraph to handle format conversion |
| 34 | int32_t framesToEcho = std::min(numInputFrames, numOutputFrames); |
| 35 | float *inputFloat = (float *)inputData; |
| 36 | float *outputFloat = (float *)outputData; |
Phil Burk | b69847c | 2019-08-23 17:33:28 -0700 | [diff] [blame] | 37 | // zero out entire output array |
| 38 | memset(outputFloat, 0, numOutputFrames * getOutputStream()->getBytesPerFrame()); |
| 39 | |
Phil Burk | d715163 | 2019-03-09 09:37:24 -0800 | [diff] [blame] | 40 | int32_t inputStride = getInputStream()->getChannelCount(); |
| 41 | int32_t outputStride = getOutputStream()->getChannelCount(); |
Phil Burk | 1d17a60 | 2019-03-12 15:37:56 -0700 | [diff] [blame] | 42 | float delayFrames = mDelayTimeSeconds * getOutputStream()->getSampleRate(); |
Phil Burk | b69847c | 2019-08-23 17:33:28 -0700 | [diff] [blame] | 43 | while (framesToEcho-- > 0) { |
| 44 | *outputFloat = mDelayLine->process(delayFrames, *inputFloat); // mono delay |
| 45 | inputFloat += inputStride; |
| 46 | outputFloat += outputStride; |
Phil Burk | 7e601f3 | 2019-03-08 16:04:43 -0800 | [diff] [blame] | 47 | } |
| 48 | return oboe::DataCallbackResult::Continue; |
| 49 | }; |