Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 ANDROID_AUDIO_RESAMPLER_DYN_H |
| 18 | #define ANDROID_AUDIO_RESAMPLER_DYN_H |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include "AudioResampler.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 28 | /* AudioResamplerDyn |
| 29 | * |
| 30 | * This class template is used for floating point and integer resamplers. |
| 31 | * |
| 32 | * Type variables: |
| 33 | * TC = filter coefficient type (one of int16_t, int32_t, or float) |
| 34 | * TI = input data type (one of int16_t or float) |
| 35 | * TO = output data type (one of int32_t or float) |
| 36 | * |
| 37 | * For integer input data types TI, the coefficient type TC is either int16_t or int32_t. |
| 38 | * For float input data types TI, the coefficient type TC is float. |
| 39 | */ |
| 40 | |
| 41 | template<typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 42 | class AudioResamplerDyn: public AudioResampler { |
| 43 | public: |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 44 | AudioResamplerDyn(int bitDepth, int inChannelCount, |
| 45 | int32_t sampleRate, src_quality quality); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 46 | |
| 47 | virtual ~AudioResamplerDyn(); |
| 48 | |
| 49 | virtual void init(); |
| 50 | |
| 51 | virtual void setSampleRate(int32_t inSampleRate); |
| 52 | |
| 53 | virtual void setVolume(int16_t left, int16_t right); |
| 54 | |
| 55 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 56 | AudioBufferProvider* provider); |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | class Constants { // stores the filter constants. |
| 61 | public: |
| 62 | Constants() : |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 63 | mL(0), mShift(0), mHalfNumCoefs(0), mFirCoefs(NULL) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 64 | {} |
| 65 | void set(int L, int halfNumCoefs, |
| 66 | int inSampleRate, int outSampleRate); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 67 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 68 | int mL; // interpolation phases in the filter. |
| 69 | int mShift; // right shift to get polyphase index |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 70 | unsigned int mHalfNumCoefs; // filter half #coefs |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 71 | const TC* mFirCoefs; // polyphase filter bank |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 74 | class InBuffer { // buffer management for input type TI |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 75 | public: |
| 76 | InBuffer(); |
| 77 | ~InBuffer(); |
| 78 | void init(); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 79 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 80 | void resize(int CHANNELS, int halfNumCoefs); |
| 81 | |
| 82 | // used for direct management of the mImpulse pointer |
| 83 | inline TI* getImpulse() { |
| 84 | return mImpulse; |
| 85 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 86 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 87 | inline void setImpulse(TI *impulse) { |
| 88 | mImpulse = impulse; |
| 89 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 90 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 91 | template<int CHANNELS> |
| 92 | inline void readAgain(TI*& impulse, const int halfNumCoefs, |
| 93 | const TI* const in, const size_t inputIndex); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 94 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 95 | template<int CHANNELS> |
| 96 | inline void readAdvance(TI*& impulse, const int halfNumCoefs, |
| 97 | const TI* const in, const size_t inputIndex); |
| 98 | |
| 99 | private: |
| 100 | // tuning parameter guidelines: 2 <= multiple <= 8 |
| 101 | static const int kStateSizeMultipleOfFilterLength = 4; |
| 102 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 103 | // in general, mRingFull = mState + mStateSize - halfNumCoefs*CHANNELS. |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 104 | TI* mState; // base pointer for the input buffer storage |
| 105 | TI* mImpulse; // current location of the impulse response (centered) |
| 106 | TI* mRingFull; // mState <= mImpulse < mRingFull |
| 107 | size_t mStateCount; // size of state in units of TI. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 110 | void createKaiserFir(Constants &c, double stopBandAtten, |
| 111 | int inSampleRate, int outSampleRate, double tbwCheat); |
| 112 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 113 | template<int CHANNELS, bool LOCKED, int STRIDE> |
| 114 | void resample(TO* out, size_t outFrameCount, AudioBufferProvider* provider); |
| 115 | |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame^] | 116 | // define a pointer to member function type for resample |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 117 | typedef void (AudioResamplerDyn<TC, TI, TO>::*resample_ABP_t)(TO* out, |
| 118 | size_t outFrameCount, AudioBufferProvider* provider); |
| 119 | |
| 120 | // data - the contiguous storage and layout of these is important. |
| 121 | InBuffer mInBuffer; |
| 122 | Constants mConstants; // current set of coefficient parameters |
| 123 | TO __attribute__ ((aligned (8))) mVolumeSimd[2]; // must be aligned or NEON may crash |
| 124 | resample_ABP_t mResampleFunc; // called function for resampling |
| 125 | int32_t mFilterSampleRate; // designed filter sample rate. |
| 126 | src_quality mFilterQuality; // designed filter quality. |
| 127 | void* mCoefBuffer; // if a filter is created, this is not null |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 130 | }; // namespace android |
| 131 | |
| 132 | #endif /*ANDROID_AUDIO_RESAMPLER_DYN_H*/ |