Permalink
Browse files

Merge pull request #139 from toofishes/math

hairtunes: math and dithering optimisations
  • Loading branch information...
2 parents f12f7a7 + db8b181 commit ceb2fbcd68dcb3ed10de879eed67f22511436fd6 JH Laird committed Feb 26, 2012
Showing with 9 additions and 3 deletions.
  1. +9 −3 hairtunes.c
View
12 hairtunes.c
@@ -548,14 +548,20 @@ 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;
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;
}
@@ -575,7 +581,7 @@ static void biquad_init(biquad_t *bq, double a[], double b[]) {
}
static void biquad_lpf(biquad_t *bq, double freq, double Q) {
- double w0 = 2*M_PI*freq/((float)sampling_rate/(float)frame_size);
+ double w0 = 2.0 * M_PI * freq * frame_size / (double)sampling_rate;
double alpha = sin(w0)/(2.0*Q);
double a_0 = 1.0 + alpha;
@@ -706,7 +712,7 @@ static int stuff_buffer(double playback_rate, short *inptr, short *outptr) {
p_stuff = 1.0 - pow(1.0 - fabs(playback_rate-1.0), frame_size);
- if ((float)rand()/((float)RAND_MAX) < p_stuff) {
+ if (rand() < p_stuff * RAND_MAX) {
stuff = playback_rate > 1.0 ? -1 : 1;
stuffsamp = rand() % (frame_size - 1);
}

0 comments on commit ceb2fbc

Please sign in to comment.