From 30a6ffec0fd7d4d930b7a891a4a18f7eadd34c8b Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 23 Dec 2017 15:35:33 -0500 Subject: [PATCH] Update samplerate.hpp API --- include/dsp/samplerate.hpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/include/dsp/samplerate.hpp b/include/dsp/samplerate.hpp index b7087b34..a4a99880 100644 --- a/include/dsp/samplerate.hpp +++ b/include/dsp/samplerate.hpp @@ -11,38 +11,33 @@ namespace rack { template struct SampleRateConverter { SpeexResamplerState *state = NULL; - bool noConversion = true; - int inRate = 44100; - int outRate = 44100; + bool bypass = false; SampleRateConverter() { int error; - state = speex_resampler_init(CHANNELS, inRate, outRate, SPEEX_RESAMPLER_QUALITY_DEFAULT, &error); + state = speex_resampler_init(CHANNELS, 44100, 44100, SPEEX_RESAMPLER_QUALITY_DEFAULT, &error); assert(error == RESAMPLER_ERR_SUCCESS); } ~SampleRateConverter() { speex_resampler_destroy(state); } - void setRates(int in, int out) { - if (in != inRate || out != outRate) { // speex doesn't optimize setting the rates to the existing values. - int error = speex_resampler_set_rate(state, in, out); - assert(error == RESAMPLER_ERR_SUCCESS); - inRate = in; - outRate = out; - noConversion = in == out; - } + void setQuality(int quality) { + speex_resampler_set_quality(state, quality); } - void setRatioSmooth(float ratio) DEPRECATED { - // FIXME: this doesn't do a smooth change -- speex doesn't appear to support that. - const int base = 1000; - setRates(base, ratio * base); + void setRates(int inRate, int outRate) { + spx_uint32_t oldInRate, oldOutRate; + speex_resampler_get_rate(state, &oldInRate, &oldOutRate); + if (inRate == (int) oldInRate && outRate == (int) oldOutRate) + return; + int error = speex_resampler_set_rate(state, inRate, outRate); + assert(error == RESAMPLER_ERR_SUCCESS); } /** `in` and `out` are interlaced with the number of channels */ void process(const Frame *in, int *inFrames, Frame *out, int *outFrames) { - if (noConversion) { + if (bypass) { int len = std::min(*inFrames, *outFrames); memcpy(out, in, len * sizeof(Frame)); *inFrames = len;