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