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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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-1.0 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>
46 #include "audiokaraoke.h"
48 #define GST_CAT_DEFAULT gst_audio_karaoke_debug
49 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
51 /* Filter signals and args */
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
72 #define ALLOWED_CAPS \
74 " format=(string){"GST_AUDIO_NE(S16)","GST_AUDIO_NE(F32)"}," \
75 " rate=(int)[1,MAX]," \
77 " channel-mask=(bitmask)0x3," \
78 " layout=(string) interleaved"
80 G_DEFINE_TYPE (GstAudioKaraoke, gst_audio_karaoke, GST_TYPE_AUDIO_FILTER);
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);
87 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
88 const GstAudioInfo * info);
89 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
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);
97 /* GObject vmethod implementations */
100 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
102 GObjectClass *gobject_class;
103 GstElementClass *gstelement_class;
106 GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0,
107 "audiokaraoke element");
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
112 gobject_class->set_property = gst_audio_karaoke_set_property;
113 gobject_class->get_property = gst_audio_karaoke_get_property;
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));
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));
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));
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));
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>");
139 caps = gst_caps_from_string (ALLOWED_CAPS);
140 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
142 gst_caps_unref (caps);
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;
148 GST_AUDIO_FILTER_CLASS (klass)->setup =
149 GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
153 gst_audio_karaoke_init (GstAudioKaraoke * filter)
155 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
156 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
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;
165 update_filter (GstAudioKaraoke * filter, const GstAudioInfo * info)
171 rate = GST_AUDIO_INFO_RATE (info);
173 rate = GST_AUDIO_FILTER_RATE (filter);
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);
191 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
192 const GValue * value, GParamSpec * pspec)
194 GstAudioKaraoke *filter;
196 filter = GST_AUDIO_KARAOKE (object);
200 filter->level = g_value_get_float (value);
202 case PROP_MONO_LEVEL:
203 filter->mono_level = g_value_get_float (value);
205 case PROP_FILTER_BAND:
206 filter->filter_band = g_value_get_float (value);
207 update_filter (filter, NULL);
209 case PROP_FILTER_WIDTH:
210 filter->filter_width = g_value_get_float (value);
211 update_filter (filter, NULL);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
221 GValue * value, GParamSpec * pspec)
223 GstAudioKaraoke *filter;
225 filter = GST_AUDIO_KARAOKE (object);
229 g_value_set_float (value, filter->level);
231 case PROP_MONO_LEVEL:
232 g_value_set_float (value, filter->mono_level);
234 case PROP_FILTER_BAND:
235 g_value_set_float (value, filter->filter_band);
237 case PROP_FILTER_WIDTH:
238 g_value_set_float (value, filter->filter_width);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 /* GstAudioFilter vmethod implementations */
249 gst_audio_karaoke_setup (GstAudioFilter * base, const GstAudioInfo * info)
251 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
254 switch (GST_AUDIO_INFO_FORMAT (info)) {
255 case GST_AUDIO_FORMAT_S16:
256 filter->process = (GstAudioKaraokeProcessFunc)
257 gst_audio_karaoke_transform_int;
259 case GST_AUDIO_FORMAT_F32:
260 filter->process = (GstAudioKaraokeProcessFunc)
261 gst_audio_karaoke_transform_float;
267 update_filter (filter, info);
273 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
274 gint16 * data, guint num_samples)
281 channels = GST_AUDIO_FILTER_CHANNELS (filter);
282 level = filter->level * 256;
284 for (i = 0; i < num_samples; i += channels) {
285 /* get left and right inputs */
290 y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
291 filter->y2 = filter->y1;
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);
306 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
307 gfloat * data, guint num_samples)
314 channels = GST_AUDIO_FILTER_CHANNELS (filter);
316 for (i = 0; i < num_samples; i += channels) {
317 /* get left and right inputs */
321 y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
322 filter->C * filter->y2;
323 filter->y2 = filter->y1;
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;
333 /* GstBaseTransform vmethod implementations */
335 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
337 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
339 GstClockTime timestamp, stream_time;
342 timestamp = GST_BUFFER_TIMESTAMP (buf);
344 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
346 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
347 GST_TIME_ARGS (timestamp));
349 if (GST_CLOCK_TIME_IS_VALID (stream_time))
350 gst_object_sync_values (GST_OBJECT (filter), stream_time);
352 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
355 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
356 num_samples = map.size / GST_AUDIO_FILTER_BPS (filter);
358 filter->process (filter, map.data, num_samples);
360 gst_buffer_unmap (buf, &map);