Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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_SINC_H |
| 18 | #define ANDROID_AUDIO_RESAMPLER_SINC_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 | |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 28 | |
| 29 | typedef const int32_t * (*readCoefficientsFn)(bool upDownSample); |
| 30 | typedef int32_t (*readResampleFirNumCoeffFn)(); |
| 31 | typedef int32_t (*readResampleFirLerpIntBitsFn)(); |
| 32 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | class AudioResamplerSinc : public AudioResampler { |
| 36 | public: |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 37 | AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate, int32_t quality = HIGH_QUALITY); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | |
Glenn Kasten | c19e224 | 2012-01-30 14:54:39 -0800 | [diff] [blame] | 39 | virtual ~AudioResamplerSinc(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | |
| 41 | virtual void resample(int32_t* out, size_t outFrameCount, |
| 42 | AudioBufferProvider* provider); |
| 43 | private: |
| 44 | void init(); |
| 45 | |
| 46 | template<int CHANNELS> |
| 47 | void resample(int32_t* out, size_t outFrameCount, |
| 48 | AudioBufferProvider* provider); |
| 49 | |
| 50 | template<int CHANNELS> |
| 51 | inline void filterCoefficient( |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 52 | int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | |
| 54 | template<int CHANNELS> |
| 55 | inline void interpolate( |
| 56 | int32_t& l, int32_t& r, |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 57 | const int32_t* coefs, int16_t lerp, const int16_t* samples); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | |
| 59 | template<int CHANNELS> |
| 60 | inline void read(int16_t*& impulse, uint32_t& phaseFraction, |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 61 | const int16_t* in, size_t inputIndex); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 63 | readCoefficientsFn mReadResampleCoefficients ; |
| 64 | readResampleFirNumCoeffFn mReadResampleFirNumCoeff; |
| 65 | readResampleFirLerpIntBitsFn mReadResampleFirLerpIntBits; |
| 66 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | int16_t *mState; |
| 68 | int16_t *mImpulse; |
| 69 | int16_t *mRingFull; |
| 70 | |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 71 | const int32_t * mFirCoefs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 72 | static const int32_t mFirCoefsDown[]; |
| 73 | static const int32_t mFirCoefsUp[]; |
| 74 | |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 75 | void * mResampleCoeffLib; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 76 | // ---------------------------------------------------------------------------- |
| 77 | static const int32_t RESAMPLE_FIR_NUM_COEF = 8; |
| 78 | static const int32_t RESAMPLE_FIR_LERP_INT_BITS = 4; |
| 79 | |
| 80 | // we have 16 coefs samples per zero-crossing |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 81 | static int coefsBits; |
| 82 | static int cShift; |
| 83 | static uint32_t cMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 84 | |
| 85 | // and we use 15 bits to interpolate between these samples |
| 86 | // this cannot change because the mul below rely on it. |
| 87 | static const int pLerpBits = 15; |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 88 | static int pShift; |
| 89 | static uint32_t pMask; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 90 | |
| 91 | // number of zero-crossing on each side |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame^] | 92 | static unsigned int halfNumCoefs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | }; // namespace android |
| 97 | |
| 98 | #endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/ |