2 * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
4 * gstspectrascope.c: simple oscilloscope
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 * Wavescope is a simple audio visualisation element. It renders the waveforms
25 * like on an oscilloscope.
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 (GstBaseScope * scope);
62 static gboolean gst_spectra_scope_render (GstBaseScope * scope,
63 GstBuffer * audio, GstBuffer * video);
66 GST_BOILERPLATE (GstSpectraScope, gst_spectra_scope, GstBaseScope,
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, "Waveform oscilloscope",
76 "Simple waveform oscilloscope", "Stefan Kost <ensonic@users.sf.net>");
78 gst_element_class_add_pad_template (element_class,
79 gst_static_pad_template_get (&gst_spectra_scope_src_template));
80 gst_element_class_add_pad_template (element_class,
81 gst_static_pad_template_get (&gst_spectra_scope_sink_template));
85 gst_spectra_scope_class_init (GstSpectraScopeClass * g_class)
87 GObjectClass *gobject_class = (GObjectClass *) g_class;
88 GstBaseScopeClass *scope_class = (GstBaseScopeClass *) g_class;
90 gobject_class->finalize = gst_spectra_scope_finalize;
92 scope_class->setup = GST_DEBUG_FUNCPTR (gst_spectra_scope_setup);
93 scope_class->render = GST_DEBUG_FUNCPTR (gst_spectra_scope_render);
97 gst_spectra_scope_init (GstSpectraScope * scope, GstSpectraScopeClass * g_class)
103 gst_spectra_scope_finalize (GObject * object)
105 GstSpectraScope *scope = GST_SPECTRA_SCOPE (object);
107 if (scope->fft_ctx) {
108 gst_fft_s16_free (scope->fft_ctx);
109 scope->fft_ctx = NULL;
111 if (scope->freq_data) {
112 g_free (scope->freq_data);
113 scope->freq_data = NULL;
116 G_OBJECT_CLASS (parent_class)->finalize (object);
120 gst_spectra_scope_setup (GstBaseScope * bscope)
122 GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
125 gst_fft_s16_free (scope->fft_ctx);
126 if (scope->freq_data)
127 g_free (scope->freq_data);
128 scope->fft_ctx = gst_fft_s16_new (bscope->width * 2 - 2, FALSE);
129 scope->freq_data = g_new (GstFFTS16Complex, bscope->width);
134 gst_spectra_scope_render (GstBaseScope * bscope, GstBuffer * audio,
137 GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
138 guint8 *vdata = GST_BUFFER_DATA (video);
139 gint16 *adata = (gint16 *) GST_BUFFER_DATA (audio);
140 GstFFTS16Complex *fdata = scope->freq_data;
142 guint l, h = bscope->height - 1;
144 guint bpp = gst_video_format_get_pixel_stride (bscope->video_format, 0);
145 guint bpl = bpp * bscope->width;
147 if (bscope->channels > 1) {
148 gint ch = bscope->channels;
149 gint num_samples = GST_BUFFER_SIZE (audio) / (ch * sizeof (gint16));
152 /* deinterleave and mixdown adata */
153 for (i = 0; i < num_samples; i++) {
155 for (c = 0; c < ch; c++) {
163 gst_fft_s16_window (scope->fft_ctx, adata, GST_FFT_WINDOW_HAMMING);
164 gst_fft_s16_fft (scope->fft_ctx, adata, fdata);
167 for (x = 0; x < bscope->width; x++) {
168 /* figure out the range so that we don't need to clip,
169 * or even better do a log mapping? */
170 fr = (gfloat) fdata[x].r / 2048.0;
171 fi = (gfloat) fdata[x].i / 2048.0;
172 y = (guint) (h * fabs (fr * fr + fi * fi));
176 off = (y * bpl) + (x * bpp);
177 vdata[off + 0] = 0xFF;
178 vdata[off + 1] = 0xFF;
179 vdata[off + 2] = 0xFF;
180 for (l = y + 1; l <= h; l++) {
182 vdata[off + 0] = 0x7F;
183 vdata[off + 1] = 0x7F;
184 vdata[off + 2] = 0x7F;
191 gst_spectra_scope_plugin_init (GstPlugin * plugin)
193 GST_DEBUG_CATEGORY_INIT (spectra_scope_debug, "spectrascope", 0,
196 return gst_element_register (plugin, "spectrascope", GST_RANK_NONE,
197 GST_TYPE_SPECTRA_SCOPE);