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 <math.h>
18 #include "IntegerRatio.h"
19 #include "PolyphaseResampler.h"
20
21 using namespace resampler;
22
PolyphaseResampler(const MultiChannelResampler::Builder & builder)23 PolyphaseResampler::PolyphaseResampler(const MultiChannelResampler::Builder &builder)
24 : MultiChannelResampler(builder)
25 {
26 assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
27
28 int32_t inputRate = builder.getInputRate();
29 int32_t outputRate = builder.getOutputRate();
30
31 int32_t numRows = mDenominator;
32 double phaseIncrement = (double) inputRate / (double) outputRate;
33 generateCoefficients(inputRate, outputRate,
34 numRows, phaseIncrement,
35 builder.getNormalizedCutoff());
36 }
37
readFrame(float * frame)38 void PolyphaseResampler::readFrame(float *frame) {
39 // Clear accumulator for mixing.
40 std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
41
42 // printf("PolyphaseResampler: mCoefficientCursor = %4d\n", mCoefficientCursor);
43 // Multiply input times windowed sinc function.
44 float *coefficients = &mCoefficients[mCoefficientCursor];
45 float *xFrame = &mX[mCursor * getChannelCount()];
46 for (int i = 0; i < mNumTaps; i++) {
47 float coefficient = *coefficients++;
48 // printf("PolyphaseResampler: coeff = %10.6f, xFrame[0] = %10.6f\n", coefficient, xFrame[0]);
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