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 G_DEFINE_TYPE (GstAudioKaraoke, gst_audio_karaoke, GST_TYPE_AUDIO_FILTER);
90 static void gst_audio_karaoke_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_audio_karaoke_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 static gboolean gst_audio_karaoke_setup (GstAudioFilter * filter,
96 GstRingBufferSpec * format);
97 static GstFlowReturn gst_audio_karaoke_transform_ip (GstBaseTransform * base,
100 static void gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
101 gint16 * data, guint num_samples);
102 static void gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
103 gfloat * data, guint num_samples);
105 /* GObject vmethod implementations */
108 gst_audio_karaoke_class_init (GstAudioKaraokeClass * klass)
110 GObjectClass *gobject_class;
111 GstElementClass *gstelement_class;
114 GST_DEBUG_CATEGORY_INIT (gst_audio_karaoke_debug, "audiokaraoke", 0,
115 "audiokaraoke element");
117 gobject_class = (GObjectClass *) klass;
118 gstelement_class = (GstElementClass *) klass;
120 gobject_class->set_property = gst_audio_karaoke_set_property;
121 gobject_class->get_property = gst_audio_karaoke_get_property;
123 g_object_class_install_property (gobject_class, PROP_LEVEL,
124 g_param_spec_float ("level", "Level",
125 "Level of the effect (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
126 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
128 g_object_class_install_property (gobject_class, PROP_MONO_LEVEL,
129 g_param_spec_float ("mono-level", "Mono Level",
130 "Level of the mono channel (1.0 = full)", 0.0, 1.0, DEFAULT_LEVEL,
131 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
133 g_object_class_install_property (gobject_class, PROP_FILTER_BAND,
134 g_param_spec_float ("filter-band", "Filter Band",
135 "The Frequency band of the filter", 0.0, 441.0, DEFAULT_FILTER_BAND,
136 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
138 g_object_class_install_property (gobject_class, PROP_FILTER_WIDTH,
139 g_param_spec_float ("filter-width", "Filter Width",
140 "The Frequency width of the filter", 0.0, 100.0, DEFAULT_FILTER_WIDTH,
141 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
143 gst_element_class_set_details_simple (gstelement_class, "AudioKaraoke",
144 "Filter/Effect/Audio",
145 "Removes voice from sound", "Wim Taymans <wim.taymans@gmail.com>");
147 caps = gst_caps_from_string (ALLOWED_CAPS);
148 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
150 gst_caps_unref (caps);
152 GST_AUDIO_FILTER_CLASS (klass)->setup =
153 GST_DEBUG_FUNCPTR (gst_audio_karaoke_setup);
154 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
155 GST_DEBUG_FUNCPTR (gst_audio_karaoke_transform_ip);
159 gst_audio_karaoke_init (GstAudioKaraoke * filter)
161 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
162 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
164 filter->level = DEFAULT_LEVEL;
165 filter->mono_level = DEFAULT_MONO_LEVEL;
166 filter->filter_band = DEFAULT_FILTER_BAND;
167 filter->filter_width = DEFAULT_FILTER_WIDTH;
171 update_filter (GstAudioKaraoke * filter, gint rate)
178 C = exp (-2 * G_PI * filter->filter_width / rate);
179 B = -4 * C / (1 + C) * cos (2 * G_PI * filter->filter_band / rate);
180 A = sqrt (1 - B * B / (4 * C)) * (1 - C);
190 gst_audio_karaoke_set_property (GObject * object, guint prop_id,
191 const GValue * value, GParamSpec * pspec)
193 GstAudioKaraoke *filter;
195 filter = GST_AUDIO_KARAOKE (object);
199 filter->level = g_value_get_float (value);
201 case PROP_MONO_LEVEL:
202 filter->mono_level = g_value_get_float (value);
204 case PROP_FILTER_BAND:
205 filter->filter_band = g_value_get_float (value);
206 update_filter (filter, filter->rate);
208 case PROP_FILTER_WIDTH:
209 filter->filter_width = g_value_get_float (value);
210 update_filter (filter, filter->rate);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gst_audio_karaoke_get_property (GObject * object, guint prop_id,
220 GValue * value, GParamSpec * pspec)
222 GstAudioKaraoke *filter;
224 filter = GST_AUDIO_KARAOKE (object);
228 g_value_set_float (value, filter->level);
230 case PROP_MONO_LEVEL:
231 g_value_set_float (value, filter->mono_level);
233 case PROP_FILTER_BAND:
234 g_value_set_float (value, filter->filter_band);
236 case PROP_FILTER_WIDTH:
237 g_value_set_float (value, filter->filter_width);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 /* GstAudioFilter vmethod implementations */
248 gst_audio_karaoke_setup (GstAudioFilter * base, GstRingBufferSpec * format)
250 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
253 filter->channels = format->channels;
254 filter->rate = format->rate;
256 if (format->type == GST_BUFTYPE_FLOAT && format->width == 32)
257 filter->process = (GstAudioKaraokeProcessFunc)
258 gst_audio_karaoke_transform_float;
259 else if (format->type == GST_BUFTYPE_LINEAR && format->width == 16)
260 filter->process = (GstAudioKaraokeProcessFunc)
261 gst_audio_karaoke_transform_int;
265 update_filter (filter, format->rate);
271 gst_audio_karaoke_transform_int (GstAudioKaraoke * filter,
272 gint16 * data, guint num_samples)
279 channels = filter->channels;
280 level = filter->level * 256;
282 for (i = 0; i < num_samples; i += channels) {
283 /* get left and right inputs */
288 y = (filter->A * x - filter->B * filter->y1) - filter->C * filter->y2;
289 filter->y2 = filter->y1;
291 /* filter mono signal */
292 o = (int) (y * filter->mono_level);
293 o = CLAMP (o, G_MININT16, G_MAXINT16);
294 o = (o * level) >> 8;
295 /* now cut the center */
296 x = l - ((r * level) >> 8) + o;
297 r = r - ((l * level) >> 8) + o;
298 data[i] = CLAMP (x, G_MININT16, G_MAXINT16);
299 data[i + 1] = CLAMP (r, G_MININT16, G_MAXINT16);
304 gst_audio_karaoke_transform_float (GstAudioKaraoke * filter,
305 gfloat * data, guint num_samples)
312 channels = filter->channels;
314 for (i = 0; i < num_samples; i += channels) {
315 /* get left and right inputs */
319 y = (filter->A * ((l + r) / 2.0) - filter->B * filter->y1) -
320 filter->C * filter->y2;
321 filter->y2 = filter->y1;
323 /* filter mono signal */
324 o = y * filter->mono_level * filter->level;
325 /* now cut the center */
326 data[i] = l - (r * filter->level) + o;
327 data[i + 1] = r - (l * filter->level) + o;
331 /* GstBaseTransform vmethod implementations */
333 gst_audio_karaoke_transform_ip (GstBaseTransform * base, GstBuffer * buf)
335 GstAudioKaraoke *filter = GST_AUDIO_KARAOKE (base);
337 GstClockTime timestamp, stream_time;
341 timestamp = GST_BUFFER_TIMESTAMP (buf);
343 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
345 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
346 GST_TIME_ARGS (timestamp));
348 if (GST_CLOCK_TIME_IS_VALID (stream_time))
349 gst_object_sync_values (G_OBJECT (filter), stream_time);
351 if (gst_base_transform_is_passthrough (base) ||
352 G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
355 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
356 num_samples = size / (GST_AUDIO_FILTER (filter)->format.width / 8);
358 filter->process (filter, data, num_samples);
360 gst_buffer_unmap (buf, data, size);