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 <algorithm> // Do NOT delete. Needed for LLVM. See #1746
18 #include <cassert>
19 #include <math.h>
20 #include "SincResampler.h"
21
22 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
23
SincResampler(const MultiChannelResampler::Builder & builder)24 SincResampler::SincResampler(const MultiChannelResampler::Builder &builder)
25 : MultiChannelResampler(builder)
26 , mSingleFrame2(builder.getChannelCount()) {
27 assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
28 mNumRows = kMaxCoefficients / getNumTaps(); // includes guard row
29 const int32_t numRowsNoGuard = mNumRows - 1;
30 mPhaseScaler = (double) numRowsNoGuard / mDenominator;
31 const double phaseIncrement = 1.0 / numRowsNoGuard;
32 generateCoefficients(builder.getInputRate(),
33 builder.getOutputRate(),
34 mNumRows,
35 phaseIncrement,
36 builder.getNormalizedCutoff());
37 }
38
readFrame(float * frame)39 void SincResampler::readFrame(float *frame) {
40 // Clear accumulator for mixing.
41 std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
42 std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
43
44 // Determine indices into coefficients table.
45 const double tablePhase = getIntegerPhase() * mPhaseScaler;
46 const int indexLow = static_cast<int>(floor(tablePhase));
47 const int indexHigh = indexLow + 1; // OK because using a guard row.
48 assert (indexHigh < mNumRows);
49 float *coefficientsLow = &mCoefficients[static_cast<size_t>(indexLow)
50 * static_cast<size_t>(getNumTaps())];
51 float *coefficientsHigh = &mCoefficients[static_cast<size_t>(indexHigh)
52 * static_cast<size_t>(getNumTaps())];
53
54 float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
55 for (int tap = 0; tap < mNumTaps; tap++) {
56 const float coefficientLow = *coefficientsLow++;
57 const float coefficientHigh = *coefficientsHigh++;
58 for (int channel = 0; channel < getChannelCount(); channel++) {
59 const float sample = *xFrame++;
60 mSingleFrame[channel] += sample * coefficientLow;
61 mSingleFrame2[channel] += sample * coefficientHigh;
62 }
63 }
64
65 // Interpolate and copy to output.
66 const float fraction = tablePhase - indexLow;
67 for (int channel = 0; channel < getChannelCount(); channel++) {
68 const float low = mSingleFrame[channel];
69 const float high = mSingleFrame2[channel];
70 frame[channel] = low + (fraction * (high - low));
71 }
72 }
73