3 * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
22 * SECTION:element-audiokaraoke
24 * Remove the voice from audio by filtering the center channel.
25 * This plugin is useful for karaoke applications.
28 * <title>Example launch line</title>
30 * gst-launch filesrc location=song.ogg ! oggdemux ! vorbisdec ! audiokaraoke ! audioconvert ! alsasink
42 #include <gst/base/gstbasetransform.h>
43 #include <gst/audio/audio.h>
44 #include <gst/audio/gstaudiofilter.h>
45 #include <gst/controller/gstcontroller.h>
47 #include "audiokaraoke.h"
49 #define GST_CAT_DEFAULT gst_audio_karaoke_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 /* Filter signals and args */
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
74 #define ALLOWED_CAPS \
78 " endianness=(int)BYTE_ORDER," \
79 " signed=(bool)TRUE," \
80 " rate=(int)[1,MAX]," \
81 " channels=(int)[1,MAX]; " \
82 "audio/x-raw-float," \
84 " endianness=(int)BYTE_ORDER," \
85 " rate=(int)[1,MAX]," \
86 " channels=(int)[1,MAX]"
88 #define DEBUG_INIT(bla) \
89 GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0, "audiokaraoke element");
91 GST_BOILERPLATE_FULL (GstAudioKaraoke, gst_audio_karaoke, GstAudioFilter,
92 GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
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);
99 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
100 GstRingBufferSpec * format);
101 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
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);
109 /* GObject vmethod implementations */
112 gst_audio_karaoke_base_init (gpointer klass)
114 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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>");
121 caps = gst_caps_from_string (ALLOWED_CAPS);
122 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
124 gst_caps_unref (caps);
128 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
130 GObjectClass *gobject_class;
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;
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));
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));
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));
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));
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);
163 gst_audio_karaoke_init (GstAudioKaraoke * filter, GstAudioKaraokeClass * klass)
165 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
166 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
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;
175 update_filter (GstAudioKaraoke * filter, gint rate)
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);
194 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
195 const GValue * value, GParamSpec * pspec)
197 GstAudioKaraoke *filter;
199 filter = GST_AUDIO_KARAOKE (object);
203 filter->level = g_value_get_float (value);
205 case PROP_MONO_LEVEL:
206 filter->mono_level = g_value_get_float (value);
208 case PROP_FILTER_BAND:
209 filter->filter_band = g_value_get_float (value);
210 update_filter (filter, filter->rate);
212 case PROP_FILTER_WIDTH:
213 filter->filter_width = g_value_get_float (value);
214 update_filter (filter, filter->rate);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
224 GValue * value, GParamSpec * pspec)
226 GstAudioKaraoke *filter;
228 filter = GST_AUDIO_KARAOKE (object);
232 g_value_set_float (value, filter->level);
234 case PROP_MONO_LEVEL:
235 g_value_set_float (value, filter->mono_level);
237 case PROP_FILTER_BAND:
238 g_value_set_float (value, filter->filter_band);
240 case PROP_FILTER_WIDTH:
241 g_value_set_float (value, filter->filter_width);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 /* GstAudioFilter vmethod implementations */
252 gst_audio_karaoke_setup (GstAudioFilter * base, GstRingBufferSpec * format)
254 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
257 filter->channels = format->channels;
258 filter->rate = format->rate;
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;
269 update_filter (filter, format->rate);
275 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
276 gint16 * data, guint num_samples)
283 channels = filter->channels;
284 level = filter->level * 256;
286 for (i = 0; i < num_samples; i += channels) {
287 /* get left and right inputs */
292 y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
293 filter->y2 = filter->y1;
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);
308 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
309 gfloat * data, guint num_samples)
316 channels = filter->channels;
318 for (i = 0; i < num_samples; i += channels) {
319 /* get left and right inputs */
323 y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
324 filter->C * filter->y2;
325 filter->y2 = filter->y1;
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;
335 /* GstBaseTransform vmethod implementations */
337 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
339 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
341 GstClockTime timestamp, stream_time;
343 timestamp = GST_BUFFER_TIMESTAMP (buf);
345 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
347 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
348 GST_TIME_ARGS (timestamp));
350 if (GST_CLOCK_TIME_IS_VALID (stream_time))
351 gst_object_sync_values (G_OBJECT (filter), stream_time);
354 GST_BUFFER_SIZE (buf) / (GST_AUDIO_FILTER (filter)->format.width / 8);
356 if (gst_base_transform_is_passthrough (base) ||
357 G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
360 filter->process (filter, GST_BUFFER_DATA (buf), num_samples);