gst/spectrum/: Post a spectrum message on the bus for every interval, even if the interval is small than the length o...

Original commit message from CVS:
* gst/spectrum/Makefile.am:
* gst/spectrum/README:
* gst/spectrum/gstspectrum.c: (gst_spectrum_base_init),
(gst_spectrum_class_init), (gst_spectrum_init),
(gst_spectrum_reset_state), (gst_spectrum_finalize),
(gst_spectrum_set_property), (gst_spectrum_start),
(gst_spectrum_stop), (gst_spectrum_setup),
(gst_spectrum_transform_ip):
* gst/spectrum/gstspectrum.h:
Post a spectrum message on the bus for every interval, even
if the interval is small than the length of the FFT.
Fixes bug #567642.
Major cleanup of the spectrum element.
This commit is contained in:
Sebastian Dröge 2009-01-14 10:46:54 +00:00
parent 75c1c9f378
commit 5d001b3ab6
4 changed files with 212 additions and 320 deletions

View file

@ -1,3 +1,20 @@
2009-01-14 Sebastian Dröge <sebastian.droege@collabora.co.uk>
* gst/spectrum/Makefile.am:
* gst/spectrum/README:
* gst/spectrum/gstspectrum.c: (gst_spectrum_base_init),
(gst_spectrum_class_init), (gst_spectrum_init),
(gst_spectrum_reset_state), (gst_spectrum_finalize),
(gst_spectrum_set_property), (gst_spectrum_start),
(gst_spectrum_stop), (gst_spectrum_setup),
(gst_spectrum_transform_ip):
* gst/spectrum/gstspectrum.h:
Post a spectrum message on the bus for every interval, even
if the interval is small than the length of the FFT.
Fixes bug #567642.
Major cleanup of the spectrum element.
2009-01-13 Sebastian Dröge <sebastian.droege@collabora.co.uk>
* configure.ac:

View file

@ -1,4 +1,3 @@
plugin_LTLIBRARIES = libgstspectrum.la
libgstspectrum_la_SOURCES = gstspectrum.c
@ -11,5 +10,3 @@ libgstspectrum_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstspectrum_la_LIBTOOLFLAGS = --tag=disable-static
noinst_HEADERS = gstspectrum.h
EXTRA_DIST = README

View file

