Home | History | Annotate | Download | only in audioflinger
      1 /*
      2  * Copyright (C) 2013 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_DYN_H
     18 #define ANDROID_AUDIO_RESAMPLER_DYN_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 #include <cutils/log.h>
     23 
     24 #include "AudioResampler.h"
     25 
     26 namespace android {
     27 
     28 /* AudioResamplerDyn
     29  *
     30  * This class template is used for floating point and integer resamplers.
     31  *
     32  * Type variables:
     33  * TC = filter coefficient type (one of int16_t, int32_t, or float)
     34  * TI = input data type (one of int16_t or float)
     35  * TO = output data type (one of int32_t or float)
     36  *
     37  * For integer input data types TI, the coefficient type TC is either int16_t or int32_t.
     38  * For float input data types TI, the coefficient type TC is float.
     39  */
     40 
     41 template<typename TC, typename TI, typename TO>
     42 class AudioResamplerDyn: public AudioResampler {
     43 public:
     44     AudioResamplerDyn(int inChannelCount,
     45             int32_t sampleRate, src_quality quality);
     46 
     47     virtual ~AudioResamplerDyn();
     48 
     49     virtual void init();
     50 
     51     virtual void setSampleRate(int32_t inSampleRate);
     52 
     53     virtual void setVolume(float left, float right);
     54 
     55     virtual size_t resample(int32_t* out, size_t outFrameCount,
     56             AudioBufferProvider* provider);
     57 
     58 private:
     59 
     60     class Constants { // stores the filter constants.
     61     public:
     62         Constants() :
     63             mL(0), mShift(0), mHalfNumCoefs(0), mFirCoefs(NULL)
     64         {}
     65         void set(int L, int halfNumCoefs,
     66                 int inSampleRate, int outSampleRate);
     67 
     68                  int mL;            // interpolation phases in the filter.
     69                  int mShift;        // right shift to get polyphase index
     70         unsigned int mHalfNumCoefs; // filter half #coefs
     71            const TC* mFirCoefs;     // polyphase filter bank
     72     };
     73 
     74     class InBuffer { // buffer management for input type TI
     75     public:
     76         InBuffer();
     77         ~InBuffer();
     78         void init();
     79 
     80         void resize(int CHANNELS, int halfNumCoefs);
     81 
     82         // used for direct management of the mImpulse pointer
     83         inline TI* getImpulse() {
     84             return mImpulse;
     85         }
     86 
     87         inline void setImpulse(TI *impulse) {
     88             mImpulse = impulse;
     89         }
     90 
     91         template<int CHANNELS>
     92         inline void readAgain(TI*& impulse, const int halfNumCoefs,
     93                 const TI* const in, const size_t inputIndex);
     94 
     95         template<int CHANNELS>
     96         inline void readAdvance(TI*& impulse, const int halfNumCoefs,
     97                 const TI* const in, const size_t inputIndex);
     98 
     99     private:
    100         // tuning parameter guidelines: 2 <= multiple <= 8
    101         static const int kStateSizeMultipleOfFilterLength = 4;
    102 
    103         // in general, mRingFull = mState + mStateSize - halfNumCoefs*CHANNELS.
    104            TI* mState;      // base pointer for the input buffer storage
    105            TI* mImpulse;    // current location of the impulse response (centered)
    106            TI* mRingFull;   // mState <= mImpulse < mRingFull
    107         size_t mStateCount; // size of state in units of TI.
    108     };
    109 
    110     void createKaiserFir(Constants &c, double stopBandAtten,
    111             int inSampleRate, int outSampleRate, double tbwCheat);
    112 
    113     template<int CHANNELS, bool LOCKED, int STRIDE>
    114     size_t resample(TO* out, size_t outFrameCount, AudioBufferProvider* provider);
    115 
    116     // define a pointer to member function type for resample
    117     typedef size_t (AudioResamplerDyn<TC, TI, TO>::*resample_ABP_t)(TO* out,
    118             size_t outFrameCount, AudioBufferProvider* provider);
    119 
    120     // data - the contiguous storage and layout of these is important.
    121            InBuffer mInBuffer;
    122           Constants mConstants;        // current set of coefficient parameters
    123     TO __attribute__ ((aligned (8))) mVolumeSimd[2]; // must be aligned or NEON may crash
    124      resample_ABP_t mResampleFunc;     // called function for resampling
    125             int32_t mFilterSampleRate; // designed filter sample rate.
    126         src_quality mFilterQuality;    // designed filter quality.
    127               void* mCoefBuffer;       // if a filter is created, this is not null
    128 };
    129 
    130 } // namespace android
    131 
    132 #endif /*ANDROID_AUDIO_RESAMPLER_DYN_H*/
    133