• 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 "PolyphaseResamplerStereo.h"
19 
20 using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
21 
22 #define STEREO  2
23 
PolyphaseResamplerStereo(const MultiChannelResampler::Builder & builder)24 PolyphaseResamplerStereo::PolyphaseResamplerStereo(const MultiChannelResampler::Builder &builder)
25         : PolyphaseResampler(builder) {
26     assert(builder.getChannelCount() == STEREO);
27 }
28 
writeFrame(const float * frame)29 void PolyphaseResamplerStereo::writeFrame(const float *frame) {
30     // Move cursor before write so that cursor points to last written frame in read.
31     if (--mCursor < 0) {
32         mCursor = getNumTaps() - 1;
33     }
34     float *dest = &mX[mCursor * STEREO];
35     const int offset = mNumTaps * STEREO;
36     // Write each channel twice so we avoid having to wrap when running the FIR.
37     const float left =  frame[0];
38     const float right = frame[1];
39     // Put ordered writes together.
40     dest[0] = left;
41     dest[1] = right;
42     dest[offset] = left;
43     dest[1 + offset] = right;
44 }
45 
readFrame(float * frame)46 void PolyphaseResamplerStereo::readFrame(float *frame) {
47     // Clear accumulators.
48     float left = 0.0;
49     float right = 0.0;
50 
51     // Multiply input times precomputed windowed sinc function.
52     const float *coefficients = &mCoefficients[mCoefficientCursor];
53     float *xFrame = &mX[mCursor * STEREO];
54     const int numLoops = mNumTaps >> 2; // n/4
55     for (int i = 0; i < numLoops; i++) {
56         // Manual loop unrolling, might get converted to SIMD.
57         float coefficient = *coefficients++;
58         left += *xFrame++ * coefficient;
59         right += *xFrame++ * coefficient;
60 
61         coefficient = *coefficients++; // next tap
62         left += *xFrame++ * coefficient;
63         right += *xFrame++ * coefficient;
64 
65         coefficient = *coefficients++;  // next tap
66         left += *xFrame++ * coefficient;
67         right += *xFrame++ * coefficient;
68 
69         coefficient = *coefficients++;  // next tap
70         left += *xFrame++ * coefficient;
71         right += *xFrame++ * coefficient;
72     }
73 
74     mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
75 
76     // Copy accumulators to output.
77     frame[0] = left;
78     frame[1] = right;
79 }
80