@ -1,7 +1,7 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* <2006> Stefan Kost <ensonic@users.sf.net>
* <2007> Sebastian Dröge <slomo@circular-chaos.org>
* <2007-2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -97,7 +97,7 @@
* <include xmlns="http://www.w3.org/2003/XInclude" href="element-spectrum-example.xml" />
* </para>
* <para>
* Last reviewed on 2008-02-09 (0.10.6)
* Last reviewed on 2009-01-14 (0.10.12)
* </para>
* </refsect2>
*/
@ -105,30 +105,15 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <gst/audio/audio.h>
#include <gst/audio/gstaudiofilter.h>
#include <math.h>
#include "gstspectrum.h"
#include <gst/fft/gstfft.h>
#include <gst/fft/gstffts16.h>
#include <gst/fft/gstffts32.h>
#include <gst/fft/gstfftf32.h>
#include <gst/fft/gstfftf64.h>
GST_DEBUG_CATEGORY_STATIC (gst_spectrum_debug);
#define GST_CAT_DEFAULT gst_spectrum_debug
/* elementfactory information */
static const GstElementDetails gst_spectrum_details =
GST_ELEMENT_DETAILS ("Spectrum analyzer",
"Filter/Analyzer/Audio",
"Run an FFT on the audio signal, output spectrum data",
"Erik Walthinsen <omega@cse.ogi.edu>, "
"Stefan Kost <ensonic@users.sf.net>, "
"Sebastian Dröge <slomo@circular-chaos.org>");
#define ALLOWED_CAPS \
"audio/x-raw-int, " \
@ -159,9 +144,6 @@ GST_ELEMENT_DETAILS ("Spectrum analyzer",
#define DEFAULT_BANDS 128
#define DEFAULT_THRESHOLD -60
#define SPECTRUM_WINDOW_BASE 9
#define SPECTRUM_WINDOW_LEN (1 << (SPECTRUM_WINDOW_BASE+1))
enum
{
PROP_0,
@ -176,7 +158,6 @@ enum
GST_BOILERPLATE (GstSpectrum, gst_spectrum, GstAudioFilter,
GST_TYPE_AUDIO_FILTER);
static void gst_spectrum_dispose (GObject * object);
static void gst_spectrum_finalize (GObject * object);
static void gst_spectrum_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
@ -184,24 +165,23 @@ static void gst_spectrum_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_spectrum_start (GstBaseTransform * trans);
static gboolean gst_spectrum_stop (GstBaseTransform * trans);
static gboolean gst_spectrum_event (GstBaseTransform * trans, GstEvent * event);
static GstFlowReturn gst_spectrum_transform_ip (GstBaseTransform * trans,
GstBuffer * in);
static gboolean gst_spectrum_setup (GstAudioFilter * base,
GstRingBufferSpec * format);
static void process_s16 (GstSpectrum * spectrum, const gint16 * samples);
static void process_s32 (GstSpectrum * spectrum, const gint32 * samples);
static void process_f32 (GstSpectrum * spectrum, const gfloat * samples);
static void process_f64 (GstSpectrum * spectrum, const gdouble * samples);
static void
gst_spectrum_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
GstCaps *caps;
gst_element_class_set_details (element_class, &gst_spectrum_details);
gst_element_class_set_details_simple (element_class, "Spectrum analyzer",
"Filter/Analyzer/Audio",
"Run an FFT on the audio signal, output spectrum data",
"Erik Walthinsen <omega@cse.ogi.edu>, "
"Stefan Kost <ensonic@users.sf.net>, "
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
caps = gst_caps_from_string (ALLOWED_CAPS);
gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
@ -218,12 +198,10 @@ gst_spectrum_class_init (GstSpectrumClass * klass)
gobject_class->set_property = gst_spectrum_set_property;
gobject_class->get_property = gst_spectrum_get_property;
gobject_class->dispose = gst_spectrum_dispose;
gobject_class->finalize = gst_spectrum_finalize;
trans_class->start = GST_DEBUG_FUNCPTR (gst_spectrum_start);
trans_class->stop = GST_DEBUG_FUNCPTR (gst_spectrum_stop);
trans_class->event = GST_DEBUG_FUNCPTR (gst_spectrum_event);
trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_spectrum_transform_ip);
trans_class->passthrough_on_same_caps = TRUE;
@ -232,33 +210,38 @@ gst_spectrum_class_init (GstSpectrumClass * klass)
g_object_class_install_property (gobject_class, PROP_MESSAGE,
g_param_spec_boolean ("message", "Message",
"Whether to post a 'spectrum' element message on the bus for each "
"passed interval", DEFAULT_MESSAGE, G_PARAM_READWRITE));
"passed interval", DEFAULT_MESSAGE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MESSAGE_MAGNITUDE,
g_param_spec_boolean ("message-magnitude", "Magnitude",
"Whether to add a 'magnitude' field to the structure of any "
"'spectrum' element messages posted on the bus",
DEFAULT_MESSAGE_MAGNITUDE, G_PARAM_READWRITE));
DEFAULT_MESSAGE_MAGNITUDE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_MESSAGE_PHASE,
g_param_spec_boolean ("message-phase", "Phase",
"Whether to add a 'phase' field to the structure of any "
"'spectrum' element messages posted on the bus",
DEFAULT_MESSAGE_PHASE, G_PARAM_READWRITE));
DEFAULT_MESSAGE_PHASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_INTERVAL,
g_param_spec_uint64 ("interval", "Interval",
"Interval of time between message posts (in nanoseconds)",
1, G_MAXUINT64, DEFAULT_INTERVAL, G_PARAM_READWRITE));
1, G_MAXUINT64, DEFAULT_INTERVAL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_BANDS,
g_param_spec_uint ("bands", "Bands", "Number of frequency bands",
0, G_MAXUINT, DEFAULT_BANDS, G_PARAM_READWRITE));
0, G_MAXUINT, DEFAULT_BANDS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class, PROP_THRESHOLD,
g_param_spec_int ("threshold", "Threshold",
"dB threshold for result. All lower values will be set to this",
G_MININT, 0, DEFAULT_THRESHOLD, G_PARAM_READWRITE));
G_MININT, 0, DEFAULT_THRESHOLD,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
GST_DEBUG_CATEGORY_INIT (gst_spectrum_debug, "spectrum", 0,
"audio spectrum analyser element");
@ -267,30 +250,33 @@ gst_spectrum_class_init (GstSpectrumClass * klass)
static void
gst_spectrum_init (GstSpectrum * spectrum, GstSpectrumClass * g_class)
{
spectrum->adapter = gst_adapter_new ();
spectrum->message = DEFAULT_MESSAGE;
spectrum->message_magnitude = DEFAULT_MESSAGE_MAGNITUDE;
spectrum->message_phase = DEFAULT_MESSAGE_PHASE;
spectrum->interval = DEFAULT_INTERVAL;
spectrum->bands = DEFAULT_BANDS;
spectrum->threshold = DEFAULT_THRESHOLD;
spectrum->spect_magnitude = g_new0 (gfloat, spectrum->bands);
spectrum->spect_phase = g_new0 (gfloat, spectrum->bands);
}
static void
gst_spectrum_dispose (GObject * object)
gst_spectrum_reset_state (GstSpectrum * spectrum)
{
GstSpectrum *spectrum = GST_SPECTRUM (object);
gst_fft_f32_free (spectrum->fft_ctx);
g_free (spectrum->input);
g_free (spectrum->input_tmp);
g_free (spectrum->freqdata);
g_free (spectrum->spect_magnitude);
g_free (spectrum->spect_phase);
if (spectrum->adapter) {
g_object_unref (spectrum->adapter);
spectrum->adapter = NULL;
}
spectrum->fft_ctx = NULL;
spectrum->input = NULL;
spectrum->input_tmp = NULL;
spectrum->spect_magnitude = NULL;
spectrum->spect_phase = NULL;
spectrum->freqdata = NULL;
G_OBJECT_CLASS (parent_class)->dispose (object);
spectrum->num_frames = 0;
spectrum->num_fft = 0;
}
static void
@ -298,20 +284,7 @@ gst_spectrum_finalize (GObject * object)
{
GstSpectrum *spectrum = GST_SPECTRUM (object);
g_free (spectrum->in);
if (spectrum->fft_free_func) {
spectrum->fft_free_func (spectrum->fft_ctx);
spectrum->fft_ctx = NULL;
spectrum->fft_free_func = NULL;
}
g_free (spectrum->freqdata);
g_free (spectrum->spect_magnitude);
g_free (spectrum->spect_phase);
spectrum->in = NULL;
spectrum->spect_magnitude = NULL;
spectrum->spect_phase = NULL;
spectrum->freqdata = NULL;
gst_spectrum_reset_state (spectrum);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
@ -333,35 +306,30 @@ gst_spectrum_set_property (GObject * object, guint prop_id,
filter->message_phase = g_value_get_boolean (value);
break;
case PROP_INTERVAL:
GST_BASE_TRANSFORM_LOCK (filter);
filter->interval = g_value_get_uint64 (value);
gst_spectrum_reset_state (filter);
GST_BASE_TRANSFORM_UNLOCK (filter);
break;
case PROP_BANDS:
GST_BASE_TRANSFORM_LOCK (filter);
filter->bands = g_value_get_uint (value);
g_free (filter->spect_magnitude);
g_free (filter->spect_phase);
g_free (filter->in);
g_free (filter->freqdata);
if (filter->fft_free_func) {
filter->fft_free_func (filter->fft_ctx);
filter->fft_ctx = NULL;
filter->fft_free_func = NULL;
if (filter->bands == g_value_get_uint (value)) {
GST_BASE_TRANSFORM_UNLOCK (filter);
break;
}
filter->in = NULL;
filter->freqdata = NULL;
filter->spect_magnitude = g_new0 (gfloat, filter->bands);
filter->spect_phase = g_new0 (gfloat, filter->bands);
filter->num_frames = 0;
filter->num_fft = 0;
filter->bands = g_value_get_uint (value);
gst_spectrum_reset_state (filter);
GST_BASE_TRANSFORM_UNLOCK (filter);
GST_DEBUG_OBJECT (filter, "reallocation, spect = %p, bands =%d ",
filter->spect_magnitude, filter->bands);
break;
case PROP_THRESHOLD:
GST_BASE_TRANSFORM_LOCK (filter);
filter->threshold = g_value_get_int (value);
gst_spectrum_reset_state (filter);
GST_BASE_TRANSFORM_UNLOCK (filter);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -407,10 +375,6 @@ gst_spectrum_start (GstBaseTransform * trans)
filter->num_frames = 0;
filter->num_fft = 0;
if (filter->spect_magnitude)
memset (filter->spect_magnitude, 0, filter->bands * sizeof (gfloat));
if (filter->spect_phase)
memset (filter->spect_phase, 0, filter->bands * sizeof (gfloat));
return TRUE;
}
@ -420,24 +384,7 @@ gst_spectrum_stop (GstBaseTransform * trans)
{
GstSpectrum *filter = GST_SPECTRUM (trans);
gst_adapter_clear (filter->adapter);
return TRUE;
}
static gboolean
gst_spectrum_event (GstBaseTransform * trans, GstEvent * event)
{
GstSpectrum *filter = GST_SPECTRUM (trans);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_STOP:
case GST_EVENT_EOS:
gst_adapter_clear (filter->adapter);
break;
default:
break;
}
gst_spectrum_reset_state (filter);
return TRUE;
}
@ -447,32 +394,7 @@ gst_spectrum_setup (GstAudioFilter * base, GstRingBufferSpec * format)
{
GstSpectrum *filter = GST_SPECTRUM (base);
if (filter->in) {
g_free (filter->in);
filter->in = NULL;
}
if (filter->fft_free_func) {
filter->fft_free_func (filter->fft_ctx);
filter->fft_ctx = NULL;
filter->fft_free_func = NULL;
}
if (filter->freqdata) {
g_free (filter->freqdata);
filter->freqdata = NULL;
}
if (format->type == GST_BUFTYPE_LINEAR && format->width == 32)
filter->process = (GstSpectrumProcessFunc) process_s32;
else if (format->type == GST_BUFTYPE_LINEAR && format->width == 16)
filter->process = (GstSpectrumProcessFunc) process_s16;
else if (format->type == GST_BUFTYPE_FLOAT && format->width == 64)
filter->process = (GstSpectrumProcessFunc) process_f64;
else if (format->type == GST_BUFTYPE_FLOAT && format->width == 32)
filter->process = (GstSpectrumProcessFunc) process_f32;
else
g_assert_not_reached ();
gst_spectrum_reset_state (filter);
return TRUE;
}
@ -542,191 +464,158 @@ gst_spectrum_message_new (GstSpectrum * spectrum, GstClockTime timestamp,
return gst_message_new_element (GST_OBJECT (spectrum), s);
}
#define DEFINE_PROCESS_FUNC_INT(width,next_width,max) \
static void \
process_s##width (GstSpectrum *spectrum, const gint##width *samples) \
{ \
gfloat *spect_magnitude = spectrum->spect_magnitude; \
gfloat *spect_phase = spectrum->spect_phase; \
gint channels = GST_AUDIO_FILTER (spectrum)->format.channels; \
gint i, j, k; \
gint##next_width acc; \
GstFFTS##width##Complex *freqdata; \
GstFFTS##width *ctx; \
gint##width *in; \
gint nfft = 2 * spectrum->bands - 2; \
\
if (!spectrum->in) \
spectrum->in = (guint8 *) g_new (gint##width, nfft); \
\
in = (gint##width *) spectrum->in; \
\
for (i = 0, j = 0; i < nfft; i++) { \
/* convert to mono */ \
for (k = 0, acc = 0; k < channels; k++) \
acc += samples[j++]; \
in[i] = (gint##width) (acc / channels); \
} \
\
if (!spectrum->fft_ctx) { \
spectrum->fft_ctx = gst_fft_s##width##_new (nfft, FALSE); \
spectrum->fft_free_func = (GstSpectrumFFTFreeFunc) gst_fft_s##width##_free; \
} \
ctx = spectrum->fft_ctx; \
\
gst_fft_s##width##_window (ctx, in, GST_FFT_WINDOW_HAMMING); \
\
if (!spectrum->freqdata) \
spectrum->freqdata = g_new (GstFFTS##width##Complex, spectrum->bands); \
\
freqdata = (GstFFTS##width##Complex *) spectrum->freqdata; \
\
gst_fft_s##width##_fft (ctx, in, freqdata); \
spectrum->num_fft++; \
\
/* Calculate magnitude in db */ \
for (i = 0; i < spectrum->bands; i++) { \
gdouble val = 0.0; \
val = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r; \
val += (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; \
val /= max*max; \
val = 10.0 * log10 (val); \
if (val < spectrum->threshold) \
val = spectrum->threshold; \
spect_magnitude[i] += val; \
} \
\
/* Calculate phase */ \
for (i = 0; i < spectrum->bands; i++) \
spect_phase[i] += atan2 (freqdata[i].i, freqdata[i].r); \
\
}
DEFINE_PROCESS_FUNC_INT (16, 32, 32767.0);
DEFINE_PROCESS_FUNC_INT (32, 64, 2147483647.0);
#define DEFINE_PROCESS_FUNC_FLOAT(width,type) \
static void \
process_f##width (GstSpectrum *spectrum, const g##type *samples) \
{ \
gfloat *spect_magnitude = spectrum->spect_magnitude; \
gfloat *spect_phase = spectrum->spect_phase; \
gint channels = GST_AUDIO_FILTER (spectrum)->format.channels; \
gint i, j, k; \
g##type acc; \
GstFFTF##width##Complex *freqdata; \
GstFFTF##width *ctx; \
g##type *in; \
gint nfft = 2 * spectrum->bands - 2; \
\
if (!spectrum->in) \
spectrum->in = (guint8 *) g_new (g##type, nfft); \
\
in = (g##type *) spectrum->in; \
\
for (i = 0, j = 0; i < nfft; i++) { \
/* convert to mono */ \
for (k = 0, acc = 0.0; k < channels; k++) \
acc += samples[j++]; \
in[i] = (g##type) (acc / channels); \
} \
\
if (!spectrum->fft_ctx) { \
spectrum->fft_ctx = gst_fft_f##width##_new (nfft, FALSE); \
spectrum->fft_free_func = (GstSpectrumFFTFreeFunc) gst_fft_f##width##_free; \
} \
ctx = spectrum->fft_ctx; \
\
gst_fft_f##width##_window (ctx, in, GST_FFT_WINDOW_HAMMING); \
\
if (!spectrum->freqdata) \
spectrum->freqdata = g_new (GstFFTF##width##Complex, spectrum->bands); \
\
freqdata = (GstFFTF##width##Complex *) spectrum->freqdata; \
\
gst_fft_f##width##_fft (ctx, in, freqdata); \
spectrum->num_fft++; \
\
/* Calculate magnitude in db */ \
for (i = 0; i < spectrum->bands; i++) { \
gdouble val = 0.0; \
val = freqdata[i].r * freqdata[i].r; \
val += freqdata[i].i * freqdata[i].i; \
val /= nfft*nfft; \
val = 10.0 * log10 (val); \
if (val < spectrum->threshold) \
val = spectrum->threshold; \
spect_magnitude[i] += val; \
} \
\
/* Calculate phase */ \
for (i = 0; i < spectrum->bands; i++) \
spect_phase[i] += atan2 (freqdata[i].i, freqdata[i].r); \
\
}
DEFINE_PROCESS_FUNC_FLOAT (32, float);
DEFINE_PROCESS_FUNC_FLOAT (64, double);
static GstFlowReturn
gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * in)
gst_spectrum_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
{
GstSpectrum *spectrum = GST_SPECTRUM (trans);
gint wanted;
gint i;
gfloat *spect_magnitude = spectrum->spect_magnitude;
gfloat *spect_phase = spectrum->spect_phase;
gint rate = GST_AUDIO_FILTER (spectrum)->format.rate;
gint channels = GST_AUDIO_FILTER (spectrum)->format.channels;
gint width = GST_AUDIO_FILTER (spectrum)->format.width / 8;
gint nfft = 2 * spectrum->bands - 2;
guint i;
guint rate = GST_AUDIO_FILTER (spectrum)->format.rate;
guint channels = GST_AUDIO_FILTER (spectrum)->format.channels;
guint width = GST_AUDIO_FILTER (spectrum)->format.width / 8;
gboolean fp = (GST_AUDIO_FILTER (spectrum)->format.type == GST_BUFTYPE_FLOAT);
guint bands = spectrum->bands;
guint nfft = 2 * bands - 2;
gint threshold = spectrum->threshold;
gfloat *input;
gfloat *input_tmp;
GstFFTF32Complex *freqdata;
gfloat *spect_magnitude;
gfloat *spect_phase;
GstFFTF32 *fft_ctx;
GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (in));
GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (buffer));
/* can we do this nicer? */
gst_adapter_push (spectrum->adapter, gst_buffer_copy (in));
/* required number of bytes */
wanted = channels * nfft * width;
if (GST_BUFFER_IS_DISCONT (buffer)) {
GST_DEBUG_OBJECT (spectrum, "Discontinuity detected -- resetting state");
gst_spectrum_reset_state (spectrum);
}
while (gst_adapter_available (spectrum->adapter) >= wanted) {
const guint8 *samples;
/* Create a subbuffer to allow us to overwrite the buffer metadata later */
buffer = gst_buffer_create_sub (buffer, 0, GST_BUFFER_SIZE (buffer));
samples = gst_adapter_peek (spectrum->adapter, wanted);
/* If we don't have a FFT context yet get one and
* allocate memory for everything
*/
if (spectrum->fft_ctx == NULL) {
spectrum->input = g_new0 (gfloat, nfft);
spectrum->input_tmp = g_new0 (gfloat, nfft);
spectrum->freqdata = g_new0 (GstFFTF32Complex, bands);
spectrum->spect_magnitude = g_new0 (gfloat, bands);
spectrum->spect_phase = g_new0 (gfloat, bands);
spectrum->fft_ctx = gst_fft_f32_new (nfft, FALSE);
spectrum->frames_per_interval =
gst_util_uint64_scale (spectrum->interval, rate, GST_SECOND);
if (spectrum->frames_per_interval == 0)
spectrum->frames_per_interval = 1;
spectrum->num_frames = 0;
spectrum->num_fft = 0;
}
spectrum->process (spectrum, samples);
if (spectrum->num_frames == 0)
spectrum->message_ts = GST_BUFFER_TIMESTAMP (buffer);
if (G_UNLIKELY (!spectrum->num_frames)) {
/* remember start timestamp for message */
spectrum->message_ts = GST_BUFFER_TIMESTAMP (in);
input = spectrum->input;
input_tmp = spectrum->input_tmp;
freqdata = spectrum->freqdata;
spect_magnitude = spectrum->spect_magnitude;
spect_phase = spectrum->spect_phase;
fft_ctx = spectrum->fft_ctx;
while (GST_BUFFER_SIZE (buffer) >= width * channels) {
/* Move the current frame into our ringbuffer and
* take the average of all channels
*/
spectrum->input[spectrum->input_pos] = 0.0;
if (fp && width == 4) {
gfloat *in = (gfloat *) GST_BUFFER_DATA (buffer);
for (i = 0; i < channels; i++)
spectrum->input[spectrum->input_pos] += in[i];
} else if (fp && width == 8) {
gdouble *in = (gdouble *) GST_BUFFER_DATA (buffer);
for (i = 0; i < channels; i++)
spectrum->input[spectrum->input_pos] += in[i];
} else if (!fp && width == 4) {
gint32 *in = (gint32 *) GST_BUFFER_DATA (buffer);
for (i = 0; i < channels; i++)
spectrum->input[spectrum->input_pos] += ((gfloat) in[i]) / G_MAXINT32;
} else if (!fp && width == 2) {
gint16 *in = (gint16 *) GST_BUFFER_DATA (buffer);
for (i = 0; i < channels; i++)
spectrum->input[spectrum->input_pos] += ((gfloat) in[i]) / G_MAXINT16;
} else {
g_assert_not_reached ();
}
spectrum->num_frames += nfft;
/* do we need to message ? */
if (spectrum->num_frames >=
GST_CLOCK_TIME_TO_FRAMES (spectrum->interval, rate)) {
spectrum->input[spectrum->input_pos] /= channels;
GST_BUFFER_DATA (buffer) += width * channels;
GST_BUFFER_SIZE (buffer) -= width * channels;
spectrum->input_pos = (spectrum->input_pos + 1) % nfft;
spectrum->num_frames++;
/* If we have enough frames for an FFT or we
* have all frames required for the interval run
* an FFT. In the last case we probably take the
* FFT of frames that we already handled.
*/
if (spectrum->num_frames % nfft == 0 ||
spectrum->num_frames == spectrum->frames_per_interval) {
for (i = 0; i < nfft; i++)
input_tmp[i] = input[(spectrum->input_pos + i) % nfft];
gst_fft_f32_window (fft_ctx, input_tmp, GST_FFT_WINDOW_HAMMING);
gst_fft_f32_fft (fft_ctx, input_tmp, freqdata);
spectrum->num_fft++;
/* Calculate magnitude in db */
for (i = 0; i < bands; i++) {
gdouble val = 0.0;
val = freqdata[i].r * freqdata[i].r;
val += freqdata[i].i * freqdata[i].i;
val /= nfft * nfft;
val = 10.0 * log10 (val);
if (val < threshold)
val = threshold;
spect_magnitude[i] += val;
}
/* Calculate phase */
for (i = 0; i < bands; i++)
spect_phase[i] += atan2 (freqdata[i].i, freqdata[i].r);
}
/* Do we have the FFTs for one interval? */
if (spectrum->num_frames == spectrum->frames_per_interval) {
if (spectrum->message) {
GstMessage *m;
GstClockTime duration =
GST_FRAMES_TO_CLOCK_TIME (spectrum->num_frames, rate);
/* Calculate average */
for (i = 0; i < spectrum->bands; i++) {
for (i = 0; i < bands; i++) {
spect_magnitude[i] /= spectrum->num_fft;
spect_phase[i] /= spectrum->num_fft;
}
m = gst_spectrum_message_new (spectrum, spectrum->message_ts, duration);
m = gst_spectrum_message_new (spectrum, spectrum->message_ts,
spectrum->interval);
gst_element_post_message (GST_ELEMENT (spectrum), m);
}
memset (spect_magnitude, 0, spectrum->bands * sizeof (gfloat));
memset (spect_phase, 0, spectrum->bands * sizeof (gfloat));
memset (spect_magnitude, 0, bands * sizeof (gfloat));
memset (spect_phase, 0, bands * sizeof (gfloat));
if (GST_CLOCK_TIME_IS_VALID (spectrum->message_ts))
spectrum->message_ts +=
gst_util_uint64_scale (spectrum->num_frames, GST_SECOND, rate);
spectrum->num_frames = 0;
spectrum->num_fft = 0;
}
gst_adapter_flush (spectrum->adapter, wanted);
}
g_assert (GST_BUFFER_SIZE (buffer) == 0);
gst_buffer_unref (buffer);
return GST_FLOW_OK;
}

View file

@ -1,5 +1,6 @@
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -21,67 +22,55 @@
#ifndef __GST_SPECTRUM_H__
#define __GST_SPECTRUM_H__
#include <gst/gst.h>
#include <gst/base/gstadapter.h>
#include <gst/base/gstbasetransform.h>
#include <gst/audio/gstaudiofilter.h>
#include <gst/fft/gstfftf32.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
G_BEGIN_DECLS
#define GST_TYPE_SPECTRUM (gst_spectrum_get_type())
#define GST_SPECTRUM(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SPECTRUM,GstSpectrum))
#define GST_IS_SPECTRUM(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SPECTRUM))
#define GST_SPECTRUM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SPECTRUM,GstSpectrumClass))
#define GST_IS_SPECTRUM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_SPECTRUM))
typedef struct _GstSpectrum GstSpectrum;
typedef struct _GstSpectrumClass GstSpectrumClass;
typedef void (*GstSpectrumProcessFunc) (GstSpectrum *, const guint8 *);
typedef void (*GstSpectrumFFTFreeFunc) (void *);
struct _GstSpectrum {
GstAudioFilter element;
GstPad *sinkpad,*srcpad;
GstAdapter *adapter;
struct _GstSpectrum
{
GstAudioFilter parent;
/* properties */
gboolean message; /* whether or not to post messages */
gboolean message_magnitude;
gboolean message_phase;
guint64 interval; /* how many seconds between emits */
guint64 interval; /* how many nanoseconds between emits */
guint64 frames_per_interval; /* how many frames per interval */
guint bands; /* number of spectrum bands */
gint threshold; /* energy level treshold */
gint num_frames; /* frame count (1 sample per channel)
guint num_frames; /* frame count (1 sample per channel)
* since last emit */
gint num_fft; /* number of FFTs since last emit */
guint num_fft; /* number of FFTs since last emit */
GstClockTime message_ts; /* starttime for next message */
/* <private> */
gfloat *input;
guint input_pos;
gfloat *input_tmp;
GstFFTF32Complex *freqdata;
gfloat *spect_magnitude;
gfloat *spect_phase;
GstSpectrumProcessFunc process;
void *fft_ctx;
GstSpectrumFFTFreeFunc fft_free_func;
void *in;
void *freqdata;
GstFFTF32 *fft_ctx;
};
struct _GstSpectrumClass {
struct _GstSpectrumClass
{
GstAudioFilterClass parent_class;
};
GType gst_spectrum_get_type (void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
G_END_DECLS
#endif /* __GST_SPECTRUM_H__ */