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> |
Mark Salyzyn | 60d0207 | 2016-09-29 08:48:48 -0700 | [diff] [blame] | 22 | #include <android/log.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | |
Andy Hung | 068561c | 2017-01-03 17:09:32 -0800 | [diff] [blame] | 24 | #include <media/AudioResampler.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 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); |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 30 | typedef int32_t (*readResampleFirNumCoeffFn)(); |
| 31 | typedef int32_t (*readResampleFirLerpIntBitsFn)(); |
SathishKumar Mani | 76b1116 | 2012-01-17 10:49:47 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | class AudioResamplerSinc : public AudioResampler { |
| 36 | public: |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 37 | AudioResamplerSinc(int inChannelCount, int32_t sampleRate, |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 38 | src_quality quality = HIGH_QUALITY); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | |
Glenn Kasten | c19e224 | 2012-01-30 14:54:39 -0800 | [diff] [blame] | 40 | virtual ~AudioResamplerSinc(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 42 | virtual size_t resample(int32_t* out, size_t outFrameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | AudioBufferProvider* provider); |
| 44 | private: |
| 45 | void init(); |
| 46 | |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 47 | virtual void setVolume(float left, float right); |
Mathias Agopian | 0d585c8 | 2012-11-10 03:26:39 -0800 | [diff] [blame] | 48 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | template<int CHANNELS> |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 50 | size_t resample(int32_t* out, size_t outFrameCount, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | AudioBufferProvider* provider); |
| 52 | |
| 53 | template<int CHANNELS> |
| 54 | inline void filterCoefficient( |
Mathias Agopian | 0d585c8 | 2012-11-10 03:26:39 -0800 | [diff] [blame] | 55 | int32_t* out, uint32_t phase, const int16_t *samples, uint32_t vRL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
| 57 | template<int CHANNELS> |
| 58 | inline void interpolate( |
| 59 | int32_t& l, int32_t& r, |
Mathias Agopian | 46afbec | 2012-11-04 02:03:49 -0800 | [diff] [blame] | 60 | const int32_t* coefs, size_t offset, |
| 61 | int32_t lerp, const int16_t* samples); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
| 63 | template<int CHANNELS> |
| 64 | inline void read(int16_t*& impulse, uint32_t& phaseFraction, |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 65 | const int16_t* in, size_t inputIndex); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | |
| 67 | int16_t *mState; |
| 68 | int16_t *mImpulse; |
| 69 | int16_t *mRingFull; |
Mathias Agopian | 0d585c8 | 2012-11-10 03:26:39 -0800 | [diff] [blame] | 70 | int32_t mVolumeSIMD[2]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 71 | |
Glenn Kasten | 54c3b66 | 2012-01-06 07:46:30 -0800 | [diff] [blame] | 72 | const int32_t * mFirCoefs; |
Glenn Kasten | c497431 | 2012-12-14 07:13:28 -0800 | [diff] [blame] | 73 | static const uint32_t mFirCoefsDown[]; |
| 74 | static const uint32_t mFirCoefsUp[]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | |
| 76 | // ---------------------------------------------------------------------------- |
| 77 | static const int32_t RESAMPLE_FIR_NUM_COEF = 8; |
Mathias Agopian | 443e696 | 2012-10-26 13:48:42 -0700 | [diff] [blame] | 78 | static const int32_t RESAMPLE_FIR_LERP_INT_BITS = 7; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 80 | struct Constants { |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 81 | int coefsBits; |
| 82 | int cShift; |
| 83 | uint32_t cMask; |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 84 | int pShift; |
| 85 | uint32_t pMask; |
Glenn Kasten | ac60205 | 2012-10-01 14:04:31 -0700 | [diff] [blame] | 86 | // number of zero-crossing on each side |
| 87 | unsigned int halfNumCoefs; |
| 88 | }; |
| 89 | |
| 90 | static Constants highQualityConstants; |
| 91 | static Constants veryHighQualityConstants; |
| 92 | const Constants *mConstants; // points to appropriate set of coefficient parameters |
| 93 | |
| 94 | static void init_routine(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 98 | } // namespace android |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 99 | |
| 100 | #endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/ |