blob: f0a07b8de20e6d292e6a9671a429eb7d7beae364 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -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// ----------------------------------------------------------------------------
29
30class AudioResamplerSinc : public AudioResampler {
31public:
32 AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate);
33
Glenn Kastenc19e2242012-01-30 14:54:39 -080034 virtual ~AudioResamplerSinc();
Mathias Agopian65ab4712010-07-14 17:59:35 -070035
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(
Glenn Kasten54c3b662012-01-06 07:46:30 -080047 int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49 template<int CHANNELS>
50 inline void interpolate(
51 int32_t& l, int32_t& r,
Glenn Kasten54c3b662012-01-06 07:46:30 -080052 const int32_t* coefs, int16_t lerp, const int16_t* samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
54 template<int CHANNELS>
55 inline void read(int16_t*& impulse, uint32_t& phaseFraction,
Glenn Kasten54c3b662012-01-06 07:46:30 -080056 const int16_t* in, size_t inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58 int16_t *mState;
59 int16_t *mImpulse;
60 int16_t *mRingFull;
61
Glenn Kasten54c3b662012-01-06 07:46:30 -080062 const int32_t * mFirCoefs;
Mathias Agopian65ab4712010-07-14 17:59:35 -070063 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*/