2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstsynaescope.c: frequency spectrum scope
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.
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.
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.
22 * SECTION:element-synaescope
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.
29 * ## Example launch line
31 * gst-launch-1.0 audiotestsrc ! audioconvert ! synaescope ! ximagesink
39 #include "gstsynaescope.h"
41 #if G_BYTE_ORDER == G_BIG_ENDIAN
42 #define RGB_ORDER "xRGB"
44 #define RGB_ORDER "BGRx"
47 static GstStaticPadTemplate gst_synae_scope_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (RGB_ORDER))
54 static GstStaticPadTemplate gst_synae_scope_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
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")
66 GST_DEBUG_CATEGORY_STATIC (synae_scope_debug);
67 #define GST_CAT_DEFAULT synae_scope_debug
69 static void gst_synae_scope_finalize (GObject * object);
71 static gboolean gst_synae_scope_setup (GstAudioVisualizer * scope);
72 static gboolean gst_synae_scope_render (GstAudioVisualizer * scope,
73 GstBuffer * audio, GstVideoFrame * video);
76 G_DEFINE_TYPE (GstSynaeScope, gst_synae_scope, GST_TYPE_AUDIO_VISUALIZER);
79 gst_synae_scope_class_init (GstSynaeScopeClass * g_class)
81 GObjectClass *gobject_class = (GObjectClass *) g_class;
82 GstElementClass *element_class = (GstElementClass *) g_class;
83 GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
85 gobject_class->finalize = gst_synae_scope_finalize;
87 gst_element_class_set_static_metadata (element_class, "Synaescope",
89 "Creates video visualizations of audio input, using stereo and pitch information",
90 "Stefan Kost <ensonic@users.sf.net>");
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);
97 scope_class->setup = GST_DEBUG_FUNCPTR (gst_synae_scope_setup);
98 scope_class->render = GST_DEBUG_FUNCPTR (gst_synae_scope_render);
102 gst_synae_scope_init (GstSynaeScope * scope)
104 guint32 *colors = scope->colors;
105 guint *shade = scope->shade;
108 #define BOUND(x) ((x) > 255 ? 255 : (x))
109 #define PEAKIFY(x) BOUND((x) - (x)*(255-(x))/255/2)
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);
116 colors[i] = (r << 16) | (g << 8) | b;
121 for (i = 0; i < 256; i++)
122 shade[i] = i * 200 >> 8;
126 gst_synae_scope_finalize (GObject * object)
128 GstSynaeScope *scope = GST_SYNAE_SCOPE (object);
130 if (scope->fft_ctx) {
131 gst_fft_s16_free (scope->fft_ctx);
132 scope->fft_ctx = NULL;
134 if (scope->freq_data_l) {
135 g_free (scope->freq_data_l);
136 scope->freq_data_l = NULL;
138 if (scope->freq_data_r) {
139 g_free (scope->freq_data_r);
140 scope->freq_data_r = NULL;
142 if (scope->adata_l) {
143 g_free (scope->adata_l);
144 scope->adata_l = NULL;
146 if (scope->adata_r) {
147 g_free (scope->adata_r);
148 scope->adata_r = NULL;
151 G_OBJECT_CLASS (gst_synae_scope_parent_class)->finalize (object);
155 gst_synae_scope_setup (GstAudioVisualizer * bscope)
157 GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
158 guint num_freq = GST_VIDEO_INFO_HEIGHT (&bscope->vinfo) + 1;
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);
167 /* FIXME: we could have horizontal or vertical layout */
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);
175 scope->adata_l = g_new (gint16, bscope->req_spf);
176 scope->adata_r = g_new (gint16, bscope->req_spf);
182 add_pixel (guint32 * _p, guint32 _c)
184 guint8 *p = (guint8 *) _p;
185 guint8 *c = (guint8 *) & _c;
187 if (p[0] < 255 - c[0])
191 if (p[1] < 255 - c[1])
195 if (p[2] < 255 - c[2])
199 if (p[3] < 255 - c[3])
206 gst_synae_scope_render (GstAudioVisualizer * bscope, GstBuffer * audio,
207 GstVideoFrame * video)
209 GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
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;
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;
224 guint ch = GST_AUDIO_INFO_CHANNELS (&bscope->ainfo);
229 gdouble fc, r, l, rr, ll;
230 gdouble frl, fil, frr, fir;
233 gst_buffer_map (audio, &amap, GST_MAP_READ);
235 vdata = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (video, 0);
236 adata = (gint16 *) amap.data;
238 num_samples = amap.size / (ch * sizeof (gint16));
241 for (i = 0, j = 0; i < num_samples; i++) {
242 adata_l[i] = adata[j++];
243 adata_r[i] = adata[j++];
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);
253 for (y = 0; y < 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;
260 ll = (frl + fil) * (frl + fil) + (frr - fir) * (frr - fir);
262 rr = (frl - fil) * (frl - fil) + (frr + fir) * (frr + fir);
264 /* out-of-phase'ness for this frequency component */
266 ((frl + fil) * (frl - fil) + (frr + fir) * (frr - fir)) /
270 x = (guint) (r * w / fc);
271 /* the brightness scaling factor was picked by experimenting */
274 br1 = br * (clarity + 128) >> 8;
275 br2 = br * (128 - clarity) >> 8;
276 br1 = CLAMP (br1, 0, 255);
277 br2 = CLAMP (br2, 0, 255);
279 GST_DEBUG ("y %3d fc %10.6f clarity %d br %d br1 %d br2 %d", y, fc, clarity,
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);
295 for (i = 1; br1 || br2; i++, br1 = shade[br1], br2 = shade[br2]) {
296 c = colors[(br1 >> 4) | (br2 & 0xf0)];
298 add_pixel (&vdata[off - i], c);
300 add_pixel (&vdata[off + i], c);
302 add_pixel (&vdata[off - i * w], c);
304 add_pixel (&vdata[off + i * w], c);
308 gst_buffer_unmap (audio, &amap);
314 gst_synae_scope_plugin_init (GstPlugin * plugin)
316 GST_DEBUG_CATEGORY_INIT (synae_scope_debug, "synaescope", 0, "synaescope");
318 return gst_element_register (plugin, "synaescope", GST_RANK_NONE,
319 GST_TYPE_SYNAE_SCOPE);