1507
|
1 /* Paranormal - A highly customizable audio visualization library
|
|
2 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com>
|
|
3 *
|
|
4 * This library is free software; you can redistribute it and/or
|
|
5 * modify it under the terms of the GNU Library General Public
|
|
6 * License as published by the Free Software Foundation; either
|
|
7 * version 2 of the License, or (at your option) any later version.
|
|
8 *
|
|
9 * This library is distributed in the hope that it will be useful,
|
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12 * Library General Public License for more details.
|
|
13 *
|
|
14 * You should have received a copy of the GNU Library General Public
|
|
15 * License along with this library; if not, write to the Free
|
|
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17 */
|
|
18
|
|
19 #include <config.h>
|
|
20
|
|
21 #include <glib.h>
|
|
22 #include "pnvis.h"
|
|
23 #include "pnactuatorfactory.h"
|
|
24 #include "pnerror.h"
|
|
25
|
|
26 static void pn_vis_class_init (PnVisClass *class);
|
|
27 static void pn_vis_init (PnVis *vis,
|
|
28 PnVisClass *class);
|
|
29
|
|
30 /* GObject signals */
|
|
31 static void pn_vis_finalize (GObject *gobject);
|
|
32
|
|
33 /* PnObject signals */
|
|
34 static void pn_vis_destroy (PnObject *object);
|
|
35
|
|
36 /* PnUserObject methods */
|
|
37 static void pn_vis_save_thyself (PnUserObject *user_object,
|
|
38 xmlNodePtr node);
|
|
39 static void pn_vis_load_thyself (PnUserObject *user_object,
|
|
40 xmlNodePtr node);
|
|
41
|
|
42 static PnUserObjectClass *parent_class = NULL;
|
|
43
|
|
44 GType
|
|
45 pn_vis_get_type (void)
|
|
46 {
|
|
47 static GType vis_type = 0;
|
|
48
|
|
49 if (! vis_type)
|
|
50 {
|
|
51 static const GTypeInfo vis_info =
|
|
52 {
|
|
53 sizeof (PnVisClass),
|
|
54 NULL, /* base_init */
|
|
55 NULL, /* base_finalize */
|
|
56 (GClassInitFunc) pn_vis_class_init,
|
|
57 NULL, /* class_finalize */
|
|
58 NULL, /* class_data */
|
|
59 sizeof (PnVis),
|
|
60 0, /* n_preallocs */
|
|
61 (GInstanceInitFunc) pn_vis_init
|
|
62 };
|
|
63
|
|
64 /* FIXME: should this be dynamic? */
|
|
65 vis_type = g_type_register_static (PN_TYPE_USER_OBJECT,
|
|
66 "PnVis",
|
|
67 &vis_info,
|
|
68 0);
|
|
69 }
|
|
70 return vis_type;
|
|
71 }
|
|
72
|
|
73 static void
|
|
74 pn_vis_class_init (PnVisClass *class)
|
|
75 {
|
|
76 GObjectClass *gobject_class;
|
|
77 PnObjectClass *object_class;
|
|
78 PnUserObjectClass *user_object_class;
|
|
79
|
|
80 parent_class = g_type_class_peek_parent (class);
|
|
81
|
|
82 gobject_class = (GObjectClass *) class;
|
|
83 object_class = (PnObjectClass *) class;
|
|
84 user_object_class = (PnUserObjectClass *) class;
|
|
85
|
|
86 /* GObject signals */
|
|
87 gobject_class->finalize = pn_vis_finalize;
|
|
88
|
|
89 /* PnObject signals */
|
|
90 object_class->destroy = pn_vis_destroy;
|
|
91
|
|
92 /* PnUserObject methods */
|
|
93 user_object_class->save_thyself = pn_vis_save_thyself;
|
|
94 user_object_class->load_thyself = pn_vis_load_thyself;
|
|
95 }
|
|
96
|
|
97 static void
|
|
98 pn_vis_init (PnVis *vis, PnVisClass *class)
|
|
99 {
|
|
100 /* Set a name for xml stuffs */
|
|
101 pn_user_object_set_name (PN_USER_OBJECT (vis), "Paranormal_Visualization");
|
|
102
|
|
103 /* Create a new image context */
|
|
104 vis->root_image = pn_image_new ();
|
|
105 pn_object_ref (PN_OBJECT (vis->root_image));
|
|
106 pn_object_sink (PN_OBJECT (vis->root_image));
|
|
107 }
|
|
108
|
|
109 static void
|
|
110 pn_vis_destroy (PnObject *object)
|
|
111 {
|
|
112 PnVis *vis = (PnVis *) object;
|
|
113
|
|
114 if (vis->root_actuator)
|
|
115 pn_object_unref (PN_OBJECT (vis->root_actuator));
|
|
116
|
|
117 if (vis->root_image)
|
|
118 pn_object_unref (PN_OBJECT (vis->root_image));
|
|
119 }
|
|
120
|
|
121 static void
|
|
122 pn_vis_finalize (GObject *gobject)
|
|
123 {
|
|
124 PnVis *vis;
|
|
125
|
|
126 vis = (PnVis *) gobject;
|
|
127 }
|
|
128
|
|
129 static void
|
|
130 pn_vis_save_thyself (PnUserObject *user_object, xmlNodePtr node)
|
|
131 {
|
|
132 PnVis *vis;
|
|
133 xmlNsPtr ns;
|
|
134 xmlNodePtr actuators_node, actuator_node;
|
|
135
|
|
136 g_return_if_fail (user_object != NULL);
|
|
137 g_return_if_fail (PN_IS_VIS (user_object));
|
|
138 g_return_if_fail (node != NULL);
|
|
139
|
|
140 vis = (PnVis *) user_object;
|
|
141
|
|
142 ns = xmlNewNs (node, PN_XML_NS_HREF, "pn");
|
|
143 xmlSetNs (node, ns);
|
|
144
|
|
145 actuators_node = xmlNewChild (node, NULL, "Actuators", NULL);
|
1526
|
146 actuator_node = xmlNewChild (actuators_node, NULL, user_object->name, NULL);
|
1507
|
147 if (vis->root_actuator)
|
|
148 pn_user_object_save_thyself (PN_USER_OBJECT (vis->root_actuator), actuator_node);
|
|
149
|
|
150 if (parent_class->save_thyself)
|
|
151 parent_class->save_thyself (user_object, node);
|
|
152 }
|
|
153
|
|
154 static void
|
|
155 pn_vis_load_thyself (PnUserObject *user_object, const xmlNodePtr node)
|
|
156 {
|
|
157 PnVis *vis;
|
1527
|
158 xmlNodePtr actuators_node, actuator_node, tptr;
|
|
159 PnActuator *root_actuator = NULL;
|
1507
|
160
|
|
161 g_return_if_fail (user_object != NULL);
|
|
162 g_return_if_fail (PN_IS_VIS (user_object));
|
|
163 g_return_if_fail (node != NULL);
|
|
164
|
|
165 vis = (PnVis *) user_object;
|
|
166
|
|
167 /* find the 'Actuators' node */
|
|
168 for (actuators_node = node->xmlChildrenNode; actuators_node; actuators_node = actuators_node->next)
|
|
169 if (g_strcasecmp (actuators_node->name, "Actuators") == 0)
|
|
170 break;
|
|
171 if (! actuators_node)
|
|
172 {
|
|
173 pn_error ("unable to load a PnVis from xml node \"%s\"", node->name);
|
|
174 return;
|
|
175 }
|
|
176
|
|
177 /* get the root actuator's node */
|
|
178 actuator_node = actuators_node->xmlChildrenNode;
|
|
179 if (! actuator_node)
|
|
180 goto done;
|
|
181
|
1527
|
182 /* Create a new actuator (reworked by nenolod) */
|
|
183 for (tptr = actuator_node; tptr != NULL && root_actuator == NULL; tptr = tptr->next)
|
|
184 {
|
|
185 xmlNodePtr ttptr;
|
|
186
|
|
187 for (ttptr = tptr; ttptr != NULL; ttptr = ttptr->xmlChildrenNode)
|
|
188 root_actuator = pn_actuator_factory_new_actuator_from_xml (ttptr);
|
|
189 }
|
|
190
|
|
191 if (!root_actuator)
|
1507
|
192 pn_error ("Unknown actuator \"%s\"", actuator_node->name);
|
|
193
|
|
194 pn_vis_set_root_actuator (vis, root_actuator);
|
|
195
|
|
196 done:
|
|
197 if (parent_class->load_thyself)
|
|
198 parent_class->load_thyself (user_object, node);
|
|
199 }
|
|
200
|
|
201 PnVis*
|
|
202 pn_vis_new (guint image_width, guint image_height)
|
|
203 {
|
|
204 PnVis *vis;
|
|
205
|
|
206 g_return_val_if_fail (image_width > 0, NULL);
|
|
207 g_return_val_if_fail (image_height > 0, NULL);
|
|
208
|
|
209 vis = (PnVis *) g_object_new (PN_TYPE_VIS, NULL);
|
|
210 pn_vis_set_image_size (vis, image_width, image_height);
|
|
211
|
|
212 return vis;
|
|
213 }
|
|
214
|
|
215 void
|
|
216 pn_vis_set_root_actuator (PnVis *vis, PnActuator *actuator)
|
|
217 {
|
|
218 g_return_if_fail (vis != NULL);
|
|
219 g_return_if_fail (PN_IS_VIS (vis));
|
|
220 g_return_if_fail (actuator != NULL);
|
|
221 g_return_if_fail (PN_IS_ACTUATOR (actuator));
|
|
222
|
|
223 if (vis->root_actuator)
|
|
224 pn_object_unref (PN_OBJECT (vis->root_actuator));
|
|
225
|
|
226 vis->root_actuator = actuator;
|
|
227
|
|
228 pn_object_ref (PN_OBJECT (actuator));
|
|
229 pn_object_sink (PN_OBJECT (actuator));
|
|
230 pn_user_object_set_owner (PN_USER_OBJECT (actuator), PN_USER_OBJECT (vis));
|
|
231
|
|
232 pn_actuator_prepare (actuator, vis->root_image);
|
|
233 }
|
|
234
|
|
235 PnActuator*
|
|
236 pn_vis_get_root_actuator (PnVis *vis)
|
|
237 {
|
|
238 g_return_val_if_fail (vis != NULL, NULL);
|
|
239 g_return_val_if_fail (PN_IS_VIS (vis), NULL);
|
|
240
|
|
241 return vis->root_actuator;
|
|
242 }
|
|
243
|
|
244 void
|
|
245 pn_vis_set_image_size (PnVis *vis, guint width, guint height)
|
|
246 {
|
|
247 g_return_if_fail (vis != NULL);
|
|
248 g_return_if_fail (PN_IS_VIS (vis));
|
|
249
|
|
250 pn_image_set_size (vis->root_image, width, height);
|
|
251
|
|
252 if (vis->root_actuator)
|
|
253 pn_actuator_prepare (vis->root_actuator, vis->root_image);
|
|
254 }
|
|
255
|
|
256 gboolean
|
|
257 pn_vis_save_to_file (PnVis *vis, const gchar *fname)
|
|
258 {
|
|
259 xmlDocPtr doc;
|
|
260
|
|
261 doc = xmlNewDoc ("1.0");
|
|
262
|
|
263 doc->xmlRootNode = xmlNewDocNode (doc, NULL, "BUG", NULL);
|
|
264
|
|
265 pn_user_object_save_thyself (PN_USER_OBJECT (vis), doc->xmlRootNode);
|
|
266
|
|
267 if ( xmlSaveFile (fname, doc) == -1)
|
|
268 {
|
|
269 xmlFreeDoc (doc);
|
|
270 return FALSE;
|
|
271 }
|
|
272
|
|
273 xmlFreeDoc (doc);
|
|
274 return TRUE;
|
|
275 }
|
|
276
|
|
277 gboolean
|
|
278 pn_vis_load_from_file (PnVis *vis, const gchar *fname)
|
|
279 {
|
|
280 xmlDocPtr doc;
|
|
281 xmlNodePtr root_node;
|
|
282 xmlNsPtr ns;
|
|
283
|
|
284 doc = xmlParseFile (fname);
|
|
285 if (! doc)
|
|
286 {
|
|
287 pn_error ("unable to parse file \"%s\"", fname);
|
|
288 return FALSE;
|
|
289 }
|
|
290
|
|
291 root_node = xmlDocGetRootElement (doc);
|
|
292 if (! root_node)
|
|
293 {
|
|
294 pn_error ("no root element for file \"%s\"", fname);
|
|
295 return FALSE;
|
|
296 }
|
|
297
|
|
298 ns = xmlSearchNsByHref (doc, root_node, PN_XML_NS_HREF);
|
|
299 if (! ns)
|
|
300 {
|
|
301 pn_error ("invalid file format: paranormal namespace not found");
|
|
302 return FALSE;
|
|
303 }
|
|
304
|
|
305 if (g_strcasecmp (root_node->name, pn_user_object_get_name (PN_USER_OBJECT (vis))))
|
|
306 {
|
|
307 pn_error ("invalid file format: this file does not contain a Paranormal visualization");
|
|
308 return FALSE;
|
|
309 }
|
|
310
|
|
311 pn_user_object_load_thyself (PN_USER_OBJECT (vis), root_node);
|
|
312
|
|
313 return TRUE;
|
|
314 }
|
|
315
|
|
316 PnImage*
|
|
317 pn_vis_render (PnVis *vis, PnAudioData *audio_data)
|
|
318 {
|
|
319 g_return_val_if_fail (vis != NULL, NULL);
|
|
320 g_return_val_if_fail (PN_IS_VIS (vis), NULL);
|
|
321 g_return_val_if_fail (audio_data != NULL, NULL);
|
|
322 /* g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), NULL); */
|
|
323
|
|
324 if (vis->root_actuator)
|
|
325 pn_actuator_execute (vis->root_actuator,
|
|
326 vis->root_image,
|
|
327 audio_data);
|
|
328
|
|
329 return vis->root_image;
|
|
330 }
|