annotate Plugins/Input/mpg123/equalizer.c @ 976:4dfb85985fe0 trunk

[svn] - Let's not forget the year and track number!
author nhjm449
date Wed, 26 Apr 2006 21:14:48 -0700
parents fa848bd484d8
children b5ae09a6c2f1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
1
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
2 #include <math.h>
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
3
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
4 #include "mpg123.h"
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
5
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
6
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
7 static void
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
8 init_spline(float *x, float *y, int n, float *y2)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
9 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
10
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
11 int i, k;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
12 float p, qn, sig, un, *u;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
13 u = (float *) g_malloc(n * sizeof(float));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
14
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
15 y2[0] = u[0] = 0.0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
16
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
17 for (i = 1; i < n - 1; i++) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
18 sig = ((float) x[i] - x[i - 1]) / ((float) x[i + 1] - x[i - 1]);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
19 p = sig * y2[i - 1] + 2.0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
20 y2[i] = (sig - 1.0) / p;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
21 u[i] = (((float) y[i + 1] - y[i]) / (x[i + 1] - x[i])) -
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
22 (((float) y[i] - y[i - 1]) / (x[i] - x[i - 1]));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
23 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
24 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
25 qn = un = 0.0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
26
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
27 y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
28 for (k = n - 2; k >= 0; k--)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
29 y2[k] = y2[k] * y2[k + 1] + u[k];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
30 g_free(u);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
31 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
32
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
33 static float
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
34 eval_spline(float xa[], float ya[], float y2a[], int n, float x)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
35 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
36 int klo, khi, k;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
37 float h, b, a;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
38
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
39 klo = 0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
40 khi = n - 1;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
41 while (khi - klo > 1) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
42 k = (khi + klo) >> 1;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
43 if (xa[k] > x)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
44 khi = k;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
45 else
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
46 klo = k;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
47 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
48 h = xa[khi] - xa[klo];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
49 a = (xa[khi] - x) / h;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
50 b = (x - xa[klo]) / h;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
51 return (a * ya[klo] + b * ya[khi] +
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
52 ((a * a * a - a) * y2a[klo] + (b * b * b - b) * y2a[khi])
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
53 * (h * h) / 6.0);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
54 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
55
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
56 void
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
57 mpg123_set_eq(int on, float preamp, float *b)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
58 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
59 float x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, yf[10], val, band[10];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
60 int bands[] = { 0, 4, 8, 16, 26, 78, 157, 313, 366, 418 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
61 int i, j;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
62
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
63 mpg123_info->eq_active = on;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
64 if (mpg123_info->eq_active) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
65 for (i = 0; i < 10; i++) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
66 band[i] = b[i] + preamp;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
67 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
68
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
69 init_spline(x, band, 10, yf);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
70 for (i = 0; i < 9; i++) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
71 for (j = bands[i]; j < bands[i + 1]; j++) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
72 val =
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
73 eval_spline(x, band, yf, 10,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
74 i +
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
75 ((float) (j - bands[i]) *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
76 (1.0 / (bands[i + 1] - bands[i]))));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
77 mpg123_info->eq_mul[j] = pow(2, val / 10.0);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
78 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
79 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
80 for (i = bands[9]; i < 576; i++)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
81 mpg123_info->eq_mul[i] = mpg123_info->eq_mul[bands[9] - 1];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
82 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
83 }