3 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
4 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-audioamplify
25 * Amplifies an audio stream by a given factor and allows the selection of different clipping modes.
26 * The difference between the clipping modes is best evaluated by testing.
29 * <title>Example launch line</title>
31 * gst-launch audiotestsrc wave=saw ! audioamplify amplification=1.5 ! alsasink
32 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audioamplify amplification=1.5 method=wrap-negative ! alsasink
33 * gst-launch audiotestsrc wave=saw ! audioconvert ! audioamplify amplification=1.5 method=wrap-positive ! audioconvert ! alsasink
43 #include <gst/base/gstbasetransform.h>
44 #include <gst/audio/audio.h>
45 #include <gst/audio/gstaudiofilter.h>
46 #include <gst/controller/gstcontroller.h>
48 #include "audioamplify.h"
50 #define GST_CAT_DEFAULT gst_audio_amplify_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 /* Filter signals and args */
76 #define GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD (gst_audio_amplify_clipping_method_get_type ())
78 gst_audio_amplify_clipping_method_get_type (void)
80 static GType gtype = 0;
83 static const GEnumValue values[] = {
84 {METHOD_CLIP, "Normal clipping (default)", "clip"},
85 {METHOD_WRAP_NEGATIVE,
86 "Push overdriven values back from the opposite side",
88 {METHOD_WRAP_POSITIVE, "Push overdriven values back from the same side",
90 {METHOD_NOCLIP, "No clipping", "none"},
94 /* FIXME 0.11: rename to GstAudioAmplifyClippingMethod */
95 gtype = g_enum_register_static ("GstAudioPanoramaClippingMethod", values);
100 #define ALLOWED_CAPS \
104 " endianness=(int)BYTE_ORDER," \
105 " signed=(bool)TRUE," \
106 " rate=(int)[1,MAX]," \
107 " channels=(int)[1,MAX]; " \
111 " endianness=(int)BYTE_ORDER," \
112 " signed=(bool)TRUE," \
113 " rate=(int)[1,MAX]," \
114 " channels=(int)[1,MAX]; " \
118 " endianness=(int)BYTE_ORDER," \
119 " signed=(bool)TRUE," \
120 " rate=(int)[1,MAX]," \
121 " channels=(int)[1,MAX]; " \
122 "audio/x-raw-float," \
123 " width=(int){32,64}," \
124 " endianness=(int)BYTE_ORDER," \
125 " rate=(int)[1,MAX]," \
126 " channels=(int)[1,MAX]"
128 #define DEBUG_INIT(bla) \
129 GST_DEBUG_CATEGORY_INIT (gst_audio_amplify_debug, "audioamplify", 0, "audioamplify element");
131 GST_BOILERPLATE_FULL (GstAudioAmplify, gst_audio_amplify, GstAudioFilter,
132 GST_TYPE_AUDIO_FILTER, DEBUG_INIT);
134 static gboolean gst_audio_amplify_set_process_function (GstAudioAmplify *
135 filter, gint clipping, gint format, gint width);
136 static void gst_audio_amplify_set_property (GObject * object, guint prop_id,
137 const GValue * value, GParamSpec * pspec);
138 static void gst_audio_amplify_get_property (GObject * object, guint prop_id,
139 GValue * value, GParamSpec * pspec);
141 static gboolean gst_audio_amplify_setup (GstAudioFilter * filter,
142 GstRingBufferSpec * format);
143 static GstFlowReturn gst_audio_amplify_transform_ip (GstBaseTransform * base,
146 #define MIN_gint8 G_MININT8
147 #define MAX_gint8 G_MAXINT8
148 #define MIN_gint16 G_MININT16
149 #define MAX_gint16 G_MAXINT16
150 #define MIN_gint32 G_MININT32
151 #define MAX_gint32 G_MAXINT32
153 #define MAKE_INT_FUNCS(type,largetype) \
155 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter, \
156 void * data, guint num_samples) \
160 while (num_samples--) { \
161 largetype val = *d * filter->amplification; \
162 *d++ = CLAMP (val, MIN_##type, MAX_##type); \
166 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify * filter, \
167 void * data, guint num_samples) \
171 while (num_samples--) { \
172 largetype val = *d * filter->amplification; \
173 if (val > MAX_##type) \
174 val = MIN_##type + (val - MIN_##type) % ((largetype) MAX_##type + 1 - \
176 else if (val < MIN_##type) \
177 val = MAX_##type - (MAX_##type - val) % ((largetype) MAX_##type + 1 - \
183 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
184 void * data, guint num_samples) \
188 while (num_samples--) { \
189 largetype val = *d * filter->amplification; \
191 if (val > MAX_##type) \
192 val = MAX_##type - (val - MAX_##type); \
193 else if (val < MIN_##type) \
194 val = MIN_##type + (MIN_##type - val); \
202 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter, \
203 void * data, guint num_samples) \
207 while (num_samples--) \
208 *d++ *= filter->amplification; \
211 #define MAKE_FLOAT_FUNCS(type) \
213 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter, \
214 void * data, guint num_samples) \
218 while (num_samples--) { \
219 type val = *d* filter->amplification; \
220 *d++ = CLAMP (val, -1.0, +1.0); \
224 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify * \
225 filter, void * data, guint num_samples) \
229 while (num_samples--) { \
230 type val = *d * filter->amplification; \
233 val = -1.0 + (val - 1.0); \
234 else if (val < -1.0) \
235 val = 1.0 - (1.0 - val); \
243 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
244 void * data, guint num_samples) \
248 while (num_samples--) { \
249 type val = *d* filter->amplification; \
252 val = 1.0 - (val - 1.0); \
253 else if (val < -1.0) \
254 val = -1.0 + (-1.0 - val); \
262 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter, \
263 void * data, guint num_samples) \
267 while (num_samples--) \
268 *d++ *= filter->amplification; \
272 MAKE_INT_FUNCS (gint8,gint)
273 MAKE_INT_FUNCS (gint16,gint)
274 MAKE_INT_FUNCS (gint32,gint64)
275 MAKE_FLOAT_FUNCS (gfloat)
276 MAKE_FLOAT_FUNCS (gdouble)
279 /* GObject vmethod implementations */
282 gst_audio_amplify_base_init (gpointer klass)
284 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
287 gst_element_class_set_details_simple (element_class, "Audio amplifier",
288 "Filter/Effect/Audio",
289 "Amplifies an audio stream by a given factor",
290 "Sebastian Dröge <slomo@circular-chaos.org>");
292 caps = gst_caps_from_string (ALLOWED_CAPS);
293 gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
295 gst_caps_unref (caps);
299 gst_audio_amplify_class_init (GstAudioAmplifyClass * klass)
301 GObjectClass *gobject_class;
303 gobject_class = (GObjectClass *) klass;
304 gobject_class->set_property = gst_audio_amplify_set_property;
305 gobject_class->get_property = gst_audio_amplify_get_property;
307 g_object_class_install_property (gobject_class, PROP_AMPLIFICATION,
308 g_param_spec_float ("amplification", "Amplification",
309 "Factor of amplification", -G_MAXFLOAT, G_MAXFLOAT,
311 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
314 * GstAudioAmplify:clipping-method
316 * Clipping method: clip mode set values higher than the maximum to the
317 * maximum. The wrap-negative mode pushes those values back from the
318 * opposite side, wrap-positive pushes them back from the same side.
321 g_object_class_install_property (gobject_class, PROP_CLIPPING_METHOD,
322 g_param_spec_enum ("clipping-method", "Clipping method",
323 "Selects how to handle values higher than the maximum",
324 GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD, METHOD_CLIP,
325 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 GST_AUDIO_FILTER_CLASS (klass)->setup =
328 GST_DEBUG_FUNCPTR (gst_audio_amplify_setup);
329 GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
330 GST_DEBUG_FUNCPTR (gst_audio_amplify_transform_ip);
334 gst_audio_amplify_init (GstAudioAmplify * filter, GstAudioAmplifyClass * klass)
336 filter->amplification = 1.0;
337 gst_audio_amplify_set_process_function (filter, METHOD_CLIP,
338 GST_BUFTYPE_LINEAR, 16);
339 gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
340 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
343 static GstAudioAmplifyProcessFunc
344 gst_audio_amplify_process_function (gint clipping, gint format, gint width)
346 static const struct process
351 GstAudioAmplifyProcessFunc func;
354 GST_BUFTYPE_FLOAT, 32, METHOD_CLIP,
355 gst_audio_amplify_transform_gfloat_clip}, {
356 GST_BUFTYPE_FLOAT, 32, METHOD_WRAP_NEGATIVE,
357 gst_audio_amplify_transform_gfloat_wrap_negative}, {
358 GST_BUFTYPE_FLOAT, 32, METHOD_WRAP_POSITIVE,
359 gst_audio_amplify_transform_gfloat_wrap_positive}, {
360 GST_BUFTYPE_FLOAT, 32, METHOD_NOCLIP,
361 gst_audio_amplify_transform_gfloat_noclip}, {
362 GST_BUFTYPE_FLOAT, 64, METHOD_CLIP,
363 gst_audio_amplify_transform_gdouble_clip}, {
364 GST_BUFTYPE_FLOAT, 64, METHOD_WRAP_NEGATIVE,
365 gst_audio_amplify_transform_gdouble_wrap_negative}, {
366 GST_BUFTYPE_FLOAT, 64, METHOD_WRAP_POSITIVE,
367 gst_audio_amplify_transform_gdouble_wrap_positive}, {
368 GST_BUFTYPE_FLOAT, 64, METHOD_NOCLIP,
369 gst_audio_amplify_transform_gdouble_noclip}, {
370 GST_BUFTYPE_LINEAR, 8, METHOD_CLIP, gst_audio_amplify_transform_gint8_clip}, {
371 GST_BUFTYPE_LINEAR, 8, METHOD_WRAP_NEGATIVE,
372 gst_audio_amplify_transform_gint8_wrap_negative}, {
373 GST_BUFTYPE_LINEAR, 8, METHOD_WRAP_POSITIVE,
374 gst_audio_amplify_transform_gint8_wrap_positive}, {
375 GST_BUFTYPE_LINEAR, 8, METHOD_NOCLIP,
376 gst_audio_amplify_transform_gint8_noclip}, {
377 GST_BUFTYPE_LINEAR, 16, METHOD_CLIP,
378 gst_audio_amplify_transform_gint16_clip}, {
379 GST_BUFTYPE_LINEAR, 16, METHOD_WRAP_NEGATIVE,
380 gst_audio_amplify_transform_gint16_wrap_negative}, {
381 GST_BUFTYPE_LINEAR, 16, METHOD_WRAP_POSITIVE,
382 gst_audio_amplify_transform_gint16_wrap_positive}, {
383 GST_BUFTYPE_LINEAR, 16, METHOD_NOCLIP,
384 gst_audio_amplify_transform_gint16_noclip}, {
385 GST_BUFTYPE_LINEAR, 32, METHOD_CLIP,
386 gst_audio_amplify_transform_gint32_clip}, {
387 GST_BUFTYPE_LINEAR, 32, METHOD_WRAP_NEGATIVE,
388 gst_audio_amplify_transform_gint32_wrap_negative}, {
389 GST_BUFTYPE_LINEAR, 32, METHOD_WRAP_POSITIVE,
390 gst_audio_amplify_transform_gint32_wrap_positive}, {
391 GST_BUFTYPE_LINEAR, 32, METHOD_NOCLIP,
392 gst_audio_amplify_transform_gint32_noclip}, {
395 const struct process *p;
397 for (p = process; p->func; p++)
398 if (p->format == format && p->width == width && p->clipping == clipping)
404 gst_audio_amplify_set_process_function (GstAudioAmplify * filter, gint
405 clipping_method, gint format, gint width)
407 GstAudioAmplifyProcessFunc process;
409 /* set processing function */
411 process = gst_audio_amplify_process_function (clipping_method, format, width);
413 GST_DEBUG ("wrong format");
417 filter->process = process;
418 filter->clipping_method = clipping_method;
419 filter->format = format;
420 filter->width = width;
426 gst_audio_amplify_set_property (GObject * object, guint prop_id,
427 const GValue * value, GParamSpec * pspec)
429 GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
432 case PROP_AMPLIFICATION:
433 filter->amplification = g_value_get_float (value);
434 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
435 filter->amplification == 1.0);
437 case PROP_CLIPPING_METHOD:
438 gst_audio_amplify_set_process_function (filter, g_value_get_enum (value),
439 filter->format, filter->width);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 gst_audio_amplify_get_property (GObject * object, guint prop_id,
449 GValue * value, GParamSpec * pspec)
451 GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
454 case PROP_AMPLIFICATION:
455 g_value_set_float (value, filter->amplification);
457 case PROP_CLIPPING_METHOD:
458 g_value_set_enum (value, filter->clipping_method);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
466 /* GstAudioFilter vmethod implementations */
468 gst_audio_amplify_setup (GstAudioFilter * base, GstRingBufferSpec * format)
470 GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
472 return gst_audio_amplify_set_process_function (filter,
473 filter->clipping_method, format->type, format->width);
476 /* GstBaseTransform vmethod implementations */
478 gst_audio_amplify_transform_ip (GstBaseTransform * base, GstBuffer * buf)
480 GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
482 GstClockTime timestamp, stream_time;
484 timestamp = GST_BUFFER_TIMESTAMP (buf);
486 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
488 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
489 GST_TIME_ARGS (timestamp));
491 if (GST_CLOCK_TIME_IS_VALID (stream_time))
492 gst_object_sync_values (G_OBJECT (filter), stream_time);
495 GST_BUFFER_SIZE (buf) / (GST_AUDIO_FILTER (filter)->format.width / 8);
497 if (gst_base_transform_is_passthrough (base) ||
498 G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
501 filter->process (filter, GST_BUFFER_DATA (buf), num_samples);