filter newlines out of GST_DEBUG statements to reflect new core behavior fixes to...
[platform/upstream/gstreamer.git] / gst / smoothwave / gstsmoothwave.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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  */
19
20
21 #include <string.h>
22
23 #include "gstsmoothwave.h"
24
25 static GstElementDetails gst_smoothwave_details = {
26   "Smooth waveform",
27   "Sink/Visualization",
28   "Fading grayscale waveform display",
29   VERSION,
30   "Erik Walthinsen <omega@cse.ogi.edu>",
31   "(C) 1999",
32 };
33
34
35 /* SmoothWave signals and args */
36 enum {
37   /* FILL ME */
38   LAST_SIGNAL
39 };
40
41 enum {
42   ARG_0,
43   ARG_WIDTH,
44   ARG_HEIGHT,
45   ARG_WIDGET,
46 };
47
48
49 static void     gst_smoothwave_class_init       (GstSmoothWaveClass *klass);
50 static void     gst_smoothwave_init             (GstSmoothWave *smoothwave);
51
52 static void     gst_smoothwave_set_property             (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
53 static void     gst_smoothwave_get_property             (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
54
55 static void     gst_smoothwave_chain            (GstPad *pad, GstBuffer *buf);
56
57 static GstElementClass *parent_class = NULL;
58 /*static guint gst_smoothwave_signals[LAST_SIGNAL] = { 0 }; */
59
60
61 GType
62 gst_smoothwave_get_type (void)
63 {
64   static GType smoothwave_type = 0;
65
66   if (!smoothwave_type) {
67     static const GTypeInfo smoothwave_info = {
68       sizeof(GstSmoothWaveClass),      NULL,
69       NULL,
70       (GClassInitFunc)gst_smoothwave_class_init,
71       NULL,
72       NULL,
73       sizeof(GstSmoothWave),
74       0,
75       (GInstanceInitFunc)gst_smoothwave_init,
76     };
77     smoothwave_type = g_type_register_static(GST_TYPE_ELEMENT, "GstSmoothWave", &smoothwave_info, 0);
78   }
79   return smoothwave_type;
80 }
81
82 static void
83 gst_smoothwave_class_init (GstSmoothWaveClass *klass)
84 {
85   GObjectClass *gobject_class;
86   GstElementClass *gstelement_class;
87
88   gobject_class = (GObjectClass*)klass;
89   gstelement_class = (GstElementClass*)klass;
90
91   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
92
93   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_WIDTH,
94     g_param_spec_int("width","width","width",
95                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
96   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_HEIGHT,
97     g_param_spec_int("height","height","height",
98                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
99   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_WIDGET,
100     g_param_spec_object("widget","widget","widget",
101                         GTK_TYPE_WIDGET,G_PARAM_READABLE)); /* CHECKME! */
102
103   gobject_class->set_property = gst_smoothwave_set_property;
104   gobject_class->get_property = gst_smoothwave_get_property;
105 }
106
107 static void
108 gst_smoothwave_init (GstSmoothWave *smoothwave)
109 {
110   int i;
111   guint32 palette[256];
112
113   smoothwave->sinkpad = gst_pad_new("sink",GST_PAD_SINK);
114   gst_element_add_pad(GST_ELEMENT(smoothwave),smoothwave->sinkpad);
115   gst_pad_set_chain_function(smoothwave->sinkpad,gst_smoothwave_chain);
116   smoothwave->srcpad = gst_pad_new("src",GST_PAD_SRC);
117   gst_element_add_pad(GST_ELEMENT(smoothwave),smoothwave->srcpad);
118
119 /*  smoothwave->meta = NULL; */
120   smoothwave->width = 512;
121   smoothwave->height = 256;
122
123   gdk_rgb_init();
124 /*  gtk_widget_set_default_colormap (gdk_rgb_get_cmap()); */
125 /*  gtk_widget_set_default_visual (gdk_rgb_get_visual()); */
126
127 /*  GST_DEBUG (0,"creating palette"); */
128   for (i=0;i<256;i++)
129     palette[i] = (i << 16) || (i << 8);
130 /*  GST_DEBUG (0,"creating cmap"); */
131   smoothwave->cmap = gdk_rgb_cmap_new(palette,256);
132 /*  GST_DEBUG (0,"created cmap"); */
133 /*  gtk_widget_set_default_colormap (smoothwave->cmap); */
134
135   smoothwave->image = gtk_drawing_area_new();
136   gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
137                         smoothwave->width,smoothwave->height);
138   gtk_widget_show(smoothwave->image);
139
140   smoothwave->imagebuffer = g_malloc(smoothwave->width*smoothwave->height);
141   memset(smoothwave->imagebuffer,0,smoothwave->width*smoothwave->height);
142 }
143
144 static void
145 gst_smoothwave_chain (GstPad *pad, GstBuffer *buf)
146 {
147   GstSmoothWave *smoothwave;
148   gint16 *samples;
149   gint samplecount,i;
150   register guint32 *ptr;
151   gint qheight;
152
153   g_return_if_fail(pad != NULL);
154   g_return_if_fail(GST_IS_PAD(pad));
155   g_return_if_fail(buf != NULL);
156 /*  g_return_if_fail(GST_IS_BUFFER(buf)); */
157
158   smoothwave = GST_SMOOTHWAVE(GST_OBJECT_PARENT (pad));
159
160   /* first deal with audio metadata */
161 /*  if (buf->meta) { */
162 /*    if (smoothwave->meta != NULL) { */
163 /*      /* FIXME: need to unref the old metadata so it goes away */ */
164 /*    } */
165 /*    /* we just make a copy of the pointer */ */
166 /*    smoothwave->meta = (MetaAudioRaw *)(buf->meta); */
167 /*    /* FIXME: now we have to ref the metadata so it doesn't go away */ */
168 /*  } */
169
170 /*  g_return_if_fail(smoothwave->meta != NULL); */
171
172   samples = (gint16 *)GST_BUFFER_DATA(buf);
173 /*  samplecount = buf->datasize / (smoothwave->meta->channels * sizeof(gint16)); */
174   samplecount = GST_BUFFER_SIZE(buf) / (2 * sizeof(gint16));
175
176   qheight = smoothwave->height/4;
177
178 /*  GST_DEBUG (0,"traversing %d",smoothwave->width); */
179   for (i=0;i<MAX(smoothwave->width,samplecount);i++) {
180     gint16 y1 = (gint32)(samples[i*2] * qheight) / 32768 +
181                 qheight;
182     gint16 y2 = (gint32)(samples[(i*2)+1] * qheight) / 32768 +
183                 (qheight*3);
184     smoothwave->imagebuffer[y1*smoothwave->width + i] = 0xff;
185     smoothwave->imagebuffer[y2*smoothwave->width + i] = 0xff;
186 /*    smoothwave->imagebuffer[i+(smoothwave->width*5)] = i; */
187   }
188
189   ptr = (guint32 *)smoothwave->imagebuffer;
190   for (i=0;i<(smoothwave->width*smoothwave->height)/4;i++) {
191     if (*ptr)
192       *(ptr++) -= ((*ptr & 0xf0f0f0f0ul) >> 4) + ((*ptr & 0xe0e0e0e0ul) >> 5);
193     else
194       ptr++;
195   }
196
197 /*  GST_DEBUG (0,"drawing"); */
198 /*  GST_DEBUG (0,"gdk_draw_indexed_image(%p,%p,%d,%d,%d,%d,%s,%p,%d,%p);",
199         smoothwave->image->window,
200         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
201         0,0,smoothwave->width,smoothwave->height,
202         "GDK_RGB_DITHER_NORMAL",
203         smoothwave->imagebuffer,smoothwave->width,
204         smoothwave->cmap);*/
205 /*  gdk_draw_indexed_image(smoothwave->image->window,
206         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
207         0,0,smoothwave->width,smoothwave->height,
208         GDK_RGB_DITHER_NONE,
209         smoothwave->imagebuffer,smoothwave->width,
210         smoothwave->cmap);*/
211   gdk_draw_gray_image(smoothwave->image->window,
212         smoothwave->image->style->fg_gc[GTK_STATE_NORMAL],
213         0,0,smoothwave->width,smoothwave->height,
214         GDK_RGB_DITHER_NORMAL,
215         smoothwave->imagebuffer,smoothwave->width);
216
217 /*  gst_trace_add_entry(NULL,0,buf,"smoothwave: calculated smoothwave"); */
218
219   gst_buffer_unref(buf);
220 }
221
222 static void
223 gst_smoothwave_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
224 {
225   GstSmoothWave *smoothwave;
226
227   /* it's not null if we got it, but it might not be ours */
228   g_return_if_fail(GST_IS_SMOOTHWAVE(object));
229   smoothwave = GST_SMOOTHWAVE(object);
230
231   switch (prop_id) {
232     case ARG_WIDTH:
233       smoothwave->width = g_value_get_int (value);
234       gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
235                             smoothwave->width,smoothwave->height);
236       gtk_widget_set_usize(GTK_WIDGET(smoothwave->image),
237                            smoothwave->width,smoothwave->height);
238       break;
239     case ARG_HEIGHT:
240       smoothwave->height = g_value_get_int (value);
241       gtk_drawing_area_size(GTK_DRAWING_AREA(smoothwave->image),
242                             smoothwave->width,smoothwave->height);
243       gtk_widget_set_usize(GTK_WIDGET(smoothwave->image),
244                            smoothwave->width,smoothwave->height);
245       break;
246     default:
247       break;
248   }
249 }
250
251 static void
252 gst_smoothwave_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
253 {
254   GstSmoothWave *smoothwave;
255
256   /* it's not null if we got it, but it might not be ours */
257   smoothwave = GST_SMOOTHWAVE(object);
258
259   switch (prop_id) {
260     case ARG_WIDTH: {
261       g_value_set_int (value, smoothwave->width);
262       break;
263     }
264     case ARG_HEIGHT: {
265       g_value_set_int (value, smoothwave->height);
266       break;
267     }
268     case ARG_WIDGET: {
269       g_value_set_object (value, smoothwave->image);
270       break;
271     }
272     default: {
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275     }
276   }
277 }
278
279
280
281 static gboolean
282 plugin_init (GModule *module, GstPlugin *plugin)
283 {
284   GstElementFactory *factory;
285
286   /* create an elementfactory for the smoothwave element */
287   factory = gst_elementfactory_new("smoothwave",GST_TYPE_SMOOTHWAVE,
288                                    &gst_smoothwave_details);
289   g_return_val_if_fail(factory != NULL, FALSE);
290
291   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
292
293   return TRUE;
294 }
295
296 GstPluginDesc plugin_desc = {
297   GST_VERSION_MAJOR,
298   GST_VERSION_MINOR,
299   "smoothwave",
300   plugin_init
301 };