1 /*
2 * Copyright 2019 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 #include <cassert>
18 #include <math.h>
19 #include "SincResampler.h"
20
21 using namespace resampler;
22
SincResampler(const MultiChannelResampler::Builder & builder)23 SincResampler::SincResampler(const MultiChannelResampler::Builder &builder)
24 : MultiChannelResampler(builder)
25 , mSingleFrame2(builder.getChannelCount()) {
26 assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
27 mNumRows = kMaxCoefficients / getNumTaps(); // no guard row needed
28 // printf("SincResampler: numRows = %d\n", mNumRows);
29 mPhaseScaler = (double) mNumRows / mDenominator;
30 double phaseIncrement = 1.0 / mNumRows;
31 generateCoefficients(builder.getInputRate(),
32 builder.getOutputRate(),
33 mNumRows,
34 phaseIncrement,
35 builder.getNormalizedCutoff());
36 }
37
readFrame(float * frame)38 void SincResampler::readFrame(float *frame) {
39 // Clear accumulator for mixing.
40 std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
41 std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
42
43 // Determine indices into coefficients table.
44 double tablePhase = getIntegerPhase() * mPhaseScaler;
45 int index1 = static_cast<int>(floor(tablePhase));
46 if (index1 >= mNumRows) { // no guard row needed because we wrap the indices
47 tablePhase -= mNumRows;
48 index1 -= mNumRows;
49 }
50
51 int index2 = index1 + 1;
52 if (index2 >= mNumRows) { // no guard row needed because we wrap the indices
53 index2 -= mNumRows;
54 }
55
56 float *coefficients1 = &mCoefficients[index1 * getNumTaps()];
57 float *coefficients2 = &mCoefficients[index2 * getNumTaps()];
58
59 float *xFrame = &mX[mCursor * getChannelCount()];
60 for (int i = 0; i < mNumTaps; i++) {
61 float coefficient1 = *coefficients1++;
62 float coefficient2 = *coefficients2++;
63 for (int channel = 0; channel < getChannelCount(); channel++) {
64 float sample = *xFrame++;
65 mSingleFrame[channel] += sample * coefficient1;
66 mSingleFrame2[channel] += sample * coefficient2;
67 }
68 }
69
70 // Interpolate and copy to output.
71 float fraction = tablePhase - index1;
72 for (int channel = 0; channel < getChannelCount(); channel++) {
73 float low = mSingleFrame[channel];
74 float high = mSingleFrame2[channel];
75 frame[channel] = low + (fraction * (high - low));
76 }
77 }
78