Resampler: Optimization to f32, no LPF code path.

This commit is contained in:
David Reid
2026-02-07 15:40:10 +10:00
parent c0b57c3aea
commit 2ea55eaeaf
+115
View File
@@ -59325,6 +59325,109 @@ static MA_INLINE ma_result ma_linear_resampler_process_pcm_frames_s16_no_lpf(ma_
return MA_SUCCESS;
}
static MA_INLINE ma_result ma_linear_resampler_process_pcm_frames_f32_no_lpf(ma_linear_resampler* pResampler, const float* pFramesInF32, ma_uint64* pFrameCountIn, float* pFramesOutF32, ma_uint64* pFrameCountOut, float invSampleRateOut)
{
ma_uint64 frameCountIn;
ma_uint64 frameCountOut;
ma_uint64 framesProcessedIn;
ma_uint64 framesProcessedOut;
ma_uint32 c;
MA_ASSERT(pResampler != NULL);
MA_ASSERT(pFramesInF32 != NULL);
MA_ASSERT(pFrameCountIn != NULL);
MA_ASSERT(pFramesOutF32 != NULL);
MA_ASSERT(pFrameCountOut != NULL);
frameCountIn = *pFrameCountIn;
frameCountOut = *pFrameCountOut;
framesProcessedIn = 0;
framesProcessedOut = 0;
/* If there's a cached frame we need to process it. */
if (pResampler->inTimeInt == 0) {
MA_ASSERT(pResampler->cachedFrameCount <= 1); /* There is at most one cached frame. */
while (pResampler->cachedFrameCount > 0 && frameCountIn > 0 && framesProcessedOut < frameCountOut) {
float a = pResampler->inTimeFrac * invSampleRateOut;
for (c = 0; c < pResampler->channels; c += 1) {
pFramesOutF32[c] = ma_mix_f32_fast(pResampler->x0.f32[c], pFramesInF32[c], a);
}
pFramesOutF32 += pResampler->channels;
framesProcessedOut += 1;
/* Advance time forward. */
pResampler->inTimeInt += pResampler->inAdvanceInt;
pResampler->inTimeFrac += pResampler->inAdvanceFrac;
if (pResampler->inTimeFrac >= pResampler->sampleRateOut) {
pResampler->inTimeFrac -= pResampler->sampleRateOut;
pResampler->inTimeInt += 1;
}
/* Subtract one from the time to account for the cached frame, but only if the entire frame was processed. */
if (pResampler->inTimeInt > 0) {
pResampler->inTimeInt -= 1;
pResampler->cachedFrameCount = 0;
}
}
} else {
/* The rate must have changed between calls. Ignore the cached frame. */
}
while (framesProcessedOut < frameCountOut && pResampler->inTimeInt < frameCountIn) {
if (pResampler->inTimeInt + 1 < frameCountIn) {
float a = pResampler->inTimeFrac * invSampleRateOut;
for (c = 0; c < pResampler->channels; c += 1) {
pFramesOutF32[c] = ma_mix_f32_fast(pFramesInF32[(pResampler->inTimeInt * pResampler->channels) + c], pFramesInF32[((pResampler->inTimeInt + 1) * pResampler->channels) + c], a);
}
pFramesOutF32 += pResampler->channels;
framesProcessedOut += 1;
/* Advance time forward. */
pResampler->inTimeInt += pResampler->inAdvanceInt;
pResampler->inTimeFrac += pResampler->inAdvanceFrac;
if (pResampler->inTimeFrac >= pResampler->sampleRateOut) {
pResampler->inTimeFrac -= pResampler->sampleRateOut;
pResampler->inTimeInt += 1;
}
} else {
/*
There is not enough input frames to interpolate. We'll need to stop here. But it's important that we cache
the frame to ensure we make some forward progress.
*/
for (c = 0; c < pResampler->channels; c += 1) {
pResampler->x0.f32[c] = pFramesInF32[(pResampler->inTimeInt * pResampler->channels) + c];
}
pResampler->cachedFrameCount = 1;
pResampler->inTimeInt += 1;
break;
}
}
/* The number of frames we processed is simply the difference between our current time and previous time, clamped. */
framesProcessedIn = pResampler->inTimeInt;
if (framesProcessedIn > frameCountIn) { /* Should never overshoot when upsampling. Downsampling could overshoot. */
framesProcessedIn = frameCountIn;
}
if (pResampler->inTimeInt >= framesProcessedIn) {
pResampler->inTimeInt -= framesProcessedIn;
} else {
pResampler->inTimeInt = 0;
}
*pFrameCountIn = framesProcessedIn;
*pFrameCountOut = framesProcessedOut;
return MA_SUCCESS;
}
static ma_result ma_linear_resampler_process_pcm_frames_s16_downsample(ma_linear_resampler* pResampler, const void* pFramesIn, ma_uint64* pFrameCountIn, void* pFramesOut, ma_uint64* pFrameCountOut)
{
const ma_int16* pFramesInS16;
@@ -59528,6 +59631,11 @@ static ma_result ma_linear_resampler_process_pcm_frames_f32_downsample(ma_linear
framesProcessedOut = 0;
invSampleRateOut = 1.0f / pResampler->sampleRateOut;
if (pResampler->lpfOrder == 0) {
/* Fast path. No LPF needed. */
return ma_linear_resampler_process_pcm_frames_f32_no_lpf(pResampler, pFramesInF32, pFrameCountIn, pFramesOutF32, pFrameCountOut, invSampleRateOut);
} else {
/* Slow path. Need LPF. */
while (framesProcessedOut < frameCountOut) {
/* Before interpolating we need to load the buffers. When doing this we need to ensure we run every input sample through the filter. */
while (pResampler->inTimeInt > 0 && frameCountIn > framesProcessedIn) {
@@ -59583,6 +59691,7 @@ static ma_result ma_linear_resampler_process_pcm_frames_f32_downsample(ma_linear
return MA_SUCCESS;
}
}
static ma_result ma_linear_resampler_process_pcm_frames_f32_upsample(ma_linear_resampler* pResampler, const void* pFramesIn, ma_uint64* pFrameCountIn, void* pFramesOut, ma_uint64* pFrameCountOut)
{
@@ -59606,6 +59715,11 @@ static ma_result ma_linear_resampler_process_pcm_frames_f32_upsample(ma_linear_r
framesProcessedOut = 0;
invSampleRateOut = 1.0f / pResampler->sampleRateOut;
if (pResampler->lpfOrder == 0) {
/* Fast path. No LPF needed. */
return ma_linear_resampler_process_pcm_frames_f32_no_lpf(pResampler, pFramesInF32, pFrameCountIn, pFramesOutF32, pFrameCountOut, invSampleRateOut);
} else {
/* Slow path. Need LPF. */
while (framesProcessedOut < frameCountOut) {
/* Before interpolating we need to load the buffers. */
while (pResampler->inTimeInt > 0 && frameCountIn > framesProcessedIn) {
@@ -59661,6 +59775,7 @@ static ma_result ma_linear_resampler_process_pcm_frames_f32_upsample(ma_linear_r
return MA_SUCCESS;
}
}
static ma_result ma_linear_resampler_process_pcm_frames_f32(ma_linear_resampler* pResampler, const void* pFramesIn, ma_uint64* pFrameCountIn, void* pFramesOut, ma_uint64* pFrameCountOut)
{