1 /*
2 * Copyright 2023 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 "FullDuplexStreamWithConversion.h"
19
start()20 oboe::Result FullDuplexStreamWithConversion::start() {
21 // Determine maximum size that could possibly be called.
22 int32_t bufferSize = getOutputStream()->getBufferCapacityInFrames()
23 * getOutputStream()->getChannelCount();
24 mInputConverter = std::make_unique<FormatConverterBox>(bufferSize,
25 getInputStream()->getFormat(),
26 oboe::AudioFormat::Float);
27 mOutputConverter = std::make_unique<FormatConverterBox>(bufferSize,
28 oboe::AudioFormat::Float,
29 getOutputStream()->getFormat());
30 return FullDuplexStream::start();
31 }
32
readInput(int32_t numFrames)33 oboe::ResultWithValue<int32_t> FullDuplexStreamWithConversion::readInput(int32_t numFrames) {
34 oboe::ResultWithValue<int32_t> result = getInputStream()->read(
35 mInputConverter->getInputBuffer(),
36 numFrames,
37 0 /* timeout */);
38 if (result == oboe::Result::OK) {
39 int32_t numSamples = result.value() * getInputStream()->getChannelCount();
40 mInputConverter->convertInternalBuffers(numSamples);
41 }
42 return result;
43 }
44
onBothStreamsReady(const void * inputData,int numInputFrames,void * outputData,int numOutputFrames)45 oboe::DataCallbackResult FullDuplexStreamWithConversion::onBothStreamsReady(
46 const void *inputData,
47 int numInputFrames,
48 void *outputData,
49 int numOutputFrames
50 ) {
51 oboe::DataCallbackResult callbackResult = oboe::DataCallbackResult::Continue;
52 callbackResult = onBothStreamsReadyFloat(
53 static_cast<const float *>(mInputConverter->getOutputBuffer()),
54 numInputFrames,
55 static_cast<float *>(mOutputConverter->getInputBuffer()),
56 numOutputFrames);
57 mOutputConverter->convertFromInternalInput( outputData,
58 numOutputFrames * getOutputStream()->getChannelCount());
59 return callbackResult;
60 }
61