2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2006> 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;
300 guint channels = (spectrum->multi_channel) ?
301 GST_AUDIO_FILTER (spectrum)->format.channels : 1;
303 GST_DEBUG_OBJECT (spectrum, "allocating data for %d channels", channels);
305 spectrum->channel_data = g_new (GstSpectrumChannel, channels);
306 for (i = 0; i < channels; i++) {
307 cd = &spectrum->channel_data[i];
308 cd->fft_ctx = gst_fft_f32_new (nfft, FALSE);
309 cd->input = g_new0 (gfloat, nfft);
310 cd->input_tmp = g_new0 (gfloat, nfft);
311 cd->freqdata = g_new0 (GstFFTF32Complex, bands);
312 cd->spect_magnitude = g_new0 (gfloat, bands);
313 cd->spect_phase = g_new0 (gfloat, bands);
318 gst_spectrum_free_channel_data (GstSpectrum * spectrum)
321 GstSpectrumChannel *cd;
322 guint channels = (spectrum->multi_channel) ?
323 GST_AUDIO_FILTER (spectrum)->format.channels : 1;
325 GST_DEBUG_OBJECT (spectrum, "freeing data for %d channels", channels);
327 if (spectrum->channel_data) {
328 for (i = 0; i < channels; i++) {
329 cd = &spectrum->channel_data[i];
331 gst_fft_f32_free (cd->fft_ctx);
333 g_free (cd->input_tmp);
334 g_free (cd->freqdata);
335 g_free (cd->spect_magnitude);
336 g_free (cd->spect_phase);
338 g_free (spectrum->channel_data);
339 spectrum->channel_data = NULL;
344 gst_spectrum_flush (GstSpectrum * spectrum)
346 spectrum->num_frames = 0;
347 spectrum->num_fft = 0;
349 spectrum->accumulated_error = 0;
353 gst_spectrum_reset_state (GstSpectrum * spectrum)
355 GST_DEBUG_OBJECT (spectrum, "resetting state");
357 gst_spectrum_free_channel_data (spectrum);
358 gst_spectrum_flush (spectrum);
362 gst_spectrum_finalize (GObject * object)
364 GstSpectrum *spectrum = GST_SPECTRUM (object);
366 gst_spectrum_reset_state (spectrum);
368 G_OBJECT_CLASS (parent_class)->finalize (object);
372 gst_spectrum_set_property (GObject * object, guint prop_id,
373 const GValue * value, GParamSpec * pspec)
375 GstSpectrum *filter = GST_SPECTRUM (object);
379 case PROP_POST_MESSAGES:
380 filter->post_messages = g_value_get_boolean (value);
382 case PROP_MESSAGE_MAGNITUDE:
383 filter->message_magnitude = g_value_get_boolean (value);
385 case PROP_MESSAGE_PHASE:
386 filter->message_phase = g_value_get_boolean (value);
389 guint64 interval = g_value_get_uint64 (value);
390 if (filter->interval != interval) {
391 GST_BASE_TRANSFORM_LOCK (filter);
392 filter->interval = interval;
393 gst_spectrum_reset_state (filter);
394 GST_BASE_TRANSFORM_UNLOCK (filter);
399 guint bands = g_value_get_uint (value);
400 if (filter->bands != bands) {
401 GST_BASE_TRANSFORM_LOCK (filter);
402 filter->bands = bands;
403 gst_spectrum_reset_state (filter);
404 GST_BASE_TRANSFORM_UNLOCK (filter);
409 filter->threshold = g_value_get_int (value);
411 case PROP_MULTI_CHANNEL:{
412 gboolean multi_channel = g_value_get_boolean (value);
413 if (filter->multi_channel != multi_channel) {
414 GST_BASE_TRANSFORM_LOCK (filter);
415 filter->multi_channel = multi_channel;
416 gst_spectrum_reset_state (filter);
417 GST_BASE_TRANSFORM_UNLOCK (filter);
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
428 gst_spectrum_get_property (GObject * object, guint prop_id,
429 GValue * value, GParamSpec * pspec)
431 GstSpectrum *filter = GST_SPECTRUM (object);
435 case PROP_POST_MESSAGES:
436 g_value_set_boolean (value, filter->post_messages);
438 case PROP_MESSAGE_MAGNITUDE:
439 g_value_set_boolean (value, filter->message_magnitude);
441 case PROP_MESSAGE_PHASE:
442 g_value_set_boolean (value, filter->message_phase);
445 g_value_set_uint64 (value, filter->interval);
448 g_value_set_uint (value, filter->bands);
451 g_value_set_int (value, filter->threshold);
453 case PROP_MULTI_CHANNEL:
454 g_value_set_boolean (value, filter->multi_channel);
457 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
463 gst_spectrum_start (GstBaseTransform * trans)
465 GstSpectrum *spectrum = GST_SPECTRUM (trans);
467 gst_spectrum_reset_state (spectrum);
473 gst_spectrum_stop (GstBaseTransform * trans)
475 GstSpectrum *spectrum = GST_SPECTRUM (trans);
477 gst_spectrum_reset_state (spectrum);
482 /* mixing data readers */
485 input_data_mixed_float (const guint8 * data, guint channels, gfloat max_value)
489 gfloat *in = (gfloat *) data;
491 for (i = 0; i < channels; i++)
498 input_data_mixed_double (const guint8 * data, guint channels, gfloat max_value)
502 gdouble *in = (gdouble *) data;
504 for (i = 0; i < channels; i++)
511 input_data_mixed_int32 (const guint8 * data, guint channels, gfloat max_value)
515 gint32 *in = (gint32 *) data;
517 for (i = 0; i < channels; i++)
524 input_data_mixed_int32_max (const guint8 * data, guint channels,
529 gint32 *in = (gint32 *) data;
531 for (i = 0; i < channels; i++)
532 v += in[i] / max_value;
538 input_data_mixed_int24 (const guint8 * data, guint channels, gfloat max_value)
543 for (i = 0; i < channels; i++) {
544 #if G_BYTE_ORDER == G_BIG_ENDIAN
545 gint32 value = GST_READ_UINT24_BE (data);
547 gint32 value = GST_READ_UINT24_LE (data);
549 if (value & 0x00800000)
558 input_data_mixed_int24_max (const guint8 * data, guint channels,
564 for (i = 0; i < channels; i++) {
565 #if G_BYTE_ORDER == G_BIG_ENDIAN
566 gint32 value = GST_READ_UINT24_BE (data);
568 gint32 value = GST_READ_UINT24_LE (data);
570 if (value & 0x00800000)
572 v += value / max_value;
579 input_data_mixed_int16 (const guint8 * data, guint channels, gfloat max_value)
583 gint16 *in = (gint16 *) data;
585 for (i = 0; i < channels; i++)
592 input_data_mixed_int16_max (const guint8 * data, guint channels,
597 gint16 *in = (gint16 *) data;
599 for (i = 0; i < channels; i++)
600 v += in[i] / max_value;
605 /* non mixing data readers */
608 input_data_float (const guint8 * data, gfloat max_value)
610 return ((gfloat *) data)[0];
614 input_data_double (const guint8 * data, gfloat max_value)
616 return (gfloat) ((gdouble *) data)[0];
620 input_data_int32 (const guint8 * data, gfloat max_value)
622 return ((gint32 *) data)[0] * 2 + 1;
626 input_data_int32_max (const guint8 * data, gfloat max_value)
628 return ((gint32 *) data)[0] / max_value;
632 input_data_int24 (const guint8 * data, gfloat max_value)
634 #if G_BYTE_ORDER == G_BIG_ENDIAN
635 gint32 in = GST_READ_UINT24_BE (data);
637 gint32 in = GST_READ_UINT24_LE (data);
645 input_data_int24_max (const guint8 * data, gfloat max_value)
647 #if G_BYTE_ORDER == G_BIG_ENDIAN
648 gint32 in = GST_READ_UINT24_BE (data);
650 gint32 in = GST_READ_UINT24_LE (data);
654 return in / max_value;
658 input_data_int16 (const guint8 * data, gfloat max_value)
660 return ((gint16 *) data)[0] * 2 + 1;
664 input_data_int16_max (const guint8 * data, gfloat max_value)
666 return ((gint16 *) data)[0] / max_value;
670 gst_spectrum_setup (GstAudioFilter * base, GstRingBufferSpec * format)
672 GstSpectrum *spectrum = GST_SPECTRUM (base);
673 guint width = format->width / 8;
674 gboolean is_float = (format->type == GST_BUFTYPE_FLOAT);
675 /* max_value will be 0 when depth is 1,
676 * interpret -1 and 0 as -1 and +1 if that's the case. */
677 gfloat max_value = (1UL << (format->depth - 1)) - 1;
679 spectrum->input_data_mixed = NULL;
680 spectrum->input_data = NULL;
684 spectrum->input_data_mixed = input_data_mixed_float;
685 spectrum->input_data = input_data_float;
686 } else if (width == 8) {
687 spectrum->input_data_mixed = input_data_mixed_double;
688 spectrum->input_data = input_data_double;
690 g_assert_not_reached ();
695 spectrum->input_data_mixed = input_data_mixed_int32_max;
696 spectrum->input_data = input_data_int32_max;
698 spectrum->input_data_mixed = input_data_mixed_int32;
699 spectrum->input_data = input_data_int32;
701 } else if (width == 3) {
703 spectrum->input_data_mixed = input_data_mixed_int24_max;
704 spectrum->input_data = input_data_int24_max;
706 spectrum->input_data_mixed = input_data_mixed_int24;
707 spectrum->input_data = input_data_int24;
709 } else if (width == 2) {
711 spectrum->input_data_mixed = input_data_mixed_int16_max;
712 spectrum->input_data = input_data_int16_max;
714 spectrum->input_data_mixed = input_data_mixed_int16;
715 spectrum->input_data = input_data_int16;
718 g_assert_not_reached ();
722 gst_spectrum_reset_state (spectrum);
727 gst_spectrum_message_add_container (GstStructure * s, GType type,
732 g_value_init (&v, type);
733 /* will copy-by-value */
734 gst_structure_set_value (s, name, &v);
736 return (GValue *) gst_structure_get_value (s, name);
740 gst_spectrum_message_add_list (GValue * cv, gfloat * data, guint num_values)
745 g_value_init (&v, G_TYPE_FLOAT);
746 for (i = 0; i < num_values; i++) {
747 g_value_set_float (&v, data[i]);
748 gst_value_list_append_value (cv, &v); /* copies by value */
754 gst_spectrum_message_add_array (GValue * cv, gfloat * data, guint num_values)
760 g_value_init (&a, GST_TYPE_ARRAY);
762 g_value_init (&v, G_TYPE_FLOAT);
763 for (i = 0; i < num_values; i++) {
764 g_value_set_float (&v, data[i]);
765 gst_value_array_append_value (&a, &v); /* copies by value */
769 gst_value_array_append_value (cv, &a); /* copies by value */
774 gst_spectrum_message_new (GstSpectrum * spectrum, GstClockTime timestamp,
775 GstClockTime duration)
777 GstBaseTransform *trans = GST_BASE_TRANSFORM_CAST (spectrum);
778 GstSpectrumChannel *cd;
780 GValue *mcv = NULL, *pcv = NULL;
781 GstClockTime endtime, running_time, stream_time;
783 GST_DEBUG_OBJECT (spectrum, "preparing message, bands =%d ", spectrum->bands);
785 running_time = gst_segment_to_running_time (&trans->segment, GST_FORMAT_TIME,
787 stream_time = gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME,
789 /* endtime is for backwards compatibility */
790 endtime = stream_time + duration;
792 s = gst_structure_new ("spectrum",
793 "endtime", GST_TYPE_CLOCK_TIME, endtime,
794 "timestamp", G_TYPE_UINT64, timestamp,
795 "stream-time", G_TYPE_UINT64, stream_time,
796 "running-time", G_TYPE_UINT64, running_time,
797 "duration", G_TYPE_UINT64, duration, NULL);
799 if (!spectrum->multi_channel) {
800 cd = &spectrum->channel_data[0];
802 if (spectrum->message_magnitude) {
803 /* FIXME 0.11: this should be an array, not a list */
804 mcv = gst_spectrum_message_add_container (s, GST_TYPE_LIST, "magnitude");
805 gst_spectrum_message_add_list (mcv, cd->spect_magnitude, spectrum->bands);
807 if (spectrum->message_phase) {
808 /* FIXME 0.11: this should be an array, not a list */
809 pcv = gst_spectrum_message_add_container (s, GST_TYPE_LIST, "phase");
810 gst_spectrum_message_add_list (pcv, cd->spect_phase, spectrum->bands);
814 guint channels = GST_AUDIO_FILTER (spectrum)->format.channels;
816 if (spectrum->message_magnitude) {
817 mcv = gst_spectrum_message_add_container (s, GST_TYPE_ARRAY, "magnitude");
819 if (spectrum->message_phase) {
820 pcv = gst_spectrum_message_add_container (s, GST_TYPE_ARRAY, "phase");
823 for (c = 0; c < channels; c++) {
824 cd = &spectrum->channel_data[c];
826 if (spectrum->message_magnitude) {
827 gst_spectrum_message_add_array (mcv, cd->spect_magnitude,
830 if (spectrum->message_phase) {
831 gst_spectrum_message_add_array (pcv, cd->spect_magnitude,
836 return gst_message_new_element (GST_OBJECT (spectrum), s);
840 gst_spectrum_run_fft (GstSpectrum * spectrum, GstSpectrumChannel * cd,
844 guint bands = spectrum->bands;
845 guint nfft = 2 * bands - 2;
846 gint threshold = spectrum->threshold;
847 gfloat *input = cd->input;
848 gfloat *input_tmp = cd->input_tmp;
849 gfloat *spect_magnitude = cd->spect_magnitude;
850 gfloat *spect_phase = cd->spect_phase;
851 GstFFTF32Complex *freqdata = cd->freqdata;
852 GstFFTF32 *fft_ctx = cd->fft_ctx;
854 for (i = 0; i < nfft; i++)
855 input_tmp[i] = input[(input_pos + i) % nfft];
857 gst_fft_f32_window (fft_ctx, input_tmp, GST_FFT_WINDOW_HAMMING);
859 gst_fft_f32_fft (fft_ctx, input_tmp, freqdata);
861 if (spectrum->message_magnitude) {
863 /* Calculate magnitude in db */
864 for (i = 0; i < bands; i++) {
865 val = freqdata[i].r * freqdata[i].r;
866 val += freqdata[i].i * freqdata[i].i;
868 val = 10.0 * log10 (val);
871 spect_magnitude[i] += val;
875 if (spectrum->message_phase) {
876 /* Calculate phase */
877 for (i = 0; i < bands; i++)
878 spect_phase[i] += atan2 (freqdata[i].i, freqdata[i].r);
883 gst_spectrum_prepare_message_data (GstSpectrum * spectrum,
884 GstSpectrumChannel * cd)
887 guint bands = spectrum->bands;
888 guint num_fft = spectrum->num_fft;
889 gfloat *spect_magnitude = cd->spect_magnitude;
890 gfloat *spect_phase = cd->spect_phase;
892 /* Calculate average */
893 for (i = 0; i < bands; i++) {
894 spect_magnitude[i] /= num_fft;
895 spect_phase[i] /= num_fft;
900 gst_spectrum_reset_message_data (GstSpectrum * spectrum,
901 GstSpectrumChannel * cd)
903 guint bands = spectrum->bands;
904 gfloat *spect_magnitude = cd->spect_magnitude;
905 gfloat *spect_phase = cd->spect_phase;
907 /* reset spectrum accumulators */
908 memset (spect_magnitude, 0, bands * sizeof (gfloat));
909 memset (spect_phase, 0, bands * sizeof (gfloat));
913 gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
915 GstSpectrum *spectrum = GST_SPECTRUM (trans);
916 GstRingBufferSpec *format = &GST_AUDIO_FILTER (spectrum)->format;
917 guint rate = format->rate;
918 guint channels = format->channels;
919 guint width = format->width / 8;
920 gfloat max_value = (1UL << (format->depth - 1)) - 1;
921 guint bands = spectrum->bands;
922 guint nfft = 2 * bands - 2;
925 const guint8 *data = GST_BUFFER_DATA (buffer);
926 guint size = GST_BUFFER_SIZE (buffer);
927 gboolean have_full_interval;
928 GstSpectrumChannel *cd;
930 GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (buffer));
932 if (GST_BUFFER_IS_DISCONT (buffer)) {
933 GST_DEBUG_OBJECT (spectrum, "Discontinuity detected -- flushing");
934 gst_spectrum_flush (spectrum);
937 /* If we don't have a FFT context yet (or it was reset due to parameter
938 * changes) get one and allocate memory for everything
940 if (spectrum->channel_data == NULL) {
941 GST_DEBUG_OBJECT (spectrum, "allocating for bands %u", bands);
943 gst_spectrum_alloc_channel_data (spectrum);
945 spectrum->frames_per_interval =
946 gst_util_uint64_scale (spectrum->interval, rate, GST_SECOND);
947 spectrum->error_per_interval = (spectrum->interval * rate) % GST_SECOND;
948 if (spectrum->frames_per_interval == 0)
949 spectrum->frames_per_interval = 1;
951 spectrum->input_pos = 0;
953 gst_spectrum_flush (spectrum);
956 if (spectrum->num_frames == 0)
957 spectrum->message_ts = GST_BUFFER_TIMESTAMP (buffer);
959 input_pos = spectrum->input_pos;
961 if (!spectrum->multi_channel) {
962 cd = &spectrum->channel_data[0];
965 while (size >= width * channels) {
966 /* Move the mixdown of current frame into our ringbuffer */
967 input[input_pos] = spectrum->input_data_mixed (data, channels, max_value);
969 data += width * channels;
970 size -= width * channels;
971 input_pos = (input_pos + 1) % nfft;
972 spectrum->num_frames++;
974 have_full_interval = (
975 (spectrum->accumulated_error < GST_SECOND
976 && spectrum->num_frames == spectrum->frames_per_interval) ||
977 (spectrum->accumulated_error >= GST_SECOND
978 && spectrum->num_frames - 1 == spectrum->frames_per_interval)
981 /* If we have enough frames for an FFT or we have all frames required for
982 * the interval run an FFT. In the last case we probably take the
983 * FFT of frames that we already handled.
985 if ((spectrum->num_frames % nfft == 0) || have_full_interval) {
986 gst_spectrum_run_fft (spectrum, cd, input_pos);
990 /* Do we have the FFTs for one interval? */
991 if (have_full_interval) {
993 GST_INFO ("nfft: %u num_frames: %" G_GUINT64_FORMAT " fpi: %"
994 G_GUINT64_FORMAT " error: %" GST_TIME_FORMAT, nfft,
995 spectrum->num_frames, spectrum->frames_per_interval,
996 GST_TIME_ARGS (spectrum->accumulated_error));
998 if (spectrum->accumulated_error >= GST_SECOND)
999 spectrum->accumulated_error -= GST_SECOND;
1001 spectrum->accumulated_error += spectrum->error_per_interval;
1003 if (spectrum->post_messages) {
1006 gst_spectrum_prepare_message_data (spectrum, cd);
1008 m = gst_spectrum_message_new (spectrum, spectrum->message_ts,
1009 spectrum->interval);
1011 gst_element_post_message (GST_ELEMENT (spectrum), m);
1014 if (GST_CLOCK_TIME_IS_VALID (spectrum->message_ts))
1015 spectrum->message_ts +=
1016 gst_util_uint64_scale (spectrum->num_frames, GST_SECOND, rate);
1018 gst_spectrum_reset_message_data (spectrum, cd);
1019 spectrum->num_frames = 0;
1020 spectrum->num_fft = 0;
1026 while (size >= width * channels) {
1027 for (c = 0; c < channels; c++) {
1028 cd = &spectrum->channel_data[c];
1030 /* Move the current frames into our ringbuffers */
1031 input[input_pos] = spectrum->input_data (data, max_value);
1034 size -= width * channels;
1035 input_pos = (input_pos + 1) % nfft;
1036 spectrum->num_frames++;
1038 have_full_interval = (
1039 (spectrum->accumulated_error < GST_SECOND
1040 && spectrum->num_frames == spectrum->frames_per_interval) ||
1041 (spectrum->accumulated_error >= GST_SECOND
1042 && spectrum->num_frames - 1 == spectrum->frames_per_interval)
1045 /* If we have enough frames for an FFT or we have all frames required for
1046 * the interval run an FFT. In the last case we probably take the
1047 * FFT of frames that we already handled.
1049 if ((spectrum->num_frames % nfft == 0) || have_full_interval) {
1050 for (c = 0; c < channels; c++) {
1051 cd = &spectrum->channel_data[c];
1052 gst_spectrum_run_fft (spectrum, cd, input_pos);
1054 spectrum->num_fft++;
1057 /* Do we have the FFTs for one interval? */
1058 if (have_full_interval) {
1060 GST_INFO ("nfft: %u num_frames: %" G_GUINT64_FORMAT " fpi: %"
1061 G_GUINT64_FORMAT " error: %" GST_TIME_FORMAT, nfft,
1062 spectrum->num_frames, spectrum->frames_per_interval,
1063 GST_TIME_ARGS (spectrum->accumulated_error));
1065 if (spectrum->accumulated_error >= GST_SECOND)
1066 spectrum->accumulated_error -= GST_SECOND;
1068 spectrum->accumulated_error += spectrum->error_per_interval;
1070 if (spectrum->post_messages) {
1073 for (c = 0; c < channels; c++) {
1074 cd = &spectrum->channel_data[c];
1075 gst_spectrum_prepare_message_data (spectrum, cd);
1078 m = gst_spectrum_message_new (spectrum, spectrum->message_ts,
1079 spectrum->interval);
1081 gst_element_post_message (GST_ELEMENT (spectrum), m);
1084 if (GST_CLOCK_TIME_IS_VALID (spectrum->message_ts))
1085 spectrum->message_ts +=
1086 gst_util_uint64_scale (spectrum->num_frames, GST_SECOND, rate);
1088 for (c = 0; c < channels; c++) {
1089 cd = &spectrum->channel_data[c];
1090 gst_spectrum_reset_message_data (spectrum, cd);
1092 spectrum->num_frames = 0;
1093 spectrum->num_fft = 0;
1098 spectrum->input_pos = input_pos;
1100 g_assert (size == 0);
1106 plugin_init (GstPlugin * plugin)
1108 return gst_element_register (plugin, "spectrum", GST_RANK_NONE,
1112 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1115 "Run an FFT on the audio signal, output spectrum data",
1116 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)