72a0814eb75ae9fc6522b1ac481bd457b7ff22d4
[platform/upstream/gstreamer.git] / gst / audiovisualizers / gstwavescope.c
1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstwavescope.c: simple oscilloscope
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION:element-wavescope
22  * @see_also: goom
23  *
24  * Wavescope is a simple audio visualisation element. It renders the waveforms
25  * like on an oscilloscope.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch audiotestsrc ! audioconvert ! wavescope ! ximagesink
31  * ]|
32  * </refsect2>
33  */
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39 #include "gstwavescope.h"
40
41 static GstStaticPadTemplate gst_wave_scope_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45 #if G_BYTE_ORDER == G_BIG_ENDIAN
46     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("xRGB"))
47 #else
48     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRx"))
49 #endif
50     );
51
52 static GstStaticPadTemplate gst_wave_scope_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("audio/x-raw, "
57         "format = (string) " GST_AUDIO_NE (S16) ", "
58         "layout = (string) interleaved, "
59         "rate = (int) [ 8000, 96000 ], "
60         "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
61     );
62
63
64 GST_DEBUG_CATEGORY_STATIC (wave_scope_debug);
65 #define GST_CAT_DEFAULT wave_scope_debug
66
67 enum
68 {
69   PROP_0,
70   PROP_STYLE
71 };
72
73 enum
74 {
75   STYLE_DOTS = 0,
76   STYLE_LINES,
77   STYLE_COLOR_DOTS,
78   STYLE_COLOR_LINES,
79   NUM_STYLES
80 };
81
82 #define GST_TYPE_WAVE_SCOPE_STYLE (gst_wave_scope_style_get_type ())
83 static GType
84 gst_wave_scope_style_get_type (void)
85 {
86   static GType gtype = 0;
87
88   if (gtype == 0) {
89     static const GEnumValue values[] = {
90       {STYLE_DOTS, "draw dots (default)", "dots"},
91       {STYLE_LINES, "draw lines", "lines"},
92       {STYLE_COLOR_DOTS, "draw color dots", "color-dots"},
93       {STYLE_COLOR_LINES, "draw color lines", "color-lines"},
94       {0, NULL, NULL}
95     };
96
97     gtype = g_enum_register_static ("GstWaveScopeStyle", values);
98   }
99   return gtype;
100 }
101
102 static void gst_wave_scope_set_property (GObject * object, guint prop_id,
103     const GValue * value, GParamSpec * pspec);
104 static void gst_wave_scope_get_property (GObject * object, guint prop_id,
105     GValue * value, GParamSpec * pspec);
106 static void gst_wave_scope_finalize (GObject * object);
107
108 static void render_dots (GstAudioVisualizer * scope, guint32 * vdata,
109     gint16 * adata, guint num_samples);
110 static void render_lines (GstAudioVisualizer * scope, guint32 * vdata,
111     gint16 * adata, guint num_samples);
112 static void render_color_dots (GstAudioVisualizer * base, guint32 * vdata,
113     gint16 * adata, guint num_samples);
114 static void render_color_lines (GstAudioVisualizer * base, guint32 * vdata,
115     gint16 * adata, guint num_samples);
116
117 static gboolean gst_wave_scope_setup (GstAudioVisualizer * scope);
118 static gboolean gst_wave_scope_render (GstAudioVisualizer * base,
119     GstBuffer * audio, GstBuffer * video);
120
121 #define gst_wave_scope_parent_class parent_class
122 G_DEFINE_TYPE (GstWaveScope, gst_wave_scope, GST_TYPE_AUDIO_VISUALIZER);
123
124 static void
125 gst_wave_scope_class_init (GstWaveScopeClass * g_class)
126 {
127   GObjectClass *gobject_class = (GObjectClass *) g_class;
128   GstElementClass *gstelement_class = (GstElementClass *) g_class;
129   GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
130
131   gobject_class->set_property = gst_wave_scope_set_property;
132   gobject_class->get_property = gst_wave_scope_get_property;
133   gobject_class->finalize = gst_wave_scope_finalize;
134
135   scope_class->setup = GST_DEBUG_FUNCPTR (gst_wave_scope_setup);
136   scope_class->render = GST_DEBUG_FUNCPTR (gst_wave_scope_render);
137
138   g_object_class_install_property (gobject_class, PROP_STYLE,
139       g_param_spec_enum ("style", "drawing style",
140           "Drawing styles for the wave form display.",
141           GST_TYPE_WAVE_SCOPE_STYLE, STYLE_DOTS,
142           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143
144   gst_element_class_set_details_simple (gstelement_class,
145       "Waveform oscilloscope", "Visualization", "Simple waveform oscilloscope",
146       "Stefan Kost <ensonic@users.sf.net>");
147
148   gst_element_class_add_pad_template (gstelement_class,
149       gst_static_pad_template_get (&gst_wave_scope_src_template));
150   gst_element_class_add_pad_template (gstelement_class,
151       gst_static_pad_template_get (&gst_wave_scope_sink_template));
152
153   scope_class->render = GST_DEBUG_FUNCPTR (gst_wave_scope_render);
154 }
155
156 static void
157 gst_wave_scope_init (GstWaveScope * scope)
158 {
159   /* do nothing */
160 }
161
162 static void
163 gst_wave_scope_finalize (GObject * object)
164 {
165   GstWaveScope *scope = GST_WAVE_SCOPE (object);
166
167   if (scope->flt) {
168     g_free (scope->flt);
169     scope->flt = NULL;
170   }
171
172   G_OBJECT_CLASS (parent_class)->finalize (object);
173 }
174
175 static gboolean
176 gst_wave_scope_setup (GstAudioVisualizer * bscope)
177 {
178   GstWaveScope *scope = GST_WAVE_SCOPE (bscope);
179
180   if (scope->flt)
181     g_free (scope->flt);
182
183   scope->flt = g_new0 (gdouble, 6 * GST_AUDIO_INFO_CHANNELS (&bscope->ainfo));
184
185   return TRUE;
186 }
187
188 static void
189 gst_wave_scope_set_property (GObject * object, guint prop_id,
190     const GValue * value, GParamSpec * pspec)
191 {
192   GstWaveScope *scope = GST_WAVE_SCOPE (object);
193
194   switch (prop_id) {
195     case PROP_STYLE:
196       scope->style = g_value_get_enum (value);
197       switch (scope->style) {
198         case STYLE_DOTS:
199           scope->process = render_dots;
200           break;
201         case STYLE_LINES:
202           scope->process = render_lines;
203           break;
204         case STYLE_COLOR_DOTS:
205           scope->process = render_color_dots;
206           break;
207         case STYLE_COLOR_LINES:
208           scope->process = render_color_lines;
209           break;
210       }
211       break;
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214       break;
215   }
216 }
217
218 static void
219 gst_wave_scope_get_property (GObject * object, guint prop_id,
220     GValue * value, GParamSpec * pspec)
221 {
222   GstWaveScope *scope = GST_WAVE_SCOPE (object);
223
224   switch (prop_id) {
225     case PROP_STYLE:
226       g_value_set_enum (value, scope->style);
227       break;
228     default:
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231   }
232 }
233
234 #include "gstdrawhelpers.h"
235
236 static void
237 render_dots (GstAudioVisualizer * base, guint32 * vdata, gint16 * adata,
238     guint num_samples)
239 {
240   gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
241   guint i, c, s, x, y, oy;
242   gfloat dx, dy;
243   guint w = base->width;
244   guint h = base->height;
245
246   /* draw dots */
247   dx = (gfloat) w / (gfloat) num_samples;
248   dy = h / 65536.0;
249   oy = h / 2;
250   for (c = 0; c < channels; c++) {
251     s = c;
252     for (i = 0; i < num_samples; i++) {
253       x = (guint) ((gfloat) i * dx);
254       y = (guint) (oy + (gfloat) adata[s] * dy);
255       s += channels;
256       draw_dot (vdata, x, y, w, 0x00FFFFFF);
257     }
258   }
259 }
260
261 static void
262 render_lines (GstAudioVisualizer * base, guint32 * vdata, gint16 * adata,
263     guint num_samples)
264 {
265   gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
266   guint i, c, s, x, y, oy;
267   gfloat dx, dy;
268   guint w = base->width;
269   guint h = base->height;
270   gint x2, y2;
271
272   /* draw lines */
273   dx = (gfloat) (w - 1) / (gfloat) num_samples;
274   dy = (h - 1) / 65536.0;
275   oy = (h - 1) / 2;
276   for (c = 0; c < channels; c++) {
277     s = c;
278     x2 = 0;
279     y2 = (guint) (oy + (gfloat) adata[s] * dy);
280     for (i = 1; i < num_samples; i++) {
281       x = (guint) ((gfloat) i * dx);
282       y = (guint) (oy + (gfloat) adata[s] * dy);
283       s += channels;
284       draw_line_aa (vdata, x2, x, y2, y, w, 0x00FFFFFF);
285       x2 = x;
286       y2 = y;
287     }
288   }
289 }
290
291 #define CUTOFF_1 0.15
292 #define CUTOFF_2 0.45
293 #define RESONANCE (1.0/0.5)
294
295 #define filter(in) G_STMT_START {                                              \
296   flt[2] = in - (flt[1] * RESONANCE) - flt[0];                                 \
297   flt[1] += (flt[2] * CUTOFF_1);                                               \
298   flt[0] += (flt[1] * CUTOFF_1);                                               \
299                                                                                \
300   flt[5] = (flt[1] + flt[2]) - (flt[4] * RESONANCE) - flt[3];                  \
301   flt[4] += (flt[5] * CUTOFF_2);                                               \
302   flt[3] += (flt[4] * CUTOFF_2);                                               \
303 } G_STMT_END
304
305 static void
306 render_color_dots (GstAudioVisualizer * base, guint32 * vdata,
307     gint16 * adata, guint num_samples)
308 {
309   GstWaveScope *scope = (GstWaveScope *) base;
310   gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
311   guint i, c, s, x, y, oy;
312   gfloat dx, dy;
313   guint w = base->width;
314   guint h = base->height, h1 = h - 2;
315   gdouble *flt = scope->flt;
316
317   /* draw dots */
318   dx = (gfloat) w / (gfloat) num_samples;
319   dy = h / 65536.0;
320   oy = h / 2;
321   for (c = 0; c < channels; c++) {
322     s = c;
323     for (i = 0; i < num_samples; i++) {
324       x = (guint) ((gfloat) i * dx);
325       filter ((gfloat) adata[s]);
326
327       y = (guint) (oy + flt[0] * dy);
328       y = CLAMP (y, 0, h1);
329       draw_dot_c (vdata, x, y, w, 0x00FF0000);
330
331       y = (guint) (oy + flt[3] * dy);
332       y = CLAMP (y, 0, h1);
333       draw_dot_c (vdata, x, y, w, 0x0000FF00);
334
335       y = (guint) (oy + (flt[4] + flt[5]) * dy);
336       y = CLAMP (y, 0, h1);
337       draw_dot_c (vdata, x, y, w, 0x000000FF);
338
339       s += channels;
340     }
341     flt += 6;
342   }
343 }
344
345 static void
346 render_color_lines (GstAudioVisualizer * base, guint32 * vdata,
347     gint16 * adata, guint num_samples)
348 {
349   GstWaveScope *scope = (GstWaveScope *) base;
350   gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
351   guint i, c, s, x, y, oy;
352   gfloat dx, dy;
353   guint w = base->width;
354   guint h = base->height, h1 = h - 2;
355   gdouble *flt = scope->flt;
356   gint x2, y2, y3, y4;
357
358   /* draw lines */
359   dx = (gfloat) (w - 1) / (gfloat) num_samples;
360   dy = (h - 1) / 65536.0;
361   oy = (h - 1) / 2;
362   for (c = 0; c < channels; c++) {
363     s = c;
364
365     /* do first pixels */
366     x2 = 0;
367     filter ((gfloat) adata[s]);
368
369     y = (guint) (oy + flt[0] * dy);
370     y2 = CLAMP (y, 0, h1);
371
372     y = (guint) (oy + flt[3] * dy);
373     y3 = CLAMP (y, 0, h1);
374
375     y = (guint) (oy + (flt[4] + flt[5]) * dy);
376     y4 = CLAMP (y, 0, h1);
377
378     for (i = 1; i < num_samples; i++) {
379       x = (guint) ((gfloat) i * dx);
380       filter ((gfloat) adata[s]);
381
382       y = (guint) (oy + flt[0] * dy);
383       y = CLAMP (y, 0, h1);
384       draw_line_aa (vdata, x2, x, y2, y, w, 0x00FF0000);
385       y2 = y;
386
387       y = (guint) (oy + flt[3] * dy);
388       y = CLAMP (y, 0, h1);
389       draw_line_aa (vdata, x2, x, y3, y, w, 0x0000FF00);
390       y3 = y;
391
392       y = (guint) (oy + (flt[4] + flt[5]) * dy);
393       y = CLAMP (y, 0, h1);
394       draw_line_aa (vdata, x2, x, y4, y, w, 0x000000FF);
395       y4 = y;
396
397       x2 = x;
398       s += channels;
399     }
400     flt += 6;
401   }
402 }
403
404 static gboolean
405 gst_wave_scope_render (GstAudioVisualizer * base, GstBuffer * audio,
406     GstBuffer * video)
407 {
408   GstWaveScope *scope = GST_WAVE_SCOPE (base);
409   GstMapInfo amap, vmap;
410   guint num_samples;
411   gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
412
413   gst_buffer_map (audio, &amap, GST_MAP_READ);
414   gst_buffer_map (video, &vmap, GST_MAP_WRITE);
415
416   num_samples = amap.size / (channels * sizeof (gint16));
417   scope->process (base, (guint32 *) vmap.data, (gint16 *) amap.data,
418       num_samples);
419
420   gst_buffer_unmap (video, &vmap);
421   gst_buffer_unmap (audio, &amap);
422
423   return TRUE;
424 }
425
426 gboolean
427 gst_wave_scope_plugin_init (GstPlugin * plugin)
428 {
429   GST_DEBUG_CATEGORY_INIT (wave_scope_debug, "wavescope", 0, "wavescope");
430
431   return gst_element_register (plugin, "wavescope", GST_RANK_NONE,
432       GST_TYPE_WAVE_SCOPE);
433 }