Mercurial > audlegacy
annotate audacious/fft.c @ 2155:840263ee3a5c trunk
[svn] - fix the logic of bmp_playback_stop(). now JTF works well even if the playing is paused.
author | yaz |
---|---|
date | Mon, 18 Dec 2006 03:34:52 -0800 |
parents | f18a5b617c34 |
children | 86f0443d0de2 |
rev | line source |
---|---|
0 | 1 /* fft.c: Iterative implementation of a FFT |
2 * Copyright (C) 1999 Richard Boulton <richard@tartarus.org> | |
3 * Convolution stuff by Ralph Loader <suckfish@ihug.co.nz> | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
2105
f18a5b617c34
[svn] - move to GPLv2-only. Based on my interpretation of the license, we are
nenolod
parents:
1459
diff
changeset
|
7 * the Free Software Foundation; under version 2 of the License. |
0 | 8 * |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
1459 | 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 17 */ |
18 | |
19 /* | |
20 * TODO | |
21 * Remove compiling in of FFT_BUFFER_SIZE? (Might slow things down, but would | |
22 * be nice to be able to change size at runtime.) | |
23 * Finish making / checking thread-safety. | |
24 * More optimisations. | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 # include "config.h" | |
29 #endif | |
30 | |
31 #include "fft.h" | |
32 | |
33 #include <glib.h> | |
34 #include <stdlib.h> | |
35 #include <math.h> | |
36 #ifndef PI | |
37 #ifdef M_PI | |
38 #define PI M_PI | |
39 #else | |
40 #define PI 3.14159265358979323846 /* pi */ | |
41 #endif | |
42 #endif | |
43 | |
44 /* ########### */ | |
45 /* # Structs # */ | |
46 /* ########### */ | |
47 | |
48 struct _struct_fft_state { | |
49 /* Temporary data stores to perform FFT in. */ | |
50 float real[FFT_BUFFER_SIZE]; | |
51 float imag[FFT_BUFFER_SIZE]; | |
52 }; | |
53 | |
54 /* ############################# */ | |
55 /* # Local function prototypes # */ | |
56 /* ############################# */ | |
57 | |
58 static void fft_prepare(const sound_sample * input, float *re, float *im); | |
59 static void fft_calculate(float *re, float *im); | |
60 static void fft_output(const float *re, const float *im, float *output); | |
61 static int reverseBits(unsigned int initial); | |
62 | |
63 /* #################### */ | |
64 /* # Global variables # */ | |
65 /* #################### */ | |
66 | |
67 /* Table to speed up bit reverse copy */ | |
68 static unsigned int bitReverse[FFT_BUFFER_SIZE]; | |
69 | |
70 /* The next two tables could be made to use less space in memory, since they | |
71 * overlap hugely, but hey. */ | |
72 static float sintable[FFT_BUFFER_SIZE / 2]; | |
73 static float costable[FFT_BUFFER_SIZE / 2]; | |
74 | |
75 /* ############################## */ | |
76 /* # Externally called routines # */ | |
77 /* ############################## */ | |
78 | |
79 /* --------- */ | |
80 /* FFT stuff */ | |
81 /* --------- */ | |
82 | |
83 /* | |
84 * Initialisation routine - sets up tables and space to work in. | |
85 * Returns a pointer to internal state, to be used when performing calls. | |
86 * On error, returns NULL. | |
87 * The pointer should be freed when it is finished with, by fft_close(). | |
88 */ | |
89 fft_state * | |
90 fft_init(void) | |
91 { | |
92 fft_state *state; | |
93 unsigned int i; | |
94 | |
95 state = (fft_state *) g_malloc(sizeof(fft_state)); | |
96 if (!state) | |
97 return NULL; | |
98 | |
99 for (i = 0; i < FFT_BUFFER_SIZE; i++) { | |
100 bitReverse[i] = reverseBits(i); | |
101 } | |
102 for (i = 0; i < FFT_BUFFER_SIZE / 2; i++) { | |
103 float j = 2 * PI * i / FFT_BUFFER_SIZE; | |
104 costable[i] = cos(j); | |
105 sintable[i] = sin(j); | |
106 } | |
107 | |
108 return state; | |
109 } | |
110 | |
111 /* | |
112 * Do all the steps of the FFT, taking as input sound data (as described in | |
113 * sound.h) and returning the intensities of each frequency as floats in the | |
114 * range 0 to ((FFT_BUFFER_SIZE / 2) * 32768) ^ 2 | |
115 * | |
116 * FIXME - the above range assumes no frequencies present have an amplitude | |
117 * larger than that of the sample variation. But this is false: we could have | |
118 * a wave such that its maximums are always between samples, and it's just | |
119 * inside the representable range at the places samples get taken. | |
120 * Question: what _is_ the maximum value possible. Twice that value? Root | |
121 * two times that value? Hmmm. Think it depends on the frequency, too. | |
122 * | |
123 * The input array is assumed to have FFT_BUFFER_SIZE elements, | |
124 * and the output array is assumed to have (FFT_BUFFER_SIZE / 2 + 1) elements. | |
125 * state is a (non-NULL) pointer returned by fft_init. | |
126 */ | |
127 void | |
128 fft_perform(const sound_sample * input, float *output, fft_state * state) | |
129 { | |
130 /* Convert data from sound format to be ready for FFT */ | |
131 fft_prepare(input, state->real, state->imag); | |
132 | |
133 /* Do the actual FFT */ | |
134 fft_calculate(state->real, state->imag); | |
135 | |
136 /* Convert the FFT output into intensities */ | |
137 fft_output(state->real, state->imag, output); | |
138 } | |
139 | |
140 /* | |
141 * Free the state. | |
142 */ | |
143 void | |
144 fft_close(fft_state * state) | |
145 { | |
146 if (state) | |
147 free(state); | |
148 } | |
149 | |
150 /* ########################### */ | |
151 /* # Locally called routines # */ | |
152 /* ########################### */ | |
153 | |
154 /* | |
155 * Prepare data to perform an FFT on | |
156 */ | |
157 static void | |
158 fft_prepare(const sound_sample * input, float *re, float *im) | |
159 { | |
160 unsigned int i; | |
161 float *realptr = re; | |
162 float *imagptr = im; | |
163 | |
164 /* Get input, in reverse bit order */ | |
165 for (i = 0; i < FFT_BUFFER_SIZE; i++) { | |
166 *realptr++ = input[bitReverse[i]]; | |
167 *imagptr++ = 0; | |
168 } | |
169 } | |
170 | |
171 /* | |
172 * Take result of an FFT and calculate the intensities of each frequency | |
173 * Note: only produces half as many data points as the input had. | |
174 * This is roughly a consequence of the Nyquist sampling theorm thingy. | |
175 * (FIXME - make this comment better, and helpful.) | |
176 * | |
177 * The two divisions by 4 are also a consequence of this: the contributions | |
178 * returned for each frequency are split into two parts, one at i in the | |
179 * table, and the other at FFT_BUFFER_SIZE - i, except for i = 0 and | |
180 * FFT_BUFFER_SIZE which would otherwise get float (and then 4* when squared) | |
181 * the contributions. | |
182 */ | |
183 static void | |
184 fft_output(const float *re, const float *im, float *output) | |
185 { | |
186 float *outputptr = output; | |
187 const float *realptr = re; | |
188 const float *imagptr = im; | |
189 float *endptr = output + FFT_BUFFER_SIZE / 2; | |
190 | |
191 #ifdef DEBUG | |
192 unsigned int i, j; | |
193 #endif | |
194 | |
195 while (outputptr <= endptr) { | |
196 *outputptr = (*realptr * *realptr) + (*imagptr * *imagptr); | |
197 outputptr++; | |
198 realptr++; | |
199 imagptr++; | |
200 } | |
201 /* Do divisions to keep the constant and highest frequency terms in scale | |
202 * with the other terms. */ | |
203 *output /= 4; | |
204 *endptr /= 4; | |
205 | |
206 #ifdef DEBUG | |
207 printf("Recalculated input:\n"); | |
208 for (i = 0; i < FFT_BUFFER_SIZE; i++) { | |
209 float val_real = 0; | |
210 float val_imag = 0; | |
211 for (j = 0; j < FFT_BUFFER_SIZE; j++) { | |
212 float fact_real = cos(-2 * j * i * PI / FFT_BUFFER_SIZE); | |
213 float fact_imag = sin(-2 * j * i * PI / FFT_BUFFER_SIZE); | |
214 val_real += fact_real * re[j] - fact_imag * im[j]; | |
215 val_imag += fact_real * im[j] + fact_imag * re[j]; | |
216 } | |
217 printf("%5d = %8f + i * %8f\n", i, | |
218 val_real / FFT_BUFFER_SIZE, val_imag / FFT_BUFFER_SIZE); | |
219 } | |
220 printf("\n"); | |
221 #endif | |
222 } | |
223 | |
224 /* | |
225 * Actually perform the FFT | |
226 */ | |
227 static void | |
228 fft_calculate(float *re, float *im) | |
229 { | |
230 unsigned int i, j, k; | |
231 unsigned int exchanges; | |
232 float fact_real, fact_imag; | |
233 float tmp_real, tmp_imag; | |
234 unsigned int factfact; | |
235 | |
236 /* Set up some variables to reduce calculation in the loops */ | |
237 exchanges = 1; | |
238 factfact = FFT_BUFFER_SIZE / 2; | |
239 | |
240 /* Loop through the divide and conquer steps */ | |
241 for (i = FFT_BUFFER_SIZE_LOG; i != 0; i--) { | |
242 /* In this step, we have 2 ^ (i - 1) exchange groups, each with | |
243 * 2 ^ (FFT_BUFFER_SIZE_LOG - i) exchanges | |
244 */ | |
245 /* Loop through the exchanges in a group */ | |
246 for (j = 0; j != exchanges; j++) { | |
247 /* Work out factor for this exchange | |
248 * factor ^ (exchanges) = -1 | |
249 * So, real = cos(j * PI / exchanges), | |
250 * imag = sin(j * PI / exchanges) | |
251 */ | |
252 fact_real = costable[j * factfact]; | |
253 fact_imag = sintable[j * factfact]; | |
254 | |
255 /* Loop through all the exchange groups */ | |
256 for (k = j; k < FFT_BUFFER_SIZE; k += exchanges << 1) { | |
257 int k1 = k + exchanges; | |
258 /* newval[k] := val[k] + factor * val[k1] | |
259 * newval[k1] := val[k] - factor * val[k1] | |
260 **/ | |
261 #ifdef DEBUG | |
262 printf("%d %d %d\n", i, j, k); | |
263 printf("Exchange %d with %d\n", k, k1); | |
264 printf("Factor %9f + i * %8f\n", fact_real, fact_imag); | |
265 #endif | |
266 /* FIXME - potential scope for more optimization here? */ | |
267 tmp_real = fact_real * re[k1] - fact_imag * im[k1]; | |
268 tmp_imag = fact_real * im[k1] + fact_imag * re[k1]; | |
269 re[k1] = re[k] - tmp_real; | |
270 im[k1] = im[k] - tmp_imag; | |
271 re[k] += tmp_real; | |
272 im[k] += tmp_imag; | |
273 #ifdef DEBUG | |
274 for (k1 = 0; k1 < FFT_BUFFER_SIZE; k1++) { | |
275 printf("%5d = %8f + i * %8f\n", k1, real[k1], imag[k1]); | |
276 } | |
277 #endif | |
278 } | |
279 } | |
280 exchanges <<= 1; | |
281 factfact >>= 1; | |
282 } | |
283 } | |
284 | |
285 static int | |
286 reverseBits(unsigned int initial) | |
287 { | |
288 unsigned int reversed = 0, loop; | |
289 for (loop = 0; loop < FFT_BUFFER_SIZE_LOG; loop++) { | |
290 reversed <<= 1; | |
291 reversed += (initial & 1); | |
292 initial >>= 1; | |
293 } | |
294 return reversed; | |
295 } |