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;
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 //    printf("PolyphaseResampler: mCoefficientCursor = %4d\n", mCoefficientCursor);
44     // Multiply input times windowed sinc function.
45     float *coefficients = &mCoefficients[mCoefficientCursor];
46     float *xFrame = &mX[mCursor * getChannelCount()];
47     for (int i = 0; i < mNumTaps; i++) {
48         float coefficient = *coefficients++;
49 //        printf("PolyphaseResampler: coeff = %10.6f, xFrame[0] = %10.6f\n", coefficient, xFrame[0]);
50         for (int channel = 0; channel < getChannelCount(); channel++) {
51             mSingleFrame[channel] += *xFrame++ * coefficient;
52         }
53     }
54 
55     // Advance and wrap through coefficients.
56     mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
57 
58     // Copy accumulator to output.
59     for (int channel = 0; channel < getChannelCount(); channel++) {
60         frame[channel] = mSingleFrame[channel];
61     }
62 }
63