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>
18 #include <cassert>
19 #include <math.h>
20 #include "IntegerRatio.h"
21 #include "PolyphaseResampler.h"
22
23 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
24
PolyphaseResampler(const MultiChannelResampler::Builder & builder)25 PolyphaseResampler::PolyphaseResampler(const MultiChannelResampler::Builder &builder)
26 : MultiChannelResampler(builder)
27 {
28 assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
29
30 int32_t inputRate = builder.getInputRate();
31 int32_t outputRate = builder.getOutputRate();
32
33 int32_t numRows = mDenominator;
34 double phaseIncrement = (double) inputRate / (double) outputRate;
35 generateCoefficients(inputRate, outputRate,
36 numRows, phaseIncrement,
37 builder.getNormalizedCutoff());
38 }
39
readFrame(float * frame)40 void PolyphaseResampler::readFrame(float *frame) {
41 // Clear accumulator for mixing.
42 std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
43
44 // Multiply input times windowed sinc function.
45 float *coefficients = &mCoefficients[mCoefficientCursor];
46 float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
47 for (int i = 0; i < mNumTaps; i++) {
48 float coefficient = *coefficients++;
49 for (int channel = 0; channel < getChannelCount(); channel++) {
50 mSingleFrame[channel] += *xFrame++ * coefficient;
51 }
52 }
53
54 // Advance and wrap through coefficients.
55 mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
56
57 // Copy accumulator to output.
58 for (int channel = 0; channel < getChannelCount(); channel++) {
59 frame[channel] = mSingleFrame[channel];
60 }
61 }
62