audiofx: adjust to changed semantics of audiofilter _setup method
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audiokaraoke.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * SECTION:element-audiokaraoke
23  *
24  * Remove the voice from audio by filtering the center channel.
25  * This plugin is useful for karaoke applications.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch filesrc location=song.ogg ! oggdemux ! vorbisdec ! audiokaraoke ! audioconvert ! alsasink
31  * ]|
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <math.h>
40
41 #include <gst/gst.h>
42 #include <gst/base/gstbasetransform.h>
43 #include <gst/audio/audio.h>
44 #include <gst/audio/gstaudiofilter.h>
45
46 #include "audiokaraoke.h"
47
48 #define GST_CAT_DEFAULT gst_audio_karaoke_debug
49 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
50
51 /* Filter signals and args */
52 enum
53 {
54   /* FILL ME */
55   LAST_SIGNAL
56 };
57
58 #define DEFAULT_LEVEL           1.0
59 #define DEFAULT_MONO_LEVEL      1.0
60 #define DEFAULT_FILTER_BAND     220.0
61 #define DEFAULT_FILTER_WIDTH    100.0
62
63 enum
64 {
65   PROP_0,
66   PROP_LEVEL,
67   PROP_MONO_LEVEL,
68   PROP_FILTER_BAND,
69   PROP_FILTER_WIDTH,
70   PROP_LAST
71 };
72
73 #define ALLOWED_CAPS \
74     "audio/x-raw,"                                                \
75     " format=(string){"GST_AUDIO_NE(S16)","GST_AUDIO_NE(F32)"},"  \
76     " rate=(int)[1,MAX],"                                         \
77     " channels=(int)2,"                                           \
78     " channel-mask=(bitmask)0x3,"                                 \
79     " layout=(string) interleaved"
80
81 G_DEFINE_TYPE (GstAudioKaraoke, gst_audio_karaoke, GST_TYPE_AUDIO_FILTER);
82
83 static void gst_audio_karaoke_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_audio_karaoke_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87
88 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
89     const GstAudioInfo * info);
90 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
91     GstBuffer * buf);
92
93 static void gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
94     gint16 * data, guint num_samples);
95 static void gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
96     gfloat * data, guint num_samples);
97
98 /* GObject vmethod implementations */
99
100 static void
101 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstElementClass *gstelement_class;
105   GstCaps *caps;
106
107   GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0,
108       "audiokaraoke element");
109
110   gobject_class = (GObjectClass *) klass;
111   gstelement_class = (GstElementClass *) klass;
112
113   gobject_class->set_property = gst_audio_karaoke_set_property;
114   gobject_class->get_property = gst_audio_karaoke_get_property;
115
116   g_object_class_install_property (gobject_class, PROP_LEVEL,
117       g_param_spec_float ("level", "Level",
118           "Level of the effect (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
119           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
120
121   g_object_class_install_property (gobject_class, PROP_MONO_LEVEL,
122       g_param_spec_float ("mono-level", "Mono Level",
123           "Level of the mono channel (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
124           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
125
126   g_object_class_install_property (gobject_class, PROP_FILTER_BAND,
127       g_param_spec_float ("filter-band", "Filter Band",
128           "The Frequency band of the filter", 0.0, 441.0, DEFAULT_FILTER_BAND,
129           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
130
131   g_object_class_install_property (gobject_class, PROP_FILTER_WIDTH,
132       g_param_spec_float ("filter-width", "Filter Width",
133           "The Frequency width of the filter", 0.0, 100.0, DEFAULT_FILTER_WIDTH,
134           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
135
136   gst_element_class_set_details_simple (gstelement_class, "AudioKaraoke",
137       "Filter/Effect/Audio",
138       "Removes voice from sound", "Wim Taymans <wim.taymans@gmail.com>");
139
140   caps = gst_caps_from_string (ALLOWED_CAPS);
141   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
142       caps);
143   gst_caps_unref (caps);
144
145   GST_AUDIO_FILTER_CLASS (klass)->setup =
146       GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
147   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
148       GST_DEBUG_FUNCPTR (gst_audio_karaoke_transform_ip);
149 }
150
151 static void
152 gst_audio_karaoke_init (GstAudioKaraoke * filter)
153 {
154   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
155   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
156
157   filter->level = DEFAULT_LEVEL;
158   filter->mono_level = DEFAULT_MONO_LEVEL;
159   filter->filter_band = DEFAULT_FILTER_BAND;
160   filter->filter_width = DEFAULT_FILTER_WIDTH;
161 }
162
163 static void
164 update_filter (GstAudioKaraoke * filter, const GstAudioInfo * info)
165 {
166   gfloat A, B, C;
167   gint rate;
168
169   if (info) {
170     rate = GST_AUDIO_INFO_RATE (info);
171   } else {
172     rate = GST_AUDIO_FILTER_RATE (filter);
173   }
174
175   if (rate == 0)
176     return;
177
178   C = exp (-2 * G_PI * filter->filter_width / rate);
179   B = -4 * C / (1 + C) * cos (2 * G_PI * filter->filter_band / rate);
180   A = sqrt (1 - B * B / (4 * C)) * (1 - C);
181
182   filter->A = A;
183   filter->B = B;
184   filter->C = C;
185   filter->y1 = 0.0;
186   filter->y2 = 0.0;
187 }
188
189 static void
190 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
191     const GValue * value, GParamSpec * pspec)
192 {
193   GstAudioKaraoke *filter;
194
195   filter = GST_AUDIO_KARAOKE (object);
196
197   switch (prop_id) {
198     case PROP_LEVEL:
199       filter->level = g_value_get_float (value);
200       break;
201     case PROP_MONO_LEVEL:
202       filter->mono_level = g_value_get_float (value);
203       break;
204     case PROP_FILTER_BAND:
205       filter->filter_band = g_value_get_float (value);
206       update_filter (filter, NULL);
207       break;
208     case PROP_FILTER_WIDTH:
209       filter->filter_width = g_value_get_float (value);
210       update_filter (filter, NULL);
211       break;
212     default:
213       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214       break;
215   }
216 }
217
218 static void
219 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
220     GValue * value, GParamSpec * pspec)
221 {
222   GstAudioKaraoke *filter;
223
224   filter = GST_AUDIO_KARAOKE (object);
225
226   switch (prop_id) {
227     case PROP_LEVEL:
228       g_value_set_float (value, filter->level);
229       break;
230     case PROP_MONO_LEVEL:
231       g_value_set_float (value, filter->mono_level);
232       break;
233     case PROP_FILTER_BAND:
234       g_value_set_float (value, filter->filter_band);
235       break;
236     case PROP_FILTER_WIDTH:
237       g_value_set_float (value, filter->filter_width);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244
245 /* GstAudioFilter vmethod implementations */
246
247 static gboolean
248 gst_audio_karaoke_setup (GstAudioFilter * base, const GstAudioInfo * info)
249 {
250   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
251   gboolean ret = TRUE;
252
253   switch (GST_AUDIO_INFO_FORMAT (info)) {
254     case GST_AUDIO_FORMAT_S16:
255       filter->process = (GstAudioKaraokeProcessFunc)
256           gst_audio_karaoke_transform_int;
257       break;
258     case GST_AUDIO_FORMAT_F32:
259       filter->process = (GstAudioKaraokeProcessFunc)
260           gst_audio_karaoke_transform_float;
261       break;
262     default:
263       ret = FALSE;
264       break;
265   }
266   update_filter (filter, info);
267
268   return ret;
269 }
270
271 static void
272 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
273     gint16 * data, guint num_samples)
274 {
275   gint i, l, r, o, x;
276   gint channels;
277   gdouble y;
278   gint level;
279
280   channels = GST_AUDIO_FILTER_CHANNELS (filter);
281   level = filter->level * 256;
282
283   for (i = 0; i < num_samples; i += channels) {
284     /* get left and right inputs */
285     l = data[i];
286     r = data[i + 1];
287     /* do filtering */
288     x = (l + r) / 2;
289     y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
290     filter->y2 = filter->y1;
291     filter->y1 = y;
292     /* filter mono signal */
293     o = (int) (y * filter->mono_level);
294     o = CLAMP (o, G_MININT16, G_MAXINT16);
295     o = (o * level) >> 8;
296     /* now cut the center */
297     x = l - ((r * level) >> 8) + o;
298     r = r - ((l * level) >> 8) + o;
299     data[i] = CLAMP (x, G_MININT16, G_MAXINT16);
300     data[i + 1] = CLAMP (r, G_MININT16, G_MAXINT16);
301   }
302 }
303
304 static void
305 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
306     gfloat * data, guint num_samples)
307 {
308   gint i;
309   gint channels;
310   gdouble l, r, o;
311   gdouble y;
312
313   channels = GST_AUDIO_FILTER_CHANNELS (filter);
314
315   for (i = 0; i < num_samples; i += channels) {
316     /* get left and right inputs */
317     l = data[i];
318     r = data[i + 1];
319     /* do filtering */
320     y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
321         filter->C * filter->y2;
322     filter->y2 = filter->y1;
323     filter->y1 = y;
324     /* filter mono signal */
325     o = y * filter->mono_level * filter->level;
326     /* now cut the center */
327     data[i] = l - (r * filter->level) + o;
328     data[i + 1] = r - (l * filter->level) + o;
329   }
330 }
331
332 /* GstBaseTransform vmethod implementations */
333 static GstFlowReturn
334 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
335 {
336   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
337   guint num_samples;
338   GstClockTime timestamp, stream_time;
339   GstMapInfo map;
340
341   timestamp = GST_BUFFER_TIMESTAMP (buf);
342   stream_time =
343       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
344
345   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
346       GST_TIME_ARGS (timestamp));
347
348   if (GST_CLOCK_TIME_IS_VALID (stream_time))
349     gst_object_sync_values (GST_OBJECT (filter), stream_time);
350
351   if (gst_base_transform_is_passthrough (base) ||
352       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
353     return GST_FLOW_OK;
354
355   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
356   num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
357
358   filter->process (filter, map.data, num_samples);
359
360   gst_buffer_unmap (buf, &map);
361
362   return GST_FLOW_OK;
363 }