2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstsynaescope.c: frequency spectrum scope
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * SECTION:element-synaescope
24 * Synaescope is an audio visualisation element. It analyzes frequencies and
25 * out-of phase properties of audio and draws this as clouds of stars.
28 * <title>Example launch line</title>
30 * gst-launch audiotestsrc ! audioconvert ! synaescope ! ximagesink
38 #include "gstsynaescope.h"
40 static GstStaticPadTemplate gst_synae_scope_src_template =
41 GST_STATIC_PAD_TEMPLATE ("src",
44 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
47 static GstStaticPadTemplate gst_synae_scope_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
55 GST_DEBUG_CATEGORY_STATIC (synae_scope_debug);
56 #define GST_CAT_DEFAULT synae_scope_debug
58 static void gst_synae_scope_finalize (GObject * object);
60 static gboolean gst_synae_scope_setup (GstBaseAudioVisualizer * scope);
61 static gboolean gst_synae_scope_render (GstBaseAudioVisualizer * scope,
62 GstBuffer * audio, GstBuffer * video);
65 GST_BOILERPLATE (GstSynaeScope, gst_synae_scope, GstBaseAudioVisualizer,
66 GST_TYPE_BASE_AUDIO_VISUALIZER);
69 gst_synae_scope_base_init (gpointer g_class)
71 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
73 gst_element_class_set_details_simple (element_class, "Synaescope",
75 "Creates video visualizations of audio input, using stereo and pitch information",
76 "Stefan Kost <ensonic@users.sf.net>");
78 gst_element_class_add_static_pad_template (element_class,
79 &gst_synae_scope_src_template);
80 gst_element_class_add_static_pad_template (element_class,
81 &gst_synae_scope_sink_template);
85 gst_synae_scope_class_init (GstSynaeScopeClass * g_class)
87 GObjectClass *gobject_class = (GObjectClass *) g_class;
88 GstBaseAudioVisualizerClass *scope_class =
89 (GstBaseAudioVisualizerClass *) g_class;
91 gobject_class->finalize = gst_synae_scope_finalize;
93 scope_class->setup = GST_DEBUG_FUNCPTR (gst_synae_scope_setup);
94 scope_class->render = GST_DEBUG_FUNCPTR (gst_synae_scope_render);
98 gst_synae_scope_init (GstSynaeScope * scope, GstSynaeScopeClass * g_class)
100 guint32 *colors = scope->colors;
101 guint *shade = scope->shade;
104 #define BOUND(x) ((x) > 255 ? 255 : (x))
105 #define PEAKIFY(x) BOUND((x) - (x)*(255-(x))/255/2)
107 for (i = 0; i < 256; i++) {
108 r = PEAKIFY ((i & 15 * 16));
109 g = PEAKIFY ((i & 15) * 16 + (i & 15 * 16) / 4);
110 b = PEAKIFY ((i & 15) * 16);
112 colors[i] = (r << 16) | (g << 8) | b;
117 for (i = 0; i < 256; i++)
118 shade[i] = i * 200 >> 8;
122 gst_synae_scope_finalize (GObject * object)
124 GstSynaeScope *scope = GST_SYNAE_SCOPE (object);
126 if (scope->fft_ctx) {
127 gst_fft_s16_free (scope->fft_ctx);
128 scope->fft_ctx = NULL;
130 if (scope->freq_data_l) {
131 g_free (scope->freq_data_l);
132 scope->freq_data_l = NULL;
134 if (scope->freq_data_r) {
135 g_free (scope->freq_data_r);
136 scope->freq_data_r = NULL;
138 if (scope->adata_l) {
139 g_free (scope->adata_l);
140 scope->adata_l = NULL;
142 if (scope->adata_r) {
143 g_free (scope->adata_r);
144 scope->adata_r = NULL;
147 G_OBJECT_CLASS (parent_class)->finalize (object);
151 gst_synae_scope_setup (GstBaseAudioVisualizer * bscope)
153 GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
154 guint num_freq = bscope->height + 1;
157 gst_fft_s16_free (scope->fft_ctx);
158 g_free (scope->freq_data_l);
159 g_free (scope->freq_data_r);
160 g_free (scope->adata_l);
161 g_free (scope->adata_r);
163 /* FIXME: we could have horizontal or vertical layout */
165 /* we'd need this amount of samples per render() call */
166 bscope->req_spf = num_freq * 2 - 2;
167 scope->fft_ctx = gst_fft_s16_new (bscope->req_spf, FALSE);
168 scope->freq_data_l = g_new (GstFFTS16Complex, num_freq);
169 scope->freq_data_r = g_new (GstFFTS16Complex, num_freq);
171 scope->adata_l = g_new (gint16, bscope->req_spf);
172 scope->adata_r = g_new (gint16, bscope->req_spf);
178 add_pixel (guint32 * _p, guint32 _c)
180 guint8 *p = (guint8 *) _p;
181 guint8 *c = (guint8 *) & _c;
183 if (p[0] < 255 - c[0])
187 if (p[1] < 255 - c[1])
191 if (p[2] < 255 - c[2])
195 if (p[3] < 255 - c[3])
202 gst_synae_scope_render (GstBaseAudioVisualizer * bscope, GstBuffer * audio,
205 GstSynaeScope *scope = GST_SYNAE_SCOPE (bscope);
206 guint32 *vdata = (guint32 *) GST_BUFFER_DATA (video);
207 gint16 *adata = (gint16 *) GST_BUFFER_DATA (audio);
208 gint16 *adata_l = scope->adata_l;
209 gint16 *adata_r = scope->adata_r;
210 GstFFTS16Complex *fdata_l = scope->freq_data_l;
211 GstFFTS16Complex *fdata_r = scope->freq_data_r;
214 guint w = bscope->width;
215 guint h = bscope->height;
216 guint32 *colors = scope->colors, c;
217 guint *shade = scope->shade;
219 guint ch = bscope->channels;
220 guint num_samples = GST_BUFFER_SIZE (audio) / (ch * sizeof (gint16));
224 gdouble fc, r, l, rr, ll;
225 gdouble frl, fil, frr, fir;
229 for (i = 0, j = 0; i < num_samples; i++) {
230 adata_l[i] = adata[j++];
231 adata_r[i] = adata[j++];
235 /*gst_fft_s16_window (scope->fft_ctx, adata_l, GST_FFT_WINDOW_HAMMING); */
236 gst_fft_s16_fft (scope->fft_ctx, adata_l, fdata_l);
237 /*gst_fft_s16_window (scope->fft_ctx, adata_r, GST_FFT_WINDOW_HAMMING); */
238 gst_fft_s16_fft (scope->fft_ctx, adata_r, fdata_r);
241 for (y = 0; y < h; y++) {
243 frl = (gdouble) fdata_l[b].r;
244 fil = (gdouble) fdata_l[b].i;
245 frr = (gdouble) fdata_r[b].r;
246 fir = (gdouble) fdata_r[b].i;
248 ll = (frl + fil) * (frl + fil) + (frr - fir) * (frr - fir);
250 rr = (frl - fil) * (frl - fil) + (frr + fir) * (frr + fir);
252 /* out-of-phase'ness for this frequency component */
254 ((frl + fil) * (frl - fil) + (frr + fir) * (frr - fir)) /
258 x = (guint) (r * w / fc);
259 /* the brighness scaling factor was picked by experimenting */
262 br1 = br * (clarity + 128) >> 8;
263 br2 = br * (128 - clarity) >> 8;
264 br1 = CLAMP (br1, 0, 255);
265 br2 = CLAMP (br2, 0, 255);
267 GST_DEBUG ("y %3d fc %10.6f clarity %d br %d br1 %d br2 %d", y, fc, clarity,
272 c = colors[(br1 >> 4) | (br2 & 0xf0)];
273 add_pixel (&vdata[off], c);
274 if ((x > (sl - 1)) && (x < (w - sl)) && (y > (sl - 1)) && (y < (h - sl))) {
275 for (i = 1; br1 || br2; i++, br1 = shade[br1], br2 = shade[br2]) {
276 c = colors[(br1 >> 4) + (br2 & 0xf0)];
277 add_pixel (&vdata[off - i], c);
278 add_pixel (&vdata[off + i], c);
279 add_pixel (&vdata[off - i * w], c);
280 add_pixel (&vdata[off + i * w], c);
283 for (i = 1; br1 || br2; i++, br1 = shade[br1], br2 = shade[br2]) {
284 c = colors[(br1 >> 4) | (br2 & 0xf0)];
286 add_pixel (&vdata[off - i], c);
288 add_pixel (&vdata[off + i], c);
290 add_pixel (&vdata[off - i * w], c);
292 add_pixel (&vdata[off + i * w], c);
301 gst_synae_scope_plugin_init (GstPlugin * plugin)
303 GST_DEBUG_CATEGORY_INIT (synae_scope_debug, "synaescope", 0, "synaescope");
305 return gst_element_register (plugin, "synaescope", GST_RANK_NONE,
306 GST_TYPE_SYNAE_SCOPE);