2 * Copyright (C) 2009 Sebastian Droege <sebastian.droege@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* This small sample application creates a bandpass FIR filter
21 * by transforming the frequency response to the filter kernel.
28 #include <gst/fft/gstfftf64.h>
31 on_message (GstBus * bus, GstMessage * message, gpointer user_data)
33 GMainLoop *loop = (GMainLoop *) user_data;
35 switch (GST_MESSAGE_TYPE (message)) {
36 case GST_MESSAGE_ERROR:
37 g_error ("Got ERROR");
38 g_main_loop_quit (loop);
40 case GST_MESSAGE_WARNING:
41 g_warning ("Got WARNING");
42 g_main_loop_quit (loop);
45 g_main_loop_quit (loop);
55 on_rate_changed (GstElement * element, gint rate, gpointer user_data)
60 GstFFTF64Complex frequency_response[17];
62 gdouble filter_kernel[32];
65 /* Create the frequency response: zero outside
66 * a small frequency band */
67 for (i = 0; i < 17; i++) {
69 frequency_response[i].r = 0.0;
71 frequency_response[i].r = 1.0;
73 frequency_response[i].i = 0.0;
76 /* Calculate the inverse FT of the frequency response */
77 fft = gst_fft_f64_new (32, TRUE);
78 gst_fft_f64_inverse_fft (fft, frequency_response, tmp);
79 gst_fft_f64_free (fft);
81 /* Shift the inverse FT of the frequency response by 16,
82 * i.e. the half of the kernel length to get the
83 * impulse response. See http://www.dspguide.com/ch17/1.htm
84 * for more information.
86 for (i = 0; i < 32; i++)
87 filter_kernel[i] = tmp[(i + 16) % 32];
89 /* Apply the hamming window to the impulse response to get
90 * a better result than given from the rectangular window
92 for (i = 0; i < 32; i++)
93 filter_kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / 32));
95 va = g_value_array_new (1);
97 g_value_init (&v, G_TYPE_DOUBLE);
98 for (i = 0; i < 32; i++) {
99 g_value_set_double (&v, filter_kernel[i]);
100 g_value_array_append (va, &v);
103 g_object_set (G_OBJECT (element), "kernel", va, NULL);
104 /* Latency is 1/2 of the kernel length for this method of
105 * calculating a filter kernel from the frequency response
107 g_object_set (G_OBJECT (element), "latency", (gint64) (32 / 2), NULL);
108 g_value_array_free (va);
112 main (gint argc, gchar * argv[])
114 GstElement *pipeline, *src, *filter, *conv, *sink;
118 gst_init (NULL, NULL);
120 pipeline = gst_element_factory_make ("pipeline", NULL);
122 src = gst_element_factory_make ("audiotestsrc", NULL);
123 g_object_set (G_OBJECT (src), "wave", 5, NULL);
125 filter = gst_element_factory_make ("audiofirfilter", NULL);
126 g_signal_connect (G_OBJECT (filter), "rate-changed",
127 G_CALLBACK (on_rate_changed), NULL);
129 conv = gst_element_factory_make ("audioconvert", NULL);
131 sink = gst_element_factory_make ("autoaudiosink", NULL);
132 g_return_val_if_fail (sink != NULL, -1);
134 gst_bin_add_many (GST_BIN (pipeline), src, filter, conv, sink, NULL);
135 if (!gst_element_link_many (src, filter, conv, sink, NULL)) {
136 g_error ("Failed to link elements");
140 loop = g_main_loop_new (NULL, FALSE);
142 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
143 gst_bus_add_signal_watch (bus);
144 g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
145 gst_object_unref (GST_OBJECT (bus));
147 if (gst_element_set_state (pipeline,
148 GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
149 g_error ("Failed to go into PLAYING state");
153 g_main_loop_run (loop);
155 gst_element_set_state (pipeline, GST_STATE_NULL);
157 g_main_loop_unref (loop);
158 gst_object_unref (pipeline);