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 | #define LOG_TAG "AudioResamplerDyn" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <malloc.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <dlfcn.h> |
| 24 | #include <math.h> |
| 25 | |
| 26 | #include <cutils/compiler.h> |
| 27 | #include <cutils/properties.h> |
Andy Hung | d549139 | 2014-04-08 18:28:09 -0700 | [diff] [blame] | 28 | #include <utils/Debug.h> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 30 | #include <audio_utils/primitives.h> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 31 | |
| 32 | #include "AudioResamplerFirOps.h" // USE_NEON and USE_INLINE_ASSEMBLY defined here |
| 33 | #include "AudioResamplerFirProcess.h" |
| 34 | #include "AudioResamplerFirProcessNeon.h" |
| 35 | #include "AudioResamplerFirGen.h" // requires math.h |
| 36 | #include "AudioResamplerDyn.h" |
| 37 | |
| 38 | //#define DEBUG_RESAMPLER |
| 39 | |
| 40 | namespace android { |
| 41 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 42 | /* |
| 43 | * InBuffer is a type agnostic input buffer. |
| 44 | * |
| 45 | * Layout of the state buffer for halfNumCoefs=8. |
| 46 | * |
| 47 | * [rrrrrrppppppppnnnnnnnnrrrrrrrrrrrrrrrrrrr.... rrrrrrr] |
| 48 | * S I R |
| 49 | * |
| 50 | * S = mState |
| 51 | * I = mImpulse |
| 52 | * R = mRingFull |
| 53 | * p = past samples, convoluted with the (p)ositive side of sinc() |
| 54 | * n = future samples, convoluted with the (n)egative side of sinc() |
| 55 | * r = extra space for implementing the ring buffer |
| 56 | */ |
| 57 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 58 | template<typename TC, typename TI, typename TO> |
| 59 | AudioResamplerDyn<TC, TI, TO>::InBuffer::InBuffer() |
| 60 | : mState(NULL), mImpulse(NULL), mRingFull(NULL), mStateCount(0) |
| 61 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 64 | template<typename TC, typename TI, typename TO> |
| 65 | AudioResamplerDyn<TC, TI, TO>::InBuffer::~InBuffer() |
| 66 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 67 | init(); |
| 68 | } |
| 69 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 70 | template<typename TC, typename TI, typename TO> |
| 71 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::init() |
| 72 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 73 | free(mState); |
| 74 | mState = NULL; |
| 75 | mImpulse = NULL; |
| 76 | mRingFull = NULL; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 77 | mStateCount = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // resizes the state buffer to accommodate the appropriate filter length |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 81 | template<typename TC, typename TI, typename TO> |
| 82 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::resize(int CHANNELS, int halfNumCoefs) |
| 83 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 84 | // calculate desired state size |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 85 | size_t stateCount = halfNumCoefs * CHANNELS * 2 * kStateSizeMultipleOfFilterLength; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 86 | |
| 87 | // check if buffer needs resizing |
| 88 | if (mState |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 89 | && stateCount == mStateCount |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 90 | && mRingFull-mState == (ssize_t) (mStateCount-halfNumCoefs*CHANNELS)) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | |
| 94 | // create new buffer |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 95 | TI* state = NULL; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 96 | (void)posix_memalign(reinterpret_cast<void**>(&state), 32, stateCount*sizeof(*state)); |
| 97 | memset(state, 0, stateCount*sizeof(*state)); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 98 | |
| 99 | // attempt to preserve state |
| 100 | if (mState) { |
| 101 | TI* srcLo = mImpulse - halfNumCoefs*CHANNELS; |
| 102 | TI* srcHi = mImpulse + halfNumCoefs*CHANNELS; |
| 103 | TI* dst = state; |
| 104 | |
| 105 | if (srcLo < mState) { |
| 106 | dst += mState-srcLo; |
| 107 | srcLo = mState; |
| 108 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 109 | if (srcHi > mState + mStateCount) { |
| 110 | srcHi = mState + mStateCount; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 111 | } |
| 112 | memcpy(dst, srcLo, (srcHi - srcLo) * sizeof(*srcLo)); |
| 113 | free(mState); |
| 114 | } |
| 115 | |
| 116 | // set class member vars |
| 117 | mState = state; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 118 | mStateCount = stateCount; |
| 119 | mImpulse = state + halfNumCoefs*CHANNELS; // actually one sample greater than needed |
| 120 | mRingFull = state + mStateCount - halfNumCoefs*CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // copy in the input data into the head (impulse+halfNumCoefs) of the buffer. |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 124 | template<typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 125 | template<int CHANNELS> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 126 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAgain(TI*& impulse, const int halfNumCoefs, |
| 127 | const TI* const in, const size_t inputIndex) |
| 128 | { |
| 129 | TI* head = impulse + halfNumCoefs*CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 130 | for (size_t i=0 ; i<CHANNELS ; i++) { |
| 131 | head[i] = in[inputIndex*CHANNELS + i]; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // advance the impulse pointer, and load in data into the head (impulse+halfNumCoefs) |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 136 | template<typename TC, typename TI, typename TO> |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 137 | template<int CHANNELS> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 138 | void AudioResamplerDyn<TC, TI, TO>::InBuffer::readAdvance(TI*& impulse, const int halfNumCoefs, |
| 139 | const TI* const in, const size_t inputIndex) |
| 140 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 141 | impulse += CHANNELS; |
| 142 | |
| 143 | if (CC_UNLIKELY(impulse >= mRingFull)) { |
| 144 | const size_t shiftDown = mRingFull - mState - halfNumCoefs*CHANNELS; |
| 145 | memcpy(mState, mState+shiftDown, halfNumCoefs*CHANNELS*2*sizeof(TI)); |
| 146 | impulse -= shiftDown; |
| 147 | } |
| 148 | readAgain<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
| 149 | } |
| 150 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 151 | template<typename TC, typename TI, typename TO> |
| 152 | void AudioResamplerDyn<TC, TI, TO>::Constants::set( |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 153 | int L, int halfNumCoefs, int inSampleRate, int outSampleRate) |
| 154 | { |
| 155 | int bits = 0; |
| 156 | int lscale = inSampleRate/outSampleRate < 2 ? L - 1 : |
| 157 | static_cast<int>(static_cast<uint64_t>(L)*inSampleRate/outSampleRate); |
| 158 | for (int i=lscale; i; ++bits, i>>=1) |
| 159 | ; |
| 160 | mL = L; |
| 161 | mShift = kNumPhaseBits - bits; |
| 162 | mHalfNumCoefs = halfNumCoefs; |
| 163 | } |
| 164 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 165 | template<typename TC, typename TI, typename TO> |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 166 | AudioResamplerDyn<TC, TI, TO>::AudioResamplerDyn( |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 167 | int inChannelCount, int32_t sampleRate, src_quality quality) |
Andy Hung | 3348e36 | 2014-07-07 10:21:44 -0700 | [diff] [blame] | 168 | : AudioResampler(inChannelCount, sampleRate, quality), |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 169 | mResampleFunc(0), mFilterSampleRate(0), mFilterQuality(DEFAULT_QUALITY), |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 170 | mCoefBuffer(NULL) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 171 | { |
| 172 | mVolumeSimd[0] = mVolumeSimd[1] = 0; |
Andy Hung | 1af3408 | 2014-02-19 17:42:25 -0800 | [diff] [blame] | 173 | // The AudioResampler base class assumes we are always ready for 1:1 resampling. |
| 174 | // We reset mInSampleRate to 0, so setSampleRate() will calculate filters for |
| 175 | // setSampleRate() for 1:1. (May be removed if precalculated filters are used.) |
| 176 | mInSampleRate = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 177 | mConstants.set(128, 8, mSampleRate, mSampleRate); // TODO: set better |
| 178 | } |
| 179 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 180 | template<typename TC, typename TI, typename TO> |
| 181 | AudioResamplerDyn<TC, TI, TO>::~AudioResamplerDyn() |
| 182 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 183 | free(mCoefBuffer); |
| 184 | } |
| 185 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 186 | template<typename TC, typename TI, typename TO> |
| 187 | void AudioResamplerDyn<TC, TI, TO>::init() |
| 188 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 189 | mFilterSampleRate = 0; // always trigger new filter generation |
| 190 | mInBuffer.init(); |
| 191 | } |
| 192 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 193 | template<typename TC, typename TI, typename TO> |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 194 | void AudioResamplerDyn<TC, TI, TO>::setVolume(float left, float right) |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 195 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 196 | AudioResampler::setVolume(left, right); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 197 | if (is_same<TO, float>::value || is_same<TO, double>::value) { |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 198 | mVolumeSimd[0] = static_cast<TO>(left); |
| 199 | mVolumeSimd[1] = static_cast<TO>(right); |
| 200 | } else { // integer requires scaling to U4_28 (rounding down) |
| 201 | // integer volumes are clamped to 0 to UNITY_GAIN so there |
| 202 | // are no issues with signed overflow. |
| 203 | mVolumeSimd[0] = u4_28_from_float(clampFloatVol(left)); |
| 204 | mVolumeSimd[1] = u4_28_from_float(clampFloatVol(right)); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 205 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 208 | template<typename T> T max(T a, T b) {return a > b ? a : b;} |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 209 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 210 | template<typename T> T absdiff(T a, T b) {return a > b ? a - b : b - a;} |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 211 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 212 | template<typename TC, typename TI, typename TO> |
| 213 | void AudioResamplerDyn<TC, TI, TO>::createKaiserFir(Constants &c, |
| 214 | double stopBandAtten, int inSampleRate, int outSampleRate, double tbwCheat) |
| 215 | { |
Glenn Kasten | a4daf0b | 2014-07-28 16:34:45 -0700 | [diff] [blame] | 216 | TC* buf = NULL; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 217 | static const double atten = 0.9998; // to avoid ripple overflow |
| 218 | double fcr; |
| 219 | double tbw = firKaiserTbw(c.mHalfNumCoefs, stopBandAtten); |
| 220 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 221 | (void)posix_memalign(reinterpret_cast<void**>(&buf), 32, (c.mL+1)*c.mHalfNumCoefs*sizeof(TC)); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 222 | if (inSampleRate < outSampleRate) { // upsample |
| 223 | fcr = max(0.5*tbwCheat - tbw/2, tbw/2); |
| 224 | } else { // downsample |
| 225 | fcr = max(0.5*tbwCheat*outSampleRate/inSampleRate - tbw/2, tbw/2); |
| 226 | } |
| 227 | // create and set filter |
| 228 | firKaiserGen(buf, c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 229 | c.mFirCoefs = buf; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 230 | if (mCoefBuffer) { |
| 231 | free(mCoefBuffer); |
| 232 | } |
| 233 | mCoefBuffer = buf; |
| 234 | #ifdef DEBUG_RESAMPLER |
| 235 | // print basic filter stats |
| 236 | printf("L:%d hnc:%d stopBandAtten:%lf fcr:%lf atten:%lf tbw:%lf\n", |
| 237 | c.mL, c.mHalfNumCoefs, stopBandAtten, fcr, atten, tbw); |
| 238 | // test the filter and report results |
| 239 | double fp = (fcr - tbw/2)/c.mL; |
| 240 | double fs = (fcr + tbw/2)/c.mL; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 241 | double passMin, passMax, passRipple; |
| 242 | double stopMax, stopRipple; |
| 243 | testFir(buf, c.mL, c.mHalfNumCoefs, fp, fs, /*passSteps*/ 1000, /*stopSteps*/ 100000, |
| 244 | passMin, passMax, passRipple, stopMax, stopRipple); |
| 245 | printf("passband(%lf, %lf): %.8lf %.8lf %.8lf\n", 0., fp, passMin, passMax, passRipple); |
| 246 | printf("stopband(%lf, %lf): %.8lf %.3lf\n", fs, 0.5, stopMax, stopRipple); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 247 | #endif |
| 248 | } |
| 249 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 250 | // recursive gcd. Using objdump, it appears the tail recursion is converted to a while loop. |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 251 | static int gcd(int n, int m) |
| 252 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 253 | if (m == 0) { |
| 254 | return n; |
| 255 | } |
| 256 | return gcd(m, n % m); |
| 257 | } |
| 258 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 259 | static bool isClose(int32_t newSampleRate, int32_t prevSampleRate, |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 260 | int32_t filterSampleRate, int32_t outSampleRate) |
| 261 | { |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 262 | |
| 263 | // different upsampling ratios do not need a filter change. |
| 264 | if (filterSampleRate != 0 |
| 265 | && filterSampleRate < outSampleRate |
| 266 | && newSampleRate < outSampleRate) |
| 267 | return true; |
| 268 | |
| 269 | // check design criteria again if downsampling is detected. |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 270 | int pdiff = absdiff(newSampleRate, prevSampleRate); |
| 271 | int adiff = absdiff(newSampleRate, filterSampleRate); |
| 272 | |
| 273 | // allow up to 6% relative change increments. |
| 274 | // allow up to 12% absolute change increments (from filter design) |
| 275 | return pdiff < prevSampleRate>>4 && adiff < filterSampleRate>>3; |
| 276 | } |
| 277 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 278 | template<typename TC, typename TI, typename TO> |
| 279 | void AudioResamplerDyn<TC, TI, TO>::setSampleRate(int32_t inSampleRate) |
| 280 | { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 281 | if (mInSampleRate == inSampleRate) { |
| 282 | return; |
| 283 | } |
| 284 | int32_t oldSampleRate = mInSampleRate; |
| 285 | int32_t oldHalfNumCoefs = mConstants.mHalfNumCoefs; |
| 286 | uint32_t oldPhaseWrapLimit = mConstants.mL << mConstants.mShift; |
| 287 | bool useS32 = false; |
| 288 | |
| 289 | mInSampleRate = inSampleRate; |
| 290 | |
| 291 | // TODO: Add precalculated Equiripple filters |
| 292 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 293 | if (mFilterQuality != getQuality() || |
| 294 | !isClose(inSampleRate, oldSampleRate, mFilterSampleRate, mSampleRate)) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 295 | mFilterSampleRate = inSampleRate; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 296 | mFilterQuality = getQuality(); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 297 | |
| 298 | // Begin Kaiser Filter computation |
| 299 | // |
| 300 | // The quantization floor for S16 is about 96db - 10*log_10(#length) + 3dB. |
| 301 | // Keep the stop band attenuation no greater than 84-85dB for 32 length S16 filters |
| 302 | // |
| 303 | // For s32 we keep the stop band attenuation at the same as 16b resolution, about |
| 304 | // 96-98dB |
| 305 | // |
| 306 | |
| 307 | double stopBandAtten; |
| 308 | double tbwCheat = 1.; // how much we "cheat" into aliasing |
| 309 | int halfLength; |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 310 | if (mFilterQuality == DYN_HIGH_QUALITY) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 311 | // 32b coefficients, 64 length |
| 312 | useS32 = true; |
| 313 | stopBandAtten = 98.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 314 | if (inSampleRate >= mSampleRate * 4) { |
| 315 | halfLength = 48; |
| 316 | } else if (inSampleRate >= mSampleRate * 2) { |
| 317 | halfLength = 40; |
| 318 | } else { |
| 319 | halfLength = 32; |
| 320 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 321 | } else if (mFilterQuality == DYN_LOW_QUALITY) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 322 | // 16b coefficients, 16-32 length |
| 323 | useS32 = false; |
| 324 | stopBandAtten = 80.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 325 | if (inSampleRate >= mSampleRate * 4) { |
| 326 | halfLength = 24; |
| 327 | } else if (inSampleRate >= mSampleRate * 2) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 328 | halfLength = 16; |
| 329 | } else { |
| 330 | halfLength = 8; |
| 331 | } |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 332 | if (inSampleRate <= mSampleRate) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 333 | tbwCheat = 1.05; |
| 334 | } else { |
| 335 | tbwCheat = 1.03; |
| 336 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 337 | } else { // DYN_MED_QUALITY |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 338 | // 16b coefficients, 32-64 length |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 339 | // note: > 64 length filters with 16b coefs can have quantization noise problems |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 340 | useS32 = false; |
| 341 | stopBandAtten = 84.; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 342 | if (inSampleRate >= mSampleRate * 4) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 343 | halfLength = 32; |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 344 | } else if (inSampleRate >= mSampleRate * 2) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 345 | halfLength = 24; |
| 346 | } else { |
| 347 | halfLength = 16; |
| 348 | } |
Andy Hung | a3bb9a3 | 2014-02-10 15:00:16 -0800 | [diff] [blame] | 349 | if (inSampleRate <= mSampleRate) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 350 | tbwCheat = 1.03; |
| 351 | } else { |
| 352 | tbwCheat = 1.01; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // determine the number of polyphases in the filterbank. |
| 357 | // for 16b, it is desirable to have 2^(16/2) = 256 phases. |
| 358 | // https://ccrma.stanford.edu/~jos/resample/Relation_Interpolation_Error_Quantization.html |
| 359 | // |
| 360 | // We are a bit more lax on this. |
| 361 | |
| 362 | int phases = mSampleRate / gcd(mSampleRate, inSampleRate); |
| 363 | |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 364 | // TODO: Once dynamic sample rate change is an option, the code below |
| 365 | // should be modified to execute only when dynamic sample rate change is enabled. |
| 366 | // |
| 367 | // as above, #phases less than 63 is too few phases for accurate linear interpolation. |
| 368 | // we increase the phases to compensate, but more phases means more memory per |
| 369 | // filter and more time to compute the filter. |
| 370 | // |
| 371 | // if we know that the filter will be used for dynamic sample rate changes, |
| 372 | // that would allow us skip this part for fixed sample rate resamplers. |
| 373 | // |
| 374 | while (phases<63) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 375 | phases *= 2; // this code only needed to support dynamic rate changes |
| 376 | } |
Andy Hung | 6582f2b | 2014-01-03 12:30:41 -0800 | [diff] [blame] | 377 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 378 | if (phases>=256) { // too many phases, always interpolate |
| 379 | phases = 127; |
| 380 | } |
| 381 | |
| 382 | // create the filter |
| 383 | mConstants.set(phases, halfLength, inSampleRate, mSampleRate); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 384 | createKaiserFir(mConstants, stopBandAtten, |
| 385 | inSampleRate, mSampleRate, tbwCheat); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 386 | } // End Kaiser filter |
| 387 | |
| 388 | // update phase and state based on the new filter. |
| 389 | const Constants& c(mConstants); |
| 390 | mInBuffer.resize(mChannelCount, c.mHalfNumCoefs); |
| 391 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
| 392 | // try to preserve as much of the phase fraction as possible for on-the-fly changes |
| 393 | mPhaseFraction = static_cast<unsigned long long>(mPhaseFraction) |
| 394 | * phaseWrapLimit / oldPhaseWrapLimit; |
| 395 | mPhaseFraction %= phaseWrapLimit; // should not do anything, but just in case. |
Andy Hung | cd04484 | 2014-08-07 11:04:34 -0700 | [diff] [blame] | 396 | mPhaseIncrement = static_cast<uint32_t>(static_cast<uint64_t>(phaseWrapLimit) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 397 | * inSampleRate / mSampleRate); |
| 398 | |
| 399 | // determine which resampler to use |
| 400 | // check if locked phase (works only if mPhaseIncrement has no "fractional phase bits") |
| 401 | int locked = (mPhaseIncrement << (sizeof(mPhaseIncrement)*8 - c.mShift)) == 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 402 | if (locked) { |
| 403 | mPhaseFraction = mPhaseFraction >> c.mShift << c.mShift; // remove fractional phase |
| 404 | } |
Andy Hung | 83be256 | 2014-02-03 14:11:09 -0800 | [diff] [blame] | 405 | |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 406 | // stride is the minimum number of filter coefficients processed per loop iteration. |
| 407 | // We currently only allow a stride of 16 to match with SIMD processing. |
| 408 | // This means that the filter length must be a multiple of 16, |
| 409 | // or half the filter length (mHalfNumCoefs) must be a multiple of 8. |
| 410 | // |
| 411 | // Note: A stride of 2 is achieved with non-SIMD processing. |
| 412 | int stride = ((c.mHalfNumCoefs & 7) == 0) ? 16 : 2; |
| 413 | LOG_ALWAYS_FATAL_IF(stride < 16, "Resampler stride must be 16 or more"); |
Andy Hung | 5e58b0a | 2014-06-23 19:07:29 -0700 | [diff] [blame] | 414 | LOG_ALWAYS_FATAL_IF(mChannelCount < 1 || mChannelCount > 8, |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 415 | "Resampler channels(%d) must be between 1 to 8", mChannelCount); |
| 416 | // stride 16 (falls back to stride 2 for machines that do not support NEON) |
| 417 | if (locked) { |
| 418 | switch (mChannelCount) { |
| 419 | case 1: |
| 420 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, true, 16>; |
| 421 | break; |
| 422 | case 2: |
| 423 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, true, 16>; |
| 424 | break; |
| 425 | case 3: |
| 426 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, true, 16>; |
| 427 | break; |
| 428 | case 4: |
| 429 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, true, 16>; |
| 430 | break; |
| 431 | case 5: |
| 432 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, true, 16>; |
| 433 | break; |
| 434 | case 6: |
| 435 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, true, 16>; |
| 436 | break; |
| 437 | case 7: |
| 438 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, true, 16>; |
| 439 | break; |
| 440 | case 8: |
| 441 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, true, 16>; |
| 442 | break; |
| 443 | } |
| 444 | } else { |
| 445 | switch (mChannelCount) { |
| 446 | case 1: |
| 447 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<1, false, 16>; |
| 448 | break; |
| 449 | case 2: |
| 450 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<2, false, 16>; |
| 451 | break; |
| 452 | case 3: |
| 453 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<3, false, 16>; |
| 454 | break; |
| 455 | case 4: |
| 456 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<4, false, 16>; |
| 457 | break; |
| 458 | case 5: |
| 459 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<5, false, 16>; |
| 460 | break; |
| 461 | case 6: |
| 462 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<6, false, 16>; |
| 463 | break; |
| 464 | case 7: |
| 465 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<7, false, 16>; |
| 466 | break; |
| 467 | case 8: |
| 468 | mResampleFunc = &AudioResamplerDyn<TC, TI, TO>::resample<8, false, 16>; |
| 469 | break; |
| 470 | } |
| 471 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 472 | #ifdef DEBUG_RESAMPLER |
| 473 | printf("channels:%d %s stride:%d %s coef:%d shift:%d\n", |
| 474 | mChannelCount, locked ? "locked" : "interpolated", |
| 475 | stride, useS32 ? "S32" : "S16", 2*c.mHalfNumCoefs, c.mShift); |
| 476 | #endif |
| 477 | } |
| 478 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 479 | template<typename TC, typename TI, typename TO> |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 480 | size_t AudioResamplerDyn<TC, TI, TO>::resample(int32_t* out, size_t outFrameCount, |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 481 | AudioBufferProvider* provider) |
| 482 | { |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 483 | return (this->*mResampleFunc)(reinterpret_cast<TO*>(out), outFrameCount, provider); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 484 | } |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 485 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 486 | template<typename TC, typename TI, typename TO> |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 487 | template<int CHANNELS, bool LOCKED, int STRIDE> |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 488 | size_t AudioResamplerDyn<TC, TI, TO>::resample(TO* out, size_t outFrameCount, |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 489 | AudioBufferProvider* provider) |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 490 | { |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 491 | // TODO Mono -> Mono is not supported. OUTPUT_CHANNELS reflects minimum of stereo out. |
| 492 | const int OUTPUT_CHANNELS = (CHANNELS < 2) ? 2 : CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 493 | const Constants& c(mConstants); |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 494 | const TC* const coefs = mConstants.mFirCoefs; |
| 495 | TI* impulse = mInBuffer.getImpulse(); |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 496 | size_t inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 497 | uint32_t phaseFraction = mPhaseFraction; |
| 498 | const uint32_t phaseIncrement = mPhaseIncrement; |
| 499 | size_t outputIndex = 0; |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 500 | size_t outputSampleCount = outFrameCount * OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 501 | const uint32_t phaseWrapLimit = c.mL << c.mShift; |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 502 | size_t inFrameCount = (phaseIncrement * (uint64_t)outFrameCount + phaseFraction) |
| 503 | / phaseWrapLimit; |
| 504 | // sanity check that inFrameCount is in signed 32 bit integer range. |
| 505 | ALOG_ASSERT(0 <= inFrameCount && inFrameCount < (1U << 31)); |
| 506 | |
| 507 | //ALOGV("inFrameCount:%d outFrameCount:%d" |
| 508 | // " phaseIncrement:%u phaseFraction:%u phaseWrapLimit:%u", |
| 509 | // inFrameCount, outFrameCount, phaseIncrement, phaseFraction, phaseWrapLimit); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 510 | |
| 511 | // NOTE: be very careful when modifying the code here. register |
| 512 | // pressure is very high and a small change might cause the compiler |
| 513 | // to generate far less efficient code. |
| 514 | // Always sanity check the result with objdump or test-resample. |
| 515 | |
| 516 | // the following logic is a bit convoluted to keep the main processing loop |
| 517 | // as tight as possible with register allocation. |
| 518 | while (outputIndex < outputSampleCount) { |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 519 | //ALOGV("LOOP: inFrameCount:%d outputIndex:%d outFrameCount:%d" |
| 520 | // " phaseFraction:%u phaseWrapLimit:%u", |
| 521 | // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit); |
| 522 | |
| 523 | // check inputIndex overflow |
| 524 | ALOG_ASSERT(inputIndex <= mBuffer.frameCount, "inputIndex%d > frameCount%d", |
| 525 | inputIndex, mBuffer.frameCount); |
| 526 | // Buffer is empty, fetch a new one if necessary (inFrameCount > 0). |
| 527 | // We may not fetch a new buffer if the existing data is sufficient. |
| 528 | while (mBuffer.frameCount == 0 && inFrameCount > 0) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 529 | mBuffer.frameCount = inFrameCount; |
| 530 | provider->getNextBuffer(&mBuffer, |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 531 | calculateOutputPTS(outputIndex / OUTPUT_CHANNELS)); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 532 | if (mBuffer.raw == NULL) { |
| 533 | goto resample_exit; |
| 534 | } |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 535 | inFrameCount -= mBuffer.frameCount; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 536 | if (phaseFraction >= phaseWrapLimit) { // read in data |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 537 | mInBuffer.template readAdvance<CHANNELS>( |
| 538 | impulse, c.mHalfNumCoefs, |
| 539 | reinterpret_cast<TI*>(mBuffer.raw), inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 540 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 541 | phaseFraction -= phaseWrapLimit; |
| 542 | while (phaseFraction >= phaseWrapLimit) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 543 | if (inputIndex >= mBuffer.frameCount) { |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 544 | inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 545 | provider->releaseBuffer(&mBuffer); |
| 546 | break; |
| 547 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 548 | mInBuffer.template readAdvance<CHANNELS>( |
| 549 | impulse, c.mHalfNumCoefs, |
| 550 | reinterpret_cast<TI*>(mBuffer.raw), inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 551 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 552 | phaseFraction -= phaseWrapLimit; |
| 553 | } |
| 554 | } |
| 555 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 556 | const TI* const in = reinterpret_cast<const TI*>(mBuffer.raw); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 557 | const size_t frameCount = mBuffer.frameCount; |
| 558 | const int coefShift = c.mShift; |
| 559 | const int halfNumCoefs = c.mHalfNumCoefs; |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 560 | const TO* const volumeSimd = mVolumeSimd; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 561 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 562 | // main processing loop |
| 563 | while (CC_LIKELY(outputIndex < outputSampleCount)) { |
| 564 | // caution: fir() is inlined and may be large. |
| 565 | // output will be loaded with the appropriate values |
| 566 | // |
| 567 | // from the input samples in impulse[-halfNumCoefs+1]... impulse[halfNumCoefs] |
| 568 | // from the polyphase filter of (phaseFraction / phaseWrapLimit) in coefs. |
| 569 | // |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 570 | //ALOGV("LOOP2: inFrameCount:%d outputIndex:%d outFrameCount:%d" |
| 571 | // " phaseFraction:%u phaseWrapLimit:%u", |
| 572 | // inFrameCount, outputIndex, outFrameCount, phaseFraction, phaseWrapLimit); |
| 573 | ALOG_ASSERT(phaseFraction < phaseWrapLimit); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 574 | fir<CHANNELS, LOCKED, STRIDE>( |
| 575 | &out[outputIndex], |
| 576 | phaseFraction, phaseWrapLimit, |
| 577 | coefShift, halfNumCoefs, coefs, |
| 578 | impulse, volumeSimd); |
Andy Hung | 075abae | 2014-04-09 19:36:43 -0700 | [diff] [blame] | 579 | |
| 580 | outputIndex += OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 581 | |
| 582 | phaseFraction += phaseIncrement; |
| 583 | while (phaseFraction >= phaseWrapLimit) { |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 584 | if (inputIndex >= frameCount) { |
| 585 | goto done; // need a new buffer |
| 586 | } |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 587 | mInBuffer.template readAdvance<CHANNELS>(impulse, halfNumCoefs, in, inputIndex); |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 588 | inputIndex++; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 589 | phaseFraction -= phaseWrapLimit; |
| 590 | } |
| 591 | } |
| 592 | done: |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 593 | // We arrive here when we're finished or when the input buffer runs out. |
| 594 | // Regardless we need to release the input buffer if we've acquired it. |
| 595 | if (inputIndex > 0) { // we've acquired a buffer (alternatively could check frameCount) |
| 596 | ALOG_ASSERT(inputIndex == frameCount, "inputIndex(%d) != frameCount(%d)", |
| 597 | inputIndex, frameCount); // must have been fully read. |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 598 | inputIndex = 0; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 599 | provider->releaseBuffer(&mBuffer); |
Andy Hung | 411cb8e | 2014-05-27 12:32:17 -0700 | [diff] [blame] | 600 | ALOG_ASSERT(mBuffer.frameCount == 0); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | resample_exit: |
Andy Hung | 7170074 | 2014-06-02 18:54:08 -0700 | [diff] [blame] | 605 | // inputIndex must be zero in all three cases: |
| 606 | // (1) the buffer never was been acquired; (2) the buffer was |
| 607 | // released at "done:"; or (3) getNextBuffer() failed. |
| 608 | ALOG_ASSERT(inputIndex == 0, "Releasing: inputindex:%d frameCount:%d phaseFraction:%u", |
| 609 | inputIndex, mBuffer.frameCount, phaseFraction); |
| 610 | ALOG_ASSERT(mBuffer.frameCount == 0); // there must be no frames in the buffer |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 611 | mInBuffer.setImpulse(impulse); |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 612 | mPhaseFraction = phaseFraction; |
Andy Hung | 6b3b7e3 | 2015-03-29 00:49:22 -0700 | [diff] [blame] | 613 | return outputIndex / OUTPUT_CHANNELS; |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Andy Hung | 771386e | 2014-04-08 18:44:38 -0700 | [diff] [blame] | 616 | /* instantiate templates used by AudioResampler::create */ |
| 617 | template class AudioResamplerDyn<float, float, float>; |
| 618 | template class AudioResamplerDyn<int16_t, int16_t, int32_t>; |
| 619 | template class AudioResamplerDyn<int32_t, int16_t, int32_t>; |
| 620 | |
Andy Hung | 86eae0e | 2013-12-09 12:12:46 -0800 | [diff] [blame] | 621 | // ---------------------------------------------------------------------------- |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 622 | } // namespace android |