blob: 9bcd8e1c36092036dd2e9c801a0556928282ace4 [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
Andy Hung6b3b7e32015-03-29 00:49:22 -070017#define LOG_TAG "AudioResamplerCubic"
Glenn Kasten5159c7e2011-07-08 09:32:50 -070018
Mathias Agopian65ab4712010-07-14 17:59:35 -070019#include <stdint.h>
20#include <string.h>
21#include <sys/types.h>
Mark Salyzyneb165612017-01-10 09:08:19 -080022
23#include <log/log.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include "AudioResamplerCubic.h"
26
Mathias Agopian65ab4712010-07-14 17:59:35 -070027namespace android {
28// ----------------------------------------------------------------------------
29
30void AudioResamplerCubic::init() {
31 memset(&left, 0, sizeof(state));
32 memset(&right, 0, sizeof(state));
33}
34
Andy Hung6b3b7e32015-03-29 00:49:22 -070035size_t AudioResamplerCubic::resample(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070036 AudioBufferProvider* provider) {
37
38 // should never happen, but we overflow if it does
Steve Blockc1dc1cb2012-01-09 18:35:44 +000039 // ALOG_ASSERT(outFrameCount < 32767);
Mathias Agopian65ab4712010-07-14 17:59:35 -070040
41 // select the appropriate resampler
42 switch (mChannelCount) {
43 case 1:
Andy Hung6b3b7e32015-03-29 00:49:22 -070044 return resampleMono16(out, outFrameCount, provider);
Mathias Agopian65ab4712010-07-14 17:59:35 -070045 case 2:
Andy Hung6b3b7e32015-03-29 00:49:22 -070046 return resampleStereo16(out, outFrameCount, provider);
47 default:
48 LOG_ALWAYS_FATAL("invalid channel count: %d", mChannelCount);
49 return 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070050 }
51}
52
Andy Hung6b3b7e32015-03-29 00:49:22 -070053size_t AudioResamplerCubic::resampleStereo16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -070054 AudioBufferProvider* provider) {
55
56 int32_t vl = mVolume[0];
57 int32_t vr = mVolume[1];
58
59 size_t inputIndex = mInputIndex;
60 uint32_t phaseFraction = mPhaseFraction;
61 uint32_t phaseIncrement = mPhaseIncrement;
62 size_t outputIndex = 0;
63 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -080064 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -070065
66 // fetch first buffer
67 if (mBuffer.frameCount == 0) {
68 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -080069 provider->getNextBuffer(&mBuffer);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070070 if (mBuffer.raw == NULL) {
Andy Hung6b3b7e32015-03-29 00:49:22 -070071 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -070072 }
Steve Block5ff1dd52012-01-05 23:22:43 +000073 // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 }
75 int16_t *in = mBuffer.i16;
76
77 while (outputIndex < outputSampleCount) {
Mathias Agopian65ab4712010-07-14 17:59:35 -070078 int32_t x;
79
80 // calculate output sample
81 x = phaseFraction >> kPreInterpShift;
82 out[outputIndex++] += vl * interp(&left, x);
83 out[outputIndex++] += vr * interp(&right, x);
84 // out[outputIndex++] += vr * in[inputIndex*2];
85
86 // increment phase
87 phaseFraction += phaseIncrement;
88 uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits);
89 phaseFraction &= kPhaseMask;
90
91 // time to fetch another sample
92 while (indexIncrement--) {
93
94 inputIndex++;
95 if (inputIndex == mBuffer.frameCount) {
96 inputIndex = 0;
97 provider->releaseBuffer(&mBuffer);
98 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -080099 provider->getNextBuffer(&mBuffer);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700100 if (mBuffer.raw == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101 goto save_state; // ugly, but efficient
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700102 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103 in = mBuffer.i16;
Glenn Kasten90bebef2012-01-27 15:24:38 -0800104 // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105 }
106
107 // advance sample state
108 advance(&left, in[inputIndex*2]);
109 advance(&right, in[inputIndex*2+1]);
110 }
111 }
112
113save_state:
Steve Block5ff1dd52012-01-05 23:22:43 +0000114 // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 mInputIndex = inputIndex;
116 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700117 return outputIndex / 2 /* channels for stereo */;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118}
119
Andy Hung6b3b7e32015-03-29 00:49:22 -0700120size_t AudioResamplerCubic::resampleMono16(int32_t* out, size_t outFrameCount,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700121 AudioBufferProvider* provider) {
122
123 int32_t vl = mVolume[0];
124 int32_t vr = mVolume[1];
125
126 size_t inputIndex = mInputIndex;
127 uint32_t phaseFraction = mPhaseFraction;
128 uint32_t phaseIncrement = mPhaseIncrement;
129 size_t outputIndex = 0;
130 size_t outputSampleCount = outFrameCount * 2;
Andy Hung24781ff2014-02-19 12:45:19 -0800131 size_t inFrameCount = getInFrameCountRequired(outFrameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132
133 // fetch first buffer
134 if (mBuffer.frameCount == 0) {
135 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800136 provider->getNextBuffer(&mBuffer);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700137 if (mBuffer.raw == NULL) {
Andy Hung6b3b7e32015-03-29 00:49:22 -0700138 return 0;
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700139 }
Glenn Kasten90bebef2012-01-27 15:24:38 -0800140 // ALOGW("New buffer: offset=%p, frames=%d", mBuffer.raw, mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 }
142 int16_t *in = mBuffer.i16;
143
144 while (outputIndex < outputSampleCount) {
145 int32_t sample;
146 int32_t x;
147
148 // calculate output sample
149 x = phaseFraction >> kPreInterpShift;
150 sample = interp(&left, x);
151 out[outputIndex++] += vl * sample;
152 out[outputIndex++] += vr * sample;
153
154 // increment phase
155 phaseFraction += phaseIncrement;
156 uint32_t indexIncrement = (phaseFraction >> kNumPhaseBits);
157 phaseFraction &= kPhaseMask;
158
159 // time to fetch another sample
160 while (indexIncrement--) {
161
162 inputIndex++;
163 if (inputIndex == mBuffer.frameCount) {
164 inputIndex = 0;
165 provider->releaseBuffer(&mBuffer);
166 mBuffer.frameCount = inFrameCount;
Glenn Kastend79072e2016-01-06 08:41:20 -0800167 provider->getNextBuffer(&mBuffer);
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700168 if (mBuffer.raw == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 goto save_state; // ugly, but efficient
Glenn Kasten6e2ebe92013-08-13 09:14:51 -0700170 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000171 // ALOGW("New buffer: offset=%p, frames=%dn", mBuffer.raw, mBuffer.frameCount);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172 in = mBuffer.i16;
173 }
174
175 // advance sample state
176 advance(&left, in[inputIndex]);
177 }
178 }
179
180save_state:
Steve Block5ff1dd52012-01-05 23:22:43 +0000181 // ALOGW("Done: index=%d, fraction=%u", inputIndex, phaseFraction);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 mInputIndex = inputIndex;
183 mPhaseFraction = phaseFraction;
Andy Hung6b3b7e32015-03-29 00:49:22 -0700184 return outputIndex;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185}
186
187// ----------------------------------------------------------------------------
Glenn Kasten63238ef2015-03-02 15:50:29 -0800188} // namespace android