Debian Codegraph @

gst/audiovisualizers/gstspacescope.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) <2011> Stefan Kost <[email protected]>
3 *
4 * gstspacescope.c: simple stereo visualizer
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-spacescope
23 * @title: spacescope
24 * @see_also: goom
25 *
26 * Spacescope is a simple audio visualisation element. It maps the left and
27 * right channel to x and y coordinates.
28 *
29 * ## Example launch line
30 * |[
31 * gst-launch-1.0 audiotestsrc ! audioconvert ! spacescope ! ximagesink
32 * ]|
33 *
34 */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <stdlib.h>
40 #include "gstspacescope.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_space_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_space_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 (space_scope_debug);
68 #define GST_CAT_DEFAULT space_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_SPACE_SCOPE_STYLE (gst_space_scope_style_get_type ())
86 static GType
87 gst_space_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 ("GstSpaceScopeStyle", values);
101 }
102 return gtype;
103 }
104
105 static void gst_space_scope_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_space_scope_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
109
110 static void render_dots (GstAudioVisualizer * base, guint32 * vdata,
111 gint16 * adata, guint num_samples);
112 static void render_lines (GstAudioVisualizer * base, guint32 * vdata,
113 gint16 * adata, guint num_samples);
114 static void render_color_dots (GstAudioVisualizer * base, guint32 * vdata,
115 gint16 * adata, guint num_samples);
116 static void render_color_lines (GstAudioVisualizer * base, guint32 * vdata,
117 gint16 * adata, guint num_samples);
118
119 static gboolean gst_space_scope_render (GstAudioVisualizer * scope,
120 GstBuffer * audio, GstVideoFrame * video);
121
122
123 G_DEFINE_TYPE_WITH_CODE (GstSpaceScope, gst_space_scope,
124 GST_TYPE_AUDIO_VISUALIZER, GST_DEBUG_CATEGORY_INIT (space_scope_debug,
125 "spacescope", 0, "spacescope"););
126 GST_ELEMENT_REGISTER_DEFINE (spacescope, "spacescope", GST_RANK_NONE,
127 GST_TYPE_SPACE_SCOPE);
128
129 static void
130 gst_space_scope_class_init (GstSpaceScopeClass * g_class)
131 {
132 GObjectClass *gobject_class = (GObjectClass *) g_class;
133 GstElementClass *element_class = (GstElementClass *) g_class;
134 GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
135
136 gst_element_class_set_static_metadata (element_class, "Stereo visualizer",
137 "Visualization",
138 "Simple stereo visualizer", "Stefan Kost <[email protected]>");
139
140 gst_element_class_add_static_pad_template (element_class,
141 &gst_space_scope_src_template);
142 gst_element_class_add_static_pad_template (element_class,
143 &gst_space_scope_sink_template);
144
145 gobject_class->set_property = gst_space_scope_set_property;
146 gobject_class->get_property = gst_space_scope_get_property;
147
148 scope_class->render = GST_DEBUG_FUNCPTR (gst_space_scope_render);
149
150 g_object_class_install_property (gobject_class, PROP_STYLE,
151 g_param_spec_enum ("style", "drawing style",
152 "Drawing styles for the space scope display.",
153 GST_TYPE_SPACE_SCOPE_STYLE, STYLE_DOTS,
154 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155
156 gst_type_mark_as_plugin_api (GST_TYPE_SPACE_SCOPE_STYLE, 0);
157 }
158
159 static void
160 gst_space_scope_init (GstSpaceScope * scope)
161 {
162 /* do nothing */
163 }
164
165 static void
166 gst_space_scope_set_property (GObject * object, guint prop_id,
167 const GValue * value, GParamSpec * pspec)
168 {
169 GstSpaceScope *scope = GST_SPACE_SCOPE (object);
170
171 switch (prop_id) {
172 case PROP_STYLE:
173 scope->style = g_value_get_enum (value);
174 switch (scope->style) {
175 case STYLE_DOTS:
176 scope->process = render_dots;
177 break;
178 case STYLE_LINES:
179 scope->process = render_lines;
180 break;
181 case STYLE_COLOR_DOTS:
182 scope->process = render_color_dots;
183 break;
184 case STYLE_COLOR_LINES:
185 scope->process = render_color_lines;
186 break;
187 }
188 break;
189 default:
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 break;
192 }
193 }
194
195 static void
196 gst_space_scope_get_property (GObject * object, guint prop_id,
197 GValue * value, GParamSpec * pspec)
198 {
199 GstSpaceScope *scope = GST_SPACE_SCOPE (object);
200
201 switch (prop_id) {
202 case PROP_STYLE:
203 g_value_set_enum (value, scope->style);
204 break;
205 default:
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 break;
208 }
209 }
210
211 #include "gstdrawhelpers.h"
212
213 static void
214 render_dots (GstAudioVisualizer * base, guint32 * vdata, gint16 * adata,
215 guint num_samples)
216 {
217 guint i, s, x, y, ox, oy;
218 gfloat dx, dy;
219 guint w = GST_VIDEO_INFO_WIDTH (&base->vinfo);
220 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo);
221
222 /* draw dots 1st channel x, 2nd channel y */
223 dx = w / 65536.0;
224 ox = w / 2;
225 dy = h / 65536.0;
226 oy = h / 2;
227 s = 0;
228 for (i = 0; i < num_samples; i++) {
229 x = (guint) (ox + (gfloat) adata[s++] * dx);
230 y = (guint) (oy + (gfloat) adata[s++] * dy);
231 draw_dot (vdata, x, y, w, 0x00FFFFFF);
232 }
233 }
234
235 static void
236 render_lines (GstAudioVisualizer * base, guint32 * vdata, gint16 * adata,
237 guint num_samples)
238 {
239 guint i, s, x, y, ox, oy;
240 gfloat dx, dy;
241 guint w = GST_VIDEO_INFO_WIDTH (&base->vinfo);
242 guint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo);
243 gint x2, y2;
244
245 /* draw lines 1st channel x, 2nd channel y */
246 dx = (w - 1) / 65536.0;
247 ox = (w - 1) / 2;
248 dy = (h - 1) / 65536.0;
249 oy = (h - 1) / 2;
250 s = 0;
251 x2 = (guint) (ox + (gfloat) adata[s++] * dx);
252 y2 = (guint) (oy + (gfloat) adata[s++] * dy);
253 for (i = 1; i < num_samples; i++) {
254 x = (guint) (ox + (gfloat) adata[s++] * dx);
255 y = (guint) (oy + (gfloat) adata[s++] * dy);
256 draw_line_aa (vdata, x2, x, y2, y, w, 0x00FFFFFF);
257 x2 = x;
258 y2 = y;
259 }
260 }
261
262 #define CUTOFF_1 0.15
263 #define CUTOFF_2 0.45
264 #define RESONANCE (1.0/0.5)
265
266 #define filter(il, ir) G_STMT_START { \
267 f1l_h = il - (f1l_m * RESONANCE) - f1l_l; \
268 f1l_m += (f1l_h * CUTOFF_1); \
269 f1l_l += (f1l_m * CUTOFF_1); \
270 \
271 f2l_h = (f1l_m + f1l_h) - (f2l_m * RESONANCE) - f2l_l; \
272 f2l_m += (f2l_h * CUTOFF_2); \
273 f2l_l += (f2l_m * CUTOFF_2); \
274 \
275 f1r_h = ir - (f1r_m * RESONANCE) - f1r_l; \
276 f1r_m += (f1r_h * CUTOFF_1); \
277 f1r_l += (f1r_m * CUTOFF_1); \
278 \
279 f2r_h = (f1r_m + f1r_h) - (f2r_m * RESONANCE) - f2r_l; \
280 f2r_m += (f2r_h * CUTOFF_2); \
281 f2r_l += (f2r_m * CUTOFF_2); \
282 } G_STMT_END
283
284 static void
285 render_color_dots (GstAudioVisualizer * base, guint32 * vdata,
286 gint16 * adata, guint num_samples)
287 {
288 GstSpaceScope *scope = (GstSpaceScope *) base;
289 guint i, s;
290 gint x, y, ox, oy;
291 gfloat dx, dy;
292 gint w = GST_VIDEO_INFO_WIDTH (&base->vinfo), w1 = w - 2;
293 gint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo), h1 = h - 2;
294 gdouble il, ir;
295 gdouble f1l_l = scope->f1l_l, f1l_m = scope->f1l_m, f1l_h = scope->f1l_h;
296 gdouble f1r_l = scope->f1r_l, f1r_m = scope->f1r_m, f1r_h = scope->f1r_h;
297 gdouble f2l_l = scope->f2l_l, f2l_m = scope->f2l_m, f2l_h = scope->f2l_h;
298 gdouble f2r_l = scope->f2r_l, f2r_m = scope->f2r_m, f2r_h = scope->f2r_h;
299
300 /* draw dots 1st channel x, 2nd channel y */
301 ox = w / 2;
302 oy = h / 2;
303 dx = w / 65536.0;
304 dy = h / 65536.0;
305 s = 0;
306 for (i = 0; i < num_samples; i++) {
307 il = (gdouble) adata[s++];
308 ir = (gdouble) adata[s++];
309
310 filter (il, ir);
311
312 x = (gint) (ox + f1l_l * dx);
313 y = (gint) (oy + f1r_l * dy);
314 x = CLAMP (x, 0, w1);
315 y = CLAMP (y, 0, h1);
316 draw_dot_c (vdata, x, y, w, 0x00FF0000);
317
318 x = (gint) (ox + f2l_l * dx);
319 y = (gint) (oy + f2r_l * dy);
320 x = CLAMP (x, 0, w1);
321 y = CLAMP (y, 0, h1);
322 draw_dot_c (vdata, x, y, w, 0x0000FF00);
323
324 x = (gint) (ox + (f2l_m + f2l_h) * dx);
325 y = (gint) (oy + (f2r_m + f2r_h) * dy);
326 x = CLAMP (x, 0, w1);
327 y = CLAMP (y, 0, h1);
328 draw_dot_c (vdata, x, y, w, 0x000000FF);
329 }
330
331 scope->f1l_l = f1l_l;
332 scope->f1l_m = f1l_m;
333 scope->f1l_h = f1l_h;
334 scope->f1r_l = f1r_l;
335 scope->f1r_m = f1r_m;
336 scope->f1r_h = f1r_h;
337 scope->f2l_l = f2l_l;
338 scope->f2l_m = f2l_m;
339 scope->f2l_h = f2l_h;
340 scope->f2r_l = f2r_l;
341 scope->f2r_m = f2r_m;
342 scope->f2r_h = f2r_h;
343 }
344
345 static void
346 render_color_lines (GstAudioVisualizer * base, guint32 * vdata,
347 gint16 * adata, guint num_samples)
348 {
349 GstSpaceScope *scope = (GstSpaceScope *) base;
350 guint i, s;
351 gint x, y, ox, oy;
352 gfloat dx, dy;
353 gint w = GST_VIDEO_INFO_WIDTH (&base->vinfo), w1 = w - 2;
354 gint h = GST_VIDEO_INFO_HEIGHT (&base->vinfo), h1 = h - 2;
355 gdouble il, ir;
356 gdouble f1l_l = scope->f1l_l, f1l_m = scope->f1l_m, f1l_h = scope->f1l_h;
357 gdouble f1r_l = scope->f1r_l, f1r_m = scope->f1r_m, f1r_h = scope->f1r_h;
358 gdouble f2l_l = scope->f2l_l, f2l_m = scope->f2l_m, f2l_h = scope->f2l_h;
359 gdouble f2r_l = scope->f2r_l, f2r_m = scope->f2r_m, f2r_h = scope->f2r_h;
360 gint x2, y2, x3, y3, x4, y4;
361
362 /* draw lines 1st channel x, 2nd channel y */
363 ox = w / 2;
364 oy = h / 2;
365 dx = w / 65536.0;
366 dy = h / 65536.0;
367 s = 0;
368
369 /* do first pixels */
370 il = (gdouble) adata[s++];
371 ir = (gdouble) adata[s++];
372
373 filter (il, ir);
374
375 x = (gint) (ox + f1l_l * dx);
376 y = (gint) (oy + f1r_l * dy);
377 x2 = CLAMP (x, 0, w1);
378 y2 = CLAMP (y, 0, h1);
379
380 x = (gint) (ox + f2l_l * dx);
381 y = (gint) (oy + f2r_l * dy);
382 x3 = CLAMP (x, 0, w1);
383 y3 = CLAMP (y, 0, h1);
384
385 x = (gint) (ox + (f2l_m + f2l_h) * dx);
386 y = (gint) (oy + (f2r_m + f2r_h) * dy);
387 x4 = CLAMP (x, 0, w1);
388 y4 = CLAMP (y, 0, h1);
389
390 for (i = 1; i < num_samples; i++) {
391 il = (gdouble) adata[s++];
392 ir = (gdouble) adata[s++];
393
394 filter (il, ir);
395
396 x = (gint) (ox + f1l_l * dx);
397 y = (gint) (oy + f1r_l * dy);
398 x = CLAMP (x, 0, w1);
399 y = CLAMP (y, 0, h1);
400 draw_line_aa (vdata, x2, x, y2, y, w, 0x00FF0000);
401 x2 = x;
402 y2 = y;
403
404 x = (gint) (ox + f2l_l * dx);
405 y = (gint) (oy + f2r_l * dy);
406 x = CLAMP (x, 0, w1);
407 y = CLAMP (y, 0, h1);
408 draw_line_aa (vdata, x3, x, y3, y, w, 0x0000FF00);
409 x3 = x;
410 y3 = y;
411
412 x = (gint) (ox + (f2l_m + f2l_h) * dx);
413 y = (gint) (oy + (f2r_m + f2r_h) * dy);
414 x = CLAMP (x, 0, w1);
415 y = CLAMP (y, 0, h1);
416 draw_line_aa (vdata, x4, x, y4, y, w, 0x000000FF);
417 x4 = x;
418 y4 = y;
419 }
420
421 scope->f1l_l = f1l_l;
422 scope->f1l_m = f1l_m;
423 scope->f1l_h = f1l_h;
424 scope->f1r_l = f1r_l;
425 scope->f1r_m = f1r_m;
426 scope->f1r_h = f1r_h;
427 scope->f2l_l = f2l_l;
428 scope->f2l_m = f2l_m;
429 scope->f2l_h = f2l_h;
430 scope->f2r_l = f2r_l;
431 scope->f2r_m = f2r_m;
432 scope->f2r_h = f2r_h;
433 }
434
435 static gboolean
436 gst_space_scope_render (GstAudioVisualizer * base, GstBuffer * audio,
437 GstVideoFrame * video)
438 {
439 GstSpaceScope *scope = GST_SPACE_SCOPE (base);
440 GstMapInfo amap;
441 guint num_samples;
442
443 gst_buffer_map (audio, &amap, GST_MAP_READ);
444
445 num_samples =
446 amap.size / (GST_AUDIO_INFO_CHANNELS (&base->ainfo) * sizeof (gint16));
447 scope->process (base, (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (video, 0),
448 (gint16 *) amap.data, num_samples);
449 gst_buffer_unmap (audio, &amap);
450 return TRUE;
451 }