reuse resampler object

This commit is contained in:
Eric Wasylishen 2011-01-17 15:34:15 -07:00
parent c1c1b1f749
commit edbad56202

View file

@ -2,8 +2,6 @@
#include "sound.h" #include "sound.h"
#include "speex_resampler.h" #include "speex_resampler.h"
// FIXME: call a real resampler :-)
void *Snd_Resample(int inrate, int inwidth, int innumsamples, int channels, const void *indata, void *Snd_Resample(int inrate, int inwidth, int innumsamples, int channels, const void *indata,
int outrate, int outwidth, int *outnumsamples) int outrate, int outwidth, int *outnumsamples)
{ {
@ -32,19 +30,36 @@ void *Snd_Resample(int inrate, int inwidth, int innumsamples, int channels, cons
} }
// Call the resampler // Call the resampler
SpeexResamplerState *resampler = speex_resampler_init(channels, inrate, outrate, 10, NULL); static SpeexResamplerState *st = NULL;
if (st == NULL)
{
st = speex_resampler_init(channels, inrate, outrate, 5, NULL);
}
else
{
speex_resampler_reset_mem(st);
}
speex_resampler_set_rate(st, inrate, outrate);
*outnumsamples = 0; *outnumsamples = 0;
unsigned int consumedtotal = 0; unsigned int consumedtotal = 0;
unsigned int outputtotal = 0;
unsigned int loops = 0; unsigned int loops = 0;
unsigned int consumed, output; unsigned int consumed, output;
while (consumedtotal < innumsamples) while (consumedtotal < innumsamples)
{ {
int roomToConsume, roomToOutput;
consumed = innumsamples - consumedtotal; consumed = innumsamples - consumedtotal;
output = maxsamples - (*outnumsamples); output = maxsamples - outputtotal;
speex_resampler_process_interleaved_int(resampler, in16bit, &consumed, outdata, &output);
roomToConsume = consumed;
roomToOutput = output;
speex_resampler_process_interleaved_int(st, in16bit + consumedtotal, &consumed, outdata + outputtotal, &output);
consumedtotal += consumed; consumedtotal += consumed;
(*outnumsamples) += output; outputtotal += output;
loops++; loops++;
if (loops > 100) if (loops > 100)
{ {
@ -52,7 +67,9 @@ void *Snd_Resample(int inrate, int inwidth, int innumsamples, int channels, cons
} }
} }
speex_resampler_destroy(resampler); *outnumsamples = outputtotal;
//speex_resampler_destroy(resampler);
if (in16bit != indata) if (in16bit != indata)
{ {