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
2876b111685010e1fea7c0a865c038aee35507fde4SathishKumar Mani
2976b111685010e1fea7c0a865c038aee35507fde4SathishKumar Manitypedef const int32_t * (*readCoefficientsFn)(bool upDownSample);
30ac6020508acedd316391dee42329040bf45f8d90Glenn Kastentypedef int32_t (*readResampleFirNumCoeffFn)();
31ac6020508acedd316391dee42329040bf45f8d90Glenn Kastentypedef int32_t (*readResampleFirLerpIntBitsFn)();
3276b111685010e1fea7c0a865c038aee35507fde4SathishKumar Mani
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioResamplerSinc : public AudioResampler {
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
373348e36c51e91e78020bcc6578eda83d97c31becAndy Hung    AudioResamplerSinc(int inChannelCount, int32_t sampleRate,
38ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten            src_quality quality = HIGH_QUALITY);
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40c19e22450e6e3d07594c935c7a9522e85e909e82Glenn Kasten    virtual ~AudioResamplerSinc();
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
426b3b7e304e0f8f167241b2c75f1eb04a9ef192ecAndy Hung    virtual size_t resample(int32_t* out, size_t outFrameCount,
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider);
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprivate:
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void init();
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
475e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung    virtual void setVolume(float left, float right);
480d585c85524eb5d398fadff5ca8dd43939ed9cb4Mathias Agopian
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
506b3b7e304e0f8f167241b2c75f1eb04a9ef192ecAndy Hung    size_t resample(int32_t* out, size_t outFrameCount,
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider);
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void filterCoefficient(
550d585c85524eb5d398fadff5ca8dd43939ed9cb4Mathias Agopian            int32_t* out, uint32_t phase, const int16_t *samples, uint32_t vRL);
5665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void interpolate(
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            int32_t& l, int32_t& r,
6046afbec3743f1d799f185273ff897d1f8e0175ddMathias Agopian            const int32_t* coefs, size_t offset,
6146afbec3743f1d799f185273ff897d1f8e0175ddMathias Agopian            int32_t lerp, const int16_t* samples);
6265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    template<int CHANNELS>
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    inline void read(int16_t*& impulse, uint32_t& phaseFraction,
6554c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten            const int16_t* in, size_t inputIndex);
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mState;
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mImpulse;
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t *mRingFull;
700d585c85524eb5d398fadff5ca8dd43939ed9cb4Mathias Agopian    int32_t mVolumeSIMD[2];
7165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7254c3b66444ebfb9f2265ee70ac3b76ccefa0506aGlenn Kasten    const int32_t * mFirCoefs;
73c4974312e5a1e2ab94eca56045f991baf1508d73Glenn Kasten    static const uint32_t mFirCoefsDown[];
74c4974312e5a1e2ab94eca56045f991baf1508d73Glenn Kasten    static const uint32_t mFirCoefsUp[];
7565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // ----------------------------------------------------------------------------
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int32_t RESAMPLE_FIR_NUM_COEF       = 8;
78443e69625d598ea578e2c838960778ce498fd773Mathias Agopian    static const int32_t RESAMPLE_FIR_LERP_INT_BITS  = 7;
7965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    struct Constants {
81ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        int coefsBits;
82ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        int cShift;
83ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        uint32_t cMask;
84ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        int pShift;
85ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        uint32_t pMask;
86ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        // number of zero-crossing on each side
87ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        unsigned int halfNumCoefs;
88ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    };
8965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static Constants highQualityConstants;
91ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static Constants veryHighQualityConstants;
92ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    const Constants *mConstants;    // points to appropriate set of coefficient parameters
9365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
94ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static void init_routine();
9565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
9665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
9863238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
9965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#endif /*ANDROID_AUDIO_RESAMPLER_SINC_H*/
101