Index: tests/check/elements/spectrum.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/tests/check/elements/spectrum.c,v
retrieving revision 1.1
diff -u -p -a -u -r1.1 spectrum.c
--- tests/check/elements/spectrum.c	20 Jun 2007 08:56:17 -0000	1.1
+++ tests/check/elements/spectrum.c	20 Aug 2007 18:33:01 -0000
@@ -33,24 +33,59 @@ gboolean have_eos = FALSE;
 GstPad *mysrcpad, *mysinkpad;
 
 #define SPECT_CAPS_TEMPLATE_STRING \
+    "audio/x-raw-int, "                                               \
+    " width = (int) 16, "                                             \
+    " depth = (int) 16, "                                             \
+    " signed = (boolean) true, "                                      \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]; "                                  \
+    "audio/x-raw-int, "                                               \
+    " width = (int) 32, "                                             \
+    " depth = (int) 32, "                                             \
+    " signed = (boolean) true, "                                      \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]; "                                  \
+    "audio/x-raw-float, "                                             \
+    " width = (int) { 32, 64 }, "                                     \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]"
+
+#define SPECT_CAPS_STRING_S16 \
   "audio/x-raw-int, " \
-    "rate = (int) [ 1, MAX ], " \
-    "channels = (int) [ 1, 8 ], " \
+    "rate = (int) 44100, " \
+    "channels = (int) 1, " \
     "endianness = (int) BYTE_ORDER, " \
-    "width = (int) {8, 16}, " \
-    "depth = (int) {8, 16}, " \
+    "width = (int) 16, " \
+    "depth = (int) 16, " \
     "signed = (boolean) true"
 
-#define SPECT_CAPS_STRING \
+#define SPECT_CAPS_STRING_S32 \
   "audio/x-raw-int, " \
     "rate = (int) 44100, " \
     "channels = (int) 1, " \
     "endianness = (int) BYTE_ORDER, " \
-    "width = (int) 16, " \
-    "depth = (int) 16, " \
+    "width = (int) 32, " \
+    "depth = (int) 32, " \
     "signed = (boolean) true"
 
-#define SPECT_BANDS 64
+#define SPECT_CAPS_STRING_F32 \
+    "audio/x-raw-float, "                                             \
+    " width = (int) 32, "                                             \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) 44100, "                                           \
+    " channels = (int) 1"
+
+#define SPECT_CAPS_STRING_F64 \
+    "audio/x-raw-float, "                                             \
+    " width = (int) 64, "                                             \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) 44100, "                                           \
+    " channels = (int) 1"
+
+#define SPECT_BANDS 256
 
 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
@@ -104,24 +139,332 @@ GST_START_TEST (test_int16)
   gint16 *data;
   const GValue *list, *value;
   GstClockTime endtime;
-  guchar level;
+  gfloat level;
 
   spectrum = setup_spectrum ();
