AudioResampler.h revision 76b111685010e1fea7c0a865c038aee35507fde4
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_H
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define ANDROID_AUDIO_RESAMPLER_H
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h>
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
232dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/AudioBufferProvider.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianclass AudioResampler {
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Determines quality of SRC.
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  LOW_QUALITY: linear interpolator (1st order)
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  MED_QUALITY: cubic interpolator (3rd order)
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // NOTE: high quality SRC will only be supported for
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // certain fixed rate conversions. Sample rate cannot be
36e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten    // changed dynamically.
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    enum src_quality {
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        DEFAULT=0,
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        LOW_QUALITY=1,
4065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        MED_QUALITY=2,
4176b111685010e1fea7c0a865c038aee35507fde4SathishKumar Mani        HIGH_QUALITY=3,
4276b111685010e1fea7c0a865c038aee35507fde4SathishKumar Mani        VERY_HIGH_QUALITY=255
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static AudioResampler* create(int bitDepth, int inChannelCount,
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            int32_t sampleRate, int quality=DEFAULT);
4765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual ~AudioResampler();
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void init() = 0;
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void setSampleRate(int32_t inSampleRate);
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void setVolume(int16_t left, int16_t right);
534ff14bae91075eb274eb1c2975982358946e7e63John Grossman    virtual void setLocalTimeFreq(uint64_t freq);
544ff14bae91075eb274eb1c2975982358946e7e63John Grossman
554ff14bae91075eb274eb1c2975982358946e7e63John Grossman    // set the PTS of the next buffer output by the resampler
564ff14bae91075eb274eb1c2975982358946e7e63John Grossman    virtual void setPTS(int64_t pts);
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void resample(int32_t* out, size_t outFrameCount,
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider) = 0;
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent    virtual void reset();
62c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kasten    virtual size_t getUnreleasedFrames() const { return mInputIndex; }
63243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprotected:
6565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int kNumPhaseBits = 30;
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // phase mask for fraction
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
7065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // multiplier to calculate fixed point phase increment
7265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const double kPhaseMultiplier = 1L << kNumPhaseBits;
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResampler(int bitDepth, int inChannelCount, int32_t sampleRate);
7565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // prevent copying
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResampler(const AudioResampler&);
7865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResampler& operator=(const AudioResampler&);
7965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
804ff14bae91075eb274eb1c2975982358946e7e63John Grossman    int64_t calculateOutputPTS(int outputFrameIndex);
814ff14bae91075eb274eb1c2975982358946e7e63John Grossman
82004f719467c498942c40de9f260be601ee45e630Glenn Kasten    const int32_t mBitDepth;
83004f719467c498942c40de9f260be601ee45e630Glenn Kasten    const int32_t mChannelCount;
84004f719467c498942c40de9f260be601ee45e630Glenn Kasten    const int32_t mSampleRate;
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int32_t mInSampleRate;
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioBufferProvider::Buffer mBuffer;
8765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    union {
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int16_t mVolume[2];
8965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t mVolumeRL;
9065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
9165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t mTargetVolume[2];
9265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    size_t mInputIndex;
9365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int32_t mPhaseIncrement;
9465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    uint32_t mPhaseFraction;
954ff14bae91075eb274eb1c2975982358946e7e63John Grossman    uint64_t mLocalTimeFreq;
964ff14bae91075eb274eb1c2975982358946e7e63John Grossman    int64_t mPTS;
9765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
9865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
10065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
10165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian; // namespace android
10265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#endif // ANDROID_AUDIO_RESAMPLER_H
104