2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstspectrascope.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-spectrascope
24 * Spectrascope is a simple spectrum visualisation element. It renders the
25 * frequency spectrum as a series of bars.
28 * <title>Example launch line</title>
30 * gst-launch audiotestsrc ! audioconvert ! spectrascope ! ximagesink
39 #include "gstspectrascope.h"
41 static GstStaticPadTemplate gst_spectra_scope_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
45 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
48 static GstStaticPadTemplate gst_spectra_scope_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
56 GST_DEBUG_CATEGORY_STATIC (spectra_scope_debug);
57 #define GST_CAT_DEFAULT spectra_scope_debug
59 static void gst_spectra_scope_finalize (GObject * object);
61 static gboolean gst_spectra_scope_setup (GstBaseAudioVisualizer * scope);
62 static gboolean gst_spectra_scope_render (GstBaseAudioVisualizer * scope,
63 GstBuffer * audio, GstBuffer * video);
66 GST_BOILERPLATE (GstSpectraScope, gst_spectra_scope, GstBaseAudioVisualizer,
67 GST_TYPE_BASE_AUDIO_VISUALIZER);
70 gst_spectra_scope_base_init (gpointer g_class)
72 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
74 gst_element_class_set_details_simple (element_class,
75 "Frequency spectrum scope", "Visualization",
76 "Simple frequency spectrum scope", "Stefan Kost <ensonic@users.sf.net>");
78 gst_element_class_add_static_pad_template (element_class,
79 &gst_spectra_scope_src_template);
80 gst_element_class_add_static_pad_template (element_class,
81 &gst_spectra_scope_sink_template);
85 gst_spectra_scope_class_init (GstSpectraScopeClass * g_class)
87 GObjectClass *gobject_class = (GObjectClass *) g_class;
88 GstBaseAudioVisualizerClass *scope_class =
89 (GstBaseAudioVisualizerClass *) g_class;
91 gobject_class->finalize = gst_spectra_scope_finalize;
93 scope_class->setup = GST_DEBUG_FUNCPTR (gst_spectra_scope_setup);
94 scope_class->render = GST_DEBUG_FUNCPTR (gst_spectra_scope_render);
98 gst_spectra_scope_init (GstSpectraScope * scope, GstSpectraScopeClass * g_class)
104 gst_spectra_scope_finalize (GObject * object)
106 GstSpectraScope *scope = GST_SPECTRA_SCOPE (object);
108 if (scope->fft_ctx) {
109 gst_fft_s16_free (scope->fft_ctx);
110 scope->fft_ctx = NULL;
112 if (scope->freq_data) {
113 g_free (scope->freq_data);
114 scope->freq_data = NULL;
117 G_OBJECT_CLASS (parent_class)->finalize (object);
121 gst_spectra_scope_setup (GstBaseAudioVisualizer * bscope)
123 GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
124 guint num_freq = bscope->width + 1;
127 gst_fft_s16_free (scope->fft_ctx);
128 g_free (scope->freq_data);
130 /* we'd need this amount of samples per render() call */
131 bscope->req_spf = num_freq * 2 - 2;
132 scope->fft_ctx = gst_fft_s16_new (bscope->req_spf, FALSE);
133 scope->freq_data = g_new (GstFFTS16Complex, num_freq);
139 add_pixel (guint32 * _p, guint32 _c)
141 guint8 *p = (guint8 *) _p;
142 guint8 *c = (guint8 *) & _c;
144 if (p[0] < 255 - c[0])
148 if (p[1] < 255 - c[1])
152 if (p[2] < 255 - c[2])
156 if (p[3] < 255 - c[3])
163 gst_spectra_scope_render (GstBaseAudioVisualizer * bscope, GstBuffer * audio,
166 GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
167 guint32 *vdata = (guint32 *) GST_BUFFER_DATA (video);
168 gint16 *adata = (gint16 *) g_memdup (GST_BUFFER_DATA (audio),
169 GST_BUFFER_SIZE (audio));
170 GstFFTS16Complex *fdata = scope->freq_data;
172 guint l, h = bscope->height - 1;
174 guint w = bscope->width;
176 if (bscope->channels > 1) {
177 guint ch = bscope->channels;
178 guint num_samples = GST_BUFFER_SIZE (audio) / (ch * sizeof (gint16));
179 guint i, c, v, s = 0;
181 /* deinterleave and mixdown adata */
182 for (i = 0; i < num_samples; i++) {
184 for (c = 0; c < ch; c++) {
192 gst_fft_s16_window (scope->fft_ctx, adata, GST_FFT_WINDOW_HAMMING);
193 gst_fft_s16_fft (scope->fft_ctx, adata, fdata);
197 for (x = 0; x < bscope->width; x++) {
198 /* figure out the range so that we don't need to clip,
199 * or even better do a log mapping? */
200 fr = (gfloat) fdata[1 + x].r / 512.0;
201 fi = (gfloat) fdata[1 + x].i / 512.0;
202 y = (guint) (h * fabs (fr * fr + fi * fi));
207 vdata[off] = 0x00FFFFFF;
208 for (l = y + 1; l <= h; l++) {
210 add_pixel (&vdata[off], 0x007F7F7F);
217 gst_spectra_scope_plugin_init (GstPlugin * plugin)
219 GST_DEBUG_CATEGORY_INIT (spectra_scope_debug, "spectrascope", 0,
222 return gst_element_register (plugin, "spectrascope", GST_RANK_NONE,
223 GST_TYPE_SPECTRA_SCOPE);