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_OUTER_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(); // includes guard row
28 const int32_t numRowsNoGuard = mNumRows - 1;
29 mPhaseScaler = (double) numRowsNoGuard / mDenominator;
30 const double phaseIncrement = 1.0 / numRowsNoGuard;
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 const double tablePhase = getIntegerPhase() * mPhaseScaler;
45 const int indexLow = static_cast<int>(floor(tablePhase));
46 const int indexHigh = indexLow + 1; // OK because using a guard row.
47 assert (indexHigh < mNumRows);
48 float *coefficientsLow = &mCoefficients[static_cast<size_t>(indexLow)
49 * static_cast<size_t>(getNumTaps())];
50 float *coefficientsHigh = &mCoefficients[static_cast<size_t>(indexHigh)
51 * static_cast<size_t>(getNumTaps())];
52
53 float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
54 for (int tap = 0; tap < mNumTaps; tap++) {
55 const float coefficientLow = *coefficientsLow++;
56 const float coefficientHigh = *coefficientsHigh++;
57 for (int channel = 0; channel < getChannelCount(); channel++) {
58 const float sample = *xFrame++;
59 mSingleFrame[channel] += sample * coefficientLow;
60 mSingleFrame2[channel] += sample * coefficientHigh;
61 }
62 }
63
64 // Interpolate and copy to output.
65 const float fraction = tablePhase - indexLow;
66 for (int channel = 0; channel < getChannelCount(); channel++) {
67 const float low = mSingleFrame[channel];
68 const float high = mSingleFrame2[channel];
69 frame[channel] = low + (fraction * (high - low));
70 }
71 }
72