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