From db8b1810388bec8bfd48f449f0428af133f90447 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sat, 25 Feb 2012 13:57:46 -0600 Subject: [PATCH] hairtunes: implement a simple LCG-based RNG for dithering We are calling out to libc's rand() function, which has significant overhead, especially since we are calling it 88200 times per second (2 channels * 44100 samples). Implement a very simple linear congruential generator in our code that is plenty good enough for dithering purposes, and small enough for the compiler to optimize and inline. --- hairtunes.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hairtunes.c b/hairtunes.c index 0d22a6b3..8b815111 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -548,6 +548,12 @@ static int init_rtp(void) { return port; } +static short lcg_rand(void) { + static unsigned long lcg_prev = 12345; + lcg_prev = lcg_prev * 69069 + 3; + return lcg_prev & 0xffff; +} + static inline short dithered_vol(short sample) { static short rand_a, rand_b; long out; @@ -555,7 +561,7 @@ static inline short dithered_vol(short sample) { out = (long)sample * fix_volume; if (fix_volume < 0x10000) { rand_b = rand_a; - rand_a = rand() & 0xffff; + rand_a = lcg_rand(); out += rand_a; out -= rand_b; }