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