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