From d08a14462744654f7097f17943d8e2ddc0dfe93e Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 3 Feb 2016 15:01:35 +0100 Subject: resample: Improve GCD calculation Use Euclids algorithm to calculate the greatest common divisor to simplify the resample ratio fraction instead of the slow iterative method. Signed-off-by: Tristan Matthews --- libspeexdsp/resample.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/libspeexdsp/resample.c b/libspeexdsp/resample.c index 4e47d67..e69b309 100644 --- a/libspeexdsp/resample.c +++ b/libspeexdsp/resample.c @@ -1068,6 +1068,18 @@ EXPORT void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_r *out_rate = st->out_rate; } +static inline spx_uint32_t _gcd(spx_uint32_t a, spx_uint32_t b) +{ + while (b != 0) + { + spx_uint32_t temp = a; + + a = b; + b = temp % b; + } + return a; +} + EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate) { spx_uint32_t fact; @@ -1081,15 +1093,11 @@ EXPORT int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t r st->out_rate = out_rate; st->num_rate = ratio_num; st->den_rate = ratio_den; - /* FIXME: This is terribly inefficient, but who cares (at least for now)? */ - for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++) - { - while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) - { - st->num_rate /= fact; - st->den_rate /= fact; - } - } + + fact = _gcd (st->num_rate, st->den_rate); + + st->num_rate /= fact; + st->den_rate /= fact; if (old_den > 0) { -- cgit v1.2.3