annotate src/audacious/iir.c @ 3759:9e54690956a0

finalised the API, I think.
author William Pitcock <nenolod@atheme.org>
date Sun, 14 Oct 2007 21:37:31 -0500
parents d1cacc65f091
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
1 /*
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
2 * PCM time-domain equalizer
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
3 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
4 * Copyright (C) 2002-2005 Felipe Rivera <liebremx at users sourceforge net>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
5 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
3121
3b6d316f8b09 GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents: 2313
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
9 * (at your option) any later version.
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
10 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful,
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
14 * GNU General Public License for more details.
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
15 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3121
3b6d316f8b09 GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents: 2313
diff changeset
17 * along with this program. If not, see <http://www.gnu.org/licenses>.
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
18 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
19 * $Id: iir.c,v 1.15 2005/10/17 01:57:59 liebremx Exp $
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
20 */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
21
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
22 #include <math.h>
3560
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
23
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
24 #include "main.h"
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
25 #include "iir.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
26
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
27 /* Coefficients */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
28 sIIRCoefficients *iir_cf;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
29
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
30 /* Volume gain
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
31 * values should be between 0.0 and 1.0
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
32 * Use the preamp from XMMS for now
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
33 * */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
34 float preamp[EQ_CHANNELS];
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
35
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
36 #ifdef BENCHMARK
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
37 #include "benchmark.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
38 double timex = 0.0;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
39 int count = 0;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
40 unsigned int blength = 0;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
41 #endif
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
42
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
43 /*
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
44 * Global vars
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
45 */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
46 gint rate;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
47 int band_count;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
48
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
49 void set_preamp(gint chn, float val)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
50 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
51 preamp[chn] = val;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
52 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
53
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
54 /* Init the filters */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
55 void init_iir()
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
56 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
57 calc_coeffs();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
58 #if 0
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
59 band_count = cfg.band_num;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
60 #endif
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
61
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
62 band_count = 10;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
63
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
64 rate = 44100;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
65
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
66 iir_cf = get_coeffs(&band_count, rate, TRUE);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
67 clean_history();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
68 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
69
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
70 #ifdef ARCH_X86
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
71 /* Round function provided by Frank Klemm which saves around 100K
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
72 * CPU cycles in my PIII for each call to the IIR function with 4K samples
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
73 */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
74 __inline__ int round_trick(float floatvalue_to_round)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
75 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
76 float floattmp ;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
77 int rounded_value ;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
78
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
79 floattmp = (int) 0x00FD8000L + (floatvalue_to_round);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
80 rounded_value = *(int*)(&floattmp) - (int)0x4B7D8000L;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
81
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
82 if ( rounded_value != (short) rounded_value )
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
83 rounded_value = ( rounded_value >> 31 ) ^ 0x7FFF;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
84 return rounded_value;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
85 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
86 #endif
3560
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
87
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
88 static void
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
89 byteswap(size_t size,
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
90 gint16 * buf)
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
91 {
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
92 gint16 *it;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
93 size &= ~1; /* must be multiple of 2 */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
94 for (it = buf; it < buf + size / 2; ++it)
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
95 *(guint16 *) it = GUINT16_SWAP_LE_BE(*(guint16 *) it);
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
96 }
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
97
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
98 void
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
99 iir_flow(FlowContext *context)
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
100 {
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
101 static int init = 0;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
102 int swapped = 0;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
103 guint myorder = G_BYTE_ORDER == G_LITTLE_ENDIAN ? FMT_S16_LE : FMT_S16_BE;
3561
d1cacc65f091 use context->fmt here.
William Pitcock <nenolod@atheme.org>
parents: 3560
diff changeset
104 int caneq = (context->fmt == FMT_S16_NE || context->fmt == myorder);
3560
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
105
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
106 if (!caneq && cfg.equalizer_active) { /* wrong byte order */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
107 byteswap(context->len, context->data); /* so convert */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
108 ++swapped;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
109 ++caneq;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
110 } /* can eq now, mark swapd */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
111 else if (caneq && !cfg.equalizer_active) /* right order but no eq */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
112 caneq = 0; /* so don't eq */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
113
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
114 if (caneq) { /* if eq enab */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
115 if (!init) { /* if first run */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
116 init_iir(); /* then init eq */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
117 ++init;
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
118 }
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
119
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
120 iir(&context->data, context->len, context->channels);
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
121
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
122 if (swapped) /* if was swapped */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
123 byteswap(context->len, context->data); /* swap back for output */
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
124 }
154f21dcd61e equalizer -> flow API
William Pitcock <nenolod@atheme.org>
parents: 3121
diff changeset
125 }