diff options
Diffstat (limited to 'gst')
-rw-r--r-- | gst/spectrum/demo-osssrc.c | 22 | ||||
-rw-r--r-- | gst/spectrum/gstspectrum.c | 26 |
2 files changed, 33 insertions, 15 deletions
diff --git a/gst/spectrum/demo-osssrc.c b/gst/spectrum/demo-osssrc.c index b51e8c2a..0312df87 100644 --- a/gst/spectrum/demo-osssrc.c +++ b/gst/spectrum/demo-osssrc.c @@ -15,12 +15,12 @@ spectrum_chain (GstElement * sink, GstBuffer * buf, GstPad * pad, gpointer unused) { gint i; - guchar *data = buf->data; - GdkRectangle rect = { 0, 0, GST_BUFFER_SIZE (buf), 25 }; + guchar *data = GST_BUFFER_DATA (buf); + GdkRectangle rect = { 0, 0, GST_BUFFER_SIZE (buf), 32 }; gdk_window_begin_paint_rect (drawingarea->window, &rect); gdk_draw_rectangle (drawingarea->window, drawingarea->style->black_gc, - TRUE, 0, 0, GST_BUFFER_SIZE (buf), 25); + TRUE, 0, 0, GST_BUFFER_SIZE (buf), 32); for (i = 0; i < GST_BUFFER_SIZE (buf); i++) { gdk_draw_rectangle (drawingarea->window, drawingarea->style->white_gc, TRUE, i, 32 - data[i], 1, data[i]); @@ -33,7 +33,7 @@ main (int argc, char *argv[]) { GstElement *bin; GstElement *src, *spectrum, *sink; - + GstCaps *filtercaps; GtkWidget *appwindow; gst_init (&argc, &argv); @@ -42,7 +42,8 @@ main (int argc, char *argv[]) bin = gst_pipeline_new ("bin"); src = gst_element_factory_make (DEFAULT_AUDIOSRC, "src"); - g_object_set (G_OBJECT (src), "buffersize", (gulong) 1024, NULL); + g_object_set (G_OBJECT (src), "buffersize", (gulong) 1024 * sizeof (gint16), + NULL); spectrum = gst_element_factory_make ("spectrum", "spectrum"); g_object_set (G_OBJECT (spectrum), "width", 256, NULL); sink = gst_element_factory_make ("fakesink", "sink"); @@ -50,9 +51,18 @@ main (int argc, char *argv[]) g_signal_connect (sink, "handoff", G_CALLBACK (spectrum_chain), NULL); gst_bin_add_many (GST_BIN (bin), src, spectrum, sink, NULL); - gst_element_link_many (src, spectrum, sink, NULL); + + filtercaps = + gst_caps_new_simple ("audio/x-raw-int", "rate", G_TYPE_INT, 11025, NULL); + if (!gst_element_link_filtered (src, spectrum, filtercaps)) + g_error ("Linking source to spectrum failed\n"); + gst_caps_free (filtercaps); + if (!gst_element_link (spectrum, sink)) + g_error ("Linking spectrum to sink failed\n"); appwindow = gtk_window_new (GTK_WINDOW_TOPLEVEL); + g_signal_connect (appwindow, "delete-event", G_CALLBACK (gtk_main_quit), + NULL); drawingarea = gtk_drawing_area_new (); gtk_drawing_area_size (GTK_DRAWING_AREA (drawingarea), 256, 32); gtk_container_add (GTK_CONTAINER (appwindow), drawingarea); diff --git a/gst/spectrum/gstspectrum.c b/gst/spectrum/gstspectrum.c index 37267cfd..c84f0e5b 100644 --- a/gst/spectrum/gstspectrum.c +++ b/gst/spectrum/gstspectrum.c @@ -25,6 +25,7 @@ #include "gstspectrum.h" #define SPECTRUM_DEFAULT_WIDTH 75 +#define SPECTRUM_MAX_WIDTH 1024 /* limited by the current FFT code */ /* elementfactory information */ static GstElementDetails gst_spectrum_details = @@ -120,7 +121,7 @@ gst_spectrum_class_init (GstSpectrumClass * klass) gobject_class->set_property = gst_spectrum_set_property; g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_WIDTH, - g_param_spec_int ("width", "width", "width", 1, G_MAXINT, + g_param_spec_int ("width", "width", "width", 1, SPECTRUM_MAX_WIDTH, SPECTRUM_DEFAULT_WIDTH, G_PARAM_WRITABLE)); } @@ -177,6 +178,7 @@ gst_spectrum_chain (GstPad * pad, GstData * _data) gint spec_base, spec_len; gint16 *re, *im, *loud; gint16 *samples; + gint samples_n; gint step, pos, i; guint8 *spect; GstBuffer *newbuf; @@ -188,28 +190,34 @@ gst_spectrum_chain (GstPad * pad, GstData * _data) spectrum = GST_SPECTRUM (GST_OBJECT_PARENT (pad)); samples = (gint16 *) GST_BUFFER_DATA (buf); + samples_n = GST_BUFFER_SIZE (buf) / (spectrum->channels * sizeof (gint16)); - spec_base = 8; + spec_base = 10; /* 2^10 = 1024 */ spec_len = 1024; + /* zero pad if samples_n < spec_len */ + re = g_new0 (gint16, spec_len); im = g_new0 (gint16, spec_len); + loud = g_new0 (gint16, spec_len); if (spectrum->channels == 2) { - re = g_malloc (spec_len * sizeof (gint16)); - for (i = 0; i < spec_len; i++) + for (i = 0; i < MIN (spec_len, samples_n); i++) re[i] = (samples[(i * 2)] + samples[(i * 2) + 1]) >> 1; } else { - re = samples; + re = memcpy (re, samples, MIN (spec_len, samples_n) * sizeof (gint16)); } - gst_spectrum_window (re, spec_len); + /* do not apply the window to the zero padding */ + gst_spectrum_window (re, MIN (spec_len, samples_n)); + gst_spectrum_fix_fft (re, im, spec_base, FALSE); gst_spectrum_fix_loud (loud, re, im, spec_len, 0); - if (re != samples) - g_free (re); + + g_free (re); g_free (im); - step = spec_len / (spectrum->width * 2); + + step = spec_len / spectrum->width; spect = g_new (guint8, spectrum->width); for (i = 0, pos = 0; i < spectrum->width; i++, pos += step) { if (loud[pos] > -60) |