Mercurial > audlegacy
annotate audacious/visualization.c @ 1557:dc3ae578e7b6 trunk
[svn] - make it compile
author | nenolod |
---|---|
date | Thu, 10 Aug 2006 19:21:37 -0700 |
parents | 697a073fd4f5 |
children | f3934d790a2e |
rev | line source |
---|---|
0 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
1459 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 20 */ |
21 | |
22 #include "visualization.h" | |
23 | |
24 #include <glib.h> | |
25 #include <stdlib.h> | |
26 #include <math.h> | |
27 #include <string.h> | |
28 | |
29 #include "fft.h" | |
30 #include "input.h" | |
31 #include "main.h" | |
538
e4e897d20791
[svn] remove libaudcore, we never did anything with it
nenolod
parents:
284
diff
changeset
|
32 #include "playback.h" |
0 | 33 #include "plugin.h" |
34 #include "prefswin.h" | |
1547 | 35 #include "widgets/widgetcore.h" |
0 | 36 |
37 VisPluginData vp_data = { | |
38 NULL, | |
39 NULL, | |
40 FALSE | |
41 }; | |
42 | |
43 GList * | |
44 get_vis_list(void) | |
45 { | |
46 return vp_data.vis_list; | |
47 } | |
48 | |
49 GList * | |
50 get_vis_enabled_list(void) | |
51 { | |
52 return vp_data.enabled_list; | |
53 } | |
54 | |
55 void | |
56 vis_disable_plugin(VisPlugin * vp) | |
57 { | |
58 gint i = g_list_index(vp_data.vis_list, vp); | |
59 enable_vis_plugin(i, FALSE); | |
60 } | |
61 | |
62 void | |
63 vis_about(gint i) | |
64 { | |
65 GList *node = g_list_nth(vp_data.vis_list, i); | |
66 | |
67 if (node && node->data && VIS_PLUGIN(node->data)->about) | |
68 VIS_PLUGIN(node->data)->about(); | |
69 } | |
70 | |
71 void | |
72 vis_configure(gint i) | |
73 { | |
74 GList *node = g_list_nth(vp_data.vis_list, i); | |
75 | |
76 if (node && node->data && VIS_PLUGIN(node->data)->configure) | |
77 VIS_PLUGIN(node->data)->configure(); | |
78 } | |
79 | |
80 void | |
81 vis_playback_start(void) | |
82 { | |
83 GList *node; | |
84 VisPlugin *vp; | |
85 | |
86 if (vp_data.playback_started) | |
87 return; | |
88 | |
89 for (node = vp_data.enabled_list; node; node = g_list_next(node)) { | |
90 vp = node->data; | |
91 if (vp->playback_start) | |
92 vp->playback_start(); | |
93 } | |
94 vp_data.playback_started = TRUE; | |
95 } | |
96 | |
97 void | |
98 vis_playback_stop(void) | |
99 { | |
100 GList *node = vp_data.enabled_list; | |
101 VisPlugin *vp; | |
102 | |
103 if (!vp_data.playback_started) | |
104 return; | |
105 | |
106 for (node = vp_data.enabled_list; node; node = g_list_next(node)) { | |
107 vp = node->data; | |
108 if (vp->playback_stop) | |
109 vp->playback_stop(); | |
110 } | |
111 vp_data.playback_started = FALSE; | |
112 } | |
113 | |
114 void | |
115 enable_vis_plugin(gint i, gboolean enable) | |
116 { | |
117 GList *node = g_list_nth(vp_data.vis_list, i); | |
118 VisPlugin *vp; | |
119 | |
120 if (!node || !(node->data)) | |
121 return; | |
122 vp = node->data; | |
123 | |
124 if (enable && !g_list_find(vp_data.enabled_list, vp)) { | |
125 vp_data.enabled_list = g_list_append(vp_data.enabled_list, vp); | |
126 if (vp->init) | |
127 vp->init(); | |
128 if (bmp_playback_get_playing() && vp->playback_start) | |
129 vp->playback_start(); | |
130 } | |
131 else if (!enable && g_list_find(vp_data.enabled_list, vp)) { | |
132 vp_data.enabled_list = g_list_remove(vp_data.enabled_list, vp); | |
133 if (bmp_playback_get_playing() && vp->playback_stop) | |
134 vp->playback_stop(); | |
135 if (vp->cleanup) | |
136 vp->cleanup(); | |
137 } | |
138 } | |
139 | |
140 gboolean | |
141 vis_enabled(gint i) | |
142 { | |
143 return (g_list_find | |
144 (vp_data.enabled_list, | |
145 g_list_nth(vp_data.vis_list, i)->data) != NULL); | |
146 } | |
147 | |
148 gchar * | |
149 vis_stringify_enabled_list(void) | |
150 { | |
151 gchar *enalist = NULL, *tmp, *tmp2; | |
152 GList *node = vp_data.enabled_list; | |
153 | |
154 if (g_list_length(node)) { | |
155 enalist = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
156 for (node = g_list_next(node); node != NULL; node = g_list_next(node)) { | |
157 tmp = enalist; | |
158 tmp2 = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
159 enalist = g_strconcat(tmp, ",", tmp2, NULL); | |
160 g_free(tmp); | |
161 g_free(tmp2); | |
162 } | |
163 } | |
164 return enalist; | |
165 } | |
166 | |
167 void | |
168 vis_enable_from_stringified_list(gchar * list) | |
169 { | |
170 gchar **plugins, *base; | |
171 GList *node; | |
172 gint i; | |
173 VisPlugin *vp; | |
174 | |
175 if (!list || !strcmp(list, "")) | |
176 return; | |
177 plugins = g_strsplit(list, ",", 0); | |
178 for (i = 0; plugins[i]; i++) { | |
179 for (node = vp_data.vis_list; node != NULL; node = g_list_next(node)) { | |
180 base = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
181 if (!strcmp(plugins[i], base)) { | |
182 vp = node->data; | |
183 vp_data.enabled_list = | |
184 g_list_append(vp_data.enabled_list, vp); | |
185 if (vp->init) | |
186 vp->init(); | |
187 if (bmp_playback_get_playing() && vp->playback_start) | |
188 vp->playback_start(); | |
189 } | |
190 g_free(base); | |
191 } | |
192 } | |
193 g_strfreev(plugins); | |
194 } | |
195 | |
196 static void | |
197 calc_stereo_pcm(gint16 dest[2][512], gint16 src[2][512], gint nch) | |
198 { | |
199 memcpy(dest[0], src[0], 512 * sizeof(gint16)); | |
200 if (nch == 1) | |
201 memcpy(dest[1], src[0], 512 * sizeof(gint16)); | |
202 else | |
203 memcpy(dest[1], src[1], 512 * sizeof(gint16)); | |
204 } | |
205 | |
206 static void | |
207 calc_mono_pcm(gint16 dest[2][512], gint16 src[2][512], gint nch) | |
208 { | |
209 gint i; | |
210 gint16 *d, *sl, *sr; | |
211 | |
212 if (nch == 1) | |
213 memcpy(dest[0], src[0], 512 * sizeof(gint16)); | |
214 else { | |
215 d = dest[0]; | |
216 sl = src[0]; | |
217 sr = src[1]; | |
218 for (i = 0; i < 512; i++) { | |
219 *(d++) = (*(sl++) + *(sr++)) >> 1; | |
220 } | |
221 } | |
222 } | |
223 | |
224 static void | |
225 calc_freq(gint16 * dest, gint16 * src) | |
226 { | |
227 static fft_state *state = NULL; | |
228 gfloat tmp_out[257]; | |
229 gint i; | |
230 | |
231 if (!state) | |
232 state = fft_init(); | |
233 | |
234 fft_perform(src, tmp_out, state); | |
235 | |
236 for (i = 0; i < 256; i++) | |
237 dest[i] = ((gint) sqrt(tmp_out[i + 1])) >> 8; | |
238 } | |
239 | |
240 static void | |
241 calc_mono_freq(gint16 dest[2][256], gint16 src[2][512], gint nch) | |
242 { | |
243 gint i; | |
244 gint16 *d, *sl, *sr, tmp[512]; | |
245 | |
246 if (nch == 1) | |
247 calc_freq(dest[0], src[0]); | |
248 else { | |
249 d = tmp; | |
250 sl = src[0]; | |
251 sr = src[1]; | |
252 for (i = 0; i < 512; i++) { | |
253 *(d++) = (*(sl++) + *(sr++)) >> 1; | |
254 } | |
255 calc_freq(dest[0], tmp); | |
256 } | |
257 } | |
258 | |
259 static void | |
260 calc_stereo_freq(gint16 dest[2][256], gint16 src[2][512], gint nch) | |
261 { | |
262 calc_freq(dest[0], src[0]); | |
263 | |
264 if (nch == 2) | |
265 calc_freq(dest[1], src[1]); | |
266 else | |
267 memcpy(dest[1], dest[0], 256 * sizeof(gint16)); | |
268 } | |
269 | |
270 void | |
271 vis_send_data(gint16 pcm_data[2][512], gint nch, gint length) | |
272 { | |
273 GList *node = vp_data.enabled_list; | |
274 VisPlugin *vp; | |
275 gint16 mono_freq[2][256], stereo_freq[2][256]; | |
276 gboolean mono_freq_calced = FALSE, stereo_freq_calced = FALSE; | |
277 gint16 mono_pcm[2][512], stereo_pcm[2][512]; | |
278 gboolean mono_pcm_calced = FALSE, stereo_pcm_calced = FALSE; | |
279 guint8 intern_vis_data[512]; | |
280 gint i; | |
281 | |
282 if (!pcm_data || nch < 1) { | |
283 if (cfg.vis_type != VIS_OFF) { | |
284 if (cfg.player_shaded && cfg.player_visible) | |
285 svis_timeout_func(mainwin_svis, NULL); | |
286 else | |
287 vis_timeout_func(active_vis, NULL); | |
288 } | |
289 return; | |
290 } | |
291 | |
292 while (node) { | |
293 vp = node->data; | |
294 if (vp->num_pcm_chs_wanted > 0 && vp->render_pcm) { | |
295 if (vp->num_pcm_chs_wanted == 1) { | |
296 if (!mono_pcm_calced) { | |
297 calc_mono_pcm(mono_pcm, pcm_data, nch); | |
298 mono_pcm_calced = TRUE; | |
299 } | |
300 vp->render_pcm(mono_pcm); | |
301 } | |
302 else { | |
303 if (!stereo_pcm_calced) { | |
304 calc_stereo_pcm(stereo_pcm, pcm_data, nch); | |
305 stereo_pcm_calced = TRUE; | |
306 } | |
307 vp->render_pcm(stereo_pcm); | |
308 } | |
309 } | |
310 if (vp->num_freq_chs_wanted > 0 && vp->render_freq) { | |
311 if (vp->num_freq_chs_wanted == 1) { | |
312 if (!mono_freq_calced) { | |
313 calc_mono_freq(mono_freq, pcm_data, nch); | |
314 mono_freq_calced = TRUE; | |
315 } | |
316 vp->render_freq(mono_freq); | |
317 } | |
318 else { | |
319 if (!stereo_freq_calced) { | |
320 calc_stereo_freq(stereo_freq, pcm_data, nch); | |
321 stereo_freq_calced = TRUE; | |
322 } | |
323 vp->render_freq(stereo_freq); | |
324 } | |
325 } | |
326 node = g_list_next(node); | |
327 } | |
328 | |
329 if (cfg.vis_type == VIS_OFF) | |
330 return; | |
331 | |
332 if (cfg.vis_type == VIS_ANALYZER) { | |
333 if (cfg.player_shaded && cfg.player_visible) { | |
334 /* VU */ | |
335 gint vu, val; | |
336 | |
337 if (!stereo_pcm_calced) | |
338 calc_stereo_pcm(stereo_pcm, pcm_data, nch); | |
339 vu = 0; | |
340 for (i = 0; i < 512; i++) { | |
341 val = abs(stereo_pcm[0][i]); | |
342 if (val > vu) | |
343 vu = val; | |
344 } | |
345 intern_vis_data[0] = (vu * 37) >> 15; | |
346 if (intern_vis_data[0] > 37) | |
347 intern_vis_data[0] = 37; | |
348 if (nch == 2) { | |
349 vu = 0; | |
350 for (i = 0; i < 512; i++) { | |
351 val = abs(stereo_pcm[1][i]); | |
352 if (val > vu) | |
353 vu = val; | |
354 } | |
355 intern_vis_data[1] = (vu * 37) >> 15; | |
356 if (intern_vis_data[1] > 37) | |
357 intern_vis_data[1] = 37; | |
358 } | |
359 else | |
360 intern_vis_data[1] = intern_vis_data[0]; | |
361 } | |
362 else { | |
363 /* Spectrum analyzer */ | |
364 /* 76 values */ | |
365 const gint long_xscale[] = | |
366 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | |
367 17, 18, | |
368 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, | |
369 34, | |
370 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, | |
371 50, 51, | |
372 52, 53, 54, 55, 56, 57, 58, 61, 66, 71, 76, 81, 87, 93, | |
373 100, 107, | |
374 114, 122, 131, 140, 150, 161, 172, 184, 255 | |
375 }; | |
376 /* 20 values */ | |
377 const int short_xscale[] = | |
378 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 15, 20, 27, | |
379 36, 47, 62, 82, 107, 141, 184, 255 | |
380 }; | |
381 const double y_scale = 3.60673760222; /* 20.0 / log(256) */ | |
382 const int *xscale; | |
383 gint j, y, max; | |
384 | |
385 if (!mono_freq_calced) | |
386 calc_mono_freq(mono_freq, pcm_data, nch); | |
387 | |
388 memset(intern_vis_data, 0, 75); | |
389 | |
390 if (cfg.analyzer_type == ANALYZER_BARS) { | |
391 max = 19; | |
392 xscale = short_xscale; | |
393 } | |
394 else { | |
395 max = 75; | |
396 xscale = long_xscale; | |
397 } | |
398 | |
399 for (i = 0; i < max; i++) { | |
400 for (j = xscale[i], y = 0; j < xscale[i + 1]; j++) { | |
401 if (mono_freq[0][j] > y) | |
402 y = mono_freq[0][j]; | |
403 } | |
404 y >>= 7; | |
405 if (y != 0) { | |
406 intern_vis_data[i] = log(y) * y_scale; | |
407 if (intern_vis_data[i] > 15) | |
408 intern_vis_data[i] = 15; | |
409 } | |
410 else | |
411 intern_vis_data[i] = 0; | |
412 } | |
413 } | |
414 } | |
415 else { /* (cfg.vis_type == VIS_SCOPE) */ | |
416 | |
417 /* Osciloscope */ | |
418 gint pos, step; | |
419 | |
420 if (!mono_pcm_calced) | |
421 calc_mono_pcm(mono_pcm, pcm_data, nch); | |
422 | |
423 step = (length << 8) / 74; | |
424 for (i = 0, pos = 0; i < 75; i++, pos += step) { | |
425 intern_vis_data[i] = ((mono_pcm[0][pos >> 8]) >> 11) + 6; | |
426 if (intern_vis_data[i] > 12) | |
427 intern_vis_data[i] = 12; | |
428 /* Do not see the point of that? (comparison always false) -larne. | |
429 if (intern_vis_data[i] < 0) | |
430 intern_vis_data[i] = 0; */ | |
431 } | |
432 } | |
433 if (cfg.player_shaded && cfg.player_visible) | |
434 svis_timeout_func(mainwin_svis, intern_vis_data); | |
435 else | |
436 vis_timeout_func(active_vis, intern_vis_data); | |
437 } |