2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2006,2011> Stefan Kost <ensonic@users.sf.net>
4 * <2007-2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 * SECTION:element-spectrum
24 * The Spectrum element analyzes the frequency spectrum of an audio signal.
25 * If the #GstSpectrum:post-messages property is #TRUE, it sends analysis results
26 * as application messages named
27 * <classname>"spectrum"</classname> after each interval of time given
28 * by the #GstSpectrum:interval property.
30 * The message's structure contains some combination of these fields:
35 * <classname>"timestamp"</classname>:
36 * the timestamp of the buffer that triggered the message.
42 * <classname>"stream-time"</classname>:
43 * the stream time of the buffer.
49 * <classname>"running-time"</classname>:
50 * the running_time of the buffer.
56 * <classname>"duration"</classname>:
57 * the duration of the buffer.
63 * <classname>"endtime"</classname>:
64 * the end time of the buffer that triggered the message as stream time (this
65 * is deprecated, as it can be calculated from stream-time + duration)
70 * #GstValueList of #gfloat
71 * <classname>"magnitude"</classname>:
72 * the level for each frequency band in dB. All values below the value of the
73 * #GstSpectrum:threshold property will be set to the threshold. Only present
74 * if the #GstSpectrum:message-magnitude property is %TRUE.
79 * #GstValueList of #gfloat
80 * <classname>"phase"</classname>:
81 * The phase for each frequency band. The value is between -pi and pi. Only
82 * present if the #GstSpectrum:message-phase property is %TRUE.
87 * If #GstSpectrum:multi-channel property is set to true. magnitude and phase
88 * fields will be each a nested #GstValueArray. The first dimension are the
89 * channels and the second dimension are the values.
92 * <title>Example application</title>
94 * <xi:include xmlns:xi="http://www.w3.org/2003/XInclude" parse="text" href="../../../../tests/examples/spectrum/spectrum-example.c" />
98 * Last reviewed on 2011-03-10 (0.10.29)
107 #include "gstspectrum.h"
109 GST_DEBUG_CATEGORY_STATIC (gst_spectrum_debug);
110 #define GST_CAT_DEFAULT gst_spectrum_debug
112 /* elementfactory information */
114 #define ALLOWED_CAPS \
115 "audio/x-raw-int, " \
116 " width = (int) 16, " \
117 " depth = (int) [ 1, 16 ], " \
118 " signed = (boolean) true, " \
119 " endianness = (int) BYTE_ORDER, " \
120 " rate = (int) [ 1, MAX ], " \
121 " channels = (int) [ 1, MAX ]; " \
122 "audio/x-raw-int, " \
123 " width = (int) 24, " \
124 " depth = (int) [ 1, 24 ], " \
125 " signed = (boolean) true, " \
126 " endianness = (int) BYTE_ORDER, " \
127 " rate = (int) [ 1, MAX ], " \
128 " channels = (int) [ 1, MAX ]; " \
129 "audio/x-raw-int, " \
130 " width = (int) 32, " \
131 " depth = (int) [ 1, 32 ], " \
132 " signed = (boolean) true, " \
133 " endianness = (int) BYTE_ORDER, " \
134 " rate = (int) [ 1, MAX ], " \
135 " channels = (int) [ 1, MAX ]; " \
136 "audio/x-raw-float, " \
137 " width = (int) { 32, 64 }, " \
138 " endianness = (int) BYTE_ORDER, " \
139 " rate = (int) [ 1, MAX ], " \
140 " channels = (int) [ 1, MAX ]"
142 /* Spectrum properties */
143 #define DEFAULT_MESSAGE TRUE
144 #define DEFAULT_POST_MESSAGES TRUE
145 #define DEFAULT_MESSAGE_MAGNITUDE TRUE
146 #define DEFAULT_MESSAGE_PHASE FALSE
147 #define DEFAULT_INTERVAL (GST_SECOND / 10)
148 #define DEFAULT_BANDS 128
149 #define DEFAULT_THRESHOLD -60
150 #define DEFAULT_MULTI_CHANNEL FALSE
157 PROP_MESSAGE_MAGNITUDE,
165 GST_BOILERPLATE (GstSpectrum, gst_spectrum, GstAudioFilter,
166 GST_TYPE_AUDIO_FILTER);
168 static void gst_spectrum_finalize (GObject * object);
169 static void gst_spectrum_set_property (GObject * object, guint prop_id,
170 const GValue * value, GParamSpec * pspec);
171 static void gst_spectrum_get_property (GObject * object, guint prop_id,
172 GValue * value, GParamSpec * pspec);
173 static gboolean gst_spectrum_start (GstBaseTransform * trans);
174 static gboolean gst_spectrum_stop (GstBaseTransform * trans);
175 static GstFlowReturn gst_spectrum_transform_ip (GstBaseTransform * trans,
177 static gboolean gst_spectrum_setup (GstAudioFilter * base,
178 GstRingBufferSpec * format);
181 gst_spectrum_base_init (gpointer g_class)
183 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
186 gst_element_class_set_details_simple (element_class, "Spectrum analyzer",
187 "Filter/Analyzer/Audio",
188 "Run an FFT on the audio signal, output spectrum data",
189 "Erik Walthinsen <omega@cse.ogi.edu>, "
190 "Stefan Kost <ensonic@users.sf.net>, "
191 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
193 caps = gst_caps_from_string (ALLOWED_CAPS);
194 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
196 gst_caps_unref (caps);
200 gst_spectrum_class_init (GstSpectrumClass * klass)
202 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
203 GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
204 GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (klass);
206 gobject_class->set_property = gst_spectrum_set_property;
207 gobject_class->get_property = gst_spectrum_get_property;
208 gobject_class->finalize = gst_spectrum_finalize;
210 trans_class->start = GST_DEBUG_FUNCPTR (gst_spectrum_start);
211 trans_class->stop = GST_DEBUG_FUNCPTR (gst_spectrum_stop);
212 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_spectrum_transform_ip);
213 trans_class->passthrough_on_same_caps = TRUE;
215 filter_class->setup = GST_DEBUG_FUNCPTR (gst_spectrum_setup);
217 /* FIXME 0.11, remove in favour of post-messages */
218 g_object_class_install_property (gobject_class, PROP_MESSAGE,
219 g_param_spec_boolean ("message", "Message",
220 "Whether to post a 'spectrum' element message on the bus for each "
221 "passed interval (deprecated, use post-messages)", DEFAULT_MESSAGE,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 * GstSpectrum:post-messages
226 * Post messages on the bus with spectrum information.
230 g_object_class_install_property (gobject_class, PROP_POST_MESSAGES,
231 g_param_spec_boolean ("post-messages", "Post Messages",
232 "Whether to post a 'spectrum' element message on the bus for each "
233 "passed interval", DEFAULT_POST_MESSAGES,
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (gobject_class, PROP_MESSAGE_MAGNITUDE,
237 g_param_spec_boolean ("message-magnitude", "Magnitude",
238 "Whether to add a 'magnitude' field to the structure of any "
239 "'spectrum' element messages posted on the bus",
240 DEFAULT_MESSAGE_MAGNITUDE,
241 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_MESSAGE_PHASE,
244 g_param_spec_boolean ("message-phase", "Phase",
245 "Whether to add a 'phase' field to the structure of any "
246 "'spectrum' element messages posted on the bus",
247 DEFAULT_MESSAGE_PHASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 g_object_class_install_property (gobject_class, PROP_INTERVAL,
250 g_param_spec_uint64 ("interval", "Interval",
251 "Interval of time between message posts (in nanoseconds)",
252 1, G_MAXUINT64, DEFAULT_INTERVAL,
253 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_BANDS,
256 g_param_spec_uint ("bands", "Bands", "Number of frequency bands",
257 0, G_MAXUINT, DEFAULT_BANDS,
258 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
261 g_param_spec_int ("threshold", "Threshold",
262 "dB threshold for result. All lower values will be set to this",
263 G_MININT, 0, DEFAULT_THRESHOLD,
264 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 * GstSpectrum:multi-channel
269 * Send separate results for each channel
273 g_object_class_install_property (gobject_class, PROP_MULTI_CHANNEL,
274 g_param_spec_boolean ("multi-channel", "Multichannel results",
275 "Send separate results for each channel",
276 DEFAULT_MULTI_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278 GST_DEBUG_CATEGORY_INIT (gst_spectrum_debug, "spectrum", 0,
279 "audio spectrum analyser element");
283 gst_spectrum_init (GstSpectrum * spectrum, GstSpectrumClass * g_class)
285 spectrum->post_messages = DEFAULT_POST_MESSAGES;
286 spectrum->message_magnitude = DEFAULT_MESSAGE_MAGNITUDE;
287 spectrum->message_phase = DEFAULT_MESSAGE_PHASE;
288 spectrum->interval = DEFAULT_INTERVAL;
289 spectrum->bands = DEFAULT_BANDS;
290 spectrum->threshold = DEFAULT_THRESHOLD;
294 gst_spectrum_alloc_channel_data (GstSpectrum * spectrum)
297 GstSpectrumChannel *cd;
298 guint bands = spectrum->bands;
299 guint nfft = 2 * bands - 2;
301 g_assert (spectrum->channel_data == NULL);
303 spectrum->num_channels = (spectrum->multi_channel) ?
304 GST_AUDIO_FILTER (spectrum)->format.channels : 1;
306 GST_DEBUG_OBJECT (spectrum, "allocating data for %d channels",
307 spectrum->num_channels);
309 spectrum->channel_data = g_new (GstSpectrumChannel, spectrum->num_channels);
310 for (i = 0; i < spectrum->num_channels; i++) {
311 cd = &spectrum->channel_data[i];
312 cd->fft_ctx = gst_fft_f32_new (nfft, FALSE);
313 cd->input = g_new0 (gfloat, nfft);
314 cd->input_tmp = g_new0 (gfloat, nfft);
315 cd->freqdata = g_new0 (GstFFTF32Complex, bands);
316 cd->spect_magnitude = g_new0 (gfloat, bands);
317 cd->spect_phase = g_new0 (gfloat, bands);
322 gst_spectrum_free_channel_data (GstSpectrum * spectrum)
324 if (spectrum->channel_data) {
326 GstSpectrumChannel *cd;
328 GST_DEBUG_OBJECT (spectrum, "freeing data for %d channels",
329 spectrum->num_channels);
331 for (i = 0; i < spectrum->num_channels; i++) {
332 cd = &spectrum->channel_data[i];
334 gst_fft_f32_free (cd->fft_ctx);
336 g_free (cd->input_tmp);
337 g_free (cd->freqdata);
338 g_free (cd->spect_magnitude);
339 g_free (cd->spect_phase);
341 g_free (spectrum->channel_data);
342 spectrum->channel_data = NULL;
347 gst_spectrum_flush (GstSpectrum * spectrum)
349 spectrum->num_frames = 0;
350 spectrum->num_fft = 0;
352 spectrum->accumulated_error = 0;
356 gst_spectrum_reset_state (GstSpectrum * spectrum)
358 GST_DEBUG_OBJECT (spectrum, "resetting state");
360 gst_spectrum_free_channel_data (spectrum);
361 gst_spectrum_flush (spectrum);
365 gst_spectrum_finalize (GObject * object)
367 GstSpectrum *spectrum = GST_SPECTRUM (object);
369 gst_spectrum_reset_state (spectrum);
371 G_OBJECT_CLASS (parent_class)->finalize (object);
375 gst_spectrum_set_property (GObject * object, guint prop_id,
376 const GValue * value, GParamSpec * pspec)
378 GstSpectrum *filter = GST_SPECTRUM (object);
382 case PROP_POST_MESSAGES:
383 filter->post_messages = g_value_get_boolean (value);
385 case PROP_MESSAGE_MAGNITUDE:
386 filter->message_magnitude = g_value_get_boolean (value);
388 case PROP_MESSAGE_PHASE:
389 filter->message_phase = g_value_get_boolean (value);
392 guint64 interval = g_value_get_uint64 (value);
393 if (filter->interval != interval) {
394 GST_BASE_TRANSFORM_LOCK (filter);
395 filter->interval = interval;
396 gst_spectrum_reset_state (filter);
397 GST_BASE_TRANSFORM_UNLOCK (filter);
402 guint bands = g_value_get_uint (value);
403 if (filter->bands != bands) {
404 GST_BASE_TRANSFORM_LOCK (filter);
405 filter->bands = bands;
406 gst_spectrum_reset_state (filter);
407 GST_BASE_TRANSFORM_UNLOCK (filter);
412 filter->threshold = g_value_get_int (value);
414 case PROP_MULTI_CHANNEL:{
415 gboolean multi_channel = g_value_get_boolean (value);
416 if (filter->multi_channel != multi_channel) {
417 GST_BASE_TRANSFORM_LOCK (filter);
418 filter->multi_channel = multi_channel;
419 gst_spectrum_reset_state (filter);
420 GST_BASE_TRANSFORM_UNLOCK (filter);
425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 gst_spectrum_get_property (GObject * object, guint prop_id,
432 GValue * value, GParamSpec * pspec)
434 GstSpectrum *filter = GST_SPECTRUM (object);
438 case PROP_POST_MESSAGES:
439 g_value_set_boolean (value, filter->post_messages);
441 case PROP_MESSAGE_MAGNITUDE:
442 g_value_set_boolean (value, filter->message_magnitude);
444 case PROP_MESSAGE_PHASE:
445 g_value_set_boolean (value, filter->message_phase);
448 g_value_set_uint64 (value, filter->interval);
451 g_value_set_uint (value, filter->bands);
454 g_value_set_int (value, filter->threshold);
456 case PROP_MULTI_CHANNEL:
457 g_value_set_boolean (value, filter->multi_channel);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 gst_spectrum_start (GstBaseTransform * trans)
468 GstSpectrum *spectrum = GST_SPECTRUM (trans);
470 gst_spectrum_reset_state (spectrum);
476 gst_spectrum_stop (GstBaseTransform * trans)
478 GstSpectrum *spectrum = GST_SPECTRUM (trans);
480 gst_spectrum_reset_state (spectrum);
485 /* mixing data readers */
488 input_data_mixed_float (const guint8 * _in, gfloat * out, guint len,
489 guint channels, gfloat max_value, guint op, guint nfft)
493 gfloat *in = (gfloat *) _in;
495 for (j = 0; j < len; j++) {
497 for (i = 1; i < channels; i++)
499 out[op] = v / channels;
500 op = (op + 1) % nfft;
505 input_data_mixed_double (const guint8 * _in, gfloat * out, guint len,
506 guint channels, gfloat max_value, guint op, guint nfft)
510 gdouble *in = (gdouble *) _in;
512 for (j = 0; j < len; j++) {
514 for (i = 1; i < channels; i++)
516 out[op] = v / channels;
517 op = (op + 1) % nfft;
522 input_data_mixed_int32 (const guint8 * _in, gfloat * out, guint len,
523 guint channels, gfloat max_value, guint op, guint nfft)
526 gint32 *in = (gint32 *) _in;
529 for (j = 0; j < len; j++) {
530 v = in[ip++] * 2 + 1;
531 for (i = 1; i < channels; i++)
532 v += in[ip++] * 2 + 1;
533 out[op] = v / channels;
534 op = (op + 1) % nfft;
539 input_data_mixed_int32_max (const guint8 * _in, gfloat * out, guint len,
540 guint channels, gfloat max_value, guint op, guint nfft)
543 gint32 *in = (gint32 *) _in;
546 for (j = 0; j < len; j++) {
547 v = in[ip++] / max_value;
548 for (i = 1; i < channels; i++)
549 v += in[ip++] / max_value;
550 out[op] = v / channels;
551 op = (op + 1) % nfft;
556 input_data_mixed_int24 (const guint8 * _in, gfloat * out, guint len,
557 guint channels, gfloat max_value, guint op, guint nfft)
562 for (j = 0; j < len; j++) {
563 for (i = 0; i < channels; i++) {
564 #if G_BYTE_ORDER == G_BIG_ENDIAN
565 gint32 value = GST_READ_UINT24_BE (_in);
567 gint32 value = GST_READ_UINT24_LE (_in);
569 if (value & 0x00800000)
574 out[op] = v / channels;
575 op = (op + 1) % nfft;
580 input_data_mixed_int24_max (const guint8 * _in, gfloat * out, guint len,
581 guint channels, gfloat max_value, guint op, guint nfft)
586 for (j = 0; j < len; j++) {
587 for (i = 0; i < channels; i++) {
588 #if G_BYTE_ORDER == G_BIG_ENDIAN
589 gint32 value = GST_READ_UINT24_BE (_in);
591 gint32 value = GST_READ_UINT24_LE (_in);
593 if (value & 0x00800000)
595 v += value / max_value;
598 out[op] = v / channels;
599 op = (op + 1) % nfft;
604 input_data_mixed_int16 (const guint8 * _in, gfloat * out, guint len,
605 guint channels, gfloat max_value, guint op, guint nfft)
608 gint16 *in = (gint16 *) _in;
611 for (j = 0; j < len; j++) {
612 v = in[ip++] * 2 + 1;
613 for (i = 1; i < channels; i++)
614 v += in[ip++] * 2 + 1;
615 out[op] = v / channels;
616 op = (op + 1) % nfft;
621 input_data_mixed_int16_max (const guint8 * _in, gfloat * out, guint len,
622 guint channels, gfloat max_value, guint op, guint nfft)
625 gint16 *in = (gint16 *) _in;
628 for (j = 0; j < len; j++) {
629 v = in[ip++] / max_value;
630 for (i = 1; i < channels; i++)
631 v += in[ip++] / max_value;
632 out[op] = v / channels;
633 op = (op + 1) % nfft;
637 /* non mixing data readers */
640 input_data_float (const guint8 * _in, gfloat * out, guint len, guint channels,
641 gfloat max_value, guint op, guint nfft)
644 gfloat *in = (gfloat *) _in;
646 for (j = 0, ip = 0; j < len; j++, ip += channels) {
648 op = (op + 1) % nfft;
653 input_data_double (const guint8 * _in, gfloat * out, guint len, guint channels,
654 gfloat max_value, guint op, guint nfft)
657 gdouble *in = (gdouble *) _in;
659 for (j = 0, ip = 0; j < len; j++, ip += channels) {
661 op = (op + 1) % nfft;
666 input_data_int32 (const guint8 * _in, gfloat * out, guint len, guint channels,
667 gfloat max_value, guint op, guint nfft)
670 gint32 *in = (gint32 *) _in;
672 for (j = 0, ip = 0; j < len; j++, ip += channels) {
673 out[op] = in[ip] * 2 + 1;
674 op = (op + 1) % nfft;
679 input_data_int32_max (const guint8 * _in, gfloat * out, guint len,
680 guint channels, gfloat max_value, guint op, guint nfft)
683 gint32 *in = (gint32 *) _in;
685 for (j = 0, ip = 0; j < len; j++, ip += channels) {
686 out[op] = in[ip] / max_value;
687 op = (op + 1) % nfft;
692 input_data_int24 (const guint8 * _in, gfloat * out, guint len, guint channels,
693 gfloat max_value, guint op, guint nfft)
697 for (j = 0; j < len; j++) {
698 #if G_BYTE_ORDER == G_BIG_ENDIAN
699 gint32 v = GST_READ_UINT24_BE (_in);
701 gint32 v = GST_READ_UINT24_LE (_in);
707 op = (op + 1) % nfft;
712 input_data_int24_max (const guint8 * _in, gfloat * out, guint len,
713 guint channels, gfloat max_value, guint op, guint nfft)
717 for (j = 0; j < len; j++) {
718 #if G_BYTE_ORDER == G_BIG_ENDIAN
719 gint32 v = GST_READ_UINT24_BE (_in);
721 gint32 v = GST_READ_UINT24_LE (_in);
726 out[op] = v / max_value;
727 op = (op + 1) % nfft;
732 input_data_int16 (const guint8 * _in, gfloat * out, guint len, guint channels,
733 gfloat max_value, guint op, guint nfft)
736 gint16 *in = (gint16 *) _in;
738 for (j = 0, ip = 0; j < len; j++, ip += channels) {
739 out[op] = in[ip] * 2 + 1;
740 op = (op + 1) % nfft;
745 input_data_int16_max (const guint8 * _in, gfloat * out, guint len,
746 guint channels, gfloat max_value, guint op, guint nfft)
749 gint16 *in = (gint16 *) _in;
751 for (j = 0, ip = 0; j < len; j++, ip += channels) {
752 out[op] = in[ip] / max_value;
753 op = (op + 1) % nfft;
758 gst_spectrum_setup (GstAudioFilter * base, GstRingBufferSpec * format)
760 GstSpectrum *spectrum = GST_SPECTRUM (base);
761 guint width = format->width / 8;
762 gboolean is_float = (format->type == GST_BUFTYPE_FLOAT);
763 /* max_value will be 0 when depth is 1,
764 * interpret -1 and 0 as -1 and +1 if that's the case. */
765 guint max_value = (1UL << (format->depth - 1)) - 1;
766 gboolean multi_channel = spectrum->multi_channel;
767 GstSpectrumInputData input_data = NULL;
771 input_data = multi_channel ? input_data_float : input_data_mixed_float;
772 } else if (width == 8) {
773 input_data = multi_channel ? input_data_double : input_data_mixed_double;
775 g_assert_not_reached ();
781 multi_channel ? input_data_int32_max : input_data_mixed_int32_max;
783 input_data = multi_channel ? input_data_int32 : input_data_mixed_int32;
785 } else if (width == 3) {
788 multi_channel ? input_data_int24_max : input_data_mixed_int24_max;
790 input_data = multi_channel ? input_data_int24 : input_data_mixed_int24;
792 } else if (width == 2) {
795 multi_channel ? input_data_int16_max : input_data_mixed_int16_max;
797 input_data = multi_channel ? input_data_int16 : input_data_mixed_int16;
800 g_assert_not_reached ();
804 spectrum->input_data = input_data;
805 gst_spectrum_reset_state (spectrum);
810 gst_spectrum_message_add_container (GstStructure * s, GType type,
815 g_value_init (&v, type);
816 /* will copy-by-value */
817 gst_structure_set_value (s, name, &v);
819 return (GValue *) gst_structure_get_value (s, name);
823 gst_spectrum_message_add_list (GValue * cv, gfloat * data, guint num_values)
828 g_value_init (&v, G_TYPE_FLOAT);
829 for (i = 0; i < num_values; i++) {
830 g_value_set_float (&v, data[i]);
831 gst_value_list_append_value (cv, &v); /* copies by value */
837 gst_spectrum_message_add_array (GValue * cv, gfloat * data, guint num_values)
843 g_value_init (&a, GST_TYPE_ARRAY);
845 g_value_init (&v, G_TYPE_FLOAT);
846 for (i = 0; i < num_values; i++) {
847 g_value_set_float (&v, data[i]);
848 gst_value_array_append_value (&a, &v); /* copies by value */
852 gst_value_array_append_value (cv, &a); /* copies by value */
857 gst_spectrum_message_new (GstSpectrum * spectrum, GstClockTime timestamp,
858 GstClockTime duration)
860 GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (spectrum);
861 GstSpectrumChannel *cd;
863 GValue *mcv = NULL, *pcv = NULL;
864 GstClockTime endtime, running_time, stream_time;
866 GST_DEBUG_OBJECT (spectrum, "preparing message, bands =%d ", spectrum->bands);
868 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
870 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
872 /* endtime is for backwards compatibility */
873 endtime = stream_time + duration;
875 s = gst_structure_new ("spectrum",
876 "endtime", GST_TYPE_CLOCK_TIME, endtime,
877 "timestamp", G_TYPE_UINT64, timestamp,
878 "stream-time", G_TYPE_UINT64, stream_time,
879 "running-time", G_TYPE_UINT64, running_time,
880 "duration", G_TYPE_UINT64, duration, NULL);
882 if (!spectrum->multi_channel) {
883 cd = &spectrum->channel_data[0];
885 if (spectrum->message_magnitude) {
886 /* FIXME 0.11: this should be an array, not a list */
887 mcv = gst_spectrum_message_add_container (s, GST_TYPE_LIST, "magnitude");
888 gst_spectrum_message_add_list (mcv, cd->spect_magnitude, spectrum->bands);
890 if (spectrum->message_phase) {
891 /* FIXME 0.11: this should be an array, not a list */
892 pcv = gst_spectrum_message_add_container (s, GST_TYPE_LIST, "phase");
893 gst_spectrum_message_add_list (pcv, cd->spect_phase, spectrum->bands);
897 guint channels = GST_AUDIO_FILTER (spectrum)->format.channels;
899 if (spectrum->message_magnitude) {
900 mcv = gst_spectrum_message_add_container (s, GST_TYPE_ARRAY, "magnitude");
902 if (spectrum->message_phase) {
903 pcv = gst_spectrum_message_add_container (s, GST_TYPE_ARRAY, "phase");
906 for (c = 0; c < channels; c++) {
907 cd = &spectrum->channel_data[c];
909 if (spectrum->message_magnitude) {
910 gst_spectrum_message_add_array (mcv, cd->spect_magnitude,
913 if (spectrum->message_phase) {
914 gst_spectrum_message_add_array (pcv, cd->spect_magnitude,
919 return gst_message_new_element (GST_OBJECT (spectrum), s);
923 gst_spectrum_run_fft (GstSpectrum * spectrum, GstSpectrumChannel * cd,
927 guint bands = spectrum->bands;
928 guint nfft = 2 * bands - 2;
929 gint threshold = spectrum->threshold;
930 gfloat *input = cd->input;
931 gfloat *input_tmp = cd->input_tmp;
932 gfloat *spect_magnitude = cd->spect_magnitude;
933 gfloat *spect_phase = cd->spect_phase;
934 GstFFTF32Complex *freqdata = cd->freqdata;
935 GstFFTF32 *fft_ctx = cd->fft_ctx;
937 for (i = 0; i < nfft; i++)
938 input_tmp[i] = input[(input_pos + i) % nfft];
940 gst_fft_f32_window (fft_ctx, input_tmp, GST_FFT_WINDOW_HAMMING);
942 gst_fft_f32_fft (fft_ctx, input_tmp, freqdata);
944 if (spectrum->message_magnitude) {
946 /* Calculate magnitude in db */
947 for (i = 0; i < bands; i++) {
948 val = freqdata[i].r * freqdata[i].r;
949 val += freqdata[i].i * freqdata[i].i;
951 val = 10.0 * log10 (val);
954 spect_magnitude[i] += val;
958 if (spectrum->message_phase) {
959 /* Calculate phase */
960 for (i = 0; i < bands; i++)
961 spect_phase[i] += atan2 (freqdata[i].i, freqdata[i].r);
966 gst_spectrum_prepare_message_data (GstSpectrum * spectrum,
967 GstSpectrumChannel * cd)
970 guint bands = spectrum->bands;
971 guint num_fft = spectrum->num_fft;
973 /* Calculate average */
974 if (spectrum->message_magnitude) {
975 gfloat *spect_magnitude = cd->spect_magnitude;
976 for (i = 0; i < bands; i++)
977 spect_magnitude[i] /= num_fft;
979 if (spectrum->message_phase) {
980 gfloat *spect_phase = cd->spect_phase;
981 for (i = 0; i < bands; i++)
982 spect_phase[i] /= num_fft;
987 gst_spectrum_reset_message_data (GstSpectrum * spectrum,
988 GstSpectrumChannel * cd)
990 guint bands = spectrum->bands;
991 gfloat *spect_magnitude = cd->spect_magnitude;
992 gfloat *spect_phase = cd->spect_phase;
994 /* reset spectrum accumulators */
995 memset (spect_magnitude, 0, bands * sizeof (gfloat));
996 memset (spect_phase, 0, bands * sizeof (gfloat));
1000 gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
1002 GstSpectrum *spectrum = GST_SPECTRUM (trans);
1003 GstRingBufferSpec *format = &GST_AUDIO_FILTER (spectrum)->format;
1004 guint rate = format->rate;
1005 guint channels = format->channels;
1006 guint output_channels = spectrum->multi_channel ? channels : 1;
1008 guint width = format->width / 8;
1009 gfloat max_value = (1UL << (format->depth - 1)) - 1;
1010 guint bands = spectrum->bands;
1011 guint nfft = 2 * bands - 2;
1014 const guint8 *data = GST_BUFFER_DATA (buffer);
1015 guint size = GST_BUFFER_SIZE (buffer);
1016 guint frame_size = width * channels;
1017 guint fft_todo, msg_todo, block_size;
1018 gboolean have_full_interval;
1019 GstSpectrumChannel *cd;
1020 GstSpectrumInputData input_data;
1022 GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (buffer));
1024 if (GST_BUFFER_IS_DISCONT (buffer)) {
1025 GST_DEBUG_OBJECT (spectrum, "Discontinuity detected -- flushing");
1026 gst_spectrum_flush (spectrum);
1029 /* If we don't have a FFT context yet (or it was reset due to parameter
1030 * changes) get one and allocate memory for everything
1032 if (spectrum->channel_data == NULL) {
1033 GST_DEBUG_OBJECT (spectrum, "allocating for bands %u", bands);
1035 gst_spectrum_alloc_channel_data (spectrum);
1037 /* number of sample frames we process before posting a message
1038 * interval is in ns */
1039 spectrum->frames_per_interval =
1040 gst_util_uint64_scale (spectrum->interval, rate, GST_SECOND);
1041 spectrum->frames_todo = spectrum->frames_per_interval;
1042 /* rounding error for frames_per_interval in ns,
1043 * aggregated it in accumulated_error */
1044 spectrum->error_per_interval = (spectrum->interval * rate) % GST_SECOND;
1045 if (spectrum->frames_per_interval == 0)
1046 spectrum->frames_per_interval = 1;
1048 GST_INFO_OBJECT (spectrum, "interval %" GST_TIME_FORMAT ", fpi %"
1049 G_GUINT64_FORMAT ", error %" GST_TIME_FORMAT,
1050 GST_TIME_ARGS (spectrum->interval), spectrum->frames_per_interval,
1051 GST_TIME_ARGS (spectrum->error_per_interval));
1053 spectrum->input_pos = 0;
1055 gst_spectrum_flush (spectrum);
1058 if (spectrum->num_frames == 0)
1059 spectrum->message_ts = GST_BUFFER_TIMESTAMP (buffer);
1061 input_pos = spectrum->input_pos;
1062 input_data = spectrum->input_data;
1064 while (size >= frame_size) {
1065 /* run input_data for a chunk of data */
1066 fft_todo = nfft - (spectrum->num_frames % nfft);
1067 msg_todo = spectrum->frames_todo - spectrum->num_frames;
1068 GST_LOG_OBJECT (spectrum,
1069 "message frames todo: %u, fft frames todo: %u, input frames %u",
1070 msg_todo, fft_todo, (size / frame_size));
1071 block_size = msg_todo;
1072 if (block_size > (size / frame_size))
1073 block_size = (size / frame_size);
1074 if (block_size > fft_todo)
1075 block_size = fft_todo;
1077 for (c = 0; c < output_channels; c++) {
1078 cd = &spectrum->channel_data[c];
1080 /* Move the current frames into our ringbuffers */
1081 input_data (data + c * width, input, block_size, channels, max_value,
1084 data += block_size * frame_size;
1085 size -= block_size * frame_size;
1086 input_pos = (input_pos + block_size) % nfft;
1087 spectrum->num_frames += block_size;
1089 have_full_interval = (spectrum->num_frames == spectrum->frames_todo);
1091 GST_LOG_OBJECT (spectrum, "size: %u, do-fft = %d, do-message = %d", size,
1092 (spectrum->num_frames % nfft == 0), have_full_interval);
1094 /* If we have enough frames for an FFT or we have all frames required for
1095 * the interval and we haven't run a FFT, then run an FFT */
1096 if ((spectrum->num_frames % nfft == 0) ||
1097 (have_full_interval && !spectrum->num_fft)) {
1098 for (c = 0; c < output_channels; c++) {
1099 cd = &spectrum->channel_data[c];
1100 gst_spectrum_run_fft (spectrum, cd, input_pos);
1102 spectrum->num_fft++;
1105 /* Do we have the FFTs for one interval? */
1106 if (have_full_interval) {
1107 GST_DEBUG_OBJECT (spectrum, "nfft: %u frames: %" G_GUINT64_FORMAT
1108 " fpi: %" G_GUINT64_FORMAT " error: %" GST_TIME_FORMAT, nfft,
1109 spectrum->num_frames, spectrum->frames_per_interval,
1110 GST_TIME_ARGS (spectrum->accumulated_error));
1112 spectrum->frames_todo = spectrum->frames_per_interval;
1113 if (spectrum->accumulated_error >= GST_SECOND) {
1114 spectrum->accumulated_error -= GST_SECOND;
1115 spectrum->frames_todo++;
1117 spectrum->accumulated_error += spectrum->error_per_interval;
1119 if (spectrum->post_messages) {
1122 for (c = 0; c < output_channels; c++) {
1123 cd = &spectrum->channel_data[c];
1124 gst_spectrum_prepare_message_data (spectrum, cd);
1127 m = gst_spectrum_message_new (spectrum, spectrum->message_ts,
1128 spectrum->interval);
1130 gst_element_post_message (GST_ELEMENT (spectrum), m);
1133 if (GST_CLOCK_TIME_IS_VALID (spectrum->message_ts))
1134 spectrum->message_ts +=
1135 gst_util_uint64_scale (spectrum->num_frames, GST_SECOND, rate);
1137 for (c = 0; c < channels; c++) {
1138 cd = &spectrum->channel_data[c];
1139 gst_spectrum_reset_message_data (spectrum, cd);
1141 spectrum->num_frames = 0;
1142 spectrum->num_fft = 0;
1146 spectrum->input_pos = input_pos;
1148 g_assert (size == 0);
1154 plugin_init (GstPlugin * plugin)
1156 return gst_element_register (plugin, "spectrum", GST_RANK_NONE,
1160 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1163 "Run an FFT on the audio signal, output spectrum data",
1164 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)