492a3c1b9269c240b11a1adc1e12240e849f7c88
[platform/upstream/gst-plugins-good.git] / gst / audiofx / audioamplify.c
1 /* 
2  * GStreamer
3  * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
4  * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-audioamplify
24  *
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.
27  *
28  * <refsect2>
29  * <title>Example launch line</title>
30  * |[
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
34  * ]|
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <gst/gst.h>
43 #include <gst/base/gstbasetransform.h>
44 #include <gst/audio/audio.h>
45 #include <gst/audio/gstaudiofilter.h>
46 #include <gst/controller/gstcontroller.h>
47
48 #include "audioamplify.h"
49
50 #define GST_CAT_DEFAULT gst_audio_amplify_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52
53 /* Filter signals and args */
54 enum
55 {
56   /* FILL ME */
57   LAST_SIGNAL
58 };
59
60 enum
61 {
62   PROP_0,
63   PROP_AMPLIFICATION,
64   PROP_CLIPPING_METHOD
65 };
66
67 enum
68 {
69   METHOD_CLIP = 0,
70   METHOD_WRAP_NEGATIVE,
71   METHOD_WRAP_POSITIVE,
72   METHOD_NOCLIP,
73   NUM_METHODS
74 };
75
76 #define GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD (gst_audio_amplify_clipping_method_get_type ())
77 static GType
78 gst_audio_amplify_clipping_method_get_type (void)
79 {
80   static GType gtype = 0;
81
82   if (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",
87           "wrap-negative"},
88       {METHOD_WRAP_POSITIVE, "Push overdriven values back from the same side",
89           "wrap-positive"},
90       {METHOD_NOCLIP, "No clipping", "none"},
91       {0, NULL, NULL}
92     };
93     gtype = g_enum_register_static ("GstAudioAmplifyClippingMethod", values);
94   }
95   return gtype;
96 }
97
98 #define ALLOWED_CAPS                                                  \
99     "audio/x-raw,"                                                    \
100     " format=(string) {S8,"GST_AUDIO_NE(S16)","GST_AUDIO_NE(S32)","   \
101                            GST_AUDIO_NE(F32)","GST_AUDIO_NE(F64)"},"  \
102     " rate=(int)[1,MAX],"                                             \
103     " channels=(int)[1,MAX]"
104
105 G_DEFINE_TYPE (GstAudioAmplify, gst_audio_amplify, GST_TYPE_AUDIO_FILTER);
106
107 static gboolean gst_audio_amplify_set_process_function (GstAudioAmplify *
108     filter, gint clipping, GstAudioFormat format);
109 static void gst_audio_amplify_set_property (GObject * object, guint prop_id,
110     const GValue * value, GParamSpec * pspec);
111 static void gst_audio_amplify_get_property (GObject * object, guint prop_id,
112     GValue * value, GParamSpec * pspec);
113
114 static gboolean gst_audio_amplify_setup (GstAudioFilter * filter,
115     GstAudioInfo * info);
116 static GstFlowReturn gst_audio_amplify_transform_ip (GstBaseTransform * base,
117     GstBuffer * buf);
118
119 #define MIN_gint8 G_MININT8
120 #define MAX_gint8 G_MAXINT8
121 #define MIN_gint16 G_MININT16
122 #define MAX_gint16 G_MAXINT16
123 #define MIN_gint32 G_MININT32
124 #define MAX_gint32 G_MAXINT32
125
126 #define MAKE_INT_FUNCS(type,largetype)                                        \
127 static void                                                                   \
128 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter,          \
129     void * data, guint num_samples)                                           \
130 {                                                                             \
131   type *d = data;                                                             \
132                                                                               \
133   while (num_samples--) {                                                     \
134     largetype val = *d * filter->amplification;                               \
135     *d++ =  CLAMP (val, MIN_##type, MAX_##type);                              \
136   }                                                                           \
137 }                                                                             \
138 static void                                                                   \
139 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify * filter, \
140     void * data, guint num_samples)                                           \
141 {                                                                             \
142   type *d = data;                                                             \
143                                                                               \
144   while (num_samples--) {                                                     \
145     largetype val = *d * filter->amplification;                               \
146     if (val > MAX_##type)                                                     \
147       val = MIN_##type + (val - MIN_##type) % ((largetype) MAX_##type + 1 -   \
148           MIN_##type);                                                        \
149     else if (val < MIN_##type)                                                \
150       val = MAX_##type - (MAX_##type - val) % ((largetype) MAX_##type + 1 -   \
151           MIN_##type);                                                        \
152     *d++ = val;                                                               \
153   }                                                                           \
154 }                                                                             \
155 static void                                                                   \
156 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
157     void * data, guint num_samples)                                           \
158 {                                                                             \
159   type *d = data;                                                             \
160                                                                               \
161   while (num_samples--) {                                                     \
162     largetype val = *d * filter->amplification;                               \
163     do {                                                                      \
164       if (val > MAX_##type)                                                   \
165         val = MAX_##type - (val - MAX_##type);                                \
166       else if (val < MIN_##type)                                              \
167         val = MIN_##type + (MIN_##type - val);                                \
168       else                                                                    \
169         break;                                                                \
170     } while (1);                                                              \
171     *d++ = val;                                                               \
172   }                                                                           \
173 }                                                                             \
174 static void                                                                   \
175 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
176     void * data, guint num_samples)                                           \
177 {                                                                             \
178   type *d = data;                                                             \
179                                                                               \
180   while (num_samples--)                                                       \
181     *d++ *= filter->amplification;                                            \
182 }
183
184 #define MAKE_FLOAT_FUNCS(type)                                                \
185 static void                                                                   \
186 gst_audio_amplify_transform_##type##_clip (GstAudioAmplify * filter,          \
187     void * data, guint num_samples)                                           \
188 {                                                                             \
189   type *d = data;                                                             \
190                                                                               \
191   while (num_samples--) {                                                     \
192     type val = *d* filter->amplification;                                     \
193     *d++ = CLAMP (val, -1.0, +1.0);                                           \
194   }                                                                           \
195 }                                                                             \
196 static void                                                                   \
197 gst_audio_amplify_transform_##type##_wrap_negative (GstAudioAmplify *         \
198     filter, void * data, guint num_samples)                                   \
199 {                                                                             \
200   type *d = data;                                                             \
201                                                                               \
202   while (num_samples--) {                                                     \
203     type val = *d * filter->amplification;                                    \
204     do {                                                                      \
205       if (val > 1.0)                                                          \
206         val = -1.0 + (val - 1.0);                                             \
207       else if (val < -1.0)                                                    \
208         val = 1.0 - (1.0 - val);                                              \
209       else                                                                    \
210         break;                                                                \
211     } while (1);                                                              \
212     *d++ = val;                                                               \
213   }                                                                           \
214 }                                                                             \
215 static void                                                                   \
216 gst_audio_amplify_transform_##type##_wrap_positive (GstAudioAmplify * filter, \
217     void * data, guint num_samples)                                           \
218 {                                                                             \
219   type *d = data;                                                             \
220                                                                               \
221   while (num_samples--) {                                                     \
222     type val = *d* filter->amplification;                                     \
223     do {                                                                      \
224       if (val > 1.0)                                                          \
225         val = 1.0 - (val - 1.0);                                              \
226       else if (val < -1.0)                                                    \
227         val = -1.0 + (-1.0 - val);                                            \
228       else                                                                    \
229         break;                                                                \
230     } while (1);                                                              \
231     *d++ = val;                                                               \
232   }                                                                           \
233 }                                                                             \
234 static void                                                                   \
235 gst_audio_amplify_transform_##type##_noclip (GstAudioAmplify * filter,        \
236     void * data, guint num_samples)                                           \
237 {                                                                             \
238   type *d = data;                                                             \
239                                                                               \
240   while (num_samples--)                                                       \
241     *d++ *= filter->amplification;                                            \
242 }
243
244 /* *INDENT-OFF* */
245 MAKE_INT_FUNCS (gint8,gint)
246 MAKE_INT_FUNCS (gint16,gint)
247 MAKE_INT_FUNCS (gint32,gint64)
248 MAKE_FLOAT_FUNCS (gfloat)
249 MAKE_FLOAT_FUNCS (gdouble)
250 /* *INDENT-ON* */
251
252 /* GObject vmethod implementations */
253
254 static void
255 gst_audio_amplify_class_init (GstAudioAmplifyClass * klass)
256 {
257   GObjectClass *gobject_class;
258   GstElementClass *gstelement_class;
259   GstCaps *caps;
260
261   GST_DEBUG_CATEGORY_INIT (gst_audio_amplify_debug, "audioamplify", 0,
262       "audioamplify element");
263
264   gobject_class = (GObjectClass *) klass;
265   gstelement_class = (GstElementClass *) klass;
266
267   gobject_class->set_property = gst_audio_amplify_set_property;
268   gobject_class->get_property = gst_audio_amplify_get_property;
269
270   g_object_class_install_property (gobject_class, PROP_AMPLIFICATION,
271       g_param_spec_float ("amplification", "Amplification",
272           "Factor of amplification", -G_MAXFLOAT, G_MAXFLOAT,
273           1.0,
274           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
275
276   /**
277    * GstAudioAmplify:clipping-method
278    *
279    * Clipping method: clip mode set values higher than the maximum to the
280    * maximum. The wrap-negative mode pushes those values back from the
281    * opposite side, wrap-positive pushes them back from the same side.
282    *
283    **/
284   g_object_class_install_property (gobject_class, PROP_CLIPPING_METHOD,
285       g_param_spec_enum ("clipping-method", "Clipping method",
286           "Selects how to handle values higher than the maximum",
287           GST_TYPE_AUDIO_AMPLIFY_CLIPPING_METHOD, METHOD_CLIP,
288           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289
290   gst_element_class_set_details_simple (gstelement_class, "Audio amplifier",
291       "Filter/Effect/Audio",
292       "Amplifies an audio stream by a given factor",
293       "Sebastian Dröge <slomo@circular-chaos.org>");
294
295   caps = gst_caps_from_string (ALLOWED_CAPS);
296   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
297       caps);
298   gst_caps_unref (caps);
299
300   GST_AUDIO_FILTER_CLASS (klass)->setup =
301       GST_DEBUG_FUNCPTR (gst_audio_amplify_setup);
302   GST_BASE_TRANSFORM_CLASS (klass)->transform_ip =
303       GST_DEBUG_FUNCPTR (gst_audio_amplify_transform_ip);
304 }
305
306 static void
307 gst_audio_amplify_init (GstAudioAmplify * filter)
308 {
309   filter->amplification = 1.0;
310   gst_audio_amplify_set_process_function (filter, METHOD_CLIP,
311       GST_AUDIO_FORMAT_S16);
312   gst_base_transform_set_in_place (GST_BASE_TRANSFORM (filter), TRUE);
313   gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
314 }
315
316 static GstAudioAmplifyProcessFunc
317 gst_audio_amplify_process_function (gint clipping, GstAudioFormat format)
318 {
319   static const struct process
320   {
321     GstAudioFormat format;
322     gint clipping;
323     GstAudioAmplifyProcessFunc func;
324   } process[] = {
325     {
326     GST_AUDIO_FORMAT_F32, METHOD_CLIP, gst_audio_amplify_transform_gfloat_clip}, {
327     GST_AUDIO_FORMAT_F32, METHOD_WRAP_NEGATIVE,
328           gst_audio_amplify_transform_gfloat_wrap_negative}, {
329     GST_AUDIO_FORMAT_F32, METHOD_WRAP_POSITIVE,
330           gst_audio_amplify_transform_gfloat_wrap_positive}, {
331     GST_AUDIO_FORMAT_F32, METHOD_NOCLIP,
332           gst_audio_amplify_transform_gfloat_noclip}, {
333     GST_AUDIO_FORMAT_F64, METHOD_CLIP,
334           gst_audio_amplify_transform_gdouble_clip}, {
335     GST_AUDIO_FORMAT_F64, METHOD_WRAP_NEGATIVE,
336           gst_audio_amplify_transform_gdouble_wrap_negative}, {
337     GST_AUDIO_FORMAT_F64, METHOD_WRAP_POSITIVE,
338           gst_audio_amplify_transform_gdouble_wrap_positive}, {
339     GST_AUDIO_FORMAT_F64, METHOD_NOCLIP,
340           gst_audio_amplify_transform_gdouble_noclip}, {
341     GST_AUDIO_FORMAT_S8, METHOD_CLIP, gst_audio_amplify_transform_gint8_clip}, {
342     GST_AUDIO_FORMAT_S8, METHOD_WRAP_NEGATIVE,
343           gst_audio_amplify_transform_gint8_wrap_negative}, {
344     GST_AUDIO_FORMAT_S8, METHOD_WRAP_POSITIVE,
345           gst_audio_amplify_transform_gint8_wrap_positive}, {
346     GST_AUDIO_FORMAT_S8, METHOD_NOCLIP,
347           gst_audio_amplify_transform_gint8_noclip}, {
348     GST_AUDIO_FORMAT_S16, METHOD_CLIP, gst_audio_amplify_transform_gint16_clip}, {
349     GST_AUDIO_FORMAT_S16, METHOD_WRAP_NEGATIVE,
350           gst_audio_amplify_transform_gint16_wrap_negative}, {
351     GST_AUDIO_FORMAT_S16, METHOD_WRAP_POSITIVE,
352           gst_audio_amplify_transform_gint16_wrap_positive}, {
353     GST_AUDIO_FORMAT_S16, METHOD_NOCLIP,
354           gst_audio_amplify_transform_gint16_noclip}, {
355     GST_AUDIO_FORMAT_S32, METHOD_CLIP, gst_audio_amplify_transform_gint32_clip}, {
356     GST_AUDIO_FORMAT_S32, METHOD_WRAP_NEGATIVE,
357           gst_audio_amplify_transform_gint32_wrap_negative}, {
358     GST_AUDIO_FORMAT_S32, METHOD_WRAP_POSITIVE,
359           gst_audio_amplify_transform_gint32_wrap_positive}, {
360     GST_AUDIO_FORMAT_S32, METHOD_NOCLIP,
361           gst_audio_amplify_transform_gint32_noclip}, {
362     0, 0, NULL}
363   };
364   const struct process *p;
365
366   for (p = process; p->func; p++)
367     if (p->format == format && p->clipping == clipping)
368       return p->func;
369   return NULL;
370 }
371
372 static gboolean
373 gst_audio_amplify_set_process_function (GstAudioAmplify * filter, gint
374     clipping_method, GstAudioFormat format)
375 {
376   GstAudioAmplifyProcessFunc process;
377
378   /* set processing function */
379
380   process = gst_audio_amplify_process_function (clipping_method, format);
381   if (!process) {
382     GST_DEBUG ("wrong format");
383     return FALSE;
384   }
385
386   filter->process = process;
387   filter->clipping_method = clipping_method;
388   filter->format = format;
389
390   return TRUE;
391 }
392
393 static void
394 gst_audio_amplify_set_property (GObject * object, guint prop_id,
395     const GValue * value, GParamSpec * pspec)
396 {
397   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
398
399   switch (prop_id) {
400     case PROP_AMPLIFICATION:
401       filter->amplification = g_value_get_float (value);
402       gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter),
403           filter->amplification == 1.0);
404       break;
405     case PROP_CLIPPING_METHOD:
406       gst_audio_amplify_set_process_function (filter, g_value_get_enum (value),
407           filter->format);
408       break;
409     default:
410       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
411       break;
412   }
413 }
414
415 static void
416 gst_audio_amplify_get_property (GObject * object, guint prop_id,
417     GValue * value, GParamSpec * pspec)
418 {
419   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (object);
420
421   switch (prop_id) {
422     case PROP_AMPLIFICATION:
423       g_value_set_float (value, filter->amplification);
424       break;
425     case PROP_CLIPPING_METHOD:
426       g_value_set_enum (value, filter->clipping_method);
427       break;
428     default:
429       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
430       break;
431   }
432 }
433
434 /* GstAudioFilter vmethod implementations */
435 static gboolean
436 gst_audio_amplify_setup (GstAudioFilter * base, GstAudioInfo * info)
437 {
438   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
439
440   return gst_audio_amplify_set_process_function (filter,
441       filter->clipping_method, GST_AUDIO_INFO_FORMAT (info));
442 }
443
444 /* GstBaseTransform vmethod implementations */
445 static GstFlowReturn
446 gst_audio_amplify_transform_ip (GstBaseTransform * base, GstBuffer * buf)
447 {
448   GstAudioAmplify *filter = GST_AUDIO_AMPLIFY (base);
449   guint num_samples;
450   GstClockTime timestamp, stream_time;
451   guint8 *data;
452   gsize size;
453
454   timestamp = GST_BUFFER_TIMESTAMP (buf);
455   stream_time =
456       gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
457
458   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
459       GST_TIME_ARGS (timestamp));
460
461   if (GST_CLOCK_TIME_IS_VALID (stream_time))
462     gst_object_sync_values (G_OBJECT (filter), stream_time);
463
464   if (gst_base_transform_is_passthrough (base) ||
465       G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))
466     return GST_FLOW_OK;
467
468   data = gst_buffer_map (buf, &size, NULL, GST_MAP_READWRITE);
469   num_samples = size / GST_AUDIO_FILTER_BPS (filter);
470
471   filter->process (filter, data, num_samples);
472
473   gst_buffer_unmap (buf, data, size);
474
475   return GST_FLOW_OK;
476 }