1 /* gstsynaesthesia.c: implementation of synaesthesia drawing element
2 * Copyright (C) <2001> Richard Boulton <richard@tartarus.org>
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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include <config.h>
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 #include "synaescope.h"
28 #define GST_TYPE_SYNAESTHESIA (gst_synaesthesia_get_type())
29 #define GST_SYNAESTHESIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SYNAESTHESIA,GstSynaesthesia))
30 #define GST_SYNAESTHESIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SYNAESTHESIA,GstSynaesthesia))
31 #define GST_IS_SYNAESTHESIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SYNAESTHESIA))
32 #define GST_IS_SYNAESTHESIA_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SYNAESTHESIA))
34 typedef struct _GstSynaesthesia GstSynaesthesia;
35 typedef struct _GstSynaesthesiaClass GstSynaesthesiaClass;
37 struct _GstSynaesthesia {
38 GstElement element;
40 /* pads */
41 GstPad *sinkpad,*srcpad;
42 GstBufferPool *peerpool;
44 /* the timestamp of the next frame */
45 guint64 next_time;
46 gint16 datain[2][512];
48 /* video state */
49 gfloat fps;
50 gint width;
51 gint height;
52 gboolean first_buffer;
53 };
55 struct _GstSynaesthesiaClass {
56 GstElementClass parent_class;
57 };
59 GType gst_synaesthesia_get_type(void);
62 /* elementfactory information */
63 static GstElementDetails gst_synaesthesia_details = {
64 "Synaesthesia",
65 "Visualization",
66 "GPL",
67 "Creates video visualizations of audio input, using stereo and pitch information",
68 VERSION,
69 "Richard Boulton <richard@tartarus.org>",
70 "(C) 2002",
71 };
73 /* signals and args */
74 enum {
75 /* FILL ME */
76 LAST_SIGNAL
77 };
79 enum {
80 ARG_0,
81 ARG_WIDTH,
82 ARG_HEIGHT,
83 ARG_FPS,
84 /* FILL ME */
85 };
87 GST_PAD_TEMPLATE_FACTORY (src_template,
88 "src",
89 GST_PAD_SRC,
90 GST_PAD_ALWAYS,
91 GST_CAPS_NEW (
92 "synaesthesiasrc",
93 "video/x-raw-rgb",
94 "bpp", GST_PROPS_INT (32),
95 "depth", GST_PROPS_INT (32),
96 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
97 "red_mask", GST_PROPS_INT (R_MASK_32),
98 "green_mask", GST_PROPS_INT (G_MASK_32),
99 "blue_mask", GST_PROPS_INT (B_MASK_32),
100 "width", GST_PROPS_INT_RANGE (16, 4096),
101 "height", GST_PROPS_INT_RANGE (16, 4096),
102 "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT)
103 )
104 )
106 GST_PAD_TEMPLATE_FACTORY (sink_template,
107 "sink", /* the name of the pads */
108 GST_PAD_SINK, /* type of the pad */
109 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
110 GST_CAPS_NEW (
111 "synaesthesiasink", /* the name of the caps */
112 "audio/x-raw-int", /* the mime type of the caps */
113 /* Properties follow: */
114 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
115 "signed", GST_PROPS_BOOLEAN (TRUE),
116 "width", GST_PROPS_INT (16),
117 "depth", GST_PROPS_INT (16),
118 "rate", GST_PROPS_INT_RANGE (8000, 96000),
119 "channels", GST_PROPS_INT (2)
120 )
121 )
124 static void gst_synaesthesia_class_init (GstSynaesthesiaClass *klass);
125 static void gst_synaesthesia_init (GstSynaesthesia *synaesthesia);
127 static void gst_synaesthesia_set_property (GObject *object, guint prop_id,
128 const GValue *value, GParamSpec *pspec);
129 static void gst_synaesthesia_get_property (GObject *object, guint prop_id,
130 GValue *value, GParamSpec *pspec);
132 static void gst_synaesthesia_chain (GstPad *pad, GstData *_data);
134 static GstElementStateReturn
135 gst_synaesthesia_change_state (GstElement *element);
137 static GstPadLinkReturn
138 gst_synaesthesia_sinkconnect (GstPad *pad, GstCaps *caps);
140 static GstElementClass *parent_class = NULL;
142 GType
143 gst_synaesthesia_get_type (void)
144 {
145 static GType type = 0;
147 if (!type) {
148 static const GTypeInfo info = {
149 sizeof (GstSynaesthesiaClass),
150 NULL,
151 NULL,
152 (GClassInitFunc) gst_synaesthesia_class_init,
153 NULL,
154 NULL,
155 sizeof (GstSynaesthesia),
156 0,
157 (GInstanceInitFunc) gst_synaesthesia_init,
158 };
159 type = g_type_register_static (GST_TYPE_ELEMENT, "GstSynaesthesia", &info, 0);
160 }
161 return type;
162 }
164 static void
165 gst_synaesthesia_class_init(GstSynaesthesiaClass *klass)
166 {
167 GObjectClass *gobject_class;
168 GstElementClass *gstelement_class;
170 gobject_class = (GObjectClass*) klass;
171 gstelement_class = (GstElementClass*) klass;
173 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
175 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_WIDTH,
176 g_param_spec_int ("width","Width","The Width",
177 1, 2048, 320, G_PARAM_READWRITE));
178 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HEIGHT,
179 g_param_spec_int ("height","Height","The height",
180 1, 2048, 320, G_PARAM_READWRITE));
181 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FPS,
182 g_param_spec_float ("fps","FPS","Frames per second",
183 0., G_MAXFLOAT, 25., G_PARAM_READWRITE));
185 gobject_class->set_property = gst_synaesthesia_set_property;
186 gobject_class->get_property = gst_synaesthesia_get_property;
188 gstelement_class->change_state = gst_synaesthesia_change_state;
189 }
191 static void
192 gst_synaesthesia_init (GstSynaesthesia *synaesthesia)
193 {
194 /* create the sink and src pads */
195 synaesthesia->sinkpad = gst_pad_new_from_template (
196 GST_PAD_TEMPLATE_GET (sink_template ), "sink");
197 synaesthesia->srcpad = gst_pad_new_from_template (
198 GST_PAD_TEMPLATE_GET (src_template ), "src");
199 gst_element_add_pad (GST_ELEMENT (synaesthesia), synaesthesia->sinkpad);
200 gst_element_add_pad (GST_ELEMENT (synaesthesia), synaesthesia->srcpad);
202 gst_pad_set_chain_function (synaesthesia->sinkpad, gst_synaesthesia_chain);
203 gst_pad_set_link_function (synaesthesia->sinkpad, gst_synaesthesia_sinkconnect);
205 GST_FLAG_SET (synaesthesia, GST_ELEMENT_EVENT_AWARE);
207 /* reset the initial video state */
208 synaesthesia->width = 320;
209 synaesthesia->height = 200;
210 synaesthesia->fps = 25.; /* desired frame rate */
212 }
214 static GstPadLinkReturn
215 gst_synaesthesia_sinkconnect (GstPad *pad, GstCaps *caps)
216 {
217 GstSynaesthesia *synaesthesia;
218 synaesthesia = GST_SYNAESTHESIA (gst_pad_get_parent (pad));
220 if (!GST_CAPS_IS_FIXED (caps)) {
221 return GST_PAD_LINK_DELAYED;
222 }
224 return GST_PAD_LINK_OK;
225 }
227 static void
228 gst_synaesthesia_chain (GstPad *pad, GstData *_data)
229 {
230 GstBuffer *bufin = GST_BUFFER (_data);
231 GstSynaesthesia *synaesthesia;
232 GstBuffer *bufout;
233 guint32 samples_in;
234 gint16 *data;
235 gint i;
237 synaesthesia = GST_SYNAESTHESIA (gst_pad_get_parent (pad));
239 GST_DEBUG ("Synaesthesia: chainfunc called");
241 if (GST_IS_EVENT (bufin)) {
242 GstEvent *event = GST_EVENT (bufin);
244 switch (GST_EVENT_TYPE (event)) {
245 case GST_EVENT_DISCONTINUOUS:
246 {
247 gint64 value = 0;
248 gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
249 synaesthesia->next_time = value;
250 }
251 default:
252 gst_pad_event_default (pad, event);
253 break;
254 }
255 return;
256 }
258 samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
260 GST_DEBUG ("input buffer has %d samples", samples_in);
262 /* FIXME: should really select the first 1024 samples after the timestamp. */
263 if (GST_BUFFER_TIMESTAMP (bufin) < synaesthesia->next_time || samples_in < 1024) {
264 GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (bufin), synaesthesia->next_time);
265 gst_buffer_unref (bufin);
266 return;
267 }
269 data = (gint16 *) GST_BUFFER_DATA (bufin);
270 for (i=0; i < 512; i++) {
271 synaesthesia->datain[0][i] = *data++;
272 synaesthesia->datain[1][i] = *data++;
273 }
275 if (synaesthesia->first_buffer) {
276 GstCaps *caps;
278 synaesthesia_init (synaesthesia->width, synaesthesia->height);
280 GST_DEBUG ("making new pad");
282 caps = GST_CAPS_NEW (
283 "synaesthesiasrc",
284 "video/x-raw-rgb",
285 "format", GST_PROPS_FOURCC (GST_STR_FOURCC ("RGB ")),
286 "bpp", GST_PROPS_INT (32),
287 "depth", GST_PROPS_INT (32),
288 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
289 "red_mask", GST_PROPS_INT (R_MASK_32),
290 "green_mask", GST_PROPS_INT (G_MASK_32),
291 "blue_mask", GST_PROPS_INT (B_MASK_32),
292 "width", GST_PROPS_INT (synaesthesia->width),
293 "height", GST_PROPS_INT (synaesthesia->height),
294 "framerate", GST_PROPS_FLOAT (synaesthesia->fps)
295 );
297 if (gst_pad_try_set_caps (synaesthesia->srcpad, caps) <= 0) {
298 gst_element_error (GST_ELEMENT (synaesthesia), "could not set caps");
299 return;
300 }
301 synaesthesia->first_buffer = FALSE;
302 }
304 bufout = gst_buffer_new ();
305 GST_BUFFER_SIZE (bufout) = synaesthesia->width * synaesthesia->height * 4;
306 GST_BUFFER_DATA (bufout) = (guchar *) synaesthesia_update (synaesthesia->datain);
307 GST_BUFFER_TIMESTAMP (bufout) = synaesthesia->next_time;
308 GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
310 synaesthesia->next_time += GST_SECOND / synaesthesia->fps;
312 gst_pad_push (synaesthesia->srcpad, GST_DATA (bufout));
314 gst_buffer_unref (bufin);
316 GST_DEBUG ("Synaesthesia: exiting chainfunc");
318 }
320 static void
321 gst_synaesthesia_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
322 {
323 GstSynaesthesia *synaesthesia;
325 /* it's not null if we got it, but it might not be ours */
326 g_return_if_fail (GST_IS_SYNAESTHESIA (object));
327 synaesthesia = GST_SYNAESTHESIA (object);
329 switch (prop_id) {
330 case ARG_WIDTH:
331 synaesthesia->width = g_value_get_int (value);
332 break;
333 case ARG_HEIGHT:
334 synaesthesia->height = g_value_get_int (value);
335 break;
336 case ARG_FPS:
337 synaesthesia->fps = g_value_get_float (value);
338 break;
339 default:
340 break;
341 }
342 }
344 static void
345 gst_synaesthesia_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
346 {
347 GstSynaesthesia *synaesthesia;
349 /* it's not null if we got it, but it might not be ours */
350 g_return_if_fail (GST_IS_SYNAESTHESIA (object));
351 synaesthesia = GST_SYNAESTHESIA (object);
353 switch (prop_id) {
354 case ARG_WIDTH:
355 g_value_set_int (value, synaesthesia->width);
356 break;
357 case ARG_HEIGHT:
358 g_value_set_int (value, synaesthesia->height);
359 break;
360 case ARG_FPS:
361 g_value_set_float (value, synaesthesia->fps);
362 break;
363 default:
364 break;
365 }
366 }
368 static GstElementStateReturn
369 gst_synaesthesia_change_state (GstElement *element)
370 {
371 GstSynaesthesia *synaesthesia;
373 synaesthesia = GST_SYNAESTHESIA (element);
375 switch (GST_STATE_TRANSITION (element)) {
376 case GST_STATE_READY_TO_PAUSED:
377 synaesthesia->next_time = 0;
378 synaesthesia->peerpool = NULL;
379 synaesthesia->first_buffer = TRUE;
380 break;
381 }
383 if (GST_ELEMENT_CLASS (parent_class)->change_state)
384 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
386 return GST_STATE_SUCCESS;
387 }
390 static gboolean
391 plugin_init (GModule *module, GstPlugin *plugin)
392 {
393 GstElementFactory *factory;
395 /* create an elementfactory for the synaesthesia element */
396 factory = gst_element_factory_new("synaesthesia",GST_TYPE_SYNAESTHESIA,
397 &gst_synaesthesia_details);
398 g_return_val_if_fail(factory != NULL, FALSE);
400 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_template));
401 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_template));
403 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
405 return TRUE;
406 }
408 GstPluginDesc plugin_desc = {
409 GST_VERSION_MAJOR,
410 GST_VERSION_MINOR,
411 "synaesthesia",
412 plugin_init
413 };