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