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