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