1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef AUDIO_PRORESAMPLER_PROCESS_H 16 #define AUDIO_PRORESAMPLER_PROCESS_H 17 18 #include <stdint.h> 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 typedef enum MultiplyFilterFunMethod { 25 MULTIPLY_FILTER_FUN_SYMMETRIC_ODD_UP, 26 MULTIPLY_FILTER_FUN_SYMMETRIC_EVEN_UP, 27 MULTIPLY_FILTER_FUN_UP, 28 MULTIPLY_FILTER_FUN_SYMMETRIC_ODD_DOWN, 29 MULTIPLY_FILTER_FUN_SYMMETRIC_EVEN_DOWN, 30 MULTIPLY_FILTER_FUN_DOWN, 31 MULTIPLY_FILTER_FUN_MAX 32 } MultiplyFilterFunT; 33 34 enum { 35 RESAMPLER_ERR_SUCCESS = 0, 36 RESAMPLER_ERR_ALLOC_FAILED = -1, 37 RESAMPLER_ERR_INVALID_ARG = -2, 38 RESAMPLER_ERR_OVERFLOW = -3 39 }; 40 41 #define MAX_RATIO_INTEGRAL_METHOD 32 42 43 typedef struct SingleStagePolyphaseResamplerState SingleStagePolyphaseResamplerState; 44 45 typedef int32_t (*ResamplerMethod)(SingleStagePolyphaseResamplerState* state, const float* in, 46 uint32_t* inputLength, float* out, uint32_t* outputLength); 47 typedef void (*MultiplyFilterFun)(SingleStagePolyphaseResamplerState* state, 48 const float* coeffs, const float* inputs, float* outputs, int32_t subfilterNum); 49 50 51 /** 52 * @brief Resampler state 53 * 54 */ 55 struct SingleStagePolyphaseResamplerState { 56 uint32_t decimateFactor; /** Integer decimation factor */ 57 uint32_t interpolateFactor; /** Integer interpolation factor */ 58 59 int32_t quality; /** Parameter for resampler quality (0~10) */ 60 uint32_t numChannels; /** Number of channels */ 61 uint32_t filterLength; /** Number of taps of anti-aliasing/imaging filter */ 62 uint32_t bufferSize; /** Number of buffer samples for each channel */ 63 uint32_t quoSamplerateRatio; /** Quotient of (input sampling frequency)/(output sampling frequency) */ 64 uint32_t remSamplerateRatio; /** remainder of (input sampling frequency)/(output sampling frequency) */ 65 float cutoff; /** Normalized cutoff frequency of anti-aliasing/imaging filter */ 66 float coshParameter; /** Parameter of cosh window for adjusting side-lobe decay of filter */ 67 int32_t isInitialized; /** If the state is initialized, isInitialized=1. */ 68 int32_t isStarted; /** Once the resampler has processed, isStarted = 1. */ 69 70 uint32_t inputIndex; /** Index of the input to be processed. */ 71 uint32_t subfilterNum; /** What number of polyphase subfilters to use. */ 72 uint32_t magicSamples; /** Used for variable sampling frequency (don't need this?) */ 73 74 float* inputMemory; /** An array that stores the inputs to be processed. */ 75 uint32_t inputMemorySize; /** Size of inputMemory. (bufferSize + filterLength + 1) */ 76 float* filterCoefficients; /** An array that stores the polyphase filters. */ 77 uint32_t filterCoefficientsSize; /** Size of filterCoefficients. */ 78 ResamplerMethod resamplerFunction; /** A pointer to the function used for resampling. */ 79 MultiplyFilterFun multiplyFunSeq[MAX_RATIO_INTEGRAL_METHOD]; 80 }; 81 82 /** 83 * @brief Create a new resampler state. 84 * 85 * @param numChannels Number of channels. 86 * @param decimateFactor Integer decimation factor (input sampling frequency). 87 * @param interpolateFactor Integer interpolation factor (output sampling frequency). 88 * @param quality Parameter for determining resampling quality level between 0 (poor) and 10 (best). 89 * @param err 90 * @return SingleStagePolyphaseResamplerState* Created resampler state used for process 91 */ 92 SingleStagePolyphaseResamplerState* SingleStagePolyphaseResamplerInit(uint32_t numChannels, 93 uint32_t decimateFactor, uint32_t interpolateFactor, int32_t quality, int32_t* err); 94 95 96 /** 97 * @brief Set (update) the input/output sampling rates. 98 * 99 * @param state Resampler state 100 * @param decimateFactor Integer decimation factor (input sampling frequency). 101 * @param interpolateFactor Integer interpolation factor (output sampling frequency). 102 * @return int32_t returns 0 if the function terminates normally. 103 */ 104 int32_t SingleStagePolyphaseResamplerSetRate(SingleStagePolyphaseResamplerState* state, 105 uint32_t decimateFactor, uint32_t interpolateFactor); 106 107 /** 108 * @brief 109 * 110 * @param state Resampler state. 111 * @return int32_t returns 0 if the function terminates normally. 112 */ 113 int32_t SingleStagePolyphaseResamplerSkipHalfTaps(SingleStagePolyphaseResamplerState* state); 114 115 116 /** 117 * @brief Reset the buffers of the resampler. 118 * 119 * @param state Resampler state. 120 * @return int32_t returns 0 if the function terminates normally. 121 */ 122 int32_t SingleStagePolyphaseResamplerResetMem(SingleStagePolyphaseResamplerState* state); 123 124 /** 125 * @brief Release the memory for the resampler state 126 * 127 * @param state Resampler state. 128 */ 129 void SingleStagePolyphaseResamplerFree(SingleStagePolyphaseResamplerState* state); 130 131 132 /** 133 * @brief Resample an input array. 134 * 135 * @param state resampler state 136 * @param in Input array. 137 * @param inputLength Number of input samples. 138 * @param out Output array. 139 * @param outputLength Number of input samples. 140 * @return int32_t returns 0 if the function terminates normally. 141 */ 142 int32_t SingleStagePolyphaseResamplerProcess(SingleStagePolyphaseResamplerState* state, 143 const float* in, uint32_t* inputLength, float* out, uint32_t* outputLength); 144 145 #ifdef __cplusplus 146 } 147 #endif 148 149 #endif