blob: c53c66df90d87f7de45718b2114ab379ad9b7c52 [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
SathishKumar Mani76b11162012-01-17 10:49:47 -080028
29typedef const int32_t * (*readCoefficientsFn)(bool upDownSample);
30typedef int32_t (*readResampleFirNumCoeffFn)();
31typedef int32_t (*readResampleFirLerpIntBitsFn)();
32
Mathias Agopian65ab4712010-07-14 17:59:35 -070033// ----------------------------------------------------------------------------
34
35class AudioResamplerSinc : public AudioResampler {
36public:
SathishKumar Mani76b11162012-01-17 10:49:47 -080037 AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate, int32_t quality = HIGH_QUALITY);
Mathias Agopian65ab4712010-07-14 17:59:35 -070038
Glenn Kastenc19e2242012-01-30 14:54:39 -080039 virtual ~AudioResamplerSinc();
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
41 virtual void resample(int32_t* out, size_t outFrameCount,
42 AudioBufferProvider* provider);
43private:
44 void init();
45
46 template<int CHANNELS>
47 void resample(int32_t* out, size_t outFrameCount,
48 AudioBufferProvider* provider);
49
50 template<int CHANNELS>
51 inline void filterCoefficient(
Glenn Kasten54c3b662012-01-06 07:46:30 -080052 int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
54 template<int CHANNELS>
55 inline void interpolate(
56 int32_t& l, int32_t& r,
Glenn Kasten54c3b662012-01-06 07:46:30 -080057 const int32_t* coefs, int16_t lerp, const int16_t* samples);
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
59 template<int CHANNELS>
60 inline void read(int16_t*& impulse, uint32_t& phaseFraction,
Glenn Kasten54c3b662012-01-06 07:46:30 -080061 const int16_t* in, size_t inputIndex);
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
SathishKumar Mani76b11162012-01-17 10:49:47 -080063 readCoefficientsFn mReadResampleCoefficients ;
64 readResampleFirNumCoeffFn mReadResampleFirNumCoeff;
65 readResampleFirLerpIntBitsFn mReadResampleFirLerpIntBits;
66
Mathias Agopian65ab4712010-07-14 17:59:35 -070067 int16_t *mState;
68 int16_t *mImpulse;
69 int16_t *mRingFull;
70
Glenn Kasten54c3b662012-01-06 07:46:30 -080071 const int32_t * mFirCoefs;
Mathias Agopian65ab4712010-07-14 17:59:35 -070072 static const int32_t mFirCoefsDown[];
73 static const int32_t mFirCoefsUp[];
74
SathishKumar Mani76b11162012-01-17 10:49:47 -080075 void * mResampleCoeffLib;
Mathias Agopian65ab4712010-07-14 17:59:35 -070076 // ----------------------------------------------------------------------------
77 static const int32_t RESAMPLE_FIR_NUM_COEF = 8;
78 static const int32_t RESAMPLE_FIR_LERP_INT_BITS = 4;
79
80 // we have 16 coefs samples per zero-crossing
SathishKumar Mani76b11162012-01-17 10:49:47 -080081 static int coefsBits;
82 static int cShift;
83 static uint32_t cMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
85 // and we use 15 bits to interpolate between these samples
86 // this cannot change because the mul below rely on it.
87 static const int pLerpBits = 15;
SathishKumar Mani76b11162012-01-17 10:49:47 -080088 static int pShift;
89 static uint32_t pMask;
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
91 // number of zero-crossing on each side
SathishKumar Mani76b11162012-01-17 10:49:47 -080092 static unsigned int halfNumCoefs;
Mathias Agopian65ab4712010-07-14 17:59:35 -070093};
94
95// ----------------------------------------------------------------------------
96}; // namespace android
97
98#endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/