Mercurial > audlegacy
annotate src/audacious/visualization.c @ 3899:2c768d923bcf
Add an event listener for volume changes.
author | William Pitcock <nenolod@atheme.org> |
---|---|
date | Tue, 06 Nov 2007 11:52:01 -0600 |
parents | 0898b8139af8 |
children | 2b7a74fce100 |
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_playback_start(void) | |
67 { | |
68 GList *node; | |
69 VisPlugin *vp; | |
70 | |
71 if (vp_data.playback_started) | |
72 return; | |
73 | |
74 for (node = vp_data.enabled_list; node; node = g_list_next(node)) { | |
75 vp = node->data; | |
76 if (vp->playback_start) | |
77 vp->playback_start(); | |
78 } | |
79 vp_data.playback_started = TRUE; | |
80 } | |
81 | |
82 void | |
83 vis_playback_stop(void) | |
84 { | |
85 GList *node = vp_data.enabled_list; | |
86 VisPlugin *vp; | |
87 | |
88 if (!vp_data.playback_started) | |
89 return; | |
90 | |
91 for (node = vp_data.enabled_list; node; node = g_list_next(node)) { | |
92 vp = node->data; | |
93 if (vp->playback_stop) | |
94 vp->playback_stop(); | |
95 } | |
96 vp_data.playback_started = FALSE; | |
97 } | |
98 | |
99 void | |
100 enable_vis_plugin(gint i, gboolean enable) | |
101 { | |
102 GList *node = g_list_nth(vp_data.vis_list, i); | |
103 VisPlugin *vp; | |
104 | |
105 if (!node || !(node->data)) | |
106 return; | |
107 vp = node->data; | |
108 | |
3437
3092a8b3fe34
Big plugin system changes (part 1 of who knows, it's still a big mess):
William Pitcock <nenolod@atheme.org>
parents:
3251
diff
changeset
|
109 if (enable && !vp->enabled) { |
2313 | 110 vp_data.enabled_list = g_list_append(vp_data.enabled_list, vp); |
111 if (vp->init) | |
112 vp->init(); | |
113 if (playback_get_playing() && vp->playback_start) | |
114 vp->playback_start(); | |
115 } | |
3437
3092a8b3fe34
Big plugin system changes (part 1 of who knows, it's still a big mess):
William Pitcock <nenolod@atheme.org>
parents:
3251
diff
changeset
|
116 else if (!enable && vp->enabled) { |
2313 | 117 vp_data.enabled_list = g_list_remove(vp_data.enabled_list, vp); |
118 if (playback_get_playing() && vp->playback_stop) | |
119 vp->playback_stop(); | |
120 if (vp->cleanup) | |
121 vp->cleanup(); | |
122 } | |
3437
3092a8b3fe34
Big plugin system changes (part 1 of who knows, it's still a big mess):
William Pitcock <nenolod@atheme.org>
parents:
3251
diff
changeset
|
123 |
3092a8b3fe34
Big plugin system changes (part 1 of who knows, it's still a big mess):
William Pitcock <nenolod@atheme.org>
parents:
3251
diff
changeset
|
124 vp->enabled = enable; |
2313 | 125 } |
126 | |
127 gchar * | |
128 vis_stringify_enabled_list(void) | |
129 { | |
130 gchar *enalist = NULL, *tmp, *tmp2; | |
131 GList *node = vp_data.enabled_list; | |
132 | |
133 if (g_list_length(node)) { | |
134 enalist = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
135 for (node = g_list_next(node); node != NULL; node = g_list_next(node)) { | |
136 tmp = enalist; | |
137 tmp2 = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
138 enalist = g_strconcat(tmp, ",", tmp2, NULL); | |
139 g_free(tmp); | |
140 g_free(tmp2); | |
141 } | |
142 } | |
143 return enalist; | |
144 } | |
145 | |
146 void | |
147 vis_enable_from_stringified_list(gchar * list) | |
148 { | |
149 gchar **plugins, *base; | |
150 GList *node; | |
151 gint i; | |
152 VisPlugin *vp; | |
153 | |
154 if (!list || !strcmp(list, "")) | |
155 return; | |
156 plugins = g_strsplit(list, ",", 0); | |
157 for (i = 0; plugins[i]; i++) { | |
158 for (node = vp_data.vis_list; node != NULL; node = g_list_next(node)) { | |
159 base = g_path_get_basename(VIS_PLUGIN(node->data)->filename); | |
160 if (!strcmp(plugins[i], base)) { | |
161 vp = node->data; | |
162 vp_data.enabled_list = | |
163 g_list_append(vp_data.enabled_list, vp); | |
164 if (vp->init) | |
165 vp->init(); | |
166 if (playback_get_playing() && vp->playback_start) | |
167 vp->playback_start(); | |
3437
3092a8b3fe34
Big plugin system changes (part 1 of who knows, it's still a big mess):
William Pitcock <nenolod@atheme.org>
parents:
3251
diff
changeset
|
168 vp->enabled = TRUE; |
2313 | 169 } |
170 g_free(base); | |
171 } | |
172 } | |
173 g_strfreev(plugins); | |
174 } | |
175 | |
176 static void | |
177 calc_stereo_pcm(gint16 dest[2][512], gint16 src[2][512], gint nch) | |
178 { | |
179 memcpy(dest[0], src[0], 512 * sizeof(gint16)); | |
180 if (nch == 1) | |
181 memcpy(dest[1], src[0], 512 * sizeof(gint16)); | |
182 else | |
183 memcpy(dest[1], src[1], 512 * sizeof(gint16)); | |
184 } | |
185 | |
186 static void | |
187 calc_mono_pcm(gint16 dest[2][512], gint16 src[2][512], gint nch) | |
188 { | |
189 gint i; | |
190 gint16 *d, *sl, *sr; | |
191 | |
192 if (nch == 1) | |
193 memcpy(dest[0], src[0], 512 * sizeof(gint16)); | |
194 else { | |
195 d = dest[0]; | |
196 sl = src[0]; | |
197 sr = src[1]; | |
198 for (i = 0; i < 512; i++) { | |
199 *(d++) = (*(sl++) + *(sr++)) >> 1; | |
200 } | |
201 } | |
202 } | |
203 | |
204 static void | |
205 calc_freq(gint16 * dest, gint16 * src) | |
206 { | |
207 static fft_state *state = NULL; | |
208 gfloat tmp_out[257]; | |
209 gint i; | |
210 | |
211 if (!state) | |
212 state = fft_init(); | |
213 | |
214 fft_perform(src, tmp_out, state); | |
215 | |
216 for (i = 0; i < 256; i++) | |
217 dest[i] = ((gint) sqrt(tmp_out[i + 1])) >> 8; | |
218 } | |
219 | |
220 static void | |
221 calc_mono_freq(gint16 dest[2][256], gint16 src[2][512], gint nch) | |
222 { | |
223 gint i; | |
224 gint16 *d, *sl, *sr, tmp[512]; | |
225 | |
226 if (nch == 1) | |
227 calc_freq(dest[0], src[0]); | |
228 else { | |
229 d = tmp; | |
230 sl = src[0]; | |
231 sr = src[1]; | |
232 for (i = 0; i < 512; i++) { | |
233 *(d++) = (*(sl++) + *(sr++)) >> 1; | |
234 } | |
235 calc_freq(dest[0], tmp); | |
236 } | |
237 } | |
238 | |
239 static void | |
240 calc_stereo_freq(gint16 dest[2][256], gint16 src[2][512], gint nch) | |
241 { | |
242 calc_freq(dest[0], src[0]); | |
243 | |
244 if (nch == 2) | |
245 calc_freq(dest[1], src[1]); | |
246 else | |
247 memcpy(dest[1], dest[0], 256 * sizeof(gint16)); | |
248 } | |
249 | |
250 void | |
251 vis_send_data(gint16 pcm_data[2][512], gint nch, gint length) | |
252 { | |
253 GList *node = vp_data.enabled_list; | |
254 VisPlugin *vp; | |
255 gint16 mono_freq[2][256], stereo_freq[2][256]; | |
256 gboolean mono_freq_calced = FALSE, stereo_freq_calced = FALSE; | |
257 gint16 mono_pcm[2][512], stereo_pcm[2][512]; | |
258 gboolean mono_pcm_calced = FALSE, stereo_pcm_calced = FALSE; | |
259 guint8 intern_vis_data[512]; | |
260 gint i; | |
261 | |
262 if (!pcm_data || nch < 1) { | |
263 if (cfg.vis_type != VIS_OFF) { | |
264 if (cfg.player_shaded && cfg.player_visible) | |
3054 | 265 ui_svis_timeout_func(mainwin_svis, NULL); |
2313 | 266 else |
3020 | 267 ui_vis_timeout_func(mainwin_vis, NULL); |
2313 | 268 } |
269 return; | |
270 } | |
271 | |
272 while (node) { | |
273 vp = node->data; | |
274 if (vp->num_pcm_chs_wanted > 0 && vp->render_pcm) { | |
275 if (vp->num_pcm_chs_wanted == 1) { | |
276 if (!mono_pcm_calced) { | |
277 calc_mono_pcm(mono_pcm, pcm_data, nch); | |
278 mono_pcm_calced = TRUE; | |
279 } | |
280 vp->render_pcm(mono_pcm); | |
281 } | |
282 else { | |
283 if (!stereo_pcm_calced) { | |
284 calc_stereo_pcm(stereo_pcm, pcm_data, nch); | |
285 stereo_pcm_calced = TRUE; | |
286 } | |
287 vp->render_pcm(stereo_pcm); | |
288 } | |
289 } | |
290 if (vp->num_freq_chs_wanted > 0 && vp->render_freq) { | |
291 if (vp->num_freq_chs_wanted == 1) { | |
292 if (!mono_freq_calced) { | |
293 calc_mono_freq(mono_freq, pcm_data, nch); | |
294 mono_freq_calced = TRUE; | |
295 } | |
296 vp->render_freq(mono_freq); | |
297 } | |
298 else { | |
299 if (!stereo_freq_calced) { | |
300 calc_stereo_freq(stereo_freq, pcm_data, nch); | |
301 stereo_freq_calced = TRUE; | |
302 } | |
303 vp->render_freq(stereo_freq); | |
304 } | |
305 } | |
306 node = g_list_next(node); | |
307 } | |
308 | |
309 if (cfg.vis_type == VIS_OFF) | |
310 return; | |
311 | |
312 if (cfg.vis_type == VIS_ANALYZER) { | |
313 /* Spectrum analyzer */ | |
314 /* 76 values */ | |
315 const gint long_xscale[] = | |
316 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | |
317 17, 18, | |
318 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, | |
319 34, | |
320 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, | |
321 50, 51, | |
322 52, 53, 54, 55, 56, 57, 58, 61, 66, 71, 76, 81, 87, 93, | |
323 100, 107, | |
324 114, 122, 131, 140, 150, 161, 172, 184, 255 | |
325 }; | |
326 /* 20 values */ | |
327 const int short_xscale[] = | |
328 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 15, 20, 27, | |
329 36, 47, 62, 82, 107, 141, 184, 255 | |
330 }; | |
331 const double y_scale = 3.60673760222; /* 20.0 / log(256) */ | |
332 const int *xscale; | |
333 gint j, y, max; | |
334 | |
335 if (!mono_freq_calced) | |
336 calc_mono_freq(mono_freq, pcm_data, nch); | |
337 | |
338 memset(intern_vis_data, 0, 75); | |
339 | |
340 if (cfg.analyzer_type == ANALYZER_BARS) { | |
341 if (cfg.player_shaded) { | |
342 max = 13; | |
343 } | |
344 else { | |
345 max = 19; | |
346 } | |
347 xscale = short_xscale; | |
348 } | |
349 else { | |
350 if (cfg.player_shaded) { | |
351 max = 37; | |
352 } | |
353 else { | |
354 max = 75; | |
355 } | |
356 xscale = long_xscale; | |
357 } | |
358 | |
359 for (i = 0; i < max; i++) { | |
360 for (j = xscale[i], y = 0; j < xscale[i + 1]; j++) { | |
361 if (mono_freq[0][j] > y) | |
362 y = mono_freq[0][j]; | |
363 } | |
364 y >>= 7; | |
365 if (y != 0) { | |
366 intern_vis_data[i] = log(y) * y_scale; | |
367 if (intern_vis_data[i] > 15) | |
368 intern_vis_data[i] = 15; | |
369 } | |
370 else | |
371 intern_vis_data[i] = 0; | |
372 } | |
373 | |
374 } | |
375 else if(cfg.vis_type == VIS_VOICEPRINT){ | |
376 if (cfg.player_shaded && cfg.player_visible) { | |
377 /* VU */ | |
378 gint vu, val; | |
379 | |
380 if (!stereo_pcm_calced) | |
381 calc_stereo_pcm(stereo_pcm, pcm_data, nch); | |
382 vu = 0; | |
383 for (i = 0; i < 512; i++) { | |
384 val = abs(stereo_pcm[0][i]); | |
385 if (val > vu) | |
386 vu = val; | |
387 } | |
388 intern_vis_data[0] = (vu * 37) >> 15; | |
389 if (intern_vis_data[0] > 37) | |
390 intern_vis_data[0] = 37; | |
391 if (nch == 2) { | |
392 vu = 0; | |
393 for (i = 0; i < 512; i++) { | |
394 val = abs(stereo_pcm[1][i]); | |
395 if (val > vu) | |
396 vu = val; | |
397 } | |
398 intern_vis_data[1] = (vu * 37) >> 15; | |
399 if (intern_vis_data[1] > 37) | |
400 intern_vis_data[1] = 37; | |
401 } | |
402 else | |
403 intern_vis_data[1] = intern_vis_data[0]; | |
404 } | |
405 else{ /*Voiceprint*/ | |
406 if (!mono_freq_calced) | |
407 calc_mono_freq(mono_freq, pcm_data, nch); | |
408 memset(intern_vis_data, 0, 256); | |
409 /* For the values [0-16] we use the frequency that's 3/2 as much. | |
410 If we assume the 512 values calculated by calc_mono_freq to cover 0-22kHz linearly | |
411 we get a range of [0-16] * 3/2 * 22000/512 = [0-1,031] Hz. | |
412 Most stuff above that is harmonics and we want to utilize the 16 samples we have | |
413 to the max[tm] | |
414 */ | |
415 for(i = 0; i < 50 ; i+=3){ | |
416 intern_vis_data[i/3] += (mono_freq[0][i/2] >> 5); | |
417 | |
418 /*Boost frequencies above 257Hz a little*/ | |
419 //if(i > 4 * 3) | |
420 // intern_vis_data[i/3] += 8; | |
421 } | |
422 } | |
423 } | |
424 else { /* (cfg.vis_type == VIS_SCOPE) */ | |
425 | |
426 /* Oscilloscope */ | |
427 gint pos, step; | |
428 | |
429 if (!mono_pcm_calced) | |
430 calc_mono_pcm(mono_pcm, pcm_data, nch); | |
431 | |
432 step = (length << 8) / 74; | |
433 for (i = 0, pos = 0; i < 75; i++, pos += step) { | |
434 intern_vis_data[i] = ((mono_pcm[0][pos >> 8]) >> 12) + 7; | |
435 if (intern_vis_data[i] == 255) | |
436 intern_vis_data[i] = 0; | |
437 else if (intern_vis_data[i] > 12) | |
438 intern_vis_data[i] = 12; | |
439 /* Do not see the point of that? (comparison always false) -larne. | |
440 if (intern_vis_data[i] < 0) | |
441 intern_vis_data[i] = 0; */ | |
442 } | |
443 } | |
444 if (cfg.player_shaded && cfg.player_visible) | |
3054 | 445 ui_svis_timeout_func(mainwin_svis, intern_vis_data); |
2313 | 446 else |
3020 | 447 ui_vis_timeout_func(mainwin_vis, intern_vis_data); |
2313 | 448 } |
3559 | 449 |
450 void | |
451 vis_flow(FlowContext *context) | |
452 { | |
453 input_add_vis_pcm(context->time, context->fmt, context->channels, | |
454 context->len, context->data); | |
455 } |