1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstspectrascope.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 /**
23  * SECTION:element-spectrascope
24  * @title: spectrascope
25  * @see_also: goom
26  *
27  * Spectrascope is a simple spectrum visualisation element. It renders the
28  * frequency spectrum as a series of bars.
29  *
30  * ## Example launch line
31  * |[
32  * gst-launch-1.0 audiotestsrc ! audioconvert ! spectrascope ! ximagesink
33  * ]|
34  *
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include <stdlib.h>
40 
41 #include "gstspectrascope.h"
42 
43 #if G_BYTE_ORDER == G_BIG_ENDIAN
44 #define RGB_ORDER "xRGB"
45 #else
46 #define RGB_ORDER "BGRx"
47 #endif
48 
49 static GstStaticPadTemplate gst_spectra_scope_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (RGB_ORDER))
54     );
55 
56 static GstStaticPadTemplate gst_spectra_scope_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58     GST_PAD_SINK,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("audio/x-raw, "
61         "format = (string) " GST_AUDIO_NE (S16) ", "
62         "layout = (string) interleaved, "
63         "rate = (int) [ 8000, 96000 ], "
64         "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
65     );
66 
67 
68 GST_DEBUG_CATEGORY_STATIC (spectra_scope_debug);
69 #define GST_CAT_DEFAULT spectra_scope_debug
70 
71 static void gst_spectra_scope_finalize (GObject * object);
72 
73 static gboolean gst_spectra_scope_setup (GstAudioVisualizer * scope);
74 static gboolean gst_spectra_scope_render (GstAudioVisualizer * scope,
75     GstBuffer * audio, GstVideoFrame * video);
76 
77 
78 G_DEFINE_TYPE (GstSpectraScope, gst_spectra_scope, GST_TYPE_AUDIO_VISUALIZER);
79 
80 static void
gst_spectra_scope_class_init(GstSpectraScopeClass * g_class)81 gst_spectra_scope_class_init (GstSpectraScopeClass * g_class)
82 {
83   GObjectClass *gobject_class = (GObjectClass *) g_class;
84   GstElementClass *element_class = (GstElementClass *) g_class;
85   GstAudioVisualizerClass *scope_class = (GstAudioVisualizerClass *) g_class;
86 
87   gobject_class->finalize = gst_spectra_scope_finalize;
88 
89   gst_element_class_set_static_metadata (element_class,
90       "Frequency spectrum scope", "Visualization",
91       "Simple frequency spectrum scope", "Stefan Kost <ensonic@users.sf.net>");
92 
93   gst_element_class_add_static_pad_template (element_class,
94       &gst_spectra_scope_src_template);
95   gst_element_class_add_static_pad_template (element_class,
96       &gst_spectra_scope_sink_template);
97 
98   scope_class->setup = GST_DEBUG_FUNCPTR (gst_spectra_scope_setup);
99   scope_class->render = GST_DEBUG_FUNCPTR (gst_spectra_scope_render);
100 }
101 
102 static void
gst_spectra_scope_init(GstSpectraScope * scope)103 gst_spectra_scope_init (GstSpectraScope * scope)
104 {
105   /* do nothing */
106 }
107 
108 static void
gst_spectra_scope_finalize(GObject * object)109 gst_spectra_scope_finalize (GObject * object)
110 {
111   GstSpectraScope *scope = GST_SPECTRA_SCOPE (object);
112 
113   if (scope->fft_ctx) {
114     gst_fft_s16_free (scope->fft_ctx);
115     scope->fft_ctx = NULL;
116   }
117   if (scope->freq_data) {
118     g_free (scope->freq_data);
119     scope->freq_data = NULL;
120   }
121 
122   G_OBJECT_CLASS (gst_spectra_scope_parent_class)->finalize (object);
123 }
124 
125 static gboolean
gst_spectra_scope_setup(GstAudioVisualizer * bscope)126 gst_spectra_scope_setup (GstAudioVisualizer * bscope)
127 {
128   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
129   guint num_freq = GST_VIDEO_INFO_WIDTH (&bscope->vinfo) + 1;
130 
131   if (scope->fft_ctx)
132     gst_fft_s16_free (scope->fft_ctx);
133   g_free (scope->freq_data);
134 
135   /* we'd need this amount of samples per render() call */
136   bscope->req_spf = num_freq * 2 - 2;
137   scope->fft_ctx = gst_fft_s16_new (bscope->req_spf, FALSE);
138   scope->freq_data = g_new (GstFFTS16Complex, num_freq);
139 
140   return TRUE;
141 }
142 
143 static inline void
add_pixel(guint32 * _p,guint32 _c)144 add_pixel (guint32 * _p, guint32 _c)
145 {
146   guint8 *p = (guint8 *) _p;
147   guint8 *c = (guint8 *) & _c;
148 
149   if (p[0] < 255 - c[0])
150     p[0] += c[0];
151   else
152     p[0] = 255;
153   if (p[1] < 255 - c[1])
154     p[1] += c[1];
155   else
156     p[1] = 255;
157   if (p[2] < 255 - c[2])
158     p[2] += c[2];
159   else
160     p[2] = 255;
161   if (p[3] < 255 - c[3])
162     p[3] += c[3];
163   else
164     p[3] = 255;
165 }
166 
167 static gboolean
gst_spectra_scope_render(GstAudioVisualizer * bscope,GstBuffer * audio,GstVideoFrame * video)168 gst_spectra_scope_render (GstAudioVisualizer * bscope, GstBuffer * audio,
169     GstVideoFrame * video)
170 {
171   GstSpectraScope *scope = GST_SPECTRA_SCOPE (bscope);
172   gint16 *mono_adata;
173   GstFFTS16Complex *fdata = scope->freq_data;
174   guint x, y, off, l;
175   guint w = GST_VIDEO_INFO_WIDTH (&bscope->vinfo);
176   guint h = GST_VIDEO_INFO_HEIGHT (&bscope->vinfo) - 1;
177   gfloat fr, fi;
178   GstMapInfo amap;
179   guint32 *vdata;
180   gint channels;
181 
182   gst_buffer_map (audio, &amap, GST_MAP_READ);
183   vdata = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (video, 0);
184 
185   channels = GST_AUDIO_INFO_CHANNELS (&bscope->ainfo);
186 
187   mono_adata = (gint16 *) g_memdup (amap.data, amap.size);
188 
189   if (channels > 1) {
190     guint ch = channels;
191     guint num_samples = amap.size / (ch * sizeof (gint16));
192     guint i, c, v, s = 0;
193 
194     /* deinterleave and mixdown adata */
195     for (i = 0; i < num_samples; i++) {
196       v = 0;
197       for (c = 0; c < ch; c++) {
198         v += mono_adata[s++];
199       }
200       mono_adata[i] = v / ch;
201     }
202   }
203 
204   /* run fft */
205   gst_fft_s16_window (scope->fft_ctx, mono_adata, GST_FFT_WINDOW_HAMMING);
206   gst_fft_s16_fft (scope->fft_ctx, mono_adata, fdata);
207   g_free (mono_adata);
208 
209   /* draw lines */
210   for (x = 0; x < w; x++) {
211     /* figure out the range so that we don't need to clip,
212      * or even better do a log mapping? */
213     fr = (gfloat) fdata[1 + x].r / 512.0;
214     fi = (gfloat) fdata[1 + x].i / 512.0;
215     y = (guint) (h * sqrt (fr * fr + fi * fi));
216     if (y > h)
217       y = h;
218     y = h - y;
219     off = (y * w) + x;
220     vdata[off] = 0x00FFFFFF;
221     for (l = y; l < h; l++) {
222       off += w;
223       add_pixel (&vdata[off], 0x007F7F7F);
224     }
225     /* ensure bottom line is full bright (especially in move-up mode) */
226     add_pixel (&vdata[off], 0x007F7F7F);
227   }
228   gst_buffer_unmap (audio, &amap);
229   return TRUE;
230 }
231 
232 gboolean
gst_spectra_scope_plugin_init(GstPlugin * plugin)233 gst_spectra_scope_plugin_init (GstPlugin * plugin)
234 {
235   GST_DEBUG_CATEGORY_INIT (spectra_scope_debug, "spectrascope", 0,
236       "spectrascope");
237 
238   return gst_element_register (plugin, "spectrascope", GST_RANK_NONE,
239       GST_TYPE_SPECTRA_SCOPE);
240 }
241