1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstsynaescope.c: frequency spectrum scope
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-synaescope
23  * @title: synaescope
24  * @see_also: goom
25  *
26  * Synaescope is an audio visualisation element. It analyzes frequencies and
27  * out-of phase properties of audio and draws this as clouds of stars.
28  *
29  * ## Example launch line
30  * |[
31  * gst-launch-1.0 audiotestsrc ! audioconvert ! synaescope ! ximagesink
32  * ]|
33  *
34  */
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include "gstsynaescope.h"
40 
41 #if G_BYTE_ORDER == G_BIG_ENDIAN
42 #define RGB_ORDER "xRGB"
43 #else
44 #define RGB_ORDER "BGRx"
45 #endif
46 
47 static GstStaticPadTemplate gst_synae_scope_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (RGB_ORDER))
52     );
53 
54 static GstStaticPadTemplate gst_synae_scope_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("audio/x-raw, "
59         "format = (string) " GST_AUDIO_NE (S16) ", "
60         "layout = (string) interleaved, "
61         "rate = (int) [ 8000, 96000 ], "
62         "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
63     );
64 
65 
66 GST_DEBUG_CATEGORY_STATIC (synae_scope_debug);
67 #define GST_CAT_DEFAULT synae_scope_debug
68 
69 static void gst_synae_scope_finalize (GObject * object);
70 
71 static gboolean gst_synae_scope_setup (GstAudioVisualizer * scope);
72 static gboolean gst_synae_scope_render (GstAudioVisualizer * scope,
73     GstBuffer * audio, GstVideoFrame * video);
74 
75 
76 G_DEFINE_TYPE (GstSynaeScope, gst_synae_scope, GST_TYPE_AUDIO_VISUALIZER);
77 
78 static void
gst_synae_scope_class_init(GstSynaeScopeClass * g_class)79 gst_synae_scope_class_init (GstSynaeScopeClass * g_class)
80 {
81   GObjectClass *gobject_class = (GObjectClass *) g_class;
82   GstElementClass *element_class = (GstElementClass *) g_class;
83   GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
84 
85   gobject_class->finalize = gst_synae_scope_finalize;
86 
87   gst_element_class_set_static_metadata (element_class, "Synaescope",
88       "Visualization",
89       "Creates video visualizations of audio input, using stereo and pitch information",
90       "Stefan Kost <ensonic@users.sf.net>");
91 
92   gst_element_class_add_static_pad_template (element_class,
93       &gst_synae_scope_src_template);
94   gst_element_class_add_static_pad_template (element_class,
95       &gst_synae_scope_sink_template);
96 
97   scope_class->setup = GST_DEBUG_FUNCPTR (gst_synae_scope_setup);
98   scope_class->render = GST_DEBUG_FUNCPTR (gst_synae_scope_render);
99 }
100 
101 static void
gst_synae_scope_init(GstSynaeScope * scope)102 gst_synae_scope_init (GstSynaeScope * scope)
103 {
104   guint32 *colors = scope->colors;
105   guint *shade = scope->shade;
106   guint i, r, g, b;
107 
108 #define BOUND(x) ((x) > 255 ? 255 : (x))
109 #define PEAKIFY(x) BOUND((x) - (x)*(255-(x))/255/2)
110 
111   for (i = 0; i < 256; i++) {
112     r = PEAKIFY ((i & 15 * 16));
113     g = PEAKIFY ((i & 15) * 16 + (i & 15 * 16) / 4);
114     b = PEAKIFY ((i & 15) * 16);
115 
116     colors[i] = (r << 16) | (g << 8) | b;
117   }
118 #undef BOUND
119 #undef PEAKIFY
120 
121   for (i = 0; i < 256; i++)
122     shade[i] = i * 200 >> 8;
123 }
124 
125 static void
gst_synae_scope_finalize(GObject * object)126 gst_synae_scope_finalize (GObject * object)
127 {
128   GstSynaeScope *scope = GST_SYNAE_SCOPE (object);
129 
130   if (scope->fft_ctx) {
131     gst_fft_s16_free (scope->fft_ctx);
132     scope->fft_ctx = NULL;
133   }
134   if (scope->freq_data_l) {
135     g_free (scope->freq_data_l);
136     scope->freq_data_l = NULL;
137   }
138   if (scope->freq_data_r) {
139     g_free (scope->freq_data_r);
140     scope->freq_data_r = NULL;
141   }
142   if (scope->adata_l) {
143     g_free (scope->adata_l);
144     scope->adata_l = NULL;
145   }
146   if (scope->adata_r) {
147     g_free (scope->adata_r);
148     scope->adata_r = NULL;
149   }
150 
151   G_OBJECT_CLASS (gst_synae_scope_parent_class)->finalize (object);
152 }
153 
154 static gboolean
gst_synae_scope_setup(GstAudioVisualizer * bscope)155 gst_synae_scope_setup (GstAudioVisualizer * bscope)
156 {
157   GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
158   guint num_freq = GST_VIDEO_INFO_HEIGHT (&bscope->vinfo) + 1;
159 
160   if (scope->fft_ctx)
161     gst_fft_s16_free (scope->fft_ctx);
162   g_free (scope->freq_data_l);
163   g_free (scope->freq_data_r);
164   g_free (scope->adata_l);
165   g_free (scope->adata_r);
166 
167   /* FIXME: we could have horizontal or vertical layout */
168 
169   /* we'd need this amount of samples per render() call */
170   bscope->req_spf = num_freq * 2 - 2;
171   scope->fft_ctx = gst_fft_s16_new (bscope->req_spf, FALSE);
172   scope->freq_data_l = g_new (GstFFTS16Complex, num_freq);
173   scope->freq_data_r = g_new (GstFFTS16Complex, num_freq);
174 
175   scope->adata_l = g_new (gint16, bscope->req_spf);
176   scope->adata_r = g_new (gint16, bscope->req_spf);
177 
178   return TRUE;
179 }
180 
181 static inline void
add_pixel(guint32 * _p,guint32 _c)182 add_pixel (guint32 * _p, guint32 _c)
183 {
184   guint8 *p = (guint8 *) _p;
185   guint8 *c = (guint8 *) & _c;
186 
187   if (p[0] < 255 - c[0])
188     p[0] += c[0];
189   else
190     p[0] = 255;
191   if (p[1] < 255 - c[1])
192     p[1] += c[1];
193   else
194     p[1] = 255;
195   if (p[2] < 255 - c[2])
196     p[2] += c[2];
197   else
198     p[2] = 255;
199   if (p[3] < 255 - c[3])
200     p[3] += c[3];
201   else
202     p[3] = 255;
203 }
204 
205 static gboolean
gst_synae_scope_render(GstAudioVisualizer * bscope,GstBuffer * audio,GstVideoFrame * video)206 gst_synae_scope_render (GstAudioVisualizer * bscope, GstBuffer * audio,
207     GstVideoFrame * video)
208 {
209   GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
210   GstMapInfo amap;
211   guint32 *vdata;
212   gint16 *adata;
213   gint16 *adata_l = scope->adata_l;
214   gint16 *adata_r = scope->adata_r;
215   GstFFTS16Complex *fdata_l = scope->freq_data_l;
216   GstFFTS16Complex *fdata_r = scope->freq_data_r;
217   gint x, y;
218   guint off;
219   guint w = GST_VIDEO_INFO_WIDTH (&bscope->vinfo);
220   guint h = GST_VIDEO_INFO_HEIGHT (&bscope->vinfo);
221   guint32 *colors = scope->colors, c;
222   guint *shade = scope->shade;
223   //guint w2 = w /2;
224   guint ch = GST_AUDIO_INFO_CHANNELS (&bscope->ainfo);
225   guint num_samples;
226   gint i, j, b;
227   gint br, br1, br2;
228   gint clarity;
229   gdouble fc, r, l, rr, ll;
230   gdouble frl, fil, frr, fir;
231   const guint sl = 30;
232 
233   gst_buffer_map (audio, &amap, GST_MAP_READ);
234 
235   vdata = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (video, 0);
236   adata = (gint16 *) amap.data;
237 
238   num_samples = amap.size / (ch * sizeof (gint16));
239 
240   /* deinterleave */
241   for (i = 0, j = 0; i < num_samples; i++) {
242     adata_l[i] = adata[j++];
243     adata_r[i] = adata[j++];
244   }
245 
246   /* run fft */
247   /*gst_fft_s16_window (scope->fft_ctx, adata_l, GST_FFT_WINDOW_HAMMING); */
248   gst_fft_s16_fft (scope->fft_ctx, adata_l, fdata_l);
249   /*gst_fft_s16_window (scope->fft_ctx, adata_r, GST_FFT_WINDOW_HAMMING); */
250   gst_fft_s16_fft (scope->fft_ctx, adata_r, fdata_r);
251 
252   /* draw stars */
253   for (y = 0; y < h; y++) {
254     b = h - y;
255     frl = (gdouble) fdata_l[b].r;
256     fil = (gdouble) fdata_l[b].i;
257     frr = (gdouble) fdata_r[b].r;
258     fir = (gdouble) fdata_r[b].i;
259 
260     ll = (frl + fil) * (frl + fil) + (frr - fir) * (frr - fir);
261     l = sqrt (ll);
262     rr = (frl - fil) * (frl - fil) + (frr + fir) * (frr + fir);
263     r = sqrt (rr);
264     /* out-of-phase'ness for this frequency component */
265     clarity = (gint) (
266         ((frl + fil) * (frl - fil) + (frr + fir) * (frr - fir)) /
267         (ll + rr) * 256);
268     fc = r + l;
269 
270     x = (guint) (r * w / fc);
271     /* the brighness scaling factor was picked by experimenting */
272     br = b * fc * 0.01;
273 
274     br1 = br * (clarity + 128) >> 8;
275     br2 = br * (128 - clarity) >> 8;
276     br1 = CLAMP (br1, 0, 255);
277     br2 = CLAMP (br2, 0, 255);
278 
279     GST_DEBUG ("y %3d fc %10.6f clarity %d br %d br1 %d br2 %d", y, fc, clarity,
280         br, br1, br2);
281 
282     /* draw a star */
283     off = (y * w) + x;
284     c = colors[(br1 >> 4) | (br2 & 0xf0)];
285     add_pixel (&vdata[off], c);
286     if ((x > (sl - 1)) && (x < (w - sl)) && (y > (sl - 1)) && (y < (h - sl))) {
287       for (i = 1; br1 || br2; i++, br1 = shade[br1], br2 = shade[br2]) {
288         c = colors[(br1 >> 4) + (br2 & 0xf0)];
289         add_pixel (&vdata[off - i], c);
290         add_pixel (&vdata[off + i], c);
291         add_pixel (&vdata[off - i * w], c);
292         add_pixel (&vdata[off + i * w], c);
293       }
294     } else {
295       for (i = 1; br1 || br2; i++, br1 = shade[br1], br2 = shade[br2]) {
296         c = colors[(br1 >> 4) | (br2 & 0xf0)];
297         if (x - i > 0)
298           add_pixel (&vdata[off - i], c);
299         if (x + i < (w - 1))
300           add_pixel (&vdata[off + i], c);
301         if (y - i > 0)
302           add_pixel (&vdata[off - i * w], c);
303         if (y + i < (h - 1))
304           add_pixel (&vdata[off + i * w], c);
305       }
306     }
307   }
308   gst_buffer_unmap (audio, &amap);
309 
310   return TRUE;
311 }
312 
313 gboolean
gst_synae_scope_plugin_init(GstPlugin * plugin)314 gst_synae_scope_plugin_init (GstPlugin * plugin)
315 {
316   GST_DEBUG_CATEGORY_INIT (synae_scope_debug, "synaescope", 0, "synaescope");
317 
318   return gst_element_register (plugin, "synaescope", GST_RANK_NONE,
319       GST_TYPE_SYNAE_SCOPE);
320 }
321