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