Skip to content

Commit

Permalink
(Re-)add a cubic spline resampler
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Apr 7, 2024
1 parent 3ad68aa commit fdd1643
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions al/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ template<> struct ResamplerName<Resampler::Point>
{ static constexpr const ALchar *Get() noexcept { return "Nearest"; } };
template<> struct ResamplerName<Resampler::Linear>
{ static constexpr const ALchar *Get() noexcept { return "Linear"; } };
template<> struct ResamplerName<Resampler::Spline>
{ static constexpr const ALchar *Get() noexcept { return "Cubic Spline"; } };
template<> struct ResamplerName<Resampler::Gaussian>
{ static constexpr const ALchar *Get() noexcept { return "4-point Gaussian"; } };
template<> struct ResamplerName<Resampler::FastBSinc12>
Expand All @@ -100,6 +102,7 @@ const ALchar *GetResamplerName(const Resampler rtype)
{
HANDLE_RESAMPLER(Resampler::Point);
HANDLE_RESAMPLER(Resampler::Linear);
HANDLE_RESAMPLER(Resampler::Spline);
HANDLE_RESAMPLER(Resampler::Gaussian);
HANDLE_RESAMPLER(Resampler::FastBSinc12);
HANDLE_RESAMPLER(Resampler::BSinc12);
Expand Down
4 changes: 4 additions & 0 deletions alc/alu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ inline ResamplerFunc SelectResampler(Resampler resampler, uint increment)
return Resample_<LerpTag,SSE2Tag>;
#endif
return Resample_<LerpTag,CTag>;
case Resampler::Spline:
case Resampler::Gaussian:
#ifdef HAVE_NEON
if((CPUCapFlags&CPU_CAP_NEON))
Expand Down Expand Up @@ -268,6 +269,9 @@ ResamplerFunc PrepareResampler(Resampler resampler, uint increment, InterpState
case Resampler::Point:
case Resampler::Linear:
break;
case Resampler::Spline:
state->emplace<CubicState>(al::span{gSplineFilter.mTable});
break;
case Resampler::Gaussian:
state->emplace<CubicState>(al::span{gGaussianFilter.mTable});
break;
Expand Down
1 change: 1 addition & 0 deletions alsoftrc.sample
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
# Selects the default resampler used when mixing sources. Valid values are:
# point - nearest sample, no interpolation
# linear - extrapolates samples using a linear slope between samples
# spline - extrapolates samples using a Catmull-Rom spline
# gaussian - extrapolates samples using a 4-point Gaussian filter
# bsinc12 - extrapolates samples using a band-limited Sinc filter (varying
# between 12 and 24 points, with anti-aliasing)
Expand Down
40 changes: 35 additions & 5 deletions core/cubic_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#include "alnumeric.h"
#include "cubic_defs.h"

/* These filter tables are inspired by the gaussian-like filter found in the
* SNES. This is based on the public domain code developed by Near, with the
* help of Ryphecha and nocash, from the nesdev.org forums.
/* These gaussian filter tables are inspired by the gaussian-like filter found
* in the SNES. This is based on the public domain code developed by Near, with
* the help of Ryphecha and nocash, from the nesdev.org forums.
*
* <https://forums.nesdev.org/viewtopic.php?p=251534#p251534>
*
* Additional changes were made here, the most obvious being that is has full
* floating-point precision instead of 11-bit fixed-point, but also an offset
* adjustment for the phase coefficients to more cleanly transition from the
* end of one sample set to the start of the next.
* adjustment for the coefficients to better preserve phase.
*/
namespace {

Expand Down Expand Up @@ -69,6 +68,37 @@ GaussianTable::GaussianTable()
mTable[pi].mDeltas[3] = mTable[0].mCoeffs[2] - mTable[pi].mCoeffs[3];
}

SplineTable::SplineTable()
{
/* This filter table is based on a Catmull-Rom spline. It retains more of
* the original high-frequency content, at the cost of increased harmonics.
*/
for(std::size_t pi{0};pi < CubicPhaseCount;++pi)
{
const double mu{static_cast<double>(pi) / double{CubicPhaseCount}};
const double mu2{mu*mu}, mu3{mu2*mu};
mTable[pi].mCoeffs[0] = static_cast<float>(-0.5*mu3 + mu2 + -0.5*mu);
mTable[pi].mCoeffs[1] = static_cast<float>( 1.5*mu3 + -2.5*mu2 + 1.0);
mTable[pi].mCoeffs[2] = static_cast<float>(-1.5*mu3 + 2.0*mu2 + 0.5*mu);
mTable[pi].mCoeffs[3] = static_cast<float>( 0.5*mu3 + -0.5*mu2);
}

for(std::size_t pi{0};pi < CubicPhaseCount-1;++pi)
{
mTable[pi].mDeltas[0] = mTable[pi+1].mCoeffs[0] - mTable[pi].mCoeffs[0];
mTable[pi].mDeltas[1] = mTable[pi+1].mCoeffs[1] - mTable[pi].mCoeffs[1];
mTable[pi].mDeltas[2] = mTable[pi+1].mCoeffs[2] - mTable[pi].mCoeffs[2];
mTable[pi].mDeltas[3] = mTable[pi+1].mCoeffs[3] - mTable[pi].mCoeffs[3];
}

const std::size_t pi{CubicPhaseCount - 1};
mTable[pi].mDeltas[0] = 0.0f - mTable[pi].mCoeffs[0];
mTable[pi].mDeltas[1] = mTable[0].mCoeffs[0] - mTable[pi].mCoeffs[1];
mTable[pi].mDeltas[2] = mTable[0].mCoeffs[1] - mTable[pi].mCoeffs[2];
mTable[pi].mDeltas[3] = mTable[0].mCoeffs[2] - mTable[pi].mCoeffs[3];
}


CubicFilter::CubicFilter()
{
static constexpr double IndexScale{512.0 / double{sTableSteps*2}};
Expand Down
3 changes: 3 additions & 0 deletions core/cubic_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct CubicTable {
struct GaussianTable : CubicTable { GaussianTable(); };
inline const GaussianTable gGaussianFilter;

struct SplineTable : CubicTable { SplineTable(); };
inline const SplineTable gSplineFilter;


struct CubicFilter {
static constexpr std::size_t sTableBits{8};
Expand Down
1 change: 1 addition & 0 deletions core/mixer/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ inline constexpr float GainSilenceThreshold{0.00001f}; /* -100dB */
enum class Resampler : std::uint8_t {
Point,
Linear,
Spline,
Gaussian,
FastBSinc12,
BSinc12,
Expand Down
1 change: 1 addition & 0 deletions core/voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void Voice::InitMixer(std::optional<std::string> resopt)
ResamplerEntry{"none"sv, Resampler::Point},
ResamplerEntry{"point"sv, Resampler::Point},
ResamplerEntry{"linear"sv, Resampler::Linear},
ResamplerEntry{"spline"sv, Resampler::Spline},
ResamplerEntry{"gaussian"sv, Resampler::Gaussian},
ResamplerEntry{"bsinc12"sv, Resampler::BSinc12},
ResamplerEntry{"fast_bsinc12"sv, Resampler::FastBSinc12},
Expand Down
1 change: 1 addition & 0 deletions utils/alsoft-config/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ constexpr std::array sampleTypeList{
constexpr std::array resamplerList{
NameValuePair{ "Point", "point" },
NameValuePair{ "Linear", "linear" },
NameValuePair{ "Cubic Spline", "spline" },
NameValuePair{ "4-point Gaussian", "gaussian" },
NameValuePair{ "Default (4-point Gaussian)", "" },
NameValuePair{ "11th order Sinc (fast)", "fast_bsinc12" },
Expand Down

0 comments on commit fdd1643

Please sign in to comment.