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_CUBIC_H
18 #define ANDROID_AUDIO_RESAMPLER_CUBIC_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <android/log.h>
23 
24 #include <media/AudioResampler.h>
25 
26 namespace android {
27 // ----------------------------------------------------------------------------
28 
29 class AudioResamplerCubic : public AudioResampler {
30 public:
AudioResamplerCubic(int inChannelCount,int32_t sampleRate)31     AudioResamplerCubic(int inChannelCount, int32_t sampleRate) :
32         AudioResampler(inChannelCount, sampleRate, MED_QUALITY) {
33     }
34     virtual size_t resample(int32_t* out, size_t outFrameCount,
35             AudioBufferProvider* provider);
36 private:
37     // number of bits used in interpolation multiply - 14 bits avoids overflow
38     static const int kNumInterpBits = 14;
39 
40     // bits to shift the phase fraction down to avoid overflow
41     static const int kPreInterpShift = kNumPhaseBits - kNumInterpBits;
42     typedef struct {
43         int32_t a, b, c, y0, y1, y2, y3;
44     } state;
45     void init();
46     size_t resampleMono16(int32_t* out, size_t outFrameCount,
47             AudioBufferProvider* provider);
48     size_t resampleStereo16(int32_t* out, size_t outFrameCount,
49             AudioBufferProvider* provider);
interp(state * p,int32_t x)50     static inline int32_t interp(state* p, int32_t x) {
51         return (((((p->a * x >> 14) + p->b) * x >> 14) + p->c) * x >> 14) + p->y1;
52     }
advance(state * p,int16_t in)53     static inline void advance(state* p, int16_t in) {
54         p->y0 = p->y1;
55         p->y1 = p->y2;
56         p->y2 = p->y3;
57         p->y3 = in;
58         p->a = (3 * (p->y1 - p->y2) - p->y0 + p->y3) >> 1;
59         p->b = (p->y2 << 1) + p->y0 - (((5 * p->y1 + p->y3)) >> 1);
60         p->c = (p->y2 - p->y0) >> 1;
61     }
62     state left, right;
63 };
64 
65 // ----------------------------------------------------------------------------
66 } // namespace android
67 
68 #endif /*ANDROID_AUDIO_RESAMPLER_CUBIC_H*/
69