• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SincResampler.h"
19 
20 using namespace resampler;
21 
SincResampler(const MultiChannelResampler::Builder & builder)22 SincResampler::SincResampler(const MultiChannelResampler::Builder &builder)
23         : MultiChannelResampler(builder)
24         , mSingleFrame2(builder.getChannelCount()) {
25     assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
26     mNumRows = kMaxCoefficients / getNumTaps(); // no guard row needed
27 //    printf("SincResampler: numRows = %d\n", mNumRows);
28     mPhaseScaler = (double) mNumRows / mDenominator;
29     double phaseIncrement = 1.0 / mNumRows;
30     generateCoefficients(builder.getInputRate(),
31                          builder.getOutputRate(),
32                          mNumRows,
33                          phaseIncrement,
34                          builder.getNormalizedCutoff());
35 }
36 
readFrame(float * frame)37 void SincResampler::readFrame(float *frame) {
38     // Clear accumulator for mixing.
39     std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
40     std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
41 
42     // Determine indices into coefficients table.
43     double tablePhase = getIntegerPhase() * mPhaseScaler;
44     int index1 = static_cast<int>(floor(tablePhase));
45     if (index1 >= mNumRows) { // no guard row needed because we wrap the indices
46         tablePhase -= mNumRows;
47         index1 -= mNumRows;
48     }
49 
50     int index2 = index1 + 1;
51     if (index2 >= mNumRows) { // no guard row needed because we wrap the indices
52         index2 -= mNumRows;
53     }
54 
55     float *coefficients1 = &mCoefficients[index1 * getNumTaps()];
56     float *coefficients2 = &mCoefficients[index2 * getNumTaps()];
57 
58     float *xFrame = &mX[mCursor * getChannelCount()];
59     for (int i = 0; i < mNumTaps; i++) {
60         float coefficient1 = *coefficients1++;
61         float coefficient2 = *coefficients2++;
62         for (int channel = 0; channel < getChannelCount(); channel++) {
63             float sample = *xFrame++;
64             mSingleFrame[channel] +=  sample * coefficient1;
65             mSingleFrame2[channel] += sample * coefficient2;
66         }
67     }
68 
69     // Interpolate and copy to output.
70     float fraction = tablePhase - index1;
71     for (int channel = 0; channel < getChannelCount(); channel++) {
72         float low = mSingleFrame[channel];
73         float high = mSingleFrame2[channel];
74         frame[channel] = low + (fraction * (high - low));
75     }
76 }
77