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