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