blob: 0da2d0029976f6558bba40d562f2a01aa56ce819 [file] [log] [blame]
Phil Burk7e601f32019-03-08 16:04:43 -08001/*
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
20oboe::Result FullDuplexEcho::start() {
Phil Burk071bf2d2019-03-13 19:42:33 -070021 int32_t delayFrames = (int32_t) (kMaxDelayTimeSeconds * getOutputStream()->getSampleRate());
Phil Burk1d17a602019-03-12 15:37:56 -070022 mDelayLine = std::make_unique<InterpolatingDelayLine>(delayFrames);
Phil Burk7e601f32019-03-08 16:04:43 -080023 return FullDuplexStream::start();
24}
25
26oboe::DataCallbackResult FullDuplexEcho::onBothStreamsReady(
27 const void *inputData,
28 int numInputFrames,
29 void *outputData,
30 int numOutputFrames) {
31 // FIXME only handles matching stream formats.
Phil Burkd7151632019-03-09 09:37:24 -080032 // 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 Burkb69847c2019-08-23 17:33:28 -070037 // zero out entire output array
38 memset(outputFloat, 0, numOutputFrames * getOutputStream()->getBytesPerFrame());
39
Phil Burkd7151632019-03-09 09:37:24 -080040 int32_t inputStride = getInputStream()->getChannelCount();
41 int32_t outputStride = getOutputStream()->getChannelCount();
Phil Burk1d17a602019-03-12 15:37:56 -070042 float delayFrames = mDelayTimeSeconds * getOutputStream()->getSampleRate();
Phil Burkb69847c2019-08-23 17:33:28 -070043 while (framesToEcho-- > 0) {
44 *outputFloat = mDelayLine->process(delayFrames, *inputFloat); // mono delay
45 inputFloat += inputStride;
46 outputFloat += outputStride;
Phil Burk7e601f32019-03-08 16:04:43 -080047 }
48 return oboe::DataCallbackResult::Continue;
49};