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