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
29typedef const int32_t * (*readCoefficientsFn)(bool upDownSample);
30typedef int32_t (*readResampleFirNumCoeffFn)();
31typedef int32_t (*readResampleFirLerpIntBitsFn)();
32
33// ----------------------------------------------------------------------------
34
35class AudioResamplerSinc : public AudioResampler {
36public:
37    AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate,
38            src_quality quality = HIGH_QUALITY);
39
40    virtual ~AudioResamplerSinc();
41
42    virtual void resample(int32_t* out, size_t outFrameCount,
43            AudioBufferProvider* provider);
44private:
45    void init();
46
47    template<int CHANNELS>
48    void resample(int32_t* out, size_t outFrameCount,
49            AudioBufferProvider* provider);
50
51    template<int CHANNELS>
52    inline void filterCoefficient(
53            int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples);
54
55    template<int CHANNELS>
56    inline void interpolate(
57            int32_t& l, int32_t& r,
58            const int32_t* coefs, int16_t lerp, const int16_t* samples);
59
60    template<int CHANNELS>
61    inline void read(int16_t*& impulse, uint32_t& phaseFraction,
62            const int16_t* in, size_t inputIndex);
63
64    int16_t *mState;
65    int16_t *mImpulse;
66    int16_t *mRingFull;
67
68    const int32_t * mFirCoefs;
69    static const int32_t mFirCoefsDown[];
70    static const int32_t mFirCoefsUp[];
71
72    // ----------------------------------------------------------------------------
73    static const int32_t RESAMPLE_FIR_NUM_COEF       = 8;
74    static const int32_t RESAMPLE_FIR_LERP_INT_BITS  = 4;
75
76    struct Constants {
77        // we have 16 coefs samples per zero-crossing
78        int coefsBits;
79        int cShift;
80        uint32_t cMask;
81
82        int pShift;
83        uint32_t pMask;
84
85        // number of zero-crossing on each side
86        unsigned int halfNumCoefs;
87    };
88
89    static Constants highQualityConstants;
90    static Constants veryHighQualityConstants;
91    const Constants *mConstants;    // points to appropriate set of coefficient parameters
92
93    static void init_routine();
94};
95
96// ----------------------------------------------------------------------------
97}; // namespace android
98
99#endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/
100