• 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 
20 #include "SincResamplerStereo.h"
21 
22 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
23 
24 #define STEREO  2
25 
SincResamplerStereo(const MultiChannelResampler::Builder & builder)26 SincResamplerStereo::SincResamplerStereo(const MultiChannelResampler::Builder &builder)
27         : SincResampler(builder) {
28     assert(builder.getChannelCount() == STEREO);
29 }
30 
writeFrame(const float * frame)31 void SincResamplerStereo::writeFrame(const float *frame) {
32     // Move cursor before write so that cursor points to last written frame in read.
33     if (--mCursor < 0) {
34         mCursor = getNumTaps() - 1;
35     }
36     float *dest = &mX[mCursor * STEREO];
37     const int offset = mNumTaps * STEREO;
38     // Write each channel twice so we avoid having to wrap when running the FIR.
39     const float left =  frame[0];
40     const float right = frame[1];
41     // Put ordered writes together.
42     dest[0] = left;
43     dest[1] = right;
44     dest[offset] = left;
45     dest[1 + offset] = right;
46 }
47 
48 // Multiply input times windowed sinc function.
readFrame(float * frame)49 void SincResamplerStereo::readFrame(float *frame) {
50     // Clear accumulator for mixing.
51     std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
52     std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
53 
54     // Determine indices into coefficients table.
55     double tablePhase = getIntegerPhase() * mPhaseScaler;
56     int index1 = static_cast<int>(floor(tablePhase));
57     float *coefficients1 = &mCoefficients[static_cast<size_t>(index1)
58             * static_cast<size_t>(getNumTaps())];
59     int index2 = (index1 + 1);
60     float *coefficients2 = &mCoefficients[static_cast<size_t>(index2)
61             * static_cast<size_t>(getNumTaps())];
62     float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
63     for (int i = 0; i < mNumTaps; i++) {
64         float coefficient1 = *coefficients1++;
65         float coefficient2 = *coefficients2++;
66         for (int channel = 0; channel < getChannelCount(); channel++) {
67             float sample = *xFrame++;
68             mSingleFrame[channel] +=  sample * coefficient1;
69             mSingleFrame2[channel] += sample * coefficient2;
70         }
71     }
72 
73     // Interpolate and copy to output.
74     float fraction = tablePhase - index1;
75     for (int channel = 0; channel < getChannelCount(); channel++) {
76         float low = mSingleFrame[channel];
77         float high = mSingleFrame2[channel];
78         frame[channel] = low + (fraction * (high - low));
79     }
80 }
81