-  g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 10,
+  g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 100,
       "bands", SPECT_BANDS, "threshold", -80, NULL);
 
   fail_unless (gst_element_set_state (spectrum,
           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
       "could not set to playing");
-
-  /* create a fake 1 sec buffer with a half-amplitude block signal */
+  
+  /* create a 1 sec buffer with an 11025 Hz sine wave */
   inbuffer = gst_buffer_new_and_alloc (44100 * sizeof (gint16));
   data = (gint16 *) GST_BUFFER_DATA (inbuffer);
-  for (j = 0; j < 44100; ++j) {
-    *data = 16536;
+
+  for (j = 0; j < 44100; j += 4) {
+    *data = 0;
+    ++data;
+    *data = 32767;
+    ++data;
+    *data = 0;
+    ++data;
+    *data = -32767;
+    ++data;
+  }
+
+  caps = gst_caps_from_string (SPECT_CAPS_STRING_S16);
+  gst_buffer_set_caps (inbuffer, caps);
+  gst_caps_unref (caps);
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+
+  /* create a bus to get the spectrum message on */
+  bus = gst_bus_new ();
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_element_set_bus (spectrum, bus);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
+  /* ... but it ends up being collected on the global buffer list */
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, -1);
+  ASSERT_OBJECT_REFCOUNT (message, "message", 1);
+
+  fail_unless (message != NULL);
+  fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (spectrum));
+  fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT);
+  structure = gst_message_get_structure (message);
+  fail_if (structure == NULL);
+  fail_unless_equals_string ((char *) gst_structure_get_name (structure),
+      "spectrum");
+  fail_unless (gst_structure_get_clock_time (structure, "endtime", &endtime));
+
+  list = gst_structure_get_value (structure, "magnitude");
+  for (i = 0; i < SPECT_BANDS; ++i) {
+    value = gst_value_list_get_value (list, i);
+    level = g_value_get_float (value);
+    GST_DEBUG ("band[%3d] is %.2f", i, level);
+    /* Only the bands in the middle should have a level above 60 */
+    fail_if ((i == SPECT_BANDS / 2 || i == SPECT_BANDS / 2 - 1) && level < 60.0);
+    fail_if ((i != SPECT_BANDS / 2 && i != SPECT_BANDS / 2 - 1) && level > 60.0);
+  }
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  /* clean up */
+  /* flush current messages,and future state change messages */
+  gst_bus_set_flushing (bus, TRUE);
+
+  /* message has a ref to the element */
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 2);
+  gst_message_unref (message);
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+
+  gst_element_set_bus (spectrum, NULL);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_object_unref (bus);
+  gst_buffer_unref (outbuffer);
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+  cleanup_spectrum (spectrum);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_int32)
+{
+  GstElement *spectrum;
+  GstBuffer *inbuffer, *outbuffer;
+  GstBus *bus;
+  GstCaps *caps;
+  GstMessage *message;
+  const GstStructure *structure;
+  int i, j;
+  gint32 *data;
+  const GValue *list, *value;
+  GstClockTime endtime;
+  gfloat level;
+
+  spectrum = setup_spectrum ();
+  g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 100,
+      "bands", SPECT_BANDS, "threshold", -80, NULL);
+
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "could not set to playing");
+
+  /* create a 1 sec buffer with an 11025 Hz sine wave */
+  inbuffer = gst_buffer_new_and_alloc (44100 * sizeof (gint32));
+  data = (gint32 *) GST_BUFFER_DATA (inbuffer);
+  for (j = 0; j < 44100; j += 4) {
+    *data = 0;
+    ++data;
+    *data = 2147483647;
+    ++data;
+    *data = 0;
+    ++data;
+    *data = -2147483647;
+    ++data;
+  }
+  caps = gst_caps_from_string (SPECT_CAPS_STRING_S32);
+  gst_buffer_set_caps (inbuffer, caps);
+  gst_caps_unref (caps);
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+
+  /* create a bus to get the spectrum message on */
+  bus = gst_bus_new ();
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_element_set_bus (spectrum, bus);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
+  /* ... but it ends up being collected on the global buffer list */
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, -1);
+  ASSERT_OBJECT_REFCOUNT (message, "message", 1);
+
+  fail_unless (message != NULL);
+  fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (spectrum));
+  fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT);
+  structure = gst_message_get_structure (message);
+  fail_if (structure == NULL);
+  fail_unless_equals_string ((char *) gst_structure_get_name (structure),
+      "spectrum");
+  fail_unless (gst_structure_get_clock_time (structure, "endtime", &endtime));
+
+  list = gst_structure_get_value (structure, "magnitude");
+  for (i = 0; i < SPECT_BANDS; ++i) {
+    value = gst_value_list_get_value (list, i);
+    level = g_value_get_float (value);
+    GST_DEBUG ("band[%3d] is %.2f", i, level);
+    /* Only the bands in the middle should have a level above 60 */
+    fail_if ((i == SPECT_BANDS / 2 || i == SPECT_BANDS / 2 - 1) && level < 60.0);
+    fail_if ((i != SPECT_BANDS / 2 && i != SPECT_BANDS / 2 - 1) && level > 60.0);
+  }
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  /* clean up */
+  /* flush current messages,and future state change messages */
+  gst_bus_set_flushing (bus, TRUE);
+
+  /* message has a ref to the element */
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 2);
+  gst_message_unref (message);
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+
+  gst_element_set_bus (spectrum, NULL);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_object_unref (bus);
+  gst_buffer_unref (outbuffer);
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+  cleanup_spectrum (spectrum);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_float32)
+{
+  GstElement *spectrum;
+  GstBuffer *inbuffer, *outbuffer;
+  GstBus *bus;
+  GstCaps *caps;
+  GstMessage *message;
+  const GstStructure *structure;
+  int i, j;
+  gfloat *data;
+  const GValue *list, *value;
+  GstClockTime endtime;
+  gfloat level;
+
+  spectrum = setup_spectrum ();
+  g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 100,
+      "bands", SPECT_BANDS, "threshold", -80, NULL);
+
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "could not set to playing");
+
+  /* create a 1 sec buffer with an 11025 Hz sine wave */
+  inbuffer = gst_buffer_new_and_alloc (44100 * sizeof (gfloat));
+  data = (gfloat *) GST_BUFFER_DATA (inbuffer);
+  for (j = 0; j < 44100; j += 4) {
+    *data = 0.0;
+    ++data;
+    *data = 1.0;
+    ++data;
+    *data = 0.0;
+    ++data;
+    *data = -1.0;
+    ++data;
+  }
+  caps = gst_caps_from_string (SPECT_CAPS_STRING_F32);
+  gst_buffer_set_caps (inbuffer, caps);
+  gst_caps_unref (caps);
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+
+  /* create a bus to get the spectrum message on */
+  bus = gst_bus_new ();
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_element_set_bus (spectrum, bus);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 2);
+
+  /* pushing gives away my reference ... */
+  fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK);
+  /* ... but it ends up being collected on the global buffer list */
+  ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  message = gst_bus_poll (bus, GST_MESSAGE_ELEMENT, -1);
+  ASSERT_OBJECT_REFCOUNT (message, "message", 1);
+
+  fail_unless (message != NULL);
+  fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (spectrum));
+  fail_unless (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT);
+  structure = gst_message_get_structure (message);
+  fail_if (structure == NULL);
+  fail_unless_equals_string ((char *) gst_structure_get_name (structure),
+      "spectrum");
+  fail_unless (gst_structure_get_clock_time (structure, "endtime", &endtime));
+
+  list = gst_structure_get_value (structure, "magnitude");
+  for (i = 0; i < SPECT_BANDS; ++i) {
+    value = gst_value_list_get_value (list, i);
+    level = g_value_get_float (value);
+    GST_DEBUG ("band[%3d] is %.2f", i, level);
+    /* Only the bands in the middle should have a level above 60 */
+    fail_if ((i == SPECT_BANDS / 2 || i == SPECT_BANDS / 2 - 1) && level < 60.0);
+    fail_if ((i != SPECT_BANDS / 2 && i != SPECT_BANDS / 2 - 1) && level > 60.0);
+  }
+  fail_unless_equals_int (g_list_length (buffers), 1);
+  fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
+  fail_unless (inbuffer == outbuffer);
+
+  /* clean up */
+  /* flush current messages,and future state change messages */
+  gst_bus_set_flushing (bus, TRUE);
+
+  /* message has a ref to the element */
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 2);
+  gst_message_unref (message);
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+
+  gst_element_set_bus (spectrum, NULL);
+  ASSERT_OBJECT_REFCOUNT (bus, "bus", 1);
+  gst_object_unref (bus);
+  gst_buffer_unref (outbuffer);
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
+  ASSERT_OBJECT_REFCOUNT (spectrum, "spectrum", 1);
+  cleanup_spectrum (spectrum);
+}
+
+GST_END_TEST;
+
+GST_START_TEST (test_float64)
+{
+  GstElement *spectrum;
+  GstBuffer *inbuffer, *outbuffer;
+  GstBus *bus;
+  GstCaps *caps;
+  GstMessage *message;
+  const GstStructure *structure;
+  int i, j;
+  gdouble *data;
+  const GValue *list, *value;
+  GstClockTime endtime;
+  gfloat level;
+
+  spectrum = setup_spectrum ();
+  g_object_set (spectrum, "message", TRUE, "interval", GST_SECOND / 100,
+      "bands", SPECT_BANDS, "threshold", -80, NULL);
+
+  fail_unless (gst_element_set_state (spectrum,
+          GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
+      "could not set to playing");
+
+  /* create a 1 sec buffer with an 11025 Hz sine wave */
+  inbuffer = gst_buffer_new_and_alloc (44100 * sizeof (gdouble));
+  data = (gdouble *) GST_BUFFER_DATA (inbuffer);
+  for (j = 0; j < 44100; j += 4) {
+    *data = 0.0;
+    ++data;
+    *data = 1.0;
+    ++data;
+    *data = 0.0;
+    ++data;
+    *data = -1.0;
     ++data;
   }
-  caps = gst_caps_from_string (SPECT_CAPS_STRING);
+  caps = gst_caps_from_string (SPECT_CAPS_STRING_F64);
   gst_buffer_set_caps (inbuffer, caps);
   gst_caps_unref (caps);
   ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
@@ -152,13 +495,14 @@ GST_START_TEST (test_int16)
       "spectrum");
   fail_unless (gst_structure_get_clock_time (structure, "endtime", &endtime));
 
-  /* block wave of half amplitude has -5.94 dB for rms, peak and decay */
-  list = gst_structure_get_value (structure, "spectrum");
+  list = gst_structure_get_value (structure, "magnitude");
   for (i = 0; i < SPECT_BANDS; ++i) {
     value = gst_value_list_get_value (list, i);
-    level = g_value_get_uchar (value);
-    GST_DEBUG ("band[%3d] is %3d", i, level);
-    fail_if (level == 0);
+    level = g_value_get_float (value);
+    GST_DEBUG ("band[%3d] is %.2f", i, level);
+    /* Only the bands in the middle should have a level above 60 */
+    fail_if ((i == SPECT_BANDS / 2 || i == SPECT_BANDS / 2 - 1) && level < 60.0);
+    fail_if ((i != SPECT_BANDS / 2 && i != SPECT_BANDS / 2 - 1) && level > 60.0);
   }
   fail_unless_equals_int (g_list_length (buffers), 1);
   fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
@@ -194,6 +538,9 @@ spectrum_suite (void)
 
   suite_add_tcase (s, tc_chain);
   tcase_add_test (tc_chain, test_int16);
+  tcase_add_test (tc_chain, test_int32);
+  tcase_add_test (tc_chain, test_float32);
+  tcase_add_test (tc_chain, test_float64);
 
   return s;
 }
