1 /* GStreamer
2  * Copyright (C) 2009 Sebastian Droege <sebastian.droege@collabora.co.uk>
3  *
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.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /* This small sample application creates a bandpass FIR filter
21  * by transforming the frequency response to the filter kernel.
22  */
23 
24 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
25  * with newer GLib versions (>= 2.31.0) */
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 
28 #include <string.h>
29 #include <math.h>
30 
31 #include <gst/gst.h>
32 #include <gst/fft/gstfftf64.h>
33 
34 static gboolean
on_message(GstBus * bus,GstMessage * message,gpointer user_data)35 on_message (GstBus * bus, GstMessage * message, gpointer user_data)
36 {
37   GMainLoop *loop = (GMainLoop *) user_data;
38 
39   switch (GST_MESSAGE_TYPE (message)) {
40     case GST_MESSAGE_ERROR:
41       g_error ("Got ERROR");
42       g_main_loop_quit (loop);
43       break;
44     case GST_MESSAGE_WARNING:
45       g_warning ("Got WARNING");
46       g_main_loop_quit (loop);
47       break;
48     case GST_MESSAGE_EOS:
49       g_main_loop_quit (loop);
50       break;
51     default:
52       break;
53   }
54 
55   return TRUE;
56 }
57 
58 static void
on_rate_changed(GstElement * element,gint rate,gpointer user_data)59 on_rate_changed (GstElement * element, gint rate, gpointer user_data)
60 {
61   GValueArray *va;
62   GValue v = { 0, };
63   GstFFTF64 *fft;
64   GstFFTF64Complex frequency_response[17];
65   gdouble tmp[32];
66   gdouble filter_kernel[32];
67   guint i;
68 
69   /* Create the frequency response: zero outside
70    * a small frequency band */
71   for (i = 0; i < 17; i++) {
72     if (i < 5 || i > 11)
73       frequency_response[i].r = 0.0;
74     else
75       frequency_response[i].r = 1.0;
76 
77     frequency_response[i].i = 0.0;
78   }
79 
80   /* Calculate the inverse FT of the frequency response */
81   fft = gst_fft_f64_new (32, TRUE);
82   gst_fft_f64_inverse_fft (fft, frequency_response, tmp);
83   gst_fft_f64_free (fft);
84 
85   /* Shift the inverse FT of the frequency response by 16,
86    * i.e. the half of the kernel length to get the
87    * impulse response. See http://www.dspguide.com/ch17/1.htm
88    * for more information.
89    */
90   for (i = 0; i < 32; i++)
91     filter_kernel[i] = tmp[(i + 16) % 32];
92 
93   /* Apply the hamming window to the impulse response to get
94    * a better result than given from the rectangular window
95    */
96   for (i = 0; i < 32; i++)
97     filter_kernel[i] *= (0.54 - 0.46 * cos (2 * G_PI * i / 32));
98 
99   va = g_value_array_new (1);
100 
101   g_value_init (&v, G_TYPE_DOUBLE);
102   for (i = 0; i < 32; i++) {
103     g_value_set_double (&v, filter_kernel[i]);
104     g_value_array_append (va, &v);
105     g_value_reset (&v);
106   }
107   g_object_set (G_OBJECT (element), "kernel", va, NULL);
108   /* Latency is 1/2 of the kernel length for this method of
109    * calculating a filter kernel from the frequency response
110    */
111   g_object_set (G_OBJECT (element), "latency", (gint64) (32 / 2), NULL);
112   g_value_array_free (va);
113 }
114 
115 gint
main(gint argc,gchar * argv[])116 main (gint argc, gchar * argv[])
117 {
118   GstElement *pipeline, *src, *filter, *conv, *sink;
119   GstBus *bus;
120   GMainLoop *loop;
121 
122   gst_init (NULL, NULL);
123 
124   pipeline = gst_element_factory_make ("pipeline", NULL);
125 
126   src = gst_element_factory_make ("audiotestsrc", NULL);
127   g_object_set (G_OBJECT (src), "wave", 5, NULL);
128 
129   filter = gst_element_factory_make ("audiofirfilter", NULL);
130   g_signal_connect (G_OBJECT (filter), "rate-changed",
131       G_CALLBACK (on_rate_changed), NULL);
132 
133   conv = gst_element_factory_make ("audioconvert", NULL);
134 
135   sink = gst_element_factory_make ("autoaudiosink", NULL);
136   g_return_val_if_fail (sink != NULL, -1);
137 
138   gst_bin_add_many (GST_BIN (pipeline), src, filter, conv, sink, NULL);
139   if (!gst_element_link_many (src, filter, conv, sink, NULL)) {
140     g_error ("Failed to link elements");
141     return -2;
142   }
143 
144   loop = g_main_loop_new (NULL, FALSE);
145 
146   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
147   gst_bus_add_signal_watch (bus);
148   g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
149   gst_object_unref (GST_OBJECT (bus));
150 
151   if (gst_element_set_state (pipeline,
152           GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
153     g_error ("Failed to go into PLAYING state");
154     return -3;
155   }
156 
157   g_main_loop_run (loop);
158 
159   gst_element_set_state (pipeline, GST_STATE_NULL);
160 
161   g_main_loop_unref (loop);
162   gst_object_unref (pipeline);
163 
164   return 0;
165 }
166