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