Home | History | Annotate | Download | only in audioflinger
      1 /*
      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_H
     18 #define ANDROID_AUDIO_RESAMPLER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 #include <cutils/compiler.h>
     23 
     24 #include <media/AudioBufferProvider.h>
     25 
     26 namespace android {
     27 // ----------------------------------------------------------------------------
     28 
     29 class ANDROID_API AudioResampler {
     30 public:
     31     // Determines quality of SRC.
     32     //  LOW_QUALITY: linear interpolator (1st order)
     33     //  MED_QUALITY: cubic interpolator (3rd order)
     34     //  HIGH_QUALITY: fixed multi-tap FIR (e.g. 48KHz->44.1KHz)
     35     // NOTE: high quality SRC will only be supported for
     36     // certain fixed rate conversions. Sample rate cannot be
     37     // changed dynamically.
     38     enum src_quality {
     39         DEFAULT_QUALITY=0,
     40         LOW_QUALITY=1,
     41         MED_QUALITY=2,
     42         HIGH_QUALITY=3,
     43         VERY_HIGH_QUALITY=4,
     44     };
     45 
     46     static AudioResampler* create(int bitDepth, int inChannelCount,
     47             int32_t sampleRate, src_quality quality=DEFAULT_QUALITY);
     48 
     49     virtual ~AudioResampler();
     50 
     51     virtual void init() = 0;
     52     virtual void setSampleRate(int32_t inSampleRate);
     53     virtual void setVolume(int16_t left, int16_t right);
     54     virtual void setLocalTimeFreq(uint64_t freq);
     55 
     56     // set the PTS of the next buffer output by the resampler
     57     virtual void setPTS(int64_t pts);
     58 
     59     // Resample int16_t samples from provider and accumulate into 'out'.
     60     // A mono provider delivers a sequence of samples.
     61     // A stereo provider delivers a sequence of interleaved pairs of samples.
     62     // Multi-channel providers are not supported.
     63     // In either case, 'out' holds interleaved pairs of fixed-point signed Q19.12.
     64     // That is, for a mono provider, there is an implicit up-channeling.
     65     // Since this method accumulates, the caller is responsible for clearing 'out' initially.
     66     // FIXME assumes provider is always successful; it should return the actual frame count.
     67     virtual void resample(int32_t* out, size_t outFrameCount,
     68             AudioBufferProvider* provider) = 0;
     69 
     70     virtual void reset();
     71     virtual size_t getUnreleasedFrames() const { return mInputIndex; }
     72 
     73     // called from destructor, so must not be virtual
     74     src_quality getQuality() const { return mQuality; }
     75 
     76 protected:
     77     // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
     78     static const int kNumPhaseBits = 30;
     79 
     80     // phase mask for fraction
     81     static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
     82 
     83     // multiplier to calculate fixed point phase increment
     84     static const double kPhaseMultiplier = 1L << kNumPhaseBits;
     85 
     86     AudioResampler(int bitDepth, int inChannelCount, int32_t sampleRate, src_quality quality);
     87 
     88     // prevent copying
     89     AudioResampler(const AudioResampler&);
     90     AudioResampler& operator=(const AudioResampler&);
     91 
     92     int64_t calculateOutputPTS(int outputFrameIndex);
     93 
     94     const int32_t mBitDepth;
     95     const int32_t mChannelCount;
     96     const int32_t mSampleRate;
     97     int32_t mInSampleRate;
     98     AudioBufferProvider::Buffer mBuffer;
     99     union {
    100         int16_t mVolume[2];
    101         uint32_t mVolumeRL;
    102     };
    103     int16_t mTargetVolume[2];
    104     size_t mInputIndex;
    105     int32_t mPhaseIncrement;
    106     uint32_t mPhaseFraction;
    107     uint64_t mLocalTimeFreq;
    108     int64_t mPTS;
    109 
    110 private:
    111     const src_quality mQuality;
    112 
    113     // Return 'true' if the quality level is supported without explicit request
    114     static bool qualityIsSupported(src_quality quality);
    115 
    116     // For pthread_once()
    117     static void init_routine();
    118 
    119     // Return the estimated CPU load for specific resampler in MHz.
    120     // The absolute number is irrelevant, it's the relative values that matter.
    121     static uint32_t qualityMHz(src_quality quality);
    122 };
    123 
    124 // ----------------------------------------------------------------------------
    125 }
    126 ; // namespace android
    127 
    128 #endif // ANDROID_AUDIO_RESAMPLER_H
    129