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 #ifndef OBOETESTER_FORMAT_CONVERTER_BOX_H 18 #define OBOETESTER_FORMAT_CONVERTER_BOX_H 19 20 #include <unistd.h> 21 #include <sys/types.h> 22 23 #include "oboe/Oboe.h" 24 #include "flowgraph/SinkFloat.h" 25 #include "flowgraph/SinkI16.h" 26 #include "flowgraph/SinkI24.h" 27 #include "flowgraph/SinkI32.h" 28 #include "flowgraph/SourceFloat.h" 29 #include "flowgraph/SourceI16.h" 30 #include "flowgraph/SourceI24.h" 31 #include "flowgraph/SourceI32.h" 32 33 /** 34 * Use flowgraph modules to convert between the various data formats. 35 * 36 * Note that this does not do channel conversions. 37 */ 38 39 class FormatConverterBox { 40 public: 41 FormatConverterBox(int32_t maxSamples, 42 oboe::AudioFormat inputFormat, 43 oboe::AudioFormat outputFormat); 44 45 /** 46 * @return internal buffer used to store input data 47 */ getOutputBuffer()48 void *getOutputBuffer() { 49 return (void *) mOutputBuffer.get(); 50 }; 51 /** 52 * @return internal buffer used to store output data 53 */ getInputBuffer()54 void *getInputBuffer() { 55 return (void *) mInputBuffer.get(); 56 }; 57 58 /** Convert the data from inputFormat to outputFormat 59 * using both internal buffers. 60 */ 61 int32_t convertInternalBuffers(int32_t numSamples); 62 63 /** 64 * Convert data from external buffer into internal output buffer. 65 * @param numSamples 66 * @param inputBuffer 67 * @return 68 */ 69 int32_t convertToInternalOutput(int32_t numSamples, const void *inputBuffer); 70 71 /** 72 * 73 * Convert data from internal input buffer into external output buffer. 74 * @param outputBuffer 75 * @param numSamples 76 * @return 77 */ 78 int32_t convertFromInternalInput(void *outputBuffer, int32_t numSamples); 79 80 /** 81 * Convert data formats between the specified external buffers. 82 * @param outputBuffer 83 * @param numSamples 84 * @param inputBuffer 85 * @return 86 */ 87 int32_t convert(void *outputBuffer, int32_t numSamples, const void *inputBuffer); 88 89 private: 90 oboe::AudioFormat mInputFormat{oboe::AudioFormat::Invalid}; 91 oboe::AudioFormat mOutputFormat{oboe::AudioFormat::Invalid}; 92 93 int32_t mMaxSamples = 0; 94 std::unique_ptr<uint8_t[]> mInputBuffer; 95 std::unique_ptr<uint8_t[]> mOutputBuffer; 96 97 std::unique_ptr<oboe::flowgraph::FlowGraphSourceBuffered> mSource; 98 std::unique_ptr<oboe::flowgraph::FlowGraphSink> mSink; 99 }; 100 101 102 #endif //OBOETESTER_FORMAT_CONVERTER_BOX_H 103