comparison Plugins/Visualization/paranormal/client.c @ 1521:752f35f5a799 trunk

[svn] - paranormal in theory works on audacious
author nenolod
date Mon, 07 Aug 2006 21:41:14 -0700
parents
children 95e71054ecbd
comparison
equal deleted inserted replaced
1520:b41b45bc86b9 1521:752f35f5a799
1 /* pnxmms - An xmms visualization plugin using the Paranormal
2 * audio visualization library
3 *
4 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <errno.h>
30
31 #include <gtk/gtk.h>
32 #include <audacious/plugin.h>
33 #include <audacious/configdb.h>
34 #include <SDL/SDL.h>
35 #include <SDL/SDL_thread.h>
36
37 #include "pn.h"
38
39 /* Paranormal visualization object */
40 static PnVis *vis;
41
42 /* Paranormal audio data object */
43 static PnAudioData *audio_data;
44
45 /* SDL window's surface */
46 static SDL_Surface *screen;
47
48 /* Synchronization stuffs */
49 static SDL_Thread *render_thread;
50 static int render_func (void *data);
51 static gboolean kill_render_thread;
52 static gboolean quit_timeout_func (gpointer data);
53 static guint quit_timeout;
54 static gboolean render_thread_finished;
55
56 static SDL_mutex *vis_mutex;
57 static SDL_mutex *audio_data_mutex;
58 static gfloat intermediate_pcm_data[3][512];
59 static gfloat intermediate_freq_data[3][256];
60
61 /* PNXMMS Options */
62 static gboolean options_read = FALSE;
63 static gboolean preset_changed = FALSE;
64 static gchar *preset;
65 static guint image_width = 320;
66 static guint image_height = 240;
67
68 /* Config dialog */
69 GtkWidget *config_dialog;
70 GtkWidget *load_button;
71
72 /* Config functions */
73 static void read_options (void);
74 static void write_options (void);
75 static void load_default_vis (void);
76 static void create_config_dialog (void);
77
78 /* Rendering functions */
79 static void draw_image (PnImage *image);
80
81 /* XMMS interface */
82 static void pn_xmms_init (void);
83 static void pn_xmms_cleanup (void);
84 static void pn_xmms_about (void);
85 static void pn_xmms_configure (void);
86 static void pn_xmms_render_pcm (gint16 data[2][512]);
87 static void pn_xmms_render_freq (gint16 data[2][256]);
88
89 static VisPlugin pn_xmms_vp =
90 {
91 NULL, /* handle */
92 NULL, /* filename */
93 0, /* xmms_session */
94 PACKAGE " " VERSION,
95 2,
96 2,
97 pn_xmms_init,
98 pn_xmms_cleanup,
99 NULL, /* pn_xmms_about, */
100 pn_xmms_configure,
101 NULL, /* disable_plugin */
102 NULL, /* playback_start */
103 NULL, /* pkayback_stop */
104 pn_xmms_render_pcm,
105 pn_xmms_render_freq
106 };
107
108 VisPlugin*
109 get_vplugin_info (void)
110 {
111 return &pn_xmms_vp;
112 }
113
114 static void
115 pn_xmms_init (void)
116 {
117 /* Load the saved options */
118 read_options ();
119
120 /* Initialize SDL */
121 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
122 {
123 g_warning ("Unable to initialize SDL: %s\n", SDL_GetError ());
124 pn_xmms_vp.disable_plugin (&pn_xmms_vp);
125 }
126
127 screen = SDL_SetVideoMode (image_width, image_height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
128 if (! screen)
129 {
130 g_warning ("Unable to create a new SDL window: %s\n", SDL_GetError ());
131 pn_xmms_vp.disable_plugin (&pn_xmms_vp);
132 }
133
134 SDL_WM_SetCaption (PACKAGE " " VERSION, PACKAGE);
135
136 /* Initialize Paranormal */
137 pn_init ();
138
139 /* Set up the visualization objects */
140 vis = pn_vis_new (image_width, image_height);
141 pn_object_ref (PN_OBJECT (vis));
142 pn_object_sink (PN_OBJECT (vis));
143
144 if (! preset || ! pn_vis_load_from_file (vis, preset))
145 load_default_vis ();
146
147 audio_data = pn_audio_data_new ();
148 pn_object_ref (PN_OBJECT (audio_data));
149 pn_object_sink (PN_OBJECT (audio_data));
150 pn_audio_data_set_stereo (audio_data, FALSE);
151 pn_audio_data_set_pcm_samples (audio_data, 512);
152 pn_audio_data_set_freq_bands (audio_data, 256);
153
154 /* Set up the render thread */
155 audio_data_mutex = SDL_CreateMutex ();
156 vis_mutex = SDL_CreateMutex ();
157 if (! audio_data_mutex || ! vis_mutex)
158 {
159 g_warning ("Unable to create mutex: %s\n", SDL_GetError ());
160 pn_xmms_vp.disable_plugin (&pn_xmms_vp);
161 }
162
163 kill_render_thread = FALSE;
164 render_thread_finished = FALSE;
165 render_thread = SDL_CreateThread (render_func, NULL);
166 if (! render_thread)
167 {
168 g_warning ("Unable to create render thread: %s\n", SDL_GetError ());
169 pn_xmms_vp.disable_plugin (&pn_xmms_vp);
170 }
171
172 /* Create the quit-checker timeout */
173 quit_timeout = gtk_timeout_add (500, quit_timeout_func, NULL);
174 }
175
176 static void
177 pn_xmms_cleanup (void)
178 {
179 if (quit_timeout != 0)
180 {
181 gtk_timeout_remove (quit_timeout);
182 quit_timeout = 0;
183 }
184
185 if (render_thread)
186 {
187 kill_render_thread = TRUE;
188 SDL_WaitThread (render_thread, NULL);
189 render_thread = NULL;
190 }
191
192 if (audio_data_mutex)
193 {
194 SDL_DestroyMutex (audio_data_mutex);
195 audio_data_mutex = NULL;
196 }
197
198 if (screen)
199 {
200 SDL_FreeSurface (screen);
201 screen = NULL;
202 }
203 SDL_Quit ();
204
205 if (vis)
206 {
207 pn_object_unref (PN_OBJECT (vis));
208 vis = NULL;
209 }
210
211 write_options ();
212 }
213
214 static void
215 pn_xmms_about (void)
216 {
217 /* FIXME: This needs to wait until XMMS supports GTK+ 2.0 */
218 }
219
220 static void
221 pn_xmms_configure (void)
222 {
223 read_options ();
224 create_config_dialog ();
225 }
226
227 static void
228 pn_xmms_render_pcm (gint16 data[2][512])
229 {
230 guint i;
231
232 /* Lock the audio data */
233 SDL_mutexP (audio_data_mutex);
234
235 /* Set up the audio data */
236 for (i=0; i<512; i++)
237 {
238 intermediate_pcm_data[PN_CHANNEL_LEFT][i] = (gfloat)(data[0][i]) / 32768.0;
239 intermediate_pcm_data[PN_CHANNEL_RIGHT][i] = (gfloat)(data[0][i]) / 32768.0;
240 intermediate_pcm_data[PN_CHANNEL_CENTER][i] = .5 * (intermediate_pcm_data[PN_CHANNEL_LEFT][i]
241 + intermediate_pcm_data[PN_CHANNEL_RIGHT][i]);
242 }
243
244 /* Unlock the audio data */
245 SDL_mutexV (audio_data_mutex);
246 }
247
248 static void
249 pn_xmms_render_freq (gint16 data[2][256])
250 {
251 guint i;
252
253 /* Lock the audio data */
254 SDL_mutexP (audio_data_mutex);
255
256 /* Set up the audio data */
257 for (i=0; i<256; i++)
258 {
259 intermediate_freq_data[PN_CHANNEL_LEFT][i] = (gfloat)(data[0][i]) / 32768.0;
260 intermediate_freq_data[PN_CHANNEL_RIGHT][i] = (gfloat)(data[0][i]) / 32768.0;
261 intermediate_freq_data[PN_CHANNEL_CENTER][i] = .5 * (intermediate_freq_data[PN_CHANNEL_LEFT][i]
262 + intermediate_freq_data[PN_CHANNEL_RIGHT][i]);
263 }
264
265 /* Unlock the audio data */
266 SDL_mutexV (audio_data_mutex);
267 }
268
269 static gboolean
270 quit_timeout_func (gpointer data)
271 {
272 if (render_thread_finished)
273 {
274 pn_xmms_vp.disable_plugin (&pn_xmms_vp);
275 quit_timeout = 0;
276 return FALSE;
277 }
278 return TRUE;
279 }
280
281 static int
282 render_func (void *data)
283 {
284 guint i;
285 PnImage *image;
286 guint last_time = 0, last_second = 0;
287 gfloat fps = 0.0;
288 guint this_time;
289 SDL_Event event;
290
291 while (! kill_render_thread)
292 {
293 /* Lock & copy the audio data */
294 SDL_mutexP (audio_data_mutex);
295
296 for (i=0; i<3; i++)
297 {
298 memcpy (PN_AUDIO_DATA_PCM_DATA (audio_data, i), intermediate_pcm_data[i], 512 * sizeof (gfloat));
299 memcpy (PN_AUDIO_DATA_FREQ_DATA (audio_data, i), intermediate_freq_data[i], 256 * sizeof (gfloat));
300 }
301
302 SDL_mutexV (audio_data_mutex);
303
304 pn_audio_data_update (audio_data);
305
306 /* Render the image */
307 SDL_mutexP (vis_mutex);
308 image = pn_vis_render (vis, audio_data);
309 SDL_mutexV (vis_mutex);
310
311 /* Draw the image */
312 draw_image (image);
313
314 /* Compute the FPS */
315 this_time = SDL_GetTicks ();
316
317 fps = fps * .95 + (1000.0 / (gfloat) (this_time - last_time)) * .05;
318 if (this_time > 2000 + last_second)
319 {
320 last_second = this_time;
321 printf ("paranormal fps: %f\n", fps);
322 }
323 last_time = this_time;
324
325 /* Handle window events */
326 while (SDL_PollEvent (&event))
327 {
328 switch (event.type)
329 {
330 case SDL_QUIT:
331 render_thread_finished = TRUE;
332 return 0;
333
334 case SDL_VIDEORESIZE:
335 image_width = event.resize.w;
336 image_height = event.resize.h;
337 screen = SDL_SetVideoMode (image_width, image_height, 32,
338 SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE);
339 printf ("changed to: w: %u, h: %u\n", image_width, image_height);
340 pn_vis_set_image_size (vis, image_width, image_height);
341 break;
342
343 case SDL_KEYDOWN:
344 switch (event.key.keysym.sym)
345 {
346 case SDLK_ESCAPE:
347 render_thread_finished = TRUE;
348 return 0;
349
350 case SDLK_RETURN:
351 if (event.key.keysym.mod & (KMOD_ALT | KMOD_META))
352 {
353 /* Toggle fullscreen */
354 SDL_WM_ToggleFullScreen (screen);
355 if (SDL_ShowCursor (SDL_QUERY) == SDL_ENABLE)
356 SDL_ShowCursor (SDL_DISABLE);
357 else
358 SDL_ShowCursor (SDL_ENABLE);
359 }
360 break;
361 case SDLK_BACKQUOTE:
362 {
363 char fname[20];
364 int i = 0;
365 struct stat buf;
366
367 do
368 {
369 sprintf (fname, "pnxmms_ss_%05u.bmp", i++);
370 } while(!(stat(fname, &buf) != 0 && errno == ENOENT ));
371 SDL_SaveBMP(screen, fname);
372 }
373 break;
374
375 default:
376 break;
377 }
378 break;
379 }
380 }
381 }
382
383 return 0;
384 }
385
386 static void
387 draw_image (PnImage *image)
388 {
389 guint width, height, src_stride, dest_stride;
390 guint i;
391 register PnColor *src;
392 register Uint32 *dest;
393
394 /* Lock the SDL surface */
395 SDL_LockSurface (screen);
396
397 /* Get the dimentions */
398 width = pn_image_get_width (image);
399 height = pn_image_get_height (image);
400
401 src_stride = pn_image_get_pitch (image) >> 2;
402 dest_stride = screen->pitch >> 2;
403
404 src = pn_image_get_image_buffer (image);
405 dest = (Uint32 *) screen->pixels;
406
407 /* Copy the pixel data */
408 while (--height > 0)
409 {
410 for (i=0; i<width; i++)
411 dest[i] = SDL_MapRGB (screen->format, src[i].red, src[i].green, src[i].blue);
412 dest += dest_stride;
413 src += src_stride;
414 }
415
416 /* Unlock the SDL surface */
417 SDL_UnlockSurface (screen);
418
419 /* Draw it to the screen */
420 SDL_Flip (screen);
421 }
422
423 static void
424 read_options (void)
425 {
426 ConfigDb *cfg;
427 gint i;
428
429 if (options_read == TRUE)
430 return;
431
432 cfg = bmp_cfg_db_open ();
433 if (!cfg)
434 return;
435
436 if (bmp_cfg_db_get_int (cfg, "pnxmms", "image_width", &i))
437 image_width = i;
438
439 if (bmp_cfg_db_get_int (cfg, "pnxmms", "image_height", &i))
440 image_height = i;
441
442 bmp_cfg_db_close (cfg);
443
444 return;
445 }
446
447 static void
448 write_options (void)
449 {
450 ConfigDb *cfg;
451
452 cfg = bmp_cfg_db_open ();
453 if (!cfg)
454 fprintf (stderr, "PNXMMS: Unable to open XMMS config file!\n");
455
456 bmp_cfg_db_set_int (cfg, "pnxmms", "image_width", image_width);
457 bmp_cfg_db_set_int (cfg, "pnxmms", "image_height", image_height);
458
459 bmp_cfg_db_close (cfg);
460 }
461
462 static void
463 load_default_vis (void)
464 {
465 PnContainer *container;
466 PnActuator *actuator;
467 PnOption *option;
468
469 /* Actuator List */
470 container = (PnContainer *) pn_actuator_list_new ();
471
472 /* Scope */
473 actuator = (PnActuator *) pn_scope_new ();
474 option = pn_actuator_get_option_by_name (actuator, "init_script");
475 pn_string_option_set_value (PN_STRING_OPTION (option), "samples = width/2;");
476 option = pn_actuator_get_option_by_name (actuator, "frame_script");
477 pn_string_option_set_value (PN_STRING_OPTION (option),
478 "base = base + .04;\n"
479 "red = abs (sin (.5 * pi + .1 * base));\n"
480 "green = .5 * abs (sin (.5 * pi - .2 * base));\n"
481 "blue = abs (sin (.5 * pi + .3 * base));");
482 option = pn_actuator_get_option_by_name (actuator, "sample_script");
483 pn_string_option_set_value (PN_STRING_OPTION (option),
484 "x = 2 * iteration - 1;\n"
485 "y = value;");
486 option = pn_actuator_get_option_by_name (actuator, "draw_method");
487 pn_list_option_set_value (PN_LIST_OPTION (option), "Lines");
488 pn_container_add_actuator (container, actuator, PN_POSITION_TAIL);
489
490 /* Distortion */
491 actuator = (PnActuator *) pn_distortion_new ();
492 option = pn_actuator_get_option_by_name (actuator, "distortion_script");
493 pn_string_option_set_value (PN_STRING_OPTION (option),
494 "intensity = .99 + .08 * r;\n"
495 "r = .98 * atan (r);\n"
496 "theta = theta + .01;");
497 option = pn_actuator_get_option_by_name (actuator, "polar_coords");
498 pn_boolean_option_set_value (PN_BOOLEAN_OPTION (option), TRUE);
499 pn_container_add_actuator (container, actuator, PN_POSITION_TAIL);
500
501 /* Add the actuator list to the vis */
502 pn_vis_set_root_actuator (vis, PN_ACTUATOR (container));
503 }
504
505 static void
506 apply_settings (void)
507 {
508 if (preset_changed)
509 {
510 preset_changed = FALSE;
511 SDL_mutexP (vis_mutex);
512 pn_vis_load_from_file (vis, preset);
513 SDL_mutexV (vis_mutex);
514 }
515
516 write_options ();
517 }
518
519 static void
520 destroy_config_dialog (GtkObject *user_data, gpointer data)
521 {
522 gtk_widget_destroy (GTK_WIDGET(config_dialog));
523 config_dialog = NULL;
524 }
525
526 static void
527 apply_button_cb (GtkButton *button, gpointer data)
528 {
529 apply_settings ();
530 }
531
532 static void
533 ok_button_cb (GtkButton *button, gpointer data)
534 {
535 apply_settings ();
536 destroy_config_dialog (NULL, NULL);
537 }
538
539 static void
540 cancel_button_cb (GtkButton *button, gpointer data)
541 {
542 destroy_config_dialog (NULL, NULL);
543 }
544
545 /* If selector != NULL, then it's 'OK', otherwise it's 'Cancel' */
546 static void
547 load_preset_cb (GtkButton *button, GtkFileSelection *selector)
548 {
549 if (selector)
550 {
551 if (preset)
552 g_free (preset);
553 preset = g_strdup (gtk_file_selection_get_filename (selector));
554 gtk_label_set_text (GTK_LABEL (GTK_BIN (load_button)->child), preset);
555 preset_changed = TRUE;
556 }
557
558 gtk_widget_set_sensitive (config_dialog, TRUE);
559 }
560
561 static void
562 load_preset_button_cb (GtkButton *button, gpointer data)
563 {
564 GtkWidget *selector;
565
566 selector = gtk_file_selection_new ("Load Preset");
567
568 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (selector)->ok_button),
569 "clicked", GTK_SIGNAL_FUNC (load_preset_cb), selector);
570 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (selector)->cancel_button),
571 "clicked", GTK_SIGNAL_FUNC (load_preset_cb), NULL);
572
573 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (selector)->ok_button),
574 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
575 (gpointer) selector);
576 gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (selector)->cancel_button),
577 "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
578 (gpointer) selector);
579
580 gtk_widget_set_sensitive (config_dialog, FALSE);
581 gtk_widget_show (selector);
582 }
583
584 static void
585 create_config_dialog (void)
586 {
587 GtkWidget *bbox, *button, *vbox, *frame, *table, *label;
588
589 if (config_dialog != NULL)
590 return;
591
592 /* Create the dialog */
593 config_dialog = gtk_dialog_new ();
594 gtk_window_set_title (GTK_WINDOW (config_dialog), PACKAGE " " VERSION
595 " - Configuration");
596 gtk_widget_set_usize (config_dialog, 500, 300);
597 gtk_container_border_width (GTK_CONTAINER (config_dialog), 8);
598 gtk_signal_connect_object (GTK_OBJECT (config_dialog), "delete-event",
599 destroy_config_dialog, NULL);
600
601 /* OK / Cancel / Apply */
602 bbox = gtk_hbutton_box_new ();
603 gtk_widget_show (bbox);
604 gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
605 gtk_button_box_set_spacing (GTK_BUTTON_BOX (bbox), 8);
606 gtk_button_box_set_child_size (GTK_BUTTON_BOX (bbox), 64, 0);
607 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (config_dialog)->action_area),
608 bbox, FALSE, FALSE, 0);
609 button = gtk_button_new_with_label ("OK");
610 gtk_widget_show (button);
611 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
612 gtk_signal_connect (GTK_OBJECT (button), "clicked",
613 GTK_SIGNAL_FUNC (ok_button_cb), NULL);
614 gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
615 button = gtk_button_new_with_label ("Cancel");
616 gtk_widget_show (button);
617 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
618 gtk_signal_connect (GTK_OBJECT (button), "clicked",
619 GTK_SIGNAL_FUNC (cancel_button_cb), NULL);
620 gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
621 button = gtk_button_new_with_label ("Apply");
622 gtk_widget_show (button);
623 gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
624 gtk_signal_connect (GTK_OBJECT (button), "clicked",
625 GTK_SIGNAL_FUNC (apply_button_cb), NULL);
626 gtk_box_pack_start (GTK_BOX (bbox), button, FALSE, FALSE, 0);
627
628
629 /* The vertical box */
630 vbox = gtk_vbox_new (FALSE, 1);
631 gtk_widget_show (vbox);
632 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (config_dialog)->vbox), vbox,
633 TRUE, TRUE, 0);
634
635 /* General options */
636 frame = gtk_frame_new (NULL);
637 gtk_widget_show (frame);
638 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
639 gtk_frame_set_label (GTK_FRAME (frame), "General");
640 table = gtk_table_new (2, 1, TRUE);
641 gtk_widget_show (table);
642 gtk_container_add (GTK_CONTAINER (frame), table);
643 label = gtk_label_new ("Preset File: ");
644 gtk_widget_show (label);
645 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
646 GTK_FILL, 0, 3, 3);
647 load_button = gtk_button_new_with_label (preset ? preset : "<default>");
648 gtk_widget_show (load_button);
649 gtk_table_attach (GTK_TABLE (table), load_button, 1, 2, 0, 1,
650 GTK_EXPAND | GTK_FILL, 0, 3, 3);
651 gtk_signal_connect (GTK_OBJECT (load_button), "clicked",
652 GTK_SIGNAL_FUNC (load_preset_button_cb), NULL);
653
654 /* Show it all */
655 gtk_widget_show (config_dialog);
656 gtk_widget_grab_focus (config_dialog);
657 }