blob: defaf33ad4dc3598411c3c4bf0a0ff25282169f0 [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_CUBIC_H
18#define ANDROID_AUDIO_RESAMPLER_CUBIC_H
19
20#include <stdint.h>
21#include <sys/types.h>
Mark Salyzyn60d02072016-09-29 08:48:48 -070022#include <android/log.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023
Andy Hung068561c2017-01-03 17:09:32 -080024#include <media/AudioResampler.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025
26namespace android {
27// ----------------------------------------------------------------------------
28
29class AudioResamplerCubic : public AudioResampler {
30public:
Andy Hung3348e362014-07-07 10:21:44 -070031 AudioResamplerCubic(int inChannelCount, int32_t sampleRate) :
32 AudioResampler(inChannelCount, sampleRate, MED_QUALITY) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070033 }
Andy Hung6b3b7e32015-03-29 00:49:22 -070034 virtual size_t resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070035 AudioBufferProvider* provider);
36private:
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();
Andy Hung6b3b7e32015-03-29 00:49:22 -070046 size_t resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070047 AudioBufferProvider* provider);
Andy Hung6b3b7e32015-03-29 00:49:22 -070048 size_t resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070049 AudioBufferProvider* provider);
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 }
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;
Glenn Kastene53b9ea2012-03-12 16:29:55 -070058 p->a = (3 * (p->y1 - p->y2) - p->y0 + p->y3) >> 1;
Mathias Agopian65ab4712010-07-14 17:59:35 -070059 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// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -080066} // namespace android
Mathias Agopian65ab4712010-07-14 17:59:35 -070067
68#endif /*ANDROID_AUDIO_RESAMPLER_CUBIC_H*/