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.
This commit is contained in:
+7
-1
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user