AudioResamplerSinc.h revision c19e22450e6e3d07594c935c7a9522e85e909e82
165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Copyright (C) 2007 The Android Open Source Project
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * you may not use this file except in compliance with the License.
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * You may obtain a copy of the License at
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Unless required by applicable law or agreed to in writing, software
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * See the License for the specific language governing permissions and
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * limitations under the License.
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian */
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#ifndef ANDROID_AUDIO_RESAMPLER_SINC_H
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ANDROID_AUDIO_RESAMPLER_SINC_H
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h>
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/log.h>
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioResampler.h"
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioResamplerSinc : public AudioResampler {
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResamplerSinc(int bitDepth, int inChannelCount, int32_t sampleRate);
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34c19e22450e6e3d07594c935c7a9522e85e909e82Glenn Kasten    virtual ~AudioResamplerSinc();
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void resample(int32_t* out, size_t outFrameCount,
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider);
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate:
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void init();
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void resample(int32_t* out, size_t outFrameCount,
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider);
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void filterCoefficient(
4754c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten            int32_t& l, int32_t& r, uint32_t phase, const int16_t *samples);
4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void interpolate(
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            int32_t& l, int32_t& r,
5254c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten            const int32_t* coefs, int16_t lerp, const int16_t* samples);
5365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
5565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void read(int16_t*& impulse, uint32_t& phaseFraction,
5654c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten            const int16_t* in, size_t inputIndex);
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mState;
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mImpulse;
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mRingFull;
6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6254c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten    const int32_t * mFirCoefs;
6365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int32_t mFirCoefsDown[];
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int32_t mFirCoefsUp[];
6565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // ----------------------------------------------------------------------------
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int32_t RESAMPLE_FIR_NUM_COEF       = 8;
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int32_t RESAMPLE_FIR_LERP_INT_BITS  = 4;
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // we have 16 coefs samples per zero-crossing
7165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int coefsBits = RESAMPLE_FIR_LERP_INT_BITS;        // 4
7265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int cShift = kNumPhaseBits - coefsBits;            // 26
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const uint32_t cMask  = ((1<<coefsBits)-1) << cShift;    // 0xf<<26 = 3c00 0000
7465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // and we use 15 bits to interpolate between these samples
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // this cannot change because the mul below rely on it.
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int pLerpBits = 15;
7865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int pShift = kNumPhaseBits - coefsBits - pLerpBits;    // 11
7965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const uint32_t pMask  = ((1<<pLerpBits)-1) << pShift;    // 0x7fff << 11
8065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // number of zero-crossing on each side
8265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const unsigned int halfNumCoefs = RESAMPLE_FIR_NUM_COEF;
8365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
8765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/
89