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-int,"                                                \
76     " depth=(int)16,"                                                 \
77     " width=(int)16,"                                                 \
78     " endianness=(int)BYTE_ORDER,"                                    \
79     " signed=(bool)TRUE,"                                             \
80     " rate=(int)[1,MAX],"                                             \
81     " channels=(int)[1,MAX]; "                                        \
82     "audio/x-raw-float,"                                              \
83     " width=(int)32,"                                                 \
84     " endianness=(int)BYTE_ORDER,"                                    \
85     " rate=(int)[1,MAX],"                                             \
86     " channels=(int)[1,MAX]"
87
88 G_DEFINE_TYPE (GstAudioKaraoke, gst_audio_karaoke, GST_TYPE_AUDIO_FILTER);
89
90 static void gst_audio_karaoke_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec);
92 static void gst_audio_karaoke_get_property (GObject * object, guint prop_id,
93     GValue * value, GParamSpec * pspec);
94
95 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
96     GstRingBufferSpec * format);
97 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
98     GstBuffer * buf);
99
100 static void gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
101     gint16 * data, guint num_samples);
102 static void gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
103     gfloat * data, guint num_samples);
104
105 /* GObject vmethod implementations */
106
107 static void
108 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
109 {
110   GObjectClass *gobject_class;
111   GstElementClass *gstelement_class;
112   GstCaps *caps;
113
114   GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0,
115       "audiokaraoke element");
116
117   gobject_class = (GObjectClass *) klass;
118   gstelement_class = (GstElementClass *) klass;
119
120   gobject_class->set_property = gst_audio_karaoke_set_property;
121   gobject_class->get_property = gst_audio_karaoke_get_property;
122
123   g_object_class_install_property (gobject_class, PROP_LEVEL,
124       g_param_spec_float ("level", "Level",
125           "Level of the effect (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
126           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
127
128   g_object_class_install_property (gobject_class, PROP_MONO_LEVEL,
129       g_param_spec_float ("mono-level", "Mono Level",
130           "Level of the mono channel (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
131           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
132
133   g_object_class_install_property (gobject_class, PROP_FILTER_BAND,
134       g_param_spec_float ("filter-band", "Filter Band",
135           "The Frequency band of the filter", 0.0, 441.0, DEFAULT_FILTER_BAND,
136           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
137
138   g_object_class_install_property (gobject_class, PROP_FILTER_WIDTH,
139       g_param_spec_float ("filter-width", "Filter Width",
140           "The Frequency width of the filter", 0.0, 100.0, DEFAULT_FILTER_WIDTH,
141           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
142
143   gst_element_class_set_details_simple (gstelement_class, "AudioKaraoke",
144       "Filter/Effect/Audio",
145       "Removes voice from sound", "Wim Taymans <wim.taymans@gmail.com>");
146
147   caps = gst_caps_from_string (ALLOWED_CAPS);
148   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
149       caps);
150   gst_caps_unref (caps);
151
152   GST_AUDIO_FILTER_CLASS (klass)->setup =
153       GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
154   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
155       GST_DEBUG_FUNCPTR (gst_audio_karaoke_transform_ip);
156 }
157
158 static void
159 gst_audio_karaoke_init (GstAudioKaraoke * filter)
160 {
161   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
162   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
163
164   filter->level = DEFAULT_LEVEL;
165   filter->mono_level = DEFAULT_MONO_LEVEL;
166   filter->filter_band = DEFAULT_FILTER_BAND;
167   filter->filter_width = DEFAULT_FILTER_WIDTH;
168 }
169
170 static void
171 update_filter (GstAudioKaraoke * filter, gint rate)
172 {
173   gfloat A, B, C;
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, filter->rate);
207       break;
208     case PROP_FILTER_WIDTH:
209       filter->filter_width = g_value_get_float (value);
210       update_filter (filter, filter->rate);
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, GstRingBufferSpec * format)
249 {
250   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
251   gboolean ret = TRUE;
252
253   filter->channels = format->channels;
254   filter->rate = format->rate;
255
256   if (format->type == GST_BUFTYPE_FLOAT && format->width == 32)
257     filter->process = (GstAudioKaraokeProcessFunc)
258         gst_audio_karaoke_transform_float;
259   else if (format->type == GST_BUFTYPE_LINEAR && format->width == 16)
260     filter->process = (GstAudioKaraokeProcessFunc)
261         gst_audio_karaoke_transform_int;
262   else
263     ret = FALSE;
264
265   update_filter (filter, format->rate);
266
267   return ret;
268 }
269
270 static void
271 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
272     gint16 * data, guint num_samples)
273 {
274   gint i, l, r, o, x;
275   gint channels;
276   gdouble y;
277   gint level;
278
279   channels = filter->channels;
280   level = filter->level * 256;
281
282   for (i = 0; i < num_samples; i += channels) {
283     /* get left and right inputs */
284     l = data[i];
285     r = data[i + 1];
286     /* do filtering */
287     x = (l + r) / 2;
288     y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
289     filter->y2 = filter->y1;
290     filter->y1 = y;
291     /* filter mono signal */
292     o = (int) (y * filter->mono_level);
293     o = CLAMP (o, G_MININT16, G_MAXINT16);
294     o = (o * level) >> 8;
295     /* now cut the center */
296     x = l - ((r * level) >> 8) + o;
297     r = r - ((l * level) >> 8) + o;
298     data[i] = CLAMP (x, G_MININT16, G_MAXINT16);
299     data[i + 1] = CLAMP (r, G_MININT16, G_MAXINT16);
300   }
301 }
302
303 static void
304 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
305     gfloat * data, guint num_samples)
306 {
307   gint i;
308   gint channels;
309   gdouble l, r, o;
310   gdouble y;
311
312   channels = filter->channels;
313
314   for (i = 0; i < num_samples; i += channels) {
315     /* get left and right inputs */
316     l = data[i];
317     r = data[i + 1];
318     /* do filtering */
319     y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
320         filter->C * filter->y2;
321     filter->y2 = filter->y1;
322     filter->y1 = y;
323     /* filter mono signal */
324     o = y * filter->mono_level * filter->level;
325     /* now cut the center */
326     data[i] = l - (r * filter->level) + o;
327     data[i + 1] = r - (l * filter->level) + o;
328   }
329 }
330
331 /* GstBaseTransform vmethod implementations */
332 static GstFlowReturn
333 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
334 {
335   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
336   guint num_samples;
337   GstClockTime timestamp, stream_time;
338   guint8 *data;
339   gsize size;
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 (G_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   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
356   num_samples = size / (GST_AUDIO_FILTER (filter)->format.width / 8);
357
358   filter->process (filter, data, num_samples);
359
360   gst_buffer_unmap (buf, data, size);
361
362   return GST_FLOW_OK;
363 }