2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
26 #include "gstsmoothwave.h"
28 static GstElementDetails gst_smoothwave_details =
29 GST_ELEMENT_DETAILS ("Smooth waveform",
31 "Fading grayscale waveform display",
32 "Erik Walthinsen <omega@cse.ogi.edu>");
35 /* SmoothWave signals and args */
50 static void gst_smoothwave_base_init (gpointer g_class);
51 static void gst_smoothwave_class_init (GstSmoothWaveClass * klass);
52 static void gst_smoothwave_init (GstSmoothWave * smoothwave);
54 static void gst_smoothwave_set_property (GObject * object, guint prop_id,
55 const GValue * value, GParamSpec * pspec);
56 static void gst_smoothwave_get_property (GObject * object, guint prop_id,
57 GValue * value, GParamSpec * pspec);
59 static void gst_smoothwave_chain (GstPad * pad, GstData * _data);
61 static GstElementClass *parent_class = NULL;
63 /*static guint gst_smoothwave_signals[LAST_SIGNAL] = { 0 }; */
67 gst_smoothwave_get_type (void)
69 static GType smoothwave_type = 0;
71 if (!smoothwave_type) {
72 static const GTypeInfo smoothwave_info = {
73 sizeof (GstSmoothWaveClass),
74 gst_smoothwave_base_init,
76 (GClassInitFunc) gst_smoothwave_class_init,
79 sizeof (GstSmoothWave),
81 (GInstanceInitFunc) gst_smoothwave_init,
84 g_type_register_static (GST_TYPE_ELEMENT, "GstSmoothWave",
87 return smoothwave_type;
91 gst_smoothwave_base_init (gpointer g_class)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
95 gst_element_class_set_details (element_class, &gst_smoothwave_details);
99 gst_smoothwave_class_init (GstSmoothWaveClass * klass)
101 GObjectClass *gobject_class;
102 GstElementClass *gstelement_class;
104 gobject_class = (GObjectClass *) klass;
105 gstelement_class = (GstElementClass *) klass;
107 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
109 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_WIDTH, g_param_spec_int ("width", "width", "width", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE)); /* CHECKME */
110 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HEIGHT, g_param_spec_int ("height", "height", "height", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE)); /* CHECKME */
111 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_WIDGET, g_param_spec_object ("widget", "widget", "widget", GTK_TYPE_WIDGET, G_PARAM_READABLE)); /* CHECKME! */
113 gobject_class->set_property = gst_smoothwave_set_property;
114 gobject_class->get_property = gst_smoothwave_get_property;
118 gst_smoothwave_init (GstSmoothWave * smoothwave)
121 guint32 palette[256];
123 smoothwave->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
124 gst_element_add_pad (GST_ELEMENT (smoothwave), smoothwave->sinkpad);
125 gst_pad_set_chain_function (smoothwave->sinkpad, gst_smoothwave_chain);
126 smoothwave->srcpad = gst_pad_new ("src", GST_PAD_SRC);
127 gst_element_add_pad (GST_ELEMENT (smoothwave), smoothwave->srcpad);
129 /* smoothwave->meta = NULL; */
130 smoothwave->width = 512;
131 smoothwave->height = 256;
134 /* gtk_widget_set_default_colormap (gdk_rgb_get_cmap()); */
135 /* gtk_widget_set_default_visual (gdk_rgb_get_visual()); */
137 /* GST_DEBUG ("creating palette"); */
138 for (i = 0; i < 256; i++)
139 palette[i] = (i << 16) || (i << 8);
140 /* GST_DEBUG ("creating cmap"); */
141 smoothwave->cmap = gdk_rgb_cmap_new (palette, 256);
142 /* GST_DEBUG ("created cmap"); */
143 /* gtk_widget_set_default_colormap (smoothwave->cmap); */
145 smoothwave->image = gtk_drawing_area_new ();
146 gtk_drawing_area_size (GTK_DRAWING_AREA (smoothwave->image),
147 smoothwave->width, smoothwave->height);
148 gtk_widget_show (smoothwave->image);
150 smoothwave->imagebuffer = g_malloc (smoothwave->width * smoothwave->height);
151 memset (smoothwave->imagebuffer, 0, smoothwave->width * smoothwave->height);
155 gst_smoothwave_chain (GstPad * pad, GstData * _data)
157 GstBuffer *buf = GST_BUFFER (_data);
158 GstSmoothWave *smoothwave;
161 register guint32 *ptr;
164 g_return_if_fail (pad != NULL);
165 g_return_if_fail (GST_IS_PAD (pad));
166 g_return_if_fail (buf != NULL);
167 /* g_return_if_fail(GST_IS_BUFFER(buf)); */
169 smoothwave = GST_SMOOTHWAVE (GST_OBJECT_PARENT (pad));
171 /* first deal with audio metadata */
174 if (smoothwave->meta != NULL) {
175 /* FIXME: need to unref the old metadata so it goes away */
177 /* we just make a copy of the pointer */
178 smoothwave->meta = (MetaAudioRaw *) (buf->meta);
179 /* FIXME: now we have to ref the metadata so it doesn't go away */
183 /* g_return_if_fail(smoothwave->meta != NULL); */
185 samples = (gint16 *) GST_BUFFER_DATA (buf);
186 /* samplecount = buf->datasize / (smoothwave->meta->channels * sizeof(gint16)); */
187 samplecount = GST_BUFFER_SIZE (buf) / (2 * sizeof (gint16));
189 qheight = smoothwave->height / 4;
191 /* GST_DEBUG ("traversing %d",smoothwave->width); */
192 for (i = 0; i < MAX (smoothwave->width, samplecount); i++) {
193 gint16 y1 = (gint32) (samples[i * 2] * qheight) / 32768 + qheight;
194 gint16 y2 = (gint32) (samples[(i * 2) + 1] * qheight) / 32768 +
196 smoothwave->imagebuffer[y1 * smoothwave->width + i] = 0xff;
197 smoothwave->imagebuffer[y2 * smoothwave->width + i] = 0xff;
198 /* smoothwave->imagebuffer[i+(smoothwave->width*5)] = i; */
201 ptr = (guint32 *) smoothwave->imagebuffer;
202 for (i = 0; i < (smoothwave->width * smoothwave->height) / 4; i++) {
204 *ptr -= ((*ptr & 0xf0f0f0f0ul) >> 4) + ((*ptr & 0xe0e0e0e0ul) >> 5);
211 /* GST_DEBUG ("drawing"); */
212 /* GST_DEBUG ("gdk_draw_indexed_image(%p,%p,%d,%d,%d,%d,%s,%p,%d,%p);",
213 smoothwave->image->window,
214 smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
215 0,0,smoothwave->width,smoothwave->height,
216 "GDK_RGB_DITHER_NORMAL",
217 smoothwave->imagebuffer,smoothwave->width,
219 /* gdk_draw_indexed_image(smoothwave->image->window,
220 smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
221 0,0,smoothwave->width,smoothwave->height,
223 smoothwave->imagebuffer,smoothwave->width,
225 gdk_draw_gray_image (smoothwave->image->window,
226 smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
227 0, 0, smoothwave->width, smoothwave->height,
228 GDK_RGB_DITHER_NORMAL, smoothwave->imagebuffer, smoothwave->width);
230 /* gst_trace_add_entry(NULL,0,buf,"smoothwave: calculated smoothwave"); */
232 gst_buffer_unref (buf);
236 gst_smoothwave_set_property (GObject * object, guint prop_id,
237 const GValue * value, GParamSpec * pspec)
239 GstSmoothWave *smoothwave;
241 /* it's not null if we got it, but it might not be ours */
242 g_return_if_fail (GST_IS_SMOOTHWAVE (object));
243 smoothwave = GST_SMOOTHWAVE (object);
247 smoothwave->width = g_value_get_int (value);
248 gtk_drawing_area_size (GTK_DRAWING_AREA (smoothwave->image),
249 smoothwave->width, smoothwave->height);
250 gtk_widget_set_usize (GTK_WIDGET (smoothwave->image),
251 smoothwave->width, smoothwave->height);
254 smoothwave->height = g_value_get_int (value);
255 gtk_drawing_area_size (GTK_DRAWING_AREA (smoothwave->image),
256 smoothwave->width, smoothwave->height);
257 gtk_widget_set_usize (GTK_WIDGET (smoothwave->image),
258 smoothwave->width, smoothwave->height);
266 gst_smoothwave_get_property (GObject * object, guint prop_id, GValue * value,
269 GstSmoothWave *smoothwave;
271 /* it's not null if we got it, but it might not be ours */
272 smoothwave = GST_SMOOTHWAVE (object);
276 g_value_set_int (value, smoothwave->width);
280 g_value_set_int (value, smoothwave->height);
284 g_value_set_object (value, smoothwave->image);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 plugin_init (GstPlugin * plugin)
299 if (!gst_element_register (plugin, "smoothwave", GST_RANK_NONE,
300 GST_TYPE_SMOOTHWAVE))
306 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
309 "Fading greyscale waveform display",
310 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)