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 (GstWaveScope, gst_wave_scope, GST_TYPE_AUDIO_VISUALIZER);
126
127 static void
gst_wave_scope_class_init(GstWaveScopeClass * g_class)128 gst_wave_scope_class_init (GstWaveScopeClass * g_class)
129 {
130 GObjectClass *gobject_class = (GObjectClass *) g_class;
131 GstElementClass *gstelement_class = (GstElementClass *) g_class;
132 GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
133
134 gobject_class->set_property = gst_wave_scope_set_property;
135 gobject_class->get_property = gst_wave_scope_get_property;
136 gobject_class->finalize = gst_wave_scope_finalize;
137
138 scope_class->setup = GST_DEBUG_FUNCPTR (gst_wave_scope_setup);
139 scope_class->render = GST_DEBUG_FUNCPTR (gst_wave_scope_render);
140
141 g_object_class_install_property (gobject_class, PROP_STYLE,
142 g_param_spec_enum ("style", "drawing style",
143 "Drawing styles for the wave form display.",
144 GST_TYPE_WAVE_SCOPE_STYLE, STYLE_DOTS,
145 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
146
147 gst_element_class_set_static_metadata (gstelement_class,
148 "Waveform oscilloscope", "Visualization", "Simple waveform oscilloscope",
149 "Stefan Kost <ensonic@users.sf.net>");
150
151 gst_element_class_add_static_pad_template (gstelement_class,
152 &gst_wave_scope_src_template);
153 gst_element_class_add_static_pad_template (gstelement_class,
154 &gst_wave_scope_sink_template);
155 }
156
157 static void
gst_wave_scope_init(GstWaveScope * scope)158 gst_wave_scope_init (GstWaveScope * scope)
159 {
160 /* do nothing */
161 }
162
163 static void
gst_wave_scope_finalize(GObject * object)164 gst_wave_scope_finalize (GObject * object)
165 {
166 GstWaveScope *scope = GST_WAVE_SCOPE (object);
167
168 if (scope->flt) {
169 g_free (scope->flt);
170 scope->flt = NULL;
171 }
172
173 G_OBJECT_CLASS (parent_class)->finalize (object);
174 }
175
176 static gboolean
gst_wave_scope_setup(GstAudioVisualizer * bscope)177 gst_wave_scope_setup (GstAudioVisualizer * bscope)
178 {
179 GstWaveScope *scope = GST_WAVE_SCOPE (bscope);
180
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
gst_wave_scope_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)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
gst_wave_scope_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)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
render_dots(GstAudioVisualizer * base,guint32 * vdata,gint16 * adata,guint num_samples)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 = GST_VIDEO_INFO_WIDTH (&base->vinfo);
244 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo);
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
render_lines(GstAudioVisualizer * base,guint32 * vdata,gint16 * adata,guint num_samples)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 = GST_VIDEO_INFO_WIDTH (&base->vinfo);
269 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo);
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
render_color_dots(GstAudioVisualizer * base,guint32 * vdata,gint16 * adata,guint num_samples)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 = GST_VIDEO_INFO_WIDTH (&base->vinfo);
314 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo), 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 = MIN (y, h1);
329 draw_dot_c (vdata, x, y, w, 0x00FF0000);
330
331 y = (guint) (oy + flt[3] * dy);
332 y = MIN (y, h1);
333 draw_dot_c (vdata, x, y, w, 0x0000FF00);
334
335 y = (guint) (oy + (flt[4] + flt[5]) * dy);
336 y = MIN (y, h1);
337 draw_dot_c (vdata, x, y, w, 0x000000FF);
338
339 s += channels;
340 }
341 flt += 6;
342 }
343 }
344
345 static void
render_color_lines(GstAudioVisualizer * base,guint32 * vdata,gint16 * adata,guint num_samples)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 = GST_VIDEO_INFO_WIDTH (&base->vinfo);
354 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo), 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 = MIN (y, h1);
371
372 y = (guint) (oy + flt[3] * dy);
373 y3 = MIN (y, h1);
374
375 y = (guint) (oy + (flt[4] + flt[5]) * dy);
376 y4 = MIN (y, 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 = MIN (y, 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 = MIN (y, 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 = MIN (y, 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
gst_wave_scope_render(GstAudioVisualizer * base,GstBuffer * audio,GstVideoFrame * video)405 gst_wave_scope_render (GstAudioVisualizer * base, GstBuffer * audio,
406 GstVideoFrame * video)
407 {
408 GstWaveScope *scope = GST_WAVE_SCOPE (base);
409 GstMapInfo amap;
410 guint num_samples;
411 gint channels = GST_AUDIO_INFO_CHANNELS (&base->ainfo);
412
413 gst_buffer_map (audio, &amap, GST_MAP_READ);
414
415 num_samples = amap.size / (channels * sizeof (gint16));
416 scope->process (base, (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (video, 0),
417 (gint16 *) amap.data, num_samples);
418
419 gst_buffer_unmap (audio, &amap);
420
421 return TRUE;
422 }
423
424 gboolean
gst_wave_scope_plugin_init(GstPlugin * plugin)425 gst_wave_scope_plugin_init (GstPlugin * plugin)
426 {
427 GST_DEBUG_CATEGORY_INIT (wave_scope_debug, "wavescope", 0, "wavescope");
428
429 return gst_element_register (plugin, "wavescope", GST_RANK_NONE,
430 GST_TYPE_WAVE_SCOPE);
431 }
432