Index: gst/spectrum/Makefile.am
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/gst/spectrum/Makefile.am,v
retrieving revision 1.13
diff -u -p -a -u -r1.13 Makefile.am
--- gst/spectrum/Makefile.am	16 Jun 2006 12:35:08 -0000	1.13
+++ gst/spectrum/Makefile.am	20 Aug 2007 18:33:01 -0000
@@ -1,10 +1,10 @@
 
 plugin_LTLIBRARIES = libgstspectrum.la
 
-libgstspectrum_la_SOURCES = gstspectrum.c fix_fft.c
+libgstspectrum_la_SOURCES = gstspectrum.c
 libgstspectrum_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) \
 	$(GST_CFLAGS)
-libgstspectrum_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(GST_LIBS)
+libgstspectrum_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) $(GST_BASE_LIBS) $(GST_LIBS) -lgstaudio-$(GST_MAJORMINOR) -lgstfft-$(GST_MAJORMINOR) $(LIBM)
 libgstspectrum_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
 
 noinst_HEADERS = gstspectrum.h
Index: gst/spectrum/demo-audiotest.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/gst/spectrum/demo-audiotest.c,v
retrieving revision 1.5
diff -u -p -a -u -r1.5 demo-audiotest.c
--- gst/spectrum/demo-audiotest.c	18 Jul 2007 07:35:32 -0000	1.5
+++ gst/spectrum/demo-audiotest.c	20 Aug 2007 18:33:01 -0000
@@ -51,7 +51,7 @@ on_frequency_changed (GtkRange * range, 
 
 /* draw frequency spectrum as a bunch of bars */
 static void
-draw_spectrum (guchar * data)
+draw_spectrum (gfloat * data)
 {
   gint i;
   GdkRectangle rect = { 0, 0, SPECT_BANDS, 50 };
@@ -78,15 +78,15 @@ message_handler (GstBus * bus, GstMessag
     const gchar *name = gst_structure_get_name (s);
 
     if (strcmp (name, "spectrum") == 0) {
-      guchar spect[SPECT_BANDS];
+      gfloat spect[SPECT_BANDS];
       const GValue *list;
       const GValue *value;
       guint i;
 
-      list = gst_structure_get_value (s, "spectrum");
+      list = gst_structure_get_value (s, "magnitude");
       for (i = 0; i < SPECT_BANDS; ++i) {
         value = gst_value_list_get_value (list, i);
-        spect[i] = g_value_get_uchar (value);
+        spect[i] = g_value_get_float (value);
       }
       draw_spectrum (spect);
     }
@@ -98,7 +98,7 @@ int
 main (int argc, char *argv[])
 {
   GstElement *bin;
-  GstElement *src, *spectrum, *sink;
+  GstElement *src, *spectrum, *audioconvert, *sink;
   GstBus *bus;
   GtkWidget *appwindow, *vbox, *widget;
 
@@ -116,10 +116,12 @@ main (int argc, char *argv[])
   g_object_set (G_OBJECT (spectrum), "bands", SPECT_BANDS, "threshold", -80,
       "message", TRUE, NULL);
 
+  audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");
+  
   sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
 
-  gst_bin_add_many (GST_BIN (bin), src, spectrum, sink, NULL);
-  if (!gst_element_link_many (src, spectrum, sink, NULL)) {
+  gst_bin_add_many (GST_BIN (bin), src, spectrum, audioconvert, sink, NULL);
+  if (!gst_element_link_many (src, spectrum, audioconvert, sink, NULL)) {
     fprintf (stderr, "cant link elements\n");
     exit (1);
   }
Index: gst/spectrum/demo-osssrc.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/gst/spectrum/demo-osssrc.c,v
retrieving revision 1.17
diff -u -p -a -u -r1.17 demo-osssrc.c
--- gst/spectrum/demo-osssrc.c	18 Jul 2007 07:35:32 -0000	1.17
+++ gst/spectrum/demo-osssrc.c	20 Aug 2007 18:33:02 -0000
@@ -43,7 +43,7 @@ on_window_destroy (GtkObject * object, g
 
 /* draw frequency spectrum as a bunch of bars */
 static void
-draw_spectrum (guchar * data)
+draw_spectrum (gfloat * data)
 {
   gint i;
   GdkRectangle rect = { 0, 0, SPECT_WIDTH, SPECT_HEIGHT };
@@ -70,15 +70,15 @@ message_handler (GstBus * bus, GstMessag
     const gchar *name = gst_structure_get_name (s);
 
     if (strcmp (name, "spectrum") == 0) {
-      guchar spect[SPECT_BANDS];
+      gfloat spect[SPECT_BANDS];
       const GValue *list;
       const GValue *value;
       guint i;
 
-      list = gst_structure_get_value (s, "spectrum");
+      list = gst_structure_get_value (s, "magnitude");
       for (i = 0; i < SPECT_BANDS; ++i) {
         value = gst_value_list_get_value (list, i);
-        spect[i] = g_value_get_uchar (value);
+        spect[i] = g_value_get_float (value);
       }
       draw_spectrum (spect);
     }
Index: gst/spectrum/gstspectrum.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/gst/spectrum/gstspectrum.c,v
retrieving revision 1.36
diff -u -p -a -u -r1.36 gstspectrum.c
--- gst/spectrum/gstspectrum.c	20 Jun 2007 08:56:17 -0000	1.36
+++ gst/spectrum/gstspectrum.c	20 Aug 2007 18:33:03 -0000
@@ -1,6 +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>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -37,18 +38,26 @@
  * </listitem>
  * <listitem>
  *   <para>
- *   #GstValueList of #guchar
- *   <classname>&quot;spectrum&quot;</classname>:
+ *   #GstValueList of #gfloat
+ *   <classname>&quot;magnitude&quot;</classname>:
  *   the level for each frequency band. A value of 0 maps to the
  *   db value given by the
  *   <link linkend="GstSpectrum--threshold">threshold property.</link>.
  *   </para>
  * </listitem>
+ * <listitem>
+ *   <para>
+ *   #GstValueList of #gfloat
+ *   <classname>&quot;phase&quot;</classname>:
+ *   the phase for each frequency band. The value is between -pi and pi.
+ *   </para>
+ * </listitem>
+
  *
  * This element cannot be used with the gst-launch command in a sensible way.
  * The included demo shows how to use it in an application.
  *
- * Last reviewed on 2006-05-21 (0.10.3)
+ * Last reviewed on 2007-08-18 (0.10.5)
  * </refsect2>
  */
 
@@ -57,8 +66,16 @@
 #endif
 #include <string.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
 
@@ -68,32 +85,34 @@ 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>");
+    "Stefan Kost <ensonic@users.sf.net>, "
+    "Sebastian Dröge <slomo@circular-chaos.org>");
 
-static GstStaticPadTemplate sink_template_factory =
-GST_STATIC_PAD_TEMPLATE ("sink",
-    GST_PAD_SINK,
-    GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("audio/x-raw-int, "
-        "rate = (int) [ 1, MAX ], "
-        "channels = (int) [1, MAX], "
-        "endianness = (int) BYTE_ORDER, "
-        "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
-    );
-
-static GstStaticPadTemplate src_template_factory =
-GST_STATIC_PAD_TEMPLATE ("src",
-    GST_PAD_SRC,
-    GST_PAD_ALWAYS,
-    GST_STATIC_CAPS ("audio/x-raw-int, "
-        "rate = (int) [ 1, MAX ], "
-        "channels = (int) [1, MAX], "
-        "endianness = (int) BYTE_ORDER, "
-        "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
-    );
+#define ALLOWED_CAPS \
+    "audio/x-raw-int, "                                               \
+    " width = (int) 16, "                                             \
+    " depth = (int) 16, "                                             \
+    " signed = (boolean) true, "                                      \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]; "                                  \
+    "audio/x-raw-int, "                                               \
+    " width = (int) 32, "                                             \
+    " depth = (int) 32, "                                             \
+    " signed = (boolean) true, "                                      \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]; "                                  \
+    "audio/x-raw-float, "                                             \
+    " width = (int) { 32, 64 }, "                                     \
+    " endianness = (int) BYTE_ORDER, "                                \
+    " rate = (int) [ 1, MAX ], "                                      \
+    " channels = (int) [ 1, MAX ]"
 
 /* Spectrum properties */
 #define DEFAULT_SIGNAL_SPECTRUM		TRUE
+#define DEFAULT_SIGNAL_MAGNITUDE	TRUE
+#define DEFAULT_SIGNAL_PHASE		FALSE
 #define DEFAULT_SIGNAL_INTERVAL		(GST_SECOND / 10)
 #define DEFAULT_BANDS			128
 #define DEFAULT_THRESHOLD		-60
@@ -105,44 +124,46 @@ enum
 {
   PROP_0,
   PROP_SIGNAL_SPECTRUM,
+  PROP_SIGNAL_MAGNITUDE,
+  PROP_SIGNAL_PHASE,
   PROP_SIGNAL_INTERVAL,
   PROP_BANDS,
   PROP_THRESHOLD
 };
 
-GST_BOILERPLATE (GstSpectrum, gst_spectrum, GstBaseTransform,
-    GST_TYPE_BASE_TRANSFORM);
+GST_BOILERPLATE (GstSpectrum, gst_spectrum, GstAudioFilter,
+    GST_TYPE_AUDIO_FILTER);
 
 static void gst_spectrum_dispose (GObject * object);
 static void gst_spectrum_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 static void gst_spectrum_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
-static gboolean gst_spectrum_set_caps (GstBaseTransform * trans, GstCaps * in,
-    GstCaps * out);
 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);
 
-
-#define fixed short
-extern int gst_spectrum_fix_fft (fixed fr[], fixed fi[], int m, int inverse);
-extern void gst_spectrum_fix_loud (fixed loud[], fixed fr[], fixed fi[], int n,
-    int scale_shift);
-extern void gst_spectrum_window (fixed fr[], int n);
+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_add_pad_template (element_class,
-      gst_static_pad_template_get (&sink_template_factory));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&src_template_factory));
   gst_element_class_set_details (element_class, &gst_spectrum_details);
+
+  caps = gst_caps_from_string (ALLOWED_CAPS);
+  gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (g_class),
+      caps);
+  gst_caps_unref (caps);
 }
 
 static void
@@ -150,23 +171,35 @@ gst_spectrum_class_init (GstSpectrumClas
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
+  GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (klass);
 
   gobject_class->set_property = gst_spectrum_set_property;
   gobject_class->get_property = gst_spectrum_get_property;
   gobject_class->dispose = gst_spectrum_dispose;
 
-  trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_spectrum_set_caps);
   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;
 
+  filter_class->setup = GST_DEBUG_FUNCPTR (gst_spectrum_setup);
+
   g_object_class_install_property (gobject_class, PROP_SIGNAL_SPECTRUM,
       g_param_spec_boolean ("message", "Message",
           "Post a level message for each passed interval",
           DEFAULT_SIGNAL_SPECTRUM, G_PARAM_READWRITE));
 
+  g_object_class_install_property (gobject_class, PROP_SIGNAL_MAGNITUDE,
+      g_param_spec_boolean ("message-magnitude", "Magnitude",
+          "Post the magnitude of the spectrum",
+          DEFAULT_SIGNAL_MAGNITUDE, G_PARAM_READWRITE));
+
+  g_object_class_install_property (gobject_class, PROP_SIGNAL_PHASE,
+      g_param_spec_boolean ("message-phase", "Phase",
+          "Post the phase of the spectrum",
+          DEFAULT_SIGNAL_PHASE, G_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class, PROP_SIGNAL_INTERVAL,
       g_param_spec_uint64 ("interval", "Interval",
           "Interval of time between message posts (in nanoseconds)",
@@ -191,14 +224,14 @@ gst_spectrum_init (GstSpectrum * spectru
   spectrum->adapter = gst_adapter_new ();
 
   spectrum->message = DEFAULT_SIGNAL_SPECTRUM;
+  spectrum->message_magnitude = DEFAULT_SIGNAL_MAGNITUDE;
+  spectrum->message_phase = DEFAULT_SIGNAL_PHASE;
   spectrum->interval = DEFAULT_SIGNAL_INTERVAL;
   spectrum->bands = DEFAULT_BANDS;
   spectrum->threshold = DEFAULT_THRESHOLD;
 
-  spectrum->loud = g_malloc (SPECTRUM_WINDOW_LEN * sizeof (gint16));
-  spectrum->im = g_malloc0 (SPECTRUM_WINDOW_LEN * sizeof (gint16));
-  spectrum->re = g_malloc0 (SPECTRUM_WINDOW_LEN * sizeof (gint16));
-  spectrum->spect = g_malloc (spectrum->bands * sizeof (guchar));
+  spectrum->spect_magnitude = g_new0 (gfloat, spectrum->bands);
+  spectrum->spect_phase = g_new0 (gfloat, spectrum->bands);
 }
 
 static void
@@ -211,15 +244,20 @@ gst_spectrum_dispose (GObject * object)
     spectrum->adapter = NULL;
   }
 
-  g_free (spectrum->re);
-  g_free (spectrum->im);
-  g_free (spectrum->loud);
-  g_free (spectrum->spect);
-
-  spectrum->re = NULL;
-  spectrum->im = NULL;
-  spectrum->loud = NULL;
-  spectrum->spect = NULL;
+  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;
 
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
@@ -234,17 +272,39 @@ gst_spectrum_set_property (GObject * obj
     case PROP_SIGNAL_SPECTRUM:
       filter->message = g_value_get_boolean (value);
       break;
+    case PROP_SIGNAL_MAGNITUDE:
+      filter->message_magnitude = g_value_get_boolean (value);
+      break;
+    case PROP_SIGNAL_PHASE:
+      filter->message_phase = g_value_get_boolean (value);
+      break;
     case PROP_SIGNAL_INTERVAL:
       filter->interval = g_value_get_uint64 (value);
       break;
     case PROP_BANDS:
       GST_BASE_TRANSFORM_LOCK (filter);
+
       filter->bands = g_value_get_uint (value);
-      g_free (filter->spect);
-      filter->spect = g_malloc (filter->bands * sizeof (guchar));
+      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;
+      }
+
+      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;
       GST_BASE_TRANSFORM_UNLOCK (filter);
       GST_DEBUG_OBJECT (filter, "reallocation, spect = %p, bands =%d ",
-          filter->spect, filter->bands);
+          filter->spect_magnitude, filter->bands);
       break;
     case PROP_THRESHOLD:
       filter->threshold = g_value_get_int (value);
@@ -265,6 +325,12 @@ gst_spectrum_get_property (GObject * obj
     case PROP_SIGNAL_SPECTRUM:
       g_value_set_boolean (value, filter->message);
       break;
+    case PROP_SIGNAL_MAGNITUDE:
+      g_value_set_boolean (value, filter->message_magnitude);
+      break;
+    case PROP_SIGNAL_PHASE:
+      g_value_set_boolean (value, filter->message_phase);
+      break;
     case PROP_SIGNAL_INTERVAL:
       g_value_set_uint64 (value, filter->interval);
       break;
@@ -281,24 +347,16 @@ gst_spectrum_get_property (GObject * obj
 }
 
 static gboolean
-gst_spectrum_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
-{
-  GstSpectrum *filter = GST_SPECTRUM (trans);
-  GstStructure *structure;
-
-  structure = gst_caps_get_structure (in, 0);
-  gst_structure_get_int (structure, "rate", &filter->rate);
-  gst_structure_get_int (structure, "channels", &filter->channels);
-
-  return TRUE;
-}
-
-static gboolean
 gst_spectrum_start (GstBaseTransform * trans)
 {
   GstSpectrum *filter = GST_SPECTRUM (trans);
 
   filter->num_frames = 0;
+  filter->num_fft = 0;
+  if (filter->spect_magnitude)
+    memset (filter->spect_magnitude, filter->bands * sizeof (gfloat), 0);
+  if (filter->spect_phase)
+    memset (filter->spect_phase, filter->bands * sizeof (gfloat), 0);
   gst_segment_init (&filter->segment, GST_FORMAT_UNDEFINED);
 
   return TRUE;
@@ -347,6 +405,41 @@ gst_spectrum_event (GstBaseTransform * t
   return TRUE;
 }
 
+static gboolean
+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 ();
+  
+  return TRUE;
+}
+
 static GstMessage *
 gst_spectrum_message_new (GstSpectrum * spectrum, GstClockTime endtime)
 {
@@ -354,99 +447,232 @@ gst_spectrum_message_new (GstSpectrum * 
   GValue v = { 0, };
   GValue *l;
   guint i;
-  guchar *spect = spectrum->spect;
+  gfloat *spect_magnitude = spectrum->spect_magnitude;
+  gfloat *spect_phase = spectrum->spect_phase;
 
   GST_DEBUG_OBJECT (spectrum, "preparing message, spect = %p, bands =%d ",
-      spect, spectrum->bands);
+      spect_magnitude, spectrum->bands);
 
   s = gst_structure_new ("spectrum", "endtime", GST_TYPE_CLOCK_TIME,
       endtime, NULL);
 
-  g_value_init (&v, GST_TYPE_LIST);
-  /* will copy-by-value */
-  gst_structure_set_value (s, "spectrum", &v);
-  g_value_unset (&v);
-
-  g_value_init (&v, G_TYPE_UCHAR);
-  l = (GValue *) gst_structure_get_value (s, "spectrum");
-  for (i = 0; i < spectrum->bands; i++) {
-    g_value_set_uchar (&v, spect[i]);
-    gst_value_list_append_value (l, &v);        /* copies by value */
+  if (spectrum->message_magnitude) {
+    g_value_init (&v, GST_TYPE_LIST);
+    /* will copy-by-value */
+    gst_structure_set_value (s, "magnitude", &v);
+    g_value_unset (&v);
+
+    g_value_init (&v, G_TYPE_FLOAT);
+    l = (GValue *) gst_structure_get_value (s, "magnitude");
+    for (i = 0; i < spectrum->bands; i++) {
+      g_value_set_float (&v, spect_magnitude[i]);
+      gst_value_list_append_value (l, &v);        /* copies by value */
+    }
+    g_value_unset (&v);
+  }
+
+  if (spectrum->message_phase) {
+    g_value_init (&v, GST_TYPE_LIST);
+    /* will copy-by-value */
+    gst_structure_set_value (s, "phase", &v);
+    g_value_unset (&v);
+
+    g_value_init (&v, G_TYPE_FLOAT);
+    l = (GValue *) gst_structure_get_value (s, "phase");
+    for (i = 0; i < spectrum->bands; i++) {
+      g_value_set_float (&v, spect_phase[i]);
+      gst_value_list_append_value (l, &v);        /* copies by value */
+    }
+    g_value_unset (&v);
   }
-  g_value_unset (&v);
 
   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 = (gdouble) freqdata[i].r * (gdouble) freqdata[i].r \
+        + (gdouble) freqdata[i].i * (gdouble) freqdata[i].i; \
+    val = sqrt (val); \
+    val = 20.0 * log10 (val / max); \
+    if (val > spectrum->threshold) \
+      val -= spectrum->threshold; \
+    else \
+      val = 0.0; \
+    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; k < channels; k++) \
+      acc += samples[j++]; \
+    in[i] = (g##type) (acc / channels); \
+    if (abs (in[i]) > 1.0) \
+      g_assert_not_reached(); \
+  } \
+  \
+  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 = freqdata[i].r * freqdata[i].r + freqdata[i].i * freqdata[i].i; \
+    val = sqrt (val); \
+    val = 20.0 * log10 (val / nfft); \
+    if (val > spectrum->threshold) \
+      val -= spectrum->threshold; \
+    else \
+      val = 0.0; \
+    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)
 {
   GstSpectrum *spectrum = GST_SPECTRUM (trans);
   gint wanted;
-  gint i, j, k;
-  gint32 acc;
-  gfloat pos, step;
-  guchar *spect = spectrum->spect;
+  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;
 
   GstClockTime endtime =
       gst_segment_to_running_time (&spectrum->segment, GST_FORMAT_TIME,
       GST_BUFFER_TIMESTAMP (in));
   GstClockTime blktime =
-      GST_FRAMES_TO_CLOCK_TIME (SPECTRUM_WINDOW_LEN, spectrum->rate);
+      GST_FRAMES_TO_CLOCK_TIME (nfft, rate);
 
   GST_LOG_OBJECT (spectrum, "input size: %d bytes", GST_BUFFER_SIZE (in));
 
   /* can we do this nicer? */
   gst_adapter_push (spectrum->adapter, gst_buffer_copy (in));
   /* required number of bytes */
-  wanted = spectrum->channels * SPECTRUM_WINDOW_LEN * sizeof (gint16);
-  /* FIXME: 4.0 was 2.0 before, but that include the mirrored spectrum */
-  step = (gfloat) SPECTRUM_WINDOW_LEN / (spectrum->bands * 4.0);
+  wanted = channels * nfft * width;
 
   while (gst_adapter_available (spectrum->adapter) >= wanted) {
-    const gint16 *samples;
-
-    samples = (const gint16 *) gst_adapter_peek (spectrum->adapter, wanted);
+    const guint8 *samples;
 
-    /* the current fft code is gint16 based, so supporting other formats would
-     * not really benefit now */
-    for (i = 0, j = 0; i < SPECTRUM_WINDOW_LEN; i++) {
-      /* convert to mono */
-      for (k = 0, acc = 0; k < spectrum->channels; k++)
-        acc += samples[j++];
-      spectrum->re[i] = (gint16) (acc / spectrum->channels);
-    }
+    samples = gst_adapter_peek (spectrum->adapter, wanted);
 
-    gst_spectrum_window (spectrum->re, SPECTRUM_WINDOW_LEN);
-    gst_spectrum_fix_fft (spectrum->re, spectrum->im, SPECTRUM_WINDOW_BASE,
-        FALSE);
-    gst_spectrum_fix_loud (spectrum->loud, spectrum->re, spectrum->im,
-        SPECTRUM_WINDOW_LEN, 0);
-
-    /* resample to requested number of bands and offset by threshold */
-    for (i = 0, pos = 0.0; i < spectrum->bands; i++, pos += step) {
-      if (spectrum->loud[(gint) pos] > spectrum->threshold) {
-        spect[i] = spectrum->loud[(gint) pos] - spectrum->threshold;
-        /*
-           if (spect[i] > 15);
-           spect[i] = 15;
-         */
-      } else
-        /* treat as silence */
-        spect[i] = 0;
-    }
+    spectrum->process (spectrum, samples);
 
-    spectrum->num_frames += SPECTRUM_WINDOW_LEN;
+    spectrum->num_frames += nfft;
     endtime += blktime;
     /* do we need to message ? */
     if (spectrum->num_frames >=
-        GST_CLOCK_TIME_TO_FRAMES (spectrum->interval, spectrum->rate)) {
+        GST_CLOCK_TIME_TO_FRAMES (spectrum->interval, rate)) {
       if (spectrum->message) {
-        GstMessage *m = gst_spectrum_message_new (spectrum, endtime);
+        GstMessage *m;
+
+	/* Calculate average */
+	for (i = 0; i < spectrum->bands; i++) {
+	  spect_magnitude[i] /= spectrum->num_fft;
+	  spect_phase[i] /= spectrum->num_fft;
+	}
+
+	m = gst_spectrum_message_new (spectrum, endtime);
 
         gst_element_post_message (GST_ELEMENT (spectrum), m);
       }
+      memset (spect_magnitude, spectrum->bands * sizeof (gfloat), 0);
+      memset (spect_phase, spectrum->bands * sizeof (gfloat), 0);
       spectrum->num_frames = 0;
+      spectrum->num_fft = 0;
     }
 
     gst_adapter_flush (spectrum->adapter, wanted);
Index: gst/spectrum/gstspectrum.h
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-bad/gst/spectrum/gstspectrum.h,v
retrieving revision 1.11
diff -u -p -a -u -r1.11 gstspectrum.h
--- gst/spectrum/gstspectrum.h	7 Mar 2007 11:23:20 -0000	1.11
+++ gst/spectrum/gstspectrum.h	20 Aug 2007 18:33:03 -0000
@@ -25,6 +25,7 @@
 #include <gst/gst.h>
 #include <gst/base/gstadapter.h>
 #include <gst/base/gstbasetransform.h>
+#include <gst/audio/gstaudiofilter.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -39,9 +40,11 @@ extern "C" {
 
 typedef struct _GstSpectrum GstSpectrum;
 typedef struct _GstSpectrumClass GstSpectrumClass;
+typedef void (*GstSpectrumProcessFunc) (GstSpectrum *, const guint8 *);
+typedef void (*GstSpectrumFFTFreeFunc) (void *);
 
 struct _GstSpectrum {
-  GstBaseTransform element;
+  GstAudioFilter element;
 
   GstPad *sinkpad,*srcpad;
   GstAdapter *adapter;
@@ -49,24 +52,28 @@ struct _GstSpectrum {
 
   /* properties */
   gboolean message;             /* whether or not to post messages */
+  gboolean message_magnitude;
+  gboolean message_phase;
   guint64 interval;             /* how many seconds between emits */
   guint bands;                  /* number of spectrum bands */
   gint threshold;               /* energy level treshold */
 
   gint num_frames;              /* frame count (1 sample per channel)
                                  * since last emit */
+  gint num_fft;                 /* number of FFTs since last emit */
                                  
-  gint rate;                    /* caps variables */
-  gint channels;
-
   /* <private> */
-  gint base, len;
-  gint16 *re, *im, *loud;
-  guchar *spect;
+  gfloat *spect_magnitude;
+  gfloat *spect_phase;
+  GstSpectrumProcessFunc process;
+  void *fft_ctx;
+  GstSpectrumFFTFreeFunc fft_free_func;
+  void *in;
+  void *freqdata;
 };
 
 struct _GstSpectrumClass {
-  GstBaseTransformClass parent_class;
+  GstAudioFilterClass parent_class;
 };
 
 GType gst_spectrum_get_type (void);
? config.status.lineno
? docs/plugins/gst-plugins-bad-plugins.args.new
? docs/plugins/gst-plugins-bad-plugins.hierarchy.new
? docs/plugins/gst-plugins-bad-plugins.interfaces.new
? docs/plugins/gst-plugins-bad-plugins.prerequisites.new
? docs/plugins/gst-plugins-bad-plugins.signals.new
? docs/plugins/inspect/plugin-app.xml
? docs/plugins/inspect/plugin-bayer.xml
? docs/plugins/inspect/plugin-cdaudio.xml
? docs/plugins/inspect/plugin-dvbsrc.xml
? docs/plugins/inspect/plugin-flvdemux.xml
? docs/plugins/inspect/plugin-gstinterlace.xml
? docs/plugins/inspect/plugin-interleave.xml
? docs/plugins/inspect/plugin-ladspa.xml
? docs/plugins/inspect/plugin-mpegvideoparse.xml
? docs/plugins/inspect/plugin-mve.xml
? docs/plugins/inspect/plugin-nuvdemux.xml
? docs/plugins/inspect/plugin-quicktime.xml
? docs/plugins/inspect/plugin-real.xml
? docs/plugins/inspect/plugin-rfbsrc.xml
? docs/plugins/inspect/plugin-sndfile.xml
? docs/plugins/inspect/plugin-stereo.xml
? docs/plugins/inspect/plugin-switch.xml
? docs/plugins/inspect/plugin-vcdsrc.xml
? docs/plugins/inspect/plugin-videocrop.xml
? docs/plugins/inspect/plugin-videoparse.xml
? docs/plugins/inspect/plugin-vmnc.xml
? docs/plugins/inspect/plugin-x264.xml
? docs/plugins/inspect/plugin-y4menc.xml
? gst/filter/libgstfilter_la-gstbpwsinc.loT
? gst/filter/log
? gst/filter/tset-ref.wav
? gst/filter/tset.wav
? gst/spectrum/kiss
? gst/spectrum/lg
M gst/spectrum/Makefile.am
M gst/spectrum/demo-audiotest.c
M gst/spectrum/demo-osssrc.c
M gst/spectrum/gstspectrum.c
M gst/spectrum/gstspectrum.h
M tests/check/elements/spectrum.c
