013f33d1130a8fc9bf3fab77f7a0d1ebc2b4129d
[platform/upstream/gstreamer.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_static_metadata (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_BASE_TRANSFORM_CLASS (klass)->transform_ip =
146       GST_DEBUG_FUNCPTR (gst_audio_karaoke_transform_ip);
147   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip_on_passthrough = FALSE;
148
149   GST_AUDIO_FILTER_CLASS (klass)->setup =
150       GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
151 }
152
153 static void
154 gst_audio_karaoke_init (GstAudioKaraoke * filter)
155 {
156   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
157   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
158
159   filter->level = DEFAULT_LEVEL;
160   filter->mono_level = DEFAULT_MONO_LEVEL;
161   filter->filter_band = DEFAULT_FILTER_BAND;
162   filter->filter_width = DEFAULT_FILTER_WIDTH;
163 }
164
165 static void
166 update_filter (GstAudioKaraoke * filter, const GstAudioInfo * info)
167 {
168   gfloat A, B, C;
169   gint rate;
170
171   if (info) {
172     rate = GST_AUDIO_INFO_RATE (info);
173   } else {
174     rate = GST_AUDIO_FILTER_RATE (filter);
175   }
176
177   if (rate == 0)
178     return;
179
180   C = exp (-2 * G_PI * filter->filter_width / rate);
181   B = -4 * C / (1 + C) * cos (2 * G_PI * filter->filter_band / rate);
182   A = sqrt (1 - B * B / (4 * C)) * (1 - C);
183
184   filter->A = A;
185   filter->B = B;
186   filter->C = C;
187   filter->y1 = 0.0;
188   filter->y2 = 0.0;
189 }
190
191 static void
192 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
193     const GValue * value, GParamSpec * pspec)
194 {
195   GstAudioKaraoke *filter;
196
197   filter = GST_AUDIO_KARAOKE (object);
198
199   switch (prop_id) {
200     case PROP_LEVEL:
201       filter->level = g_value_get_float (value);
202       break;
203     case PROP_MONO_LEVEL:
204       filter->mono_level = g_value_get_float (value);
205       break;
206     case PROP_FILTER_BAND:
207       filter->filter_band = g_value_get_float (value);
208       update_filter (filter, NULL);
209       break;
210     case PROP_FILTER_WIDTH:
211       filter->filter_width = g_value_get_float (value);
212       update_filter (filter, NULL);
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220 static void
221 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224   GstAudioKaraoke *filter;
225
226   filter = GST_AUDIO_KARAOKE (object);
227
228   switch (prop_id) {
229     case PROP_LEVEL:
230       g_value_set_float (value, filter->level);
231       break;
232     case PROP_MONO_LEVEL:
233       g_value_set_float (value, filter->mono_level);
234       break;
235     case PROP_FILTER_BAND:
236       g_value_set_float (value, filter->filter_band);
237       break;
238     case PROP_FILTER_WIDTH:
239       g_value_set_float (value, filter->filter_width);
240       break;
241     default:
242       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243       break;
244   }
245 }
246
247 /* GstAudioFilter vmethod implementations */
248
249 static gboolean
250 gst_audio_karaoke_setup (GstAudioFilter * base, const GstAudioInfo * info)
251 {
252   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
253   gboolean ret = TRUE;
254
255   switch (GST_AUDIO_INFO_FORMAT (info)) {
256     case GST_AUDIO_FORMAT_S16:
257       filter->process = (GstAudioKaraokeProcessFunc)
258           gst_audio_karaoke_transform_int;
259       break;
260     case GST_AUDIO_FORMAT_F32:
261       filter->process = (GstAudioKaraokeProcessFunc)
262           gst_audio_karaoke_transform_float;
263       break;
264     default:
265       ret = FALSE;
266       break;
267   }
268   update_filter (filter, info);
269
270   return ret;
271 }
272
273 static void
274 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
275     gint16 * data, guint num_samples)
276 {
277   gint i, l, r, o, x;
278   gint channels;
279   gdouble y;
280   gint level;
281
282   channels = GST_AUDIO_FILTER_CHANNELS (filter);
283   level = filter->level * 256;
284
285   for (i = 0; i < num_samples; i += channels) {
286     /* get left and right inputs */
287     l = data[i];
288     r = data[i + 1];
289     /* do filtering */
290     x = (l + r) / 2;
291     y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
292     filter->y2 = filter->y1;
293     filter->y1 = y;
294     /* filter mono signal */
295     o = (int) (y * filter->mono_level);
296     o = CLAMP (o, G_MININT16, G_MAXINT16);
297     o = (o * level) >> 8;
298     /* now cut the center */
299     x = l - ((r * level) >> 8) + o;
300     r = r - ((l * level) >> 8) + o;
301     data[i] = CLAMP (x, G_MININT16, G_MAXINT16);
302     data[i + 1] = CLAMP (r, G_MININT16, G_MAXINT16);
303   }
304 }
305
306 static void
307 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
308     gfloat * data, guint num_samples)
309 {
310   gint i;
311   gint channels;
312   gdouble l, r, o;
313   gdouble y;
314
315   channels = GST_AUDIO_FILTER_CHANNELS (filter);
316
317   for (i = 0; i < num_samples; i += channels) {
318     /* get left and right inputs */
319     l = data[i];
320     r = data[i + 1];
321     /* do filtering */
322     y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
323         filter->C * filter->y2;
324     filter->y2 = filter->y1;
325     filter->y1 = y;
326     /* filter mono signal */
327     o = y * filter->mono_level * filter->level;
328     /* now cut the center */
329     data[i] = l - (r * filter->level) + o;
330     data[i + 1] = r - (l * filter->level) + o;
331   }
332 }
333
334 /* GstBaseTransform vmethod implementations */
335 static GstFlowReturn
336 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
337 {
338   GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
339   guint num_samples;
340   GstClockTime timestamp, stream_time;
341   GstMapInfo map;
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 (GST_OBJECT (filter), stream_time);
352
353   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
354     return GST_FLOW_OK;
355
356   gst_buffer_map (buf, &map, GST_MAP_READWRITE);
357   num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
358
359   filter->process (filter, map.data, num_samples);
360
361   gst_buffer_unmap (buf, &map);
362
363   return GST_FLOW_OK;
364 }