1 /*
2 * Copyright 2021 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 "FormatConverterBox.h"
18
FormatConverterBox(int32_t maxSamples,oboe::AudioFormat inputFormat,oboe::AudioFormat outputFormat)19 FormatConverterBox::FormatConverterBox(int32_t maxSamples,
20 oboe::AudioFormat inputFormat,
21 oboe::AudioFormat outputFormat) {
22 mInputFormat = inputFormat;
23 mOutputFormat = outputFormat;
24
25 mMaxSamples = maxSamples;
26 mInputBuffer = std::make_unique<uint8_t[]>(maxSamples * sizeof(int32_t));
27 mOutputBuffer = std::make_unique<uint8_t[]>(maxSamples * sizeof(int32_t));
28
29 mSource.reset();
30 switch (mInputFormat) {
31 case oboe::AudioFormat::I16:
32 case oboe::AudioFormat::IEC61937:
33 mSource = std::make_unique<oboe::flowgraph::SourceI16>(1);
34 break;
35 case oboe::AudioFormat::I24:
36 mSource = std::make_unique<oboe::flowgraph::SourceI24>(1);
37 break;
38 case oboe::AudioFormat::I32:
39 mSource = std::make_unique<oboe::flowgraph::SourceI32>(1);
40 break;
41 case oboe::AudioFormat::Float:
42 case oboe::AudioFormat::Invalid:
43 case oboe::AudioFormat::Unspecified:
44 mSource = std::make_unique<oboe::flowgraph::SourceFloat>(1);
45 break;
46 case oboe::AudioFormat::MP3:
47 case oboe::AudioFormat::AAC_LC:
48 case oboe::AudioFormat::AAC_HE_V1:
49 case oboe::AudioFormat::AAC_HE_V2:
50 case oboe::AudioFormat::AAC_ELD:
51 case oboe::AudioFormat::AAC_XHE:
52 case oboe::AudioFormat::OPUS:
53 break;
54 }
55
56 mSink.reset();
57 switch (mOutputFormat) {
58 case oboe::AudioFormat::I16:
59 case oboe::AudioFormat::IEC61937:
60 mSink = std::make_unique<oboe::flowgraph::SinkI16>(1);
61 break;
62 case oboe::AudioFormat::I24:
63 mSink = std::make_unique<oboe::flowgraph::SinkI24>(1);
64 break;
65 case oboe::AudioFormat::I32:
66 mSink = std::make_unique<oboe::flowgraph::SinkI32>(1);
67 break;
68 case oboe::AudioFormat::Float:
69 case oboe::AudioFormat::Invalid:
70 case oboe::AudioFormat::Unspecified:
71 mSink = std::make_unique<oboe::flowgraph::SinkFloat>(1);
72 break;
73 case oboe::AudioFormat::MP3:
74 case oboe::AudioFormat::AAC_LC:
75 case oboe::AudioFormat::AAC_HE_V1:
76 case oboe::AudioFormat::AAC_HE_V2:
77 case oboe::AudioFormat::AAC_ELD:
78 case oboe::AudioFormat::AAC_XHE:
79 case oboe::AudioFormat::OPUS:
80 break;
81 }
82
83 if (mSource && mSink) {
84 mSource->output.connect(&mSink->input);
85 mSink->pullReset();
86 }
87 }
88
convertInternalBuffers(int32_t numSamples)89 int32_t FormatConverterBox::convertInternalBuffers(int32_t numSamples) {
90 assert(numSamples <= mMaxSamples);
91 return convert(getOutputBuffer(), numSamples, getInputBuffer());
92 }
93
convertToInternalOutput(int32_t numSamples,const void * inputBuffer)94 int32_t FormatConverterBox::convertToInternalOutput(int32_t numSamples, const void *inputBuffer) {
95 assert(numSamples <= mMaxSamples);
96 return convert(getOutputBuffer(), numSamples, inputBuffer);
97 }
98
convertFromInternalInput(void * outputBuffer,int32_t numSamples)99 int32_t FormatConverterBox::convertFromInternalInput(void *outputBuffer, int32_t numSamples) {
100 assert(numSamples <= mMaxSamples);
101 return convert(outputBuffer, numSamples, getInputBuffer());
102 }
103
convert(void * outputBuffer,int32_t numSamples,const void * inputBuffer)104 int32_t FormatConverterBox::convert(void *outputBuffer, int32_t numSamples, const void *inputBuffer) {
105 mSource->setData(inputBuffer, numSamples);
106 return mSink->read(outputBuffer, numSamples);
107 }
108