AudioResampler.h revision 63238efb0d674758902918e3cdaac322126484b7
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>
2236802bd18b7b4e8c87fa019c7e3068bee330d174Dan Albert
23e762be91c3280d837b1d48455cba90459ced7511Mathias Agopian#include <cutils/compiler.h>
2436802bd18b7b4e8c87fa019c7e3068bee330d174Dan Albert#include <utils/Compat.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
262dd4bdd715f586d4d30cf90cc6fc2bbfbce60fe0Glenn Kasten#include <media/AudioBufferProvider.h>
273348e36c51e91e78020bcc6578eda83d97c31becAndy Hung#include <system/audio.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32e762be91c3280d837b1d48455cba90459ced7511Mathias Agopianclass ANDROID_API AudioResampler {
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpublic:
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // Determines quality of SRC.
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  LOW_QUALITY: linear interpolator (1st order)
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  MED_QUALITY: cubic interpolator (3rd order)
3765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    //  HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // NOTE: high quality SRC will only be supported for
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // certain fixed rate conversions. Sample rate cannot be
40e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten    // changed dynamically.
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    enum src_quality {
42ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        DEFAULT_QUALITY=0,
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        LOW_QUALITY=1,
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        MED_QUALITY=2,
4576b111685010e1fea7c0a865c038aee35507fde4SathishKumar Mani        HIGH_QUALITY=3,
46ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten        VERY_HIGH_QUALITY=4,
4786eae0e5931103e040ac2cdd023ef5db252e09f6Andy Hung        DYN_LOW_QUALITY=5,
4886eae0e5931103e040ac2cdd023ef5db252e09f6Andy Hung        DYN_MED_QUALITY=6,
4986eae0e5931103e040ac2cdd023ef5db252e09f6Andy Hung        DYN_HIGH_QUALITY=7,
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5236802bd18b7b4e8c87fa019c7e3068bee330d174Dan Albert    static const CONSTEXPR float UNITY_GAIN_FLOAT = 1.0f;
535e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung
543348e36c51e91e78020bcc6578eda83d97c31becAndy Hung    static AudioResampler* create(audio_format_t format, int inChannelCount,
55ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten            int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
5665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual ~AudioResampler();
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void init() = 0;
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void setSampleRate(int32_t inSampleRate);
615e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung    virtual void setVolume(float left, float right);
624ff14bae91075eb274eb1c2975982358946e7e63John Grossman    virtual void setLocalTimeFreq(uint64_t freq);
634ff14bae91075eb274eb1c2975982358946e7e63John Grossman
644ff14bae91075eb274eb1c2975982358946e7e63John Grossman    // set the PTS of the next buffer output by the resampler
654ff14bae91075eb274eb1c2975982358946e7e63John Grossman    virtual void setPTS(int64_t pts);
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6734af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // Resample int16_t samples from provider and accumulate into 'out'.
6834af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // A mono provider delivers a sequence of samples.
6934af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // A stereo provider delivers a sequence of interleaved pairs of samples.
7034af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // Multi-channel providers are not supported.
7184a0c6e87c48f58a0d3be71961432c086a4d24ccAndy Hung    // In either case, 'out' holds interleaved pairs of fixed-point Q4.27.
7234af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // That is, for a mono provider, there is an implicit up-channeling.
7334af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // Since this method accumulates, the caller is responsible for clearing 'out' initially.
7434af02647b387a252fb02bab8e2cb9f7bd9c8abbGlenn Kasten    // FIXME assumes provider is always successful; it should return the actual frame count.
7565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    virtual void resample(int32_t* out, size_t outFrameCount,
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioBufferProvider* provider) = 0;
7765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent    virtual void reset();
79c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kasten    virtual size_t getUnreleasedFrames() const { return mInputIndex; }
80243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent
81ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    // called from destructor, so must not be virtual
82ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    src_quality getQuality() const { return mQuality; }
83ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianprotected:
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const int kNumPhaseBits = 30;
8765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // phase mask for fraction
8965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
9065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // multiplier to calculate fixed point phase increment
9201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten    static const double kPhaseMultiplier;
9365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
943348e36c51e91e78020bcc6578eda83d97c31becAndy Hung    AudioResampler(int inChannelCount, int32_t sampleRate, src_quality quality);
9565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
9665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // prevent copying
9765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResampler(const AudioResampler&);
9865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioResampler& operator=(const AudioResampler&);
9965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1004ff14bae91075eb274eb1c2975982358946e7e63John Grossman    int64_t calculateOutputPTS(int outputFrameIndex);
1014ff14bae91075eb274eb1c2975982358946e7e63John Grossman
102004f719467c498942c40de9f260be601ee45e630Glenn Kasten    const int32_t mChannelCount;
103004f719467c498942c40de9f260be601ee45e630Glenn Kasten    const int32_t mSampleRate;
10465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int32_t mInSampleRate;
10565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioBufferProvider::Buffer mBuffer;
10665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    union {
10765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        int16_t mVolume[2];
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t mVolumeRL;
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    };
11065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int16_t mTargetVolume[2];
11165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    size_t mInputIndex;
11265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int32_t mPhaseIncrement;
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    uint32_t mPhaseFraction;
1144ff14bae91075eb274eb1c2975982358946e7e63John Grossman    uint64_t mLocalTimeFreq;
1154ff14bae91075eb274eb1c2975982358946e7e63John Grossman    int64_t mPTS;
116ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten
11724781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // returns the inFrameCount required to generate outFrameCount frames.
11824781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
11924781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // Placed here to be a consistent for all resamplers.
12024781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
12124781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // Right now, we use the upper bound without regards to the current state of the
12224781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // input buffer using integer arithmetic, as follows:
12324781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
12424781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // (static_cast<uint64_t>(outFrameCount)*mInSampleRate + (mSampleRate - 1))/mSampleRate;
12524781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
12624781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // The double precision equivalent (float may not be precise enough):
12724781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // ceil(static_cast<double>(outFrameCount) * mInSampleRate / mSampleRate);
12824781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
12924781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // this relies on the fact that the mPhaseIncrement is rounded down from
13024781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // #phases * mInSampleRate/mSampleRate and the fact that Sum(Floor(x)) <= Floor(Sum(x)).
13124781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // http://www.proofwiki.org/wiki/Sum_of_Floors_Not_Greater_Than_Floor_of_Sums
13224781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
13324781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // (so long as double precision is computed accurately enough to be considered
13424781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // greater than or equal to the Floor(x) value in int32_t arithmetic; thus this
13524781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // will not necessarily hold for floats).
13624781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
13724781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // TODO:
13824781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // Greater accuracy and a tight bound is obtained by:
13924781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // 1) subtract and adjust for the current state of the AudioBufferProvider buffer.
14024781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    // 2) using the exact integer formula where (ignoring 64b casting)
14124781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //  inFrameCount = (mPhaseIncrement * (outFrameCount - 1) + mPhaseFraction) / phaseWrapLimit;
14224781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //  phaseWrapLimit is the wraparound (1 << kNumPhaseBits), if not specified explicitly.
14324781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    //
14424781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    inline size_t getInFrameCountRequired(size_t outFrameCount) {
14524781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung        return (static_cast<uint64_t>(outFrameCount)*mInSampleRate
14624781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung                + (mSampleRate - 1))/mSampleRate;
14724781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung    }
14824781fff62a4cf7279d3dac83c33e2ac612712baAndy Hung
1495e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung    inline float clampFloatVol(float volume) {
1505e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung        if (volume > UNITY_GAIN_FLOAT) {
1515e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung            return UNITY_GAIN_FLOAT;
1525e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung        } else if (volume >= 0.) {
1535e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung            return volume;
1545e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung        }
1555e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung        return 0.;  // NaN or negative volume maps to 0.
1565e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung    }
1575e58b0abe5b6c8f5bd96a8f78bbeeeb4d3892020Andy Hung
158ac6020508acedd316391dee42329040bf45f8d90Glenn Kastenprivate:
159ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    const src_quality mQuality;
160ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten
161ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    // Return 'true' if the quality level is supported without explicit request
162ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static bool qualityIsSupported(src_quality quality);
163ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten
164ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    // For pthread_once()
165ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static void init_routine();
166ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten
167ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    // Return the estimated CPU load for specific resampler in MHz.
168ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    // The absolute number is irrelevant, it's the relative values that matter.
169ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten    static uint32_t qualityMHz(src_quality quality);
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian};
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
17363238efb0d674758902918e3cdaac322126484b7Glenn Kasten} // namespace android
17465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
17565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#endif // ANDROID_AUDIO_RESAMPLER_H
176