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
26namespace android {
27
28// ----------------------------------------------------------------------------
29
30class AudioResamplerSinc : public AudioResampler {
31public:
32    AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate);
33
34    ~AudioResamplerSinc();
35
36    virtual void resample(int32_t* out, size_t outFrameCount,
37            AudioBufferProvider* provider);
38private:
39    void init();
40
41    template<int CHANNELS>
42    void resample(int32_t* out, size_t outFrameCount,
43            AudioBufferProvider* provider);
44
45    template<int CHANNELS>
46    inline void filterCoefficient(
47            int32_t& l, int32_t& r, uint32_t phase, int16_t const *samples);
48
49    template<int CHANNELS>
50    inline void interpolate(
51            int32_t& l, int32_t& r,
52            int32_t const* coefs, int16_t lerp, int16_t const* samples);
53
54    template<int CHANNELS>
55    inline void read(int16_t*& impulse, uint32_t& phaseFraction,
56            int16_t const* in, size_t inputIndex);
57
58    int16_t *mState;
59    int16_t *mImpulse;
60    int16_t *mRingFull;
61
62    int32_t const * mFirCoefs;
63    static const int32_t mFirCoefsDown[];
64    static const int32_t mFirCoefsUp[];
65
66    // ----------------------------------------------------------------------------
67    static const int32_t RESAMPLE_FIR_NUM_COEF       = 8;
68    static const int32_t RESAMPLE_FIR_LERP_INT_BITS  = 4;
69
70    // we have 16 coefs samples per zero-crossing
71    static const int coefsBits = RESAMPLE_FIR_LERP_INT_BITS;        // 4
72    static const int cShift = kNumPhaseBits - coefsBits;            // 26
73    static const uint32_t cMask  = ((1<<coefsBits)-1) << cShift;    // 0xf<<26 = 3c00 0000
74
75    // and we use 15 bits to interpolate between these samples
76    // this cannot change because the mul below rely on it.
77    static const int pLerpBits = 15;
78    static const int pShift = kNumPhaseBits - coefsBits - pLerpBits;    // 11
79    static const uint32_t pMask  = ((1<<pLerpBits)-1) << pShift;    // 0x7fff << 11
80
81    // number of zero-crossing on each side
82    static const unsigned int halfNumCoefs = RESAMPLE_FIR_NUM_COEF;
83};
84
85// ----------------------------------------------------------------------------
86}; // namespace android
87
88#endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/
89