Merge branch 'master' into 0.11
[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 #include <gst/controller/gstcontroller.h>
46
47 #include "audiokaraoke.h"
48
49 #define GST_CAT_DEFAULT gst_audio_karaoke_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
51
52 /* Filter signals and args */
53 enum
54 {
55   /* FILL ME */
56   LAST_SIGNAL
57 };
58
59 #define DEFAULT_LEVEL           1.0
60 #define DEFAULT_MONO_LEVEL      1.0
61 #define DEFAULT_FILTER_BAND     220.0
62 #define DEFAULT_FILTER_WIDTH    100.0
63
64 enum
65 {
66   PROP_0,
67   PROP_LEVEL,
68   PROP_MONO_LEVEL,
69   PROP_FILTER_BAND,
70   PROP_FILTER_WIDTH,
71   PROP_LAST
72 };
73
74 #define ALLOWED_CAPS \
75     "audio/x-raw,"                                                \
76     " format=(string){"GST_AUDIO_NE(S16)","GST_AUDIO_NE(F32)"},"  \
77     " rate=(int)[1,MAX],"                                         \
78     " channels=(int)[1,MAX]"
79
80 G_DEFINE_TYPE (GstAudioKaraoke, gst_audio_karaoke, GST_TYPE_AUDIO_FILTER);
81
82 static void gst_audio_karaoke_set_property (GObject * object, guint prop_id,
83     const GValue * value, GParamSpec * pspec);
84 static void gst_audio_karaoke_get_property (GObject * object, guint prop_id,
85     GValue * value, GParamSpec * pspec);
86
87 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
88     const GstAudioInfo * info);
89 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
90     GstBuffer * buf);
91
92 static void gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
93     gint16 * data, guint num_samples);
94 static void gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
95     gfloat * data, guint num_samples);
96
97 /* GObject vmethod implementations */
98
99 static void
100 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104   GstCaps *caps;
105
106   GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0,
107       "audiokaraoke element");
108
109   gobject_class = (GObjectClass *) klass;
110   gstelement_class = (GstElementClass *) klass;
111
112   gobject_class->set_property = gst_audio_karaoke_set_property;
113   gobject_class->get_property = gst_audio_karaoke_get_property;
114
115   g_object_class_install_property (gobject_class, PROP_LEVEL,
116       g_param_spec_float ("level", "Level",
117           "Level of the effect (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
118           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
119
120   g_object_class_install_property (gobject_class, PROP_MONO_LEVEL,
121       g_param_spec_float ("mono-level", "Mono Level",
122           "Level of the mono channel (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
123           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
124
125   g_object_class_install_property (gobject_class, PROP_FILTER_BAND,
126       g_param_spec_float ("filter-band", "Filter Band",
127           "The Frequency band of the filter", 0.0, 441.0, DEFAULT_FILTER_BAND,
128           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
129
130   g_object_class_install_property (gobject_class, PROP_FILTER_WIDTH,
131       g_param_spec_float ("filter-width", "Filter Width",
132           "The Frequency width of the filter", 0.0, 100.0, DEFAULT_FILTER_WIDTH,
133           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
134
135   gst_element_class_set_details_simple (gstelement_class, "AudioKaraoke",
136       "Filter/Effect/Audio",
137       "Removes voice from sound", "Wim Taymans <wim.taymans@gmail.com>");
138
139   caps = gst_caps_from_string (ALLOWED_CAPS);
140   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
141       caps);
142   gst_caps_unref (caps);
143
144   GST_AUDIO_FILTER_CLASS (klass)->setup =
145       GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
146   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
147       GST_DEBUG_FUNCPTR (gst_audio_karaoke_transform_ip);
148 }
149
150 static void
151 gst_audio_karaoke_init (GstAudioKaraoke * filter)
152 {
153   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
154   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
155
156   filter->level = DEFAULT_LEVEL;
157   filter->mono_level = DEFAULT_MONO_LEVEL;
158   filter->filter_band = DEFAULT_FILTER_BAND;
159   filter->filter_width = DEFAULT_FILTER_WIDTH;
160 }
161
162 static void
163 update_filter (GstAudioKaraoke * filter)
164 {
165   gfloat A, B, C;
166   gint rate;
167
168   rate = GST_AUDIO_FILTER_RATE (filter);
169   if (rate == 0)
170     return;
171
172   C = exp (-2 * G_PI * filter->filter_width / rate);
173   B = -4 * C / (1 + C) * cos (2 * G_PI * filter->filter_band / rate);
174   A = sqrt (1 - B * B / (4 * C)) * (1 - C);
175
176   filter->A = A;
177   filter->B = B;
178   filter->C = C;
179   filter->y1 = 0.0;
180   filter->y2 = 0.0;
181 }
182
183 static void
184 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
185     const GValue * value, GParamSpec * pspec)
186 {
187   GstAudioKaraoke *filter;
188
189   filter = GST_AUDIO_KARAOKE (object);
190
191   switch (prop_id) {
192     case PROP_LEVEL:
193       filter->level = g_value_get_float (value);
194       break;
195     case PROP_MONO_LEVEL:
196       filter->mono_level = g_value_get_float (value);
197       break;
198     case PROP_FILTER_BAND:
199       filter->filter_band = g_value_get_float (value);
200       update_filter (filter);
201       break;
202     case PROP_FILTER_WIDTH:
203       filter->filter_width = g_value_get_float (value);
204       update_filter (filter);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208       break;
209   }
210 }
211
212 static void
213 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
214     GValue * value, GParamSpec * pspec)
215 {
216   GstAudioKaraoke *filter;
217
218   filter = GST_AUDIO_KARAOKE (object);
219
220   switch (prop_id) {
221     case PROP_LEVEL:
222       g_value_set_float (value, filter->level);
223       break;
224     case PROP_MONO_LEVEL:
225       g_value_set_float (value, filter->mono_level);
226       break;
227     case PROP_FILTER_BAND:
228       g_value_set_float (value, filter->filter_band);
229       break;
230     case PROP_FILTER_WIDTH:
231       g_value_set_float (value, filter->filter_width);
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235       break;
236   }
237 }
238
239 /* GstAudioFilter vmethod implementations */
240
241 static gboolean
242 gst_audio_karaoke_setup (GstAudioFilter * base, const GstAudioInfo * info)
243 {
244   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
245   gboolean ret = TRUE;
246
247   switch (GST_AUDIO_INFO_FORMAT (info)) {
248     case GST_AUDIO_FORMAT_S16:
249       filter->process = (GstAudioKaraokeProcessFunc)
250           gst_audio_karaoke_transform_int;
251       break;
252     case GST_AUDIO_FORMAT_F32:
253       filter->process = (GstAudioKaraokeProcessFunc)
254           gst_audio_karaoke_transform_float;
255       break;
256     default:
257       ret = FALSE;
258       break;
259   }
260   update_filter (filter);
261
262   return ret;
263 }
264
265 static void
266 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
267     gint16 * data, guint num_samples)
268 {
269   gint i, l, r, o, x;
270   gint channels;
271   gdouble y;
272   gint level;
273
274   channels = GST_AUDIO_FILTER_CHANNELS (filter);
275   level = filter->level * 256;
276
277   for (i = 0; i < num_samples; i += channels) {
278     /* get left and right inputs */
279     l = data[i];
280     r = data[i + 1];
281     /* do filtering */
282     x = (l + r) / 2;
283     y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
284     filter->y2 = filter->y1;
285     filter->y1 = y;
286     /* filter mono signal */
287     o = (int) (y * filter->mono_level);
288     o = CLAMP (o, G_MININT16, G_MAXINT16);
289     o = (o * level) >> 8;
290     /* now cut the center */
291     x = l - ((r * level) >> 8) + o;
292     r = r - ((l * level) >> 8) + o;
293     data[i] = CLAMP (x, G_MININT16, G_MAXINT16);
294     data[i + 1] = CLAMP (r, G_MININT16, G_MAXINT16);
295   }
296 }
297
298 static void
299 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
300     gfloat * data, guint num_samples)
301 {
302   gint i;
303   gint channels;
304   gdouble l, r, o;
305   gdouble y;
306
307   channels = GST_AUDIO_FILTER_CHANNELS (filter);
308
309   for (i = 0; i < num_samples; i += channels) {
310     /* get left and right inputs */
311     l = data[i];
312     r = data[i + 1];
313     /* do filtering */
314     y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
315         filter->C * filter->y2;
316     filter->y2 = filter->y1;
317     filter->y1 = y;
318     /* filter mono signal */
319     o = y * filter->mono_level * filter->level;
320     /* now cut the center */
321     data[i] = l - (r * filter->level) + o;
322     data[i + 1] = r - (l * filter->level) + o;
323   }
324 }
325
326 /* GstBaseTransform vmethod implementations */
327 static GstFlowReturn
328 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
329 {
330   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
331   guint num_samples;
332   GstClockTime timestamp, stream_time;
333   guint8 *data;
334   gsize size;
335
336   timestamp = GST_BUFFER_TIMESTAMP (buf);
337   stream_time =
338       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
339
340   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
341       GST_TIME_ARGS (timestamp));
342
343   if (GST_CLOCK_TIME_IS_VALID (stream_time))
344     gst_object_sync_values (G_OBJECT (filter), stream_time);
345
346   if (gst_base_transform_is_passthrough (base) ||
347       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
348     return GST_FLOW_OK;
349
350   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
351   num_samples = size / GST_AUDIO_FILTER_BPS (filter);
352
353   filter->process (filter, data, num_samples);
354
355   gst_buffer_unmap (buf, data, size);
356
357   return GST_FLOW_OK;
358 }