AudioResamplerDyn.h revision 6582f2b14a21e630654c5522ef9ad64e80d5058d
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#ifndef ANDROID_AUDIO_RESAMPLER_DYN_H
18#define ANDROID_AUDIO_RESAMPLER_DYN_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
28class AudioResamplerDyn: public AudioResampler {
29public:
30    AudioResamplerDyn(int bitDepth, int inChannelCount, int32_t sampleRate,
31            src_quality quality);
32
33    virtual ~AudioResamplerDyn();
34
35    virtual void init();
36
37    virtual void setSampleRate(int32_t inSampleRate);
38
39    virtual void setVolume(int16_t left, int16_t right);
40
41    virtual void resample(int32_t* out, size_t outFrameCount,
42            AudioBufferProvider* provider);
43
44private:
45
46    class Constants { // stores the filter constants.
47    public:
48        Constants() :
49            mL(0), mShift(0), mHalfNumCoefs(0), mFirCoefsS16(NULL)
50        {}
51        void set(int L, int halfNumCoefs,
52                int inSampleRate, int outSampleRate);
53        inline void setBuf(int16_t* buf) {
54            mFirCoefsS16 = buf;
55        }
56        inline void setBuf(int32_t* buf) {
57            mFirCoefsS32 = buf;
58        }
59
60        int mL;       // interpolation phases in the filter.
61        int mShift;   // right shift to get polyphase index
62        unsigned int mHalfNumCoefs; // filter half #coefs
63        union {       // polyphase filter bank
64            const int16_t* mFirCoefsS16;
65            const int32_t* mFirCoefsS32;
66        };
67    };
68
69    // Input buffer management for a given input type TI, now (int16_t)
70    // Is agnostic of the actual type, can work with int32_t and float.
71    template<typename TI>
72    class InBuffer {
73    public:
74        InBuffer();
75        ~InBuffer();
76        void init();
77        void resize(int CHANNELS, int halfNumCoefs);
78
79        // used for direct management of the mImpulse pointer
80        inline TI* getImpulse() {
81            return mImpulse;
82        }
83        inline void setImpulse(TI *impulse) {
84            mImpulse = impulse;
85        }
86        template<int CHANNELS>
87        inline void readAgain(TI*& impulse, const int halfNumCoefs,
88                const TI* const in, const size_t inputIndex);
89        template<int CHANNELS>
90        inline void readAdvance(TI*& impulse, const int halfNumCoefs,
91                const TI* const in, const size_t inputIndex);
92
93    private:
94        // tuning parameter guidelines: 2 <= multiple <= 8
95        static const int kStateSizeMultipleOfFilterLength = 4;
96
97        TI* mState;    // base pointer for the input buffer storage
98        TI* mImpulse;  // current location of the impulse response (centered)
99        TI* mRingFull; // mState <= mImpulse < mRingFull
100        // in general, mRingFull = mState + mStateSize - halfNumCoefs*CHANNELS.
101        size_t mStateSize; // in units of TI.
102    };
103
104    template<int CHANNELS, bool LOCKED, int STRIDE, typename TC>
105    void resample(int32_t* out, size_t outFrameCount,
106            const TC* const coefs, AudioBufferProvider* provider);
107
108    template<typename T>
109    void createKaiserFir(Constants &c, double stopBandAtten,
110            int inSampleRate, int outSampleRate, double tbwCheat);
111
112    InBuffer<int16_t> mInBuffer;
113    Constants mConstants;  // current set of coefficient parameters
114    int32_t __attribute__ ((aligned (8))) mVolumeSimd[2];
115    int32_t mResampleType; // contains the resample type.
116    int32_t mFilterSampleRate; // designed filter sample rate.
117    src_quality mFilterQuality; // designed filter quality.
118    void* mCoefBuffer; // if a filter is created, this is not null
119};
120
121// ----------------------------------------------------------------------------
122}; // namespace android
123
124#endif /*ANDROID_AUDIO_RESAMPLER_DYN_H*/
125