Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sdatkinson committed Apr 4, 2024
1 parent 86a4d3c commit cd40877
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
73 changes: 38 additions & 35 deletions dsp/ResamplingContainer/Dependencies/LanczosResampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class LanczosResampler
// WARNING: hard-coded to accommodate 8192 samples, from 44.1 to 192k!
// If someone is past this, then maybe they know what they're doing ;)
// (size_t)(8192 * 3 * 192000 / 44100)+1 = 106998
static constexpr size_t kBufferSize = 131072; // Round up to pow2. I don't know why, but it doesn't work otherwise.
static constexpr size_t kBufferSize = 131072; // Round up to pow2. I don't know why, but it doesn't work otherwise.
// The filter width. 2x because the filter goes from -A to A
static constexpr size_t kFilterWidth = A * 2;
// The discretization resolution for the filter table.
Expand Down Expand Up @@ -201,7 +201,9 @@ class LanczosResampler
* Use the fact that mPhaseInIncr = mInputSampleRate and find
* res > (A+1) - (mPhaseIn - mPhaseOut + mPhaseOutIncr * desiredOutputs) * sri
*/
double res = A + 1.0 - ((double) (mPhaseInNumerator - mPhaseOutNumerator - mPhaseOutIncrNumerator * (long) nOutputSamples)) / ((double) mPhaseDenominator);
double res = A + 1.0
- ((double)(mPhaseInNumerator - mPhaseOutNumerator - mPhaseOutIncrNumerator * (long)nOutputSamples))
/ ((double)mPhaseDenominator);

return static_cast<size_t>(std::max(res + 1.0, 0.0));
}
Expand All @@ -226,7 +228,7 @@ class LanczosResampler
int populated = 0;
while (populated < max && (mPhaseInNumerator - mPhaseOutNumerator) > mPhaseDenominator * (A + 1))
{
ReadSamples(((double)(mPhaseInNumerator - mPhaseOutNumerator)) / ((double) mPhaseDenominator), outputs, populated);
ReadSamples(((double)(mPhaseInNumerator - mPhaseOutNumerator)) / ((double)mPhaseDenominator), outputs, populated);
mPhaseOutNumerator += mPhaseOutIncrNumerator;
populated++;
}
Expand Down Expand Up @@ -342,41 +344,42 @@ class LanczosResampler
}
}
#endif
void SetPhases() {
// This is going to assume I can treat the sample rates as longs...
// But if they're not, then things will sound just a little wrong and honestly I'm fine with that.
// It's your fault for not using something normal like 44.1k, 48k, or their multiples.
// (Looking at you, VST3PluginTestHost!)
auto AssertLongLikeSampleRate = [](double x) {
if ((double)((long)x) != x)
{
std::cerr << "Expected long-like sample rate; got " << x << " instead! Truncating..." << std::endl;
}
return (long)x;
};
void SetPhases()
{
// This is going to assume I can treat the sample rates as longs...
// But if they're not, then things will sound just a little wrong and honestly I'm fine with that.
// It's your fault for not using something normal like 44.1k, 48k, or their multiples.
// (Looking at you, VST3PluginTestHost!)
auto AssertLongLikeSampleRate = [](double x) {
if ((double)((long)x) != x)
{
std::cerr << "Expected long-like sample rate; got " << x << " instead! Truncating..." << std::endl;
}
return (long)x;
};

// Greatest common denominator
auto gcd = [](long a, long b) -> long {
while (b != 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
};

const long inputSampleRate = AssertLongLikeSampleRate(mInputSampleRate);
const long outputSampleRate = AssertLongLikeSampleRate(mOutputSampleRate);
const long g = gcd(inputSampleRate, outputSampleRate);

// mPhaseInIncr = 1.0
// mPhaseOutIncr = mInputSampleRate / mOutputSampleRate
mPhaseInIncrNumerator = outputSampleRate / g;
mPhaseDenominator = mPhaseInIncrNumerator; // val / val = 1
mPhaseOutIncrNumerator = inputSampleRate / g;
// Greatest common denominator
auto gcd = [](long a, long b) -> long {
while (b != 0)
{
long temp = b;
b = a % b;
a = temp;
}
return a;
};

const long inputSampleRate = AssertLongLikeSampleRate(mInputSampleRate);
const long outputSampleRate = AssertLongLikeSampleRate(mOutputSampleRate);
const long g = gcd(inputSampleRate, outputSampleRate);

// mPhaseInIncr = 1.0
// mPhaseOutIncr = mInputSampleRate / mOutputSampleRate
mPhaseInIncrNumerator = outputSampleRate / g;
mPhaseDenominator = mPhaseInIncrNumerator; // val / val = 1
mPhaseOutIncrNumerator = inputSampleRate / g;
};

static T sTable alignas(16)[kTablePoints + 1][kFilterWidth];
static T sDeltaTable alignas(16)[kTablePoints + 1][kFilterWidth];
static bool sTablesInitialized;
Expand Down
8 changes: 5 additions & 3 deletions dsp/ResamplingContainer/ResamplingContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ class ResamplingContainer
{
std::cerr << "Did not yield enough samples (" << populated2 << ") to provide the required output buffer (expected"
<< nFrames << ")! Filling with last sample..." << std::endl;
for (int c = 0; c < NCHANS; c++) {
for (int c = 0; c < NCHANS; c++)
{
const T lastSample = populated2 > 0 ? outputs[c][populated2 - 1] : 0.0;
for (int i = populated2; i < nFrames; i++) {
outputs[c][i] = lastSample;
for (int i = populated2; i < nFrames; i++)
{
outputs[c][i] = lastSample;
}
}
}
Expand Down

0 comments on commit cd40877

Please sign in to comment.