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