Util: Allow audio resampler to use different interpolators

This commit is contained in:
Vicki Pfau 2024-04-21 16:32:45 -07:00
parent afa8a25b5b
commit febedc3a38
3 changed files with 35 additions and 8 deletions

View File

@ -21,11 +21,16 @@ struct mAudioResampler {
double timestamp; double timestamp;
double lowWaterMark; double lowWaterMark;
double highWaterMark; double highWaterMark;
struct mInterpolatorSinc interp; enum mInterpolatorType interpType;
union {
struct mInterpolator interp;
struct mInterpolatorSinc sinc;
struct mInterpolatorCosine cosine;
};
bool consume; bool consume;
}; };
void mAudioResamplerInit(struct mAudioResampler*); void mAudioResamplerInit(struct mAudioResampler*, enum mInterpolatorType);
void mAudioResamplerDeinit(struct mAudioResampler*); void mAudioResamplerDeinit(struct mAudioResampler*);
void mAudioResamplerSetSource(struct mAudioResampler*, struct mAudioBuffer* source, double rate, bool consume); void mAudioResamplerSetSource(struct mAudioResampler*, struct mAudioBuffer* source, double rate, bool consume);
void mAudioResamplerSetDestination(struct mAudioResampler*, struct mAudioBuffer* destination, double rate); void mAudioResamplerSetDestination(struct mAudioResampler*, struct mAudioBuffer* destination, double rate);

View File

@ -10,6 +10,11 @@
CXX_GUARD_START CXX_GUARD_START
enum mInterpolatorType {
mINTERPOLATOR_SINC,
mINTERPOLATOR_COSINE,
};
struct mInterpolationData { struct mInterpolationData {
int16_t (*at)(int index, const void* context); int16_t (*at)(int index, const void* context);
void* context; void* context;

View File

@ -22,15 +22,32 @@ static int16_t _sampleAt(int index, const void* context) {
return mAudioBufferPeek(data->resampler->source, data->channel, index); 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)); memset(resampler, 0, sizeof(*resampler));
mInterpolatorSincInit(&resampler->interp, 0, 0); resampler->interpType = interpType;
resampler->lowWaterMark = resampler->interp.width; switch (interpType) {
resampler->highWaterMark = 0; 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) { 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->source = NULL;
resampler->destination = NULL; resampler->destination = NULL;
} }
@ -50,7 +67,7 @@ size_t mAudioResamplerProcess(struct mAudioResampler* resampler) {
int16_t sampleBuffer[MAX_CHANNELS] = {0}; int16_t sampleBuffer[MAX_CHANNELS] = {0};
double timestep = resampler->sourceRate / resampler->destRate; double timestep = resampler->sourceRate / resampler->destRate;
double timestamp = resampler->timestamp; double timestamp = resampler->timestamp;
struct mInterpolator* interp = &resampler->interp.d; struct mInterpolator* interp = &resampler->interp;
struct mAudioResamplerData context = { struct mAudioResamplerData context = {
.resampler = resampler, .resampler = resampler,
}; };