61
|
1
|
|
2 #include <math.h>
|
|
3
|
|
4 #include "mpg123.h"
|
|
5
|
|
6
|
|
7 static void
|
|
8 init_spline(float *x, float *y, int n, float *y2)
|
|
9 {
|
|
10
|
|
11 int i, k;
|
|
12 float p, qn, sig, un, *u;
|
|
13 u = (float *) g_malloc(n * sizeof(float));
|
|
14
|
|
15 y2[0] = u[0] = 0.0;
|
|
16
|
|
17 for (i = 1; i < n - 1; i++) {
|
|
18 sig = ((float) x[i] - x[i - 1]) / ((float) x[i + 1] - x[i - 1]);
|
|
19 p = sig * y2[i - 1] + 2.0;
|
|
20 y2[i] = (sig - 1.0) / p;
|
|
21 u[i] = (((float) y[i + 1] - y[i]) / (x[i + 1] - x[i])) -
|
|
22 (((float) y[i] - y[i - 1]) / (x[i] - x[i - 1]));
|
|
23 u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
|
|
24 }
|
|
25 qn = un = 0.0;
|
|
26
|
|
27 y2[n - 1] = (un - qn * u[n - 2]) / (qn * y2[n - 2] + 1.0);
|
|
28 for (k = n - 2; k >= 0; k--)
|
|
29 y2[k] = y2[k] * y2[k + 1] + u[k];
|
|
30 g_free(u);
|
|
31 }
|
|
32
|
|
33 static float
|
|
34 eval_spline(float xa[], float ya[], float y2a[], int n, float x)
|
|
35 {
|
|
36 int klo, khi, k;
|
|
37 float h, b, a;
|
|
38
|
|
39 klo = 0;
|
|
40 khi = n - 1;
|
|
41 while (khi - klo > 1) {
|
|
42 k = (khi + klo) >> 1;
|
|
43 if (xa[k] > x)
|
|
44 khi = k;
|
|
45 else
|
|
46 klo = k;
|
|
47 }
|
|
48 h = xa[khi] - xa[klo];
|
|
49 a = (xa[khi] - x) / h;
|
|
50 b = (x - xa[klo]) / h;
|
|
51 return (a * ya[klo] + b * ya[khi] +
|
|
52 ((a * a * a - a) * y2a[klo] + (b * b * b - b) * y2a[khi])
|
|
53 * (h * h) / 6.0);
|
|
54 }
|
|
55
|
|
56 void
|
|
57 mpg123_set_eq(int on, float preamp, float *b)
|
|
58 {
|
|
59 float x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, yf[10], val, band[10];
|
|
60 int bands[] = { 0, 4, 8, 16, 26, 78, 157, 313, 366, 418 };
|
|
61 int i, j;
|
|
62
|
|
63 mpg123_info->eq_active = on;
|
|
64 if (mpg123_info->eq_active) {
|
|
65 for (i = 0; i < 10; i++) {
|
|
66 band[i] = b[i] + preamp;
|
|
67 }
|
|
68
|
|
69 init_spline(x, band, 10, yf);
|
|
70 for (i = 0; i < 9; i++) {
|
|
71 for (j = bands[i]; j < bands[i + 1]; j++) {
|
|
72 val =
|
|
73 eval_spline(x, band, yf, 10,
|
|
74 i +
|
|
75 ((float) (j - bands[i]) *
|
|
76 (1.0 / (bands[i + 1] - bands[i]))));
|
|
77 mpg123_info->eq_mul[j] = pow(2, val / 10.0);
|
|
78 }
|
|
79 }
|
|
80 for (i = bands[9]; i < 576; i++)
|
|
81 mpg123_info->eq_mul[i] = mpg123_info->eq_mul[bands[9] - 1];
|
|
82 }
|
|
83 }
|