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