• 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 <cassert>
18 #include <math.h>
19 #include "SincResampler.h"
20 
21 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
22 
SincResampler(const MultiChannelResampler::Builder & builder)23 SincResampler::SincResampler(const MultiChannelResampler::Builder &builder)
24         : MultiChannelResampler(builder)
25         , mSingleFrame2(builder.getChannelCount()) {
26     assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
27     mNumRows = kMaxCoefficients / getNumTaps(); // no guard row needed
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[static_cast<size_t>(index1)
56             * static_cast<size_t>(getNumTaps())];
57     float *coefficients2 = &mCoefficients[static_cast<size_t>(index2)
58             * static_cast<size_t>(getNumTaps())];
59 
60     float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
61     for (int i = 0; i < mNumTaps; i++) {
62         float coefficient1 = *coefficients1++;
63         float coefficient2 = *coefficients2++;
64         for (int channel = 0; channel < getChannelCount(); channel++) {
65             float sample = *xFrame++;
66             mSingleFrame[channel] +=  sample * coefficient1;
67             mSingleFrame2[channel] += sample * coefficient2;
68         }
69     }
70 
71     // Interpolate and copy to output.
72     float fraction = tablePhase - index1;
73     for (int channel = 0; channel < getChannelCount(); channel++) {
74         float low = mSingleFrame[channel];
75         float high = mSingleFrame2[channel];
76         frame[channel] = low + (fraction * (high - low));
77     }
78 }
79