From febedc3a389a88e29ee0237a87b6dee3dfbdec2f Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 21 Apr 2024 16:32:45 -0700 Subject: [PATCH] Util: Allow audio resampler to use different interpolators --- include/mgba-util/audio-resampler.h | 9 +++++++-- include/mgba-util/interpolator.h | 5 +++++ src/util/audio-resampler.c | 29 +++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/include/mgba-util/audio-resampler.h b/include/mgba-util/audio-resampler.h index 50f298930..e1ec3d84c 100644 --- a/include/mgba-util/audio-resampler.h +++ b/include/mgba-util/audio-resampler.h @@ -21,11 +21,16 @@ struct mAudioResampler { double timestamp; double lowWaterMark; double highWaterMark; - struct mInterpolatorSinc interp; + enum mInterpolatorType interpType; + union { + struct mInterpolator interp; + struct mInterpolatorSinc sinc; + struct mInterpolatorCosine cosine; + }; bool consume; }; -void mAudioResamplerInit(struct mAudioResampler*); +void mAudioResamplerInit(struct mAudioResampler*, enum mInterpolatorType); void mAudioResamplerDeinit(struct mAudioResampler*); void mAudioResamplerSetSource(struct mAudioResampler*, struct mAudioBuffer* source, double rate, bool consume); void mAudioResamplerSetDestination(struct mAudioResampler*, struct mAudioBuffer* destination, double rate); diff --git a/include/mgba-util/interpolator.h b/include/mgba-util/interpolator.h index 538115c37..b7a59acd5 100644 --- a/include/mgba-util/interpolator.h +++ b/include/mgba-util/interpolator.h @@ -10,6 +10,11 @@ CXX_GUARD_START +enum mInterpolatorType { + mINTERPOLATOR_SINC, + mINTERPOLATOR_COSINE, +}; + struct mInterpolationData { int16_t (*at)(int index, const void* context); void* context; diff --git a/src/util/audio-resampler.c b/src/util/audio-resampler.c index 0f0147c34..5b7a069e4 100644 --- a/src/util/audio-resampler.c +++ b/src/util/audio-resampler.c @@ -22,15 +22,32 @@ static int16_t _sampleAt(int index, const void* context) { return mAudioBufferPeek(data->resampler->source, data->channel, index); } -void mAudioResamplerInit(struct mAudioResampler* resampler) { +void mAudioResamplerInit(struct mAudioResampler* resampler, enum mInterpolatorType interpType) { memset(resampler, 0, sizeof(*resampler)); - mInterpolatorSincInit(&resampler->interp, 0, 0); - resampler->lowWaterMark = resampler->interp.width; - resampler->highWaterMark = 0; + resampler->interpType = interpType; + switch (interpType) { + case mINTERPOLATOR_SINC: + mInterpolatorSincInit(&resampler->sinc, 0, 0); + resampler->lowWaterMark = resampler->sinc.width; + resampler->highWaterMark = resampler->sinc.width; + break; + case mINTERPOLATOR_COSINE: + mInterpolatorCosineInit(&resampler->cosine, 0); + resampler->lowWaterMark = 0; + resampler->highWaterMark = 1; + break; + } } void mAudioResamplerDeinit(struct mAudioResampler* resampler) { - mInterpolatorSincDeinit(&resampler->interp); + switch (resampler->interpType) { + case mINTERPOLATOR_SINC: + mInterpolatorSincDeinit(&resampler->sinc); + break; + case mINTERPOLATOR_COSINE: + mInterpolatorCosineDeinit(&resampler->cosine); + break; + } resampler->source = NULL; resampler->destination = NULL; } @@ -50,7 +67,7 @@ size_t mAudioResamplerProcess(struct mAudioResampler* resampler) { int16_t sampleBuffer[MAX_CHANNELS] = {0}; double timestep = resampler->sourceRate / resampler->destRate; double timestamp = resampler->timestamp; - struct mInterpolator* interp = &resampler->interp.d; + struct mInterpolator* interp = &resampler->interp; struct mAudioResamplerData context = { .resampler = resampler, };