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);
|
|
146 actuator_node = xmlNewChild (actuators_node, NULL, "BUG", NULL);
|
|
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;
|
|
158 xmlNodePtr actuators_node, actuator_node;
|
|
159 PnActuator *root_actuator;
|
|
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
|
|
182 /* Create a new actuator */
|
|
183 root_actuator = pn_actuator_factory_new_actuator_from_xml (actuator_node);
|
|
184 if (! root_actuator)
|
|
185 pn_error ("Unknown actuator \"%s\"", actuator_node->name);
|
|
186
|
|
187 pn_vis_set_root_actuator (vis, root_actuator);
|
|
188
|
|
189 done:
|
|
190 if (parent_class->load_thyself)
|
|
191 parent_class->load_thyself (user_object, node);
|
|
192 }
|
|
193
|
|
194 PnVis*
|
|
195 pn_vis_new (guint image_width, guint image_height)
|
|
196 {
|
|
197 PnVis *vis;
|
|
198
|
|
199 g_return_val_if_fail (image_width > 0, NULL);
|
|
200 g_return_val_if_fail (image_height > 0, NULL);
|
|
201
|
|
202 vis = (PnVis *) g_object_new (PN_TYPE_VIS, NULL);
|
|
203 pn_vis_set_image_size (vis, image_width, image_height);
|
|
204
|
|
205 return vis;
|
|
206 }
|
|
207
|
|
208 void
|
|
209 pn_vis_set_root_actuator (PnVis *vis, PnActuator *actuator)
|
|
210 {
|
|
211 g_return_if_fail (vis != NULL);
|
|
212 g_return_if_fail (PN_IS_VIS (vis));
|
|
213 g_return_if_fail (actuator != NULL);
|
|
214 g_return_if_fail (PN_IS_ACTUATOR (actuator));
|
|
215
|
|
216 if (vis->root_actuator)
|
|
217 pn_object_unref (PN_OBJECT (vis->root_actuator));
|
|
218
|
|
219 vis->root_actuator = actuator;
|
|
220
|
|
221 pn_object_ref (PN_OBJECT (actuator));
|
|
222 pn_object_sink (PN_OBJECT (actuator));
|
|
223 pn_user_object_set_owner (PN_USER_OBJECT (actuator), PN_USER_OBJECT (vis));
|
|
224
|
|
225 pn_actuator_prepare (actuator, vis->root_image);
|
|
226 }
|
|
227
|
|
228 PnActuator*
|
|
229 pn_vis_get_root_actuator (PnVis *vis)
|
|
230 {
|
|
231 g_return_val_if_fail (vis != NULL, NULL);
|
|
232 g_return_val_if_fail (PN_IS_VIS (vis), NULL);
|
|
233
|
|
234 return vis->root_actuator;
|
|
235 }
|
|
236
|
|
237 void
|
|
238 pn_vis_set_image_size (PnVis *vis, guint width, guint height)
|
|
239 {
|
|
240 g_return_if_fail (vis != NULL);
|
|
241 g_return_if_fail (PN_IS_VIS (vis));
|
|
242
|
|
243 pn_image_set_size (vis->root_image, width, height);
|
|
244
|
|
245 if (vis->root_actuator)
|
|
246 pn_actuator_prepare (vis->root_actuator, vis->root_image);
|
|
247 }
|
|
248
|
|
249 gboolean
|
|
250 pn_vis_save_to_file (PnVis *vis, const gchar *fname)
|
|
251 {
|
|
252 xmlDocPtr doc;
|
|
253
|
|
254 doc = xmlNewDoc ("1.0");
|
|
255
|
|
256 doc->xmlRootNode = xmlNewDocNode (doc, NULL, "BUG", NULL);
|
|
257
|
|
258 pn_user_object_save_thyself (PN_USER_OBJECT (vis), doc->xmlRootNode);
|
|
259
|
|
260 if ( xmlSaveFile (fname, doc) == -1)
|
|
261 {
|
|
262 xmlFreeDoc (doc);
|
|
263 return FALSE;
|
|
264 }
|
|
265
|
|
266 xmlFreeDoc (doc);
|
|
267 return TRUE;
|
|
268 }
|
|
269
|
|
270 gboolean
|
|
271 pn_vis_load_from_file (PnVis *vis, const gchar *fname)
|
|
272 {
|
|
273 xmlDocPtr doc;
|
|
274 xmlNodePtr root_node;
|
|
275 xmlNsPtr ns;
|
|
276
|
|
277 doc = xmlParseFile (fname);
|
|
278 if (! doc)
|
|
279 {
|
|
280 pn_error ("unable to parse file \"%s\"", fname);
|
|
281 return FALSE;
|
|
282 }
|
|
283
|
|
284 root_node = xmlDocGetRootElement (doc);
|
|
285 if (! root_node)
|
|
286 {
|
|
287 pn_error ("no root element for file \"%s\"", fname);
|
|
288 return FALSE;
|
|
289 }
|
|
290
|
|
291 ns = xmlSearchNsByHref (doc, root_node, PN_XML_NS_HREF);
|
|
292 if (! ns)
|
|
293 {
|
|
294 pn_error ("invalid file format: paranormal namespace not found");
|
|
295 return FALSE;
|
|
296 }
|
|
297
|
|
298 if (g_strcasecmp (root_node->name, pn_user_object_get_name (PN_USER_OBJECT (vis))))
|
|
299 {
|
|
300 pn_error ("invalid file format: this file does not contain a Paranormal visualization");
|
|
301 return FALSE;
|
|
302 }
|
|
303
|
|
304 pn_user_object_load_thyself (PN_USER_OBJECT (vis), root_node);
|
|
305
|
|
306 return TRUE;
|
|
307 }
|
|
308
|
|
309 PnImage*
|
|
310 pn_vis_render (PnVis *vis, PnAudioData *audio_data)
|
|
311 {
|
|
312 g_return_val_if_fail (vis != NULL, NULL);
|
|
313 g_return_val_if_fail (PN_IS_VIS (vis), NULL);
|
|
314 g_return_val_if_fail (audio_data != NULL, NULL);
|
|
315 /* g_return_val_if_fail (PN_IS_AUDIO_DATA (audio_data), NULL); */
|
|
316
|
|
317 if (vis->root_actuator)
|
|
318 pn_actuator_execute (vis->root_actuator,
|
|
319 vis->root_image,
|
|
320 audio_data);
|
|
321
|
|
322 return vis->root_image;
|
|
323 }
|