blob: 2b1488b3c8b3589991719b2acb302ccb0a15f5e5 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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 "AudioBufferProvider.h"
24
25namespace android {
26// ----------------------------------------------------------------------------
27
28class AudioResampler {
29public:
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=0,
39 LOW_QUALITY=1,
40 MED_QUALITY=2,
41 HIGH_QUALITY=3
42 };
43
44 static AudioResampler* create(int bitDepth, int inChannelCount,
45 int32_t sampleRate, int quality=DEFAULT);
46
47 virtual ~AudioResampler();
48
49 virtual void init() = 0;
50 virtual void setSampleRate(int32_t inSampleRate);
51 virtual void setVolume(int16_t left, int16_t right);
Mike J. Chen500e5102011-08-15 13:28:26 -070052 virtual void setLocalTimeFreq(uint64_t freq);
53
54 // set the PTS of the next buffer output by the resampler
55 virtual void setPTS(int64_t pts);
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57 virtual void resample(int32_t* out, size_t outFrameCount,
58 AudioBufferProvider* provider) = 0;
59
Eric Laurent243f5f92011-02-28 16:52:51 -080060 virtual void reset();
Eric Laurent071ccd52011-12-22 16:08:41 -080061 virtual size_t getUnreleasedFrames() { return mInputIndex; }
Eric Laurent243f5f92011-02-28 16:52:51 -080062
Mathias Agopian65ab4712010-07-14 17:59:35 -070063protected:
64 // number of bits for phase fraction - 30 bits allows nearly 2x downsampling
65 static const int kNumPhaseBits = 30;
66
67 // phase mask for fraction
68 static const uint32_t kPhaseMask = (1LU<<kNumPhaseBits)-1;
69
70 // multiplier to calculate fixed point phase increment
71 static const double kPhaseMultiplier = 1L << kNumPhaseBits;
72
73 enum format {MONO_16_BIT, STEREO_16_BIT};
74 AudioResampler(int bitDepth, int inChannelCount, int32_t sampleRate);
75
76 // prevent copying
77 AudioResampler(const AudioResampler&);
78 AudioResampler& operator=(const AudioResampler&);
79
Mike J. Chen500e5102011-08-15 13:28:26 -070080 int64_t calculateOutputPTS(int outputFrameIndex);
81
Mathias Agopian65ab4712010-07-14 17:59:35 -070082 int32_t mBitDepth;
83 int32_t mChannelCount;
84 int32_t mSampleRate;
85 int32_t mInSampleRate;
86 AudioBufferProvider::Buffer mBuffer;
87 union {
88 int16_t mVolume[2];
89 uint32_t mVolumeRL;
90 };
91 int16_t mTargetVolume[2];
92 format mFormat;
93 size_t mInputIndex;
94 int32_t mPhaseIncrement;
95 uint32_t mPhaseFraction;
Mike J. Chen500e5102011-08-15 13:28:26 -070096 uint64_t mLocalTimeFreq;
97 int64_t mPTS;
Mathias Agopian65ab4712010-07-14 17:59:35 -070098};
99
100// ----------------------------------------------------------------------------
101}
102; // namespace android
103
104#endif // ANDROID_AUDIO_RESAMPLER_H