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