Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H
#define RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H
#include <math.h>
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Calculate a HyperbolicCosineWindow window centered at 0.
* This can be used in place of a Kaiser window.
*
* The code is based on an anonymous contribution by "a concerned citizen":
* https://dsp.stackexchange.com/questions/37714/kaiser-window-approximation
*/
class HyperbolicCosineWindow {
public:
HyperbolicCosineWindow() {
setStopBandAttenuation(60);
}
/**
* @param attenuation typical values range from 30 to 90 dB
* @return beta
*/
double setStopBandAttenuation(double attenuation) {
double alpha = ((-325.1e-6 * attenuation + 0.1677) * attenuation) - 3.149;
setAlpha(alpha);
return alpha;
}
void setAlpha(double alpha) {
mAlpha = alpha;
mInverseCoshAlpha = 1.0 / cosh(alpha);
}
/**
* @param x ranges from -1.0 to +1.0
*/
double operator()(double x) {
double x2 = x * x;
if (x2 >= 1.0) return 0.0;
double w = mAlpha * sqrt(1.0 - x2);
return cosh(w) * mInverseCoshAlpha;
}
private:
double mAlpha = 0.0;
double mInverseCoshAlpha = 1.0;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "IntegerRatio.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
// Enough primes to cover the common sample rates.
static const int kPrimes[] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199};
void IntegerRatio::reduce() {
for (int prime : kPrimes) {
if (mNumerator < prime || mDenominator < prime) {
break;
}
// Find biggest prime factor for numerator.
while (true) {
int top = mNumerator / prime;
int bottom = mDenominator / prime;
if ((top >= 1)
&& (bottom >= 1)
&& (top * prime == mNumerator) // divided evenly?
&& (bottom * prime == mDenominator)) {
mNumerator = top;
mDenominator = bottom;
} else {
break;
}
}
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_INTEGER_RATIO_H
#define RESAMPLER_INTEGER_RATIO_H
#include <sys/types.h>
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Represent the ratio of two integers.
*/
class IntegerRatio {
public:
IntegerRatio(int32_t numerator, int32_t denominator)
: mNumerator(numerator), mDenominator(denominator) {}
/**
* Reduce by removing common prime factors.
*/
void reduce();
int32_t getNumerator() {
return mNumerator;
}
int32_t getDenominator() {
return mDenominator;
}
private:
int32_t mNumerator;
int32_t mDenominator;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_INTEGER_RATIO_H

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_KAISER_WINDOW_H
#define RESAMPLER_KAISER_WINDOW_H
#include <math.h>
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Calculate a Kaiser window centered at 0.
*/
class KaiserWindow {
public:
KaiserWindow() {
setStopBandAttenuation(60);
}
/**
* @param attenuation typical values range from 30 to 90 dB
* @return beta
*/
double setStopBandAttenuation(double attenuation) {
double beta = 0.0;
if (attenuation > 50) {
beta = 0.1102 * (attenuation - 8.7);
} else if (attenuation >= 21) {
double a21 = attenuation - 21;
beta = 0.5842 * pow(a21, 0.4) + (0.07886 * a21);
}
setBeta(beta);
return beta;
}
void setBeta(double beta) {
mBeta = beta;
mInverseBesselBeta = 1.0 / bessel(beta);
}
/**
* @param x ranges from -1.0 to +1.0
*/
double operator()(double x) {
double x2 = x * x;
if (x2 >= 1.0) return 0.0;
double w = mBeta * sqrt(1.0 - x2);
return bessel(w) * mInverseBesselBeta;
}
// Approximation of a
// modified zero order Bessel function of the first kind.
// Based on a discussion at:
// https://dsp.stackexchange.com/questions/37714/kaiser-window-approximation
static double bessel(double x) {
double y = cosh(0.970941817426052 * x);
y += cosh(0.8854560256532099 * x);
y += cosh(0.7485107481711011 * x);
y += cosh(0.5680647467311558 * x);
y += cosh(0.3546048870425356 * x);
y += cosh(0.120536680255323 * x);
y *= 2;
y += cosh(x);
y /= 13;
return y;
}
private:
double mBeta = 0.0;
double mInverseBesselBeta = 1.0;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_KAISER_WINDOW_H

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "LinearResampler.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
LinearResampler::LinearResampler(const MultiChannelResampler::Builder &builder)
: MultiChannelResampler(builder) {
mPreviousFrame = std::make_unique<float[]>(getChannelCount());
mCurrentFrame = std::make_unique<float[]>(getChannelCount());
}
void LinearResampler::writeFrame(const float *frame) {
memcpy(mPreviousFrame.get(), mCurrentFrame.get(), sizeof(float) * getChannelCount());
memcpy(mCurrentFrame.get(), frame, sizeof(float) * getChannelCount());
}
void LinearResampler::readFrame(float *frame) {
float *previous = mPreviousFrame.get();
float *current = mCurrentFrame.get();
float phase = (float) getIntegerPhase() / mDenominator;
// iterate across samples in the frame
for (int channel = 0; channel < getChannelCount(); channel++) {
float f0 = *previous++;
float f1 = *current++;
*frame++ = f0 + (phase * (f1 - f0));
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_LINEAR_RESAMPLER_H
#define RESAMPLER_LINEAR_RESAMPLER_H
#include <memory>
#include <sys/types.h>
#include <unistd.h>
#include "MultiChannelResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Simple resampler that uses bi-linear interpolation.
*/
class LinearResampler : public MultiChannelResampler {
public:
explicit LinearResampler(const MultiChannelResampler::Builder &builder);
void writeFrame(const float *frame) override;
void readFrame(float *frame) override;
private:
std::unique_ptr<float[]> mPreviousFrame;
std::unique_ptr<float[]> mCurrentFrame;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_LINEAR_RESAMPLER_H

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <math.h>
#include "IntegerRatio.h"
#include "LinearResampler.h"
#include "MultiChannelResampler.h"
#include "PolyphaseResampler.h"
#include "PolyphaseResamplerMono.h"
#include "PolyphaseResamplerStereo.h"
#include "SincResampler.h"
#include "SincResamplerStereo.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
MultiChannelResampler::MultiChannelResampler(const MultiChannelResampler::Builder &builder)
: mNumTaps(builder.getNumTaps())
, mX(static_cast<size_t>(builder.getChannelCount())
* static_cast<size_t>(builder.getNumTaps()) * 2)
, mSingleFrame(builder.getChannelCount())
, mChannelCount(builder.getChannelCount())
{
// Reduce sample rates to the smallest ratio.
// For example 44100/48000 would become 147/160.
IntegerRatio ratio(builder.getInputRate(), builder.getOutputRate());
ratio.reduce();
mNumerator = ratio.getNumerator();
mDenominator = ratio.getDenominator();
mIntegerPhase = mDenominator; // so we start with a write needed
}
// static factory method
MultiChannelResampler *MultiChannelResampler::make(int32_t channelCount,
int32_t inputRate,
int32_t outputRate,
Quality quality) {
Builder builder;
builder.setInputRate(inputRate);
builder.setOutputRate(outputRate);
builder.setChannelCount(channelCount);
switch (quality) {
case Quality::Fastest:
builder.setNumTaps(2);
break;
case Quality::Low:
builder.setNumTaps(4);
break;
case Quality::Medium:
default:
builder.setNumTaps(8);
break;
case Quality::High:
builder.setNumTaps(16);
break;
case Quality::Best:
builder.setNumTaps(32);
break;
}
// Set the cutoff frequency so that we do not get aliasing when down-sampling.
if (inputRate > outputRate) {
builder.setNormalizedCutoff(kDefaultNormalizedCutoff);
}
return builder.build();
}
MultiChannelResampler *MultiChannelResampler::Builder::build() {
if (getNumTaps() == 2) {
// Note that this does not do low pass filteringh.
return new LinearResampler(*this);
}
IntegerRatio ratio(getInputRate(), getOutputRate());
ratio.reduce();
bool usePolyphase = (getNumTaps() * ratio.getDenominator()) <= kMaxCoefficients;
if (usePolyphase) {
if (getChannelCount() == 1) {
return new PolyphaseResamplerMono(*this);
} else if (getChannelCount() == 2) {
return new PolyphaseResamplerStereo(*this);
} else {
return new PolyphaseResampler(*this);
}
} else {
// Use less optimized resampler that uses a float phaseIncrement.
// TODO mono resampler
if (getChannelCount() == 2) {
return new SincResamplerStereo(*this);
} else {
return new SincResampler(*this);
}
}
}
void MultiChannelResampler::writeFrame(const float *frame) {
// Move cursor before write so that cursor points to last written frame in read.
if (--mCursor < 0) {
mCursor = getNumTaps() - 1;
}
float *dest = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
int offset = getNumTaps() * getChannelCount();
for (int channel = 0; channel < getChannelCount(); channel++) {
// Write twice so we avoid having to wrap when reading.
dest[channel] = dest[channel + offset] = frame[channel];
}
}
float MultiChannelResampler::sinc(float radians) {
if (fabsf(radians) < 1.0e-9f) return 1.0f; // avoid divide by zero
return sinf(radians) / radians; // Sinc function
}
// Generate coefficients in the order they will be used by readFrame().
// This is more complicated but readFrame() is called repeatedly and should be optimized.
void MultiChannelResampler::generateCoefficients(int32_t inputRate,
int32_t outputRate,
int32_t numRows,
double phaseIncrement,
float normalizedCutoff) {
mCoefficients.resize(static_cast<size_t>(getNumTaps()) * static_cast<size_t>(numRows));
int coefficientIndex = 0;
double phase = 0.0; // ranges from 0.0 to 1.0, fraction between samples
// Stretch the sinc function for low pass filtering.
const float cutoffScaler = (outputRate < inputRate)
? (normalizedCutoff * (float)outputRate / inputRate)
: 1.0f; // Do not filter when upsampling.
const int numTapsHalf = getNumTaps() / 2; // numTaps must be even.
const float numTapsHalfInverse = 1.0f / numTapsHalf;
for (int i = 0; i < numRows; i++) {
float tapPhase = phase - numTapsHalf;
float gain = 0.0; // sum of raw coefficients
int gainCursor = coefficientIndex;
for (int tap = 0; tap < getNumTaps(); tap++) {
float radians = tapPhase * M_PI;
#if MCR_USE_KAISER
float window = mKaiserWindow(tapPhase * numTapsHalfInverse);
#else
float window = mCoshWindow(static_cast<double>(tapPhase) * numTapsHalfInverse);
#endif
float coefficient = sinc(radians * cutoffScaler) * window;
mCoefficients.at(coefficientIndex++) = coefficient;
gain += coefficient;
tapPhase += 1.0;
}
phase += phaseIncrement;
while (phase >= 1.0) {
phase -= 1.0;
}
// Correct for gain variations.
float gainCorrection = 1.0 / gain; // normalize the gain
for (int tap = 0; tap < getNumTaps(); tap++) {
mCoefficients.at(gainCursor + tap) *= gainCorrection;
}
}
}

View File

@@ -0,0 +1,281 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_MULTICHANNEL_RESAMPLER_H
#define RESAMPLER_MULTICHANNEL_RESAMPLER_H
#include <memory>
#include <vector>
#include <sys/types.h>
#include <unistd.h>
#ifndef MCR_USE_KAISER
// It appears from the spectrogram that the HyperbolicCosine window leads to fewer artifacts.
// And it is faster to calculate.
#define MCR_USE_KAISER 0
#endif
#if MCR_USE_KAISER
#include "KaiserWindow.h"
#else
#include "HyperbolicCosineWindow.h"
#endif
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
class MultiChannelResampler {
public:
enum class Quality : int32_t {
Fastest,
Low,
Medium,
High,
Best,
};
class Builder {
public:
/**
* Construct an optimal resampler based on the specified parameters.
* @return address of a resampler
*/
MultiChannelResampler *build();
/**
* The number of taps in the resampling filter.
* More taps gives better quality but uses more CPU time.
* This typically ranges from 4 to 64. Default is 16.
*
* For polyphase filters, numTaps must be a multiple of four for loop unrolling.
* @param numTaps number of taps for the filter
* @return address of this builder for chaining calls
*/
Builder *setNumTaps(int32_t numTaps) {
mNumTaps = numTaps;
return this;
}
/**
* Use 1 for mono, 2 for stereo, etc. Default is 1.
*
* @param channelCount number of channels
* @return address of this builder for chaining calls
*/
Builder *setChannelCount(int32_t channelCount) {
mChannelCount = channelCount;
return this;
}
/**
* Default is 48000.
*
* @param inputRate sample rate of the input stream
* @return address of this builder for chaining calls
*/
Builder *setInputRate(int32_t inputRate) {
mInputRate = inputRate;
return this;
}
/**
* Default is 48000.
*
* @param outputRate sample rate of the output stream
* @return address of this builder for chaining calls
*/
Builder *setOutputRate(int32_t outputRate) {
mOutputRate = outputRate;
return this;
}
/**
* Set cutoff frequency relative to the Nyquist rate of the output sample rate.
* Set to 1.0 to match the Nyquist frequency.
* Set lower to reduce aliasing.
* Default is 0.70.
*
* Note that this value is ignored when upsampling, which is when
* the outputRate is higher than the inputRate.
*
* @param normalizedCutoff anti-aliasing filter cutoff
* @return address of this builder for chaining calls
*/
Builder *setNormalizedCutoff(float normalizedCutoff) {
mNormalizedCutoff = normalizedCutoff;
return this;
}
int32_t getNumTaps() const {
return mNumTaps;
}
int32_t getChannelCount() const {
return mChannelCount;
}
int32_t getInputRate() const {
return mInputRate;
}
int32_t getOutputRate() const {
return mOutputRate;
}
float getNormalizedCutoff() const {
return mNormalizedCutoff;
}
protected:
int32_t mChannelCount = 1;
int32_t mNumTaps = 16;
int32_t mInputRate = 48000;
int32_t mOutputRate = 48000;
float mNormalizedCutoff = kDefaultNormalizedCutoff;
};
virtual ~MultiChannelResampler() = default;
/**
* Factory method for making a resampler that is optimal for the given inputs.
*
* @param channelCount number of channels, 2 for stereo
* @param inputRate sample rate of the input stream
* @param outputRate sample rate of the output stream
* @param quality higher quality sounds better but uses more CPU
* @return an optimal resampler
*/
static MultiChannelResampler *make(int32_t channelCount,
int32_t inputRate,
int32_t outputRate,
Quality quality);
bool isWriteNeeded() const {
return mIntegerPhase >= mDenominator;
}
/**
* Write a frame containing N samples.
*
* @param frame pointer to the first sample in a frame
*/
void writeNextFrame(const float *frame) {
writeFrame(frame);
advanceWrite();
}
/**
* Read a frame containing N samples.
*
* @param frame pointer to the first sample in a frame
*/
void readNextFrame(float *frame) {
readFrame(frame);
advanceRead();
}
int getNumTaps() const {
return mNumTaps;
}
int getChannelCount() const {
return mChannelCount;
}
static float hammingWindow(float radians, float spread);
static float sinc(float radians);
protected:
explicit MultiChannelResampler(const MultiChannelResampler::Builder &builder);
/**
* Write a frame containing N samples.
* Call advanceWrite() after calling this.
* @param frame pointer to the first sample in a frame
*/
virtual void writeFrame(const float *frame);
/**
* Read a frame containing N samples using interpolation.
* Call advanceRead() after calling this.
* @param frame pointer to the first sample in a frame
*/
virtual void readFrame(float *frame) = 0;
void advanceWrite() {
mIntegerPhase -= mDenominator;
}
void advanceRead() {
mIntegerPhase += mNumerator;
}
/**
* Generate the filter coefficients in optimal order.
*
* Note that normalizedCutoff is ignored when upsampling, which is when
* the outputRate is higher than the inputRate.
*
* @param inputRate sample rate of the input stream
* @param outputRate sample rate of the output stream
* @param numRows number of rows in the array that contain a set of tap coefficients
* @param phaseIncrement how much to increment the phase between rows
* @param normalizedCutoff filter cutoff frequency normalized to Nyquist rate of output
*/
void generateCoefficients(int32_t inputRate,
int32_t outputRate,
int32_t numRows,
double phaseIncrement,
float normalizedCutoff);
int32_t getIntegerPhase() {
return mIntegerPhase;
}
static constexpr int kMaxCoefficients = 8 * 1024;
std::vector<float> mCoefficients;
const int mNumTaps;
int mCursor = 0;
std::vector<float> mX; // delayed input values for the FIR
std::vector<float> mSingleFrame; // one frame for temporary use
int32_t mIntegerPhase = 0;
int32_t mNumerator = 0;
int32_t mDenominator = 0;
private:
#if MCR_USE_KAISER
KaiserWindow mKaiserWindow;
#else
HyperbolicCosineWindow mCoshWindow;
#endif
static constexpr float kDefaultNormalizedCutoff = 0.70f;
const int mChannelCount;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_MULTICHANNEL_RESAMPLER_H

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm> // Do NOT delete. Needed for LLVM. See #1746
#include <cassert>
#include <math.h>
#include "IntegerRatio.h"
#include "PolyphaseResampler.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
PolyphaseResampler::PolyphaseResampler(const MultiChannelResampler::Builder &builder)
: MultiChannelResampler(builder)
{
assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
int32_t inputRate = builder.getInputRate();
int32_t outputRate = builder.getOutputRate();
int32_t numRows = mDenominator;
double phaseIncrement = (double) inputRate / (double) outputRate;
generateCoefficients(inputRate, outputRate,
numRows, phaseIncrement,
builder.getNormalizedCutoff());
}
void PolyphaseResampler::readFrame(float *frame) {
// Clear accumulator for mixing.
std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
// Multiply input times windowed sinc function.
float *coefficients = &mCoefficients[mCoefficientCursor];
float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
for (int i = 0; i < mNumTaps; i++) {
float coefficient = *coefficients++;
for (int channel = 0; channel < getChannelCount(); channel++) {
mSingleFrame[channel] += *xFrame++ * coefficient;
}
}
// Advance and wrap through coefficients.
mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
// Copy accumulator to output.
for (int channel = 0; channel < getChannelCount(); channel++) {
frame[channel] = mSingleFrame[channel];
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_POLYPHASE_RESAMPLER_H
#define RESAMPLER_POLYPHASE_RESAMPLER_H
#include <memory>
#include <vector>
#include <sys/types.h>
#include <unistd.h>
#include "MultiChannelResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Resampler that is optimized for a reduced ratio of sample rates.
* All of the coefficients for each possible phase value are pre-calculated.
*/
class PolyphaseResampler : public MultiChannelResampler {
public:
/**
*
* @param builder containing lots of parameters
*/
explicit PolyphaseResampler(const MultiChannelResampler::Builder &builder);
virtual ~PolyphaseResampler() = default;
void readFrame(float *frame) override;
protected:
int32_t mCoefficientCursor = 0;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_POLYPHASE_RESAMPLER_H

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cassert>
#include "PolyphaseResamplerMono.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
#define MONO 1
PolyphaseResamplerMono::PolyphaseResamplerMono(const MultiChannelResampler::Builder &builder)
: PolyphaseResampler(builder) {
assert(builder.getChannelCount() == MONO);
}
void PolyphaseResamplerMono::writeFrame(const float *frame) {
// Move cursor before write so that cursor points to last written frame in read.
if (--mCursor < 0) {
mCursor = getNumTaps() - 1;
}
float *dest = &mX[mCursor * MONO];
const int offset = mNumTaps * MONO;
// Write each channel twice so we avoid having to wrap when running the FIR.
const float sample = frame[0];
// Put ordered writes together.
dest[0] = sample;
dest[offset] = sample;
}
void PolyphaseResamplerMono::readFrame(float *frame) {
// Clear accumulator.
float sum = 0.0;
// Multiply input times precomputed windowed sinc function.
const float *coefficients = &mCoefficients[mCoefficientCursor];
float *xFrame = &mX[mCursor * MONO];
const int numLoops = mNumTaps >> 2; // n/4
for (int i = 0; i < numLoops; i++) {
// Manual loop unrolling, might get converted to SIMD.
sum += *xFrame++ * *coefficients++;
sum += *xFrame++ * *coefficients++;
sum += *xFrame++ * *coefficients++;
sum += *xFrame++ * *coefficients++;
}
mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
// Copy accumulator to output.
frame[0] = sum;
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_POLYPHASE_RESAMPLER_MONO_H
#define RESAMPLER_POLYPHASE_RESAMPLER_MONO_H
#include <sys/types.h>
#include <unistd.h>
#include "PolyphaseResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
class PolyphaseResamplerMono : public PolyphaseResampler {
public:
explicit PolyphaseResamplerMono(const MultiChannelResampler::Builder &builder);
virtual ~PolyphaseResamplerMono() = default;
void writeFrame(const float *frame) override;
void readFrame(float *frame) override;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_POLYPHASE_RESAMPLER_MONO_H

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cassert>
#include "PolyphaseResamplerStereo.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
#define STEREO 2
PolyphaseResamplerStereo::PolyphaseResamplerStereo(const MultiChannelResampler::Builder &builder)
: PolyphaseResampler(builder) {
assert(builder.getChannelCount() == STEREO);
}
void PolyphaseResamplerStereo::writeFrame(const float *frame) {
// Move cursor before write so that cursor points to last written frame in read.
if (--mCursor < 0) {
mCursor = getNumTaps() - 1;
}
float *dest = &mX[mCursor * STEREO];
const int offset = mNumTaps * STEREO;
// Write each channel twice so we avoid having to wrap when running the FIR.
const float left = frame[0];
const float right = frame[1];
// Put ordered writes together.
dest[0] = left;
dest[1] = right;
dest[offset] = left;
dest[1 + offset] = right;
}
void PolyphaseResamplerStereo::readFrame(float *frame) {
// Clear accumulators.
float left = 0.0;
float right = 0.0;
// Multiply input times precomputed windowed sinc function.
const float *coefficients = &mCoefficients[mCoefficientCursor];
float *xFrame = &mX[mCursor * STEREO];
const int numLoops = mNumTaps >> 2; // n/4
for (int i = 0; i < numLoops; i++) {
// Manual loop unrolling, might get converted to SIMD.
float coefficient = *coefficients++;
left += *xFrame++ * coefficient;
right += *xFrame++ * coefficient;
coefficient = *coefficients++; // next tap
left += *xFrame++ * coefficient;
right += *xFrame++ * coefficient;
coefficient = *coefficients++; // next tap
left += *xFrame++ * coefficient;
right += *xFrame++ * coefficient;
coefficient = *coefficients++; // next tap
left += *xFrame++ * coefficient;
right += *xFrame++ * coefficient;
}
mCoefficientCursor = (mCoefficientCursor + mNumTaps) % mCoefficients.size();
// Copy accumulators to output.
frame[0] = left;
frame[1] = right;
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_POLYPHASE_RESAMPLER_STEREO_H
#define RESAMPLER_POLYPHASE_RESAMPLER_STEREO_H
#include <sys/types.h>
#include <unistd.h>
#include "PolyphaseResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
class PolyphaseResamplerStereo : public PolyphaseResampler {
public:
explicit PolyphaseResamplerStereo(const MultiChannelResampler::Builder &builder);
virtual ~PolyphaseResamplerStereo() = default;
void writeFrame(const float *frame) override;
void readFrame(float *frame) override;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_POLYPHASE_RESAMPLER_STEREO_H

View File

@@ -0,0 +1,101 @@
# Sample Rate Converter
This folder contains a sample rate converter, or "resampler".
The converter is based on a sinc function that has been windowed by a hyperbolic cosine.
We found this had fewer artifacts than the more traditional Kaiser window.
## Building the Resampler
It is part of [Oboe](https://github.com/google/oboe) but has no dependencies on Oboe.
So the contents of this folder can be used outside of Oboe.
To build it for use outside of Oboe:
1. Copy the "resampler" folder to a folder in your project that is in the include path.
2. Add all of the \*.cpp files in the resampler folder to your project IDE or Makefile.
3. In ResamplerDefinitions.h, define RESAMPLER_OUTER_NAMESPACE with your own project name. Alternatively, use -DRESAMPLER_OUTER_NAMESPACE=mynamespace when compiling to avoid modifying the resampler code.
## Creating a Resampler
Include the [main header](MultiChannelResampler.h) for the resampler.
#include "resampler/MultiChannelResampler.h"
Here is an example of creating a stereo resampler that will convert from 44100 to 48000 Hz.
Only do this once, when you open your stream. Then use the sample resampler to process multiple buffers.
MultiChannelResampler *resampler = MultiChannelResampler::make(
2, // channel count
44100, // input sampleRate
48000, // output sampleRate
MultiChannelResampler::Quality::Medium); // conversion quality
Possible values for quality include { Fastest, Low, Medium, High, Best }.
Higher quality levels will sound better but consume more CPU because they have more taps in the filter.
## Fractional Frame Counts
Note that the number of output frames generated for a given number of input frames can vary.
For example, suppose you are converting from 44100 Hz to 48000 Hz and using an input buffer with 960 frames. If you calculate the number of output frames you get:
960.0 * 48000 / 44100 = 1044.897959...
You cannot generate a fractional number of frames. So the resampler will sometimes generate 1044 frames and sometimes 1045 frames. On average it will generate 1044.897959 frames. The resampler stores the fraction internally and keeps track of when to consume or generate a frame.
You can either use a fixed number of input frames or a fixed number of output frames. The other frame count will vary.
## Calling the Resampler with a fixed number of OUTPUT frames
In this example, suppose we have a fixed number of output frames and a variable number of input frames.
Assume you start with these variables and a method that returns the next input frame:
float *outputBuffer; // multi-channel buffer to be filled
int numOutputFrames; // number of frames of output
The resampler has a method isWriteNeeded() that tells you whether to write to or read from the resampler.
int outputFramesLeft = numOutputFrames;
while (outputFramesLeft > 0) {
if(resampler->isWriteNeeded()) {
const float *frame = getNextInputFrame(); // you provide this
resampler->writeNextFrame(frame);
} else {
resampler->readNextFrame(outputBuffer);
outputBuffer += channelCount;
outputFramesLeft--;
}
}
## Calling the Resampler with a fixed number of INPUT frames
In this example, suppose we have a fixed number of input frames and a variable number of output frames.
Assume you start with these variables:
float *inputBuffer; // multi-channel buffer to be consumed
float *outputBuffer; // multi-channel buffer to be filled
int numInputFrames; // number of frames of input
int numOutputFrames = 0;
int channelCount; // 1 for mono, 2 for stereo
int inputFramesLeft = numInputFrames;
while (inputFramesLeft > 0) {
if(resampler->isWriteNeeded()) {
resampler->writeNextFrame(inputBuffer);
inputBuffer += channelCount;
inputFramesLeft--;
} else {
resampler->readNextFrame(outputBuffer);
outputBuffer += channelCount;
numOutputFrames++;
}
}
## Deleting the Resampler
When you are done, you should delete the Resampler to avoid a memory leak.
delete resampler;

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Set flag RESAMPLER_OUTER_NAMESPACE based on whether compiler flag
// __ANDROID_NDK__ is defined. __ANDROID_NDK__ should be defined in oboe
// but not in android.
#ifndef RESAMPLER_OUTER_NAMESPACE
#ifdef __ANDROID_NDK__
#define RESAMPLER_OUTER_NAMESPACE oboe
#else
#define RESAMPLER_OUTER_NAMESPACE aaudio
#endif // __ANDROID_NDK__
#endif // RESAMPLER_OUTER_NAMESPACE

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm> // Do NOT delete. Needed for LLVM. See #1746
#include <cassert>
#include <math.h>
#include "SincResampler.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
SincResampler::SincResampler(const MultiChannelResampler::Builder &builder)
: MultiChannelResampler(builder)
, mSingleFrame2(builder.getChannelCount()) {
assert((getNumTaps() % 4) == 0); // Required for loop unrolling.
mNumRows = kMaxCoefficients / getNumTaps(); // includes guard row
const int32_t numRowsNoGuard = mNumRows - 1;
mPhaseScaler = (double) numRowsNoGuard / mDenominator;
const double phaseIncrement = 1.0 / numRowsNoGuard;
generateCoefficients(builder.getInputRate(),
builder.getOutputRate(),
mNumRows,
phaseIncrement,
builder.getNormalizedCutoff());
}
void SincResampler::readFrame(float *frame) {
// Clear accumulator for mixing.
std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
// Determine indices into coefficients table.
const double tablePhase = getIntegerPhase() * mPhaseScaler;
const int indexLow = static_cast<int>(floor(tablePhase));
const int indexHigh = indexLow + 1; // OK because using a guard row.
assert (indexHigh < mNumRows);
float *coefficientsLow = &mCoefficients[static_cast<size_t>(indexLow)
* static_cast<size_t>(getNumTaps())];
float *coefficientsHigh = &mCoefficients[static_cast<size_t>(indexHigh)
* static_cast<size_t>(getNumTaps())];
float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
for (int tap = 0; tap < mNumTaps; tap++) {
const float coefficientLow = *coefficientsLow++;
const float coefficientHigh = *coefficientsHigh++;
for (int channel = 0; channel < getChannelCount(); channel++) {
const float sample = *xFrame++;
mSingleFrame[channel] += sample * coefficientLow;
mSingleFrame2[channel] += sample * coefficientHigh;
}
}
// Interpolate and copy to output.
const float fraction = tablePhase - indexLow;
for (int channel = 0; channel < getChannelCount(); channel++) {
const float low = mSingleFrame[channel];
const float high = mSingleFrame2[channel];
frame[channel] = low + (fraction * (high - low));
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_SINC_RESAMPLER_H
#define RESAMPLER_SINC_RESAMPLER_H
#include <memory>
#include <sys/types.h>
#include <unistd.h>
#include "MultiChannelResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
/**
* Resampler that can interpolate between coefficients.
* This can be used to support arbitrary ratios.
*/
class SincResampler : public MultiChannelResampler {
public:
explicit SincResampler(const MultiChannelResampler::Builder &builder);
virtual ~SincResampler() = default;
void readFrame(float *frame) override;
protected:
std::vector<float> mSingleFrame2; // for interpolation
int32_t mNumRows = 0;
double mPhaseScaler = 1.0;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_SINC_RESAMPLER_H

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm> // Do NOT delete. Needed for LLVM. See #1746
#include <cassert>
#include <math.h>
#include "SincResamplerStereo.h"
using namespace RESAMPLER_OUTER_NAMESPACE::resampler;
#define STEREO 2
SincResamplerStereo::SincResamplerStereo(const MultiChannelResampler::Builder &builder)
: SincResampler(builder) {
assert(builder.getChannelCount() == STEREO);
}
void SincResamplerStereo::writeFrame(const float *frame) {
// Move cursor before write so that cursor points to last written frame in read.
if (--mCursor < 0) {
mCursor = getNumTaps() - 1;
}
float *dest = &mX[mCursor * STEREO];
const int offset = mNumTaps * STEREO;
// Write each channel twice so we avoid having to wrap when running the FIR.
const float left = frame[0];
const float right = frame[1];
// Put ordered writes together.
dest[0] = left;
dest[1] = right;
dest[offset] = left;
dest[1 + offset] = right;
}
// Multiply input times windowed sinc function.
void SincResamplerStereo::readFrame(float *frame) {
// Clear accumulator for mixing.
std::fill(mSingleFrame.begin(), mSingleFrame.end(), 0.0);
std::fill(mSingleFrame2.begin(), mSingleFrame2.end(), 0.0);
// Determine indices into coefficients table.
double tablePhase = getIntegerPhase() * mPhaseScaler;
int index1 = static_cast<int>(floor(tablePhase));
float *coefficients1 = &mCoefficients[static_cast<size_t>(index1)
* static_cast<size_t>(getNumTaps())];
int index2 = (index1 + 1);
float *coefficients2 = &mCoefficients[static_cast<size_t>(index2)
* static_cast<size_t>(getNumTaps())];
float *xFrame = &mX[static_cast<size_t>(mCursor) * static_cast<size_t>(getChannelCount())];
for (int i = 0; i < mNumTaps; i++) {
float coefficient1 = *coefficients1++;
float coefficient2 = *coefficients2++;
for (int channel = 0; channel < getChannelCount(); channel++) {
float sample = *xFrame++;
mSingleFrame[channel] += sample * coefficient1;
mSingleFrame2[channel] += sample * coefficient2;
}
}
// Interpolate and copy to output.
float fraction = tablePhase - index1;
for (int channel = 0; channel < getChannelCount(); channel++) {
float low = mSingleFrame[channel];
float high = mSingleFrame2[channel];
frame[channel] = low + (fraction * (high - low));
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RESAMPLER_SINC_RESAMPLER_STEREO_H
#define RESAMPLER_SINC_RESAMPLER_STEREO_H
#include <sys/types.h>
#include <unistd.h>
#include "SincResampler.h"
#include "ResamplerDefinitions.h"
namespace RESAMPLER_OUTER_NAMESPACE::resampler {
class SincResamplerStereo : public SincResampler {
public:
explicit SincResamplerStereo(const MultiChannelResampler::Builder &builder);
virtual ~SincResamplerStereo() = default;
void writeFrame(const float *frame) override;
void readFrame(float *frame) override;
};
} /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
#endif //RESAMPLER_SINC_RESAMPLER_STEREO_H