1 /* -*- c-basic-offset: 2 -*-
2 * vi:si:et:sw=2:sts=8:ts=8:expandtab
5 * Copyright (C) 1999-2001 Erik Walthinsen <omega@cse.ogi.edu>
6 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
7 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-volume
28 * The volume element changes the volume of the audio data.
31 * <title>Example launch line</title>
33 * gst-launch -v -m audiotestsrc ! volume volume=0.5 ! level ! fakesink silent=TRUE
34 * ]| This pipeline shows that the level of audiotestsrc has been halved
35 * (peak values are around -6 dB and RMS around -9 dB) compared to
36 * the same pipeline without the volume element.
46 #include <gst/base/gstbasetransform.h>
47 #include <gst/audio/audio.h>
48 #include <gst/interfaces/mixer.h>
49 #include <gst/controller/gstcontroller.h>
50 #include <gst/audio/audio.h>
51 #include <gst/audio/gstaudiofilter.h>
54 #include <orc/orcfunctions.h>
56 #define orc_memset memset
59 #include "gstvolumeorc.h"
60 #include "gstvolume.h"
62 /* some defines for audio processing */
63 /* the volume factor is a range from 0.0 to (arbitrary) VOLUME_MAX_DOUBLE = 10.0
64 * we map 1.0 to VOLUME_UNITY_INT*
66 #define VOLUME_UNITY_INT8 8 /* internal int for unity 2^(8-5) */
67 #define VOLUME_UNITY_INT8_BIT_SHIFT 3 /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT16 2048 /* internal int for unity 2^(16-5) */
69 #define VOLUME_UNITY_INT16_BIT_SHIFT 11 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT24 524288 /* internal int for unity 2^(24-5) */
71 #define VOLUME_UNITY_INT24_BIT_SHIFT 19 /* number of bits to shift for unity */
72 #define VOLUME_UNITY_INT32 134217728 /* internal int for unity 2^(32-5) */
73 #define VOLUME_UNITY_INT32_BIT_SHIFT 27
74 #define VOLUME_MAX_DOUBLE 10.0
75 #define VOLUME_MAX_INT8 G_MAXINT8
76 #define VOLUME_MIN_INT8 G_MININT8
77 #define VOLUME_MAX_INT16 G_MAXINT16
78 #define VOLUME_MIN_INT16 G_MININT16
79 #define VOLUME_MAX_INT24 8388607
80 #define VOLUME_MIN_INT24 -8388608
81 #define VOLUME_MAX_INT32 G_MAXINT32
82 #define VOLUME_MIN_INT32 G_MININT32
84 /* number of steps we use for the mixer interface to go from 0.0 to 1.0 */
85 # define VOLUME_STEPS 100
87 #define GST_CAT_DEFAULT gst_volume_debug
88 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
90 /* Filter signals and args */
97 #define DEFAULT_PROP_MUTE FALSE
98 #define DEFAULT_PROP_VOLUME 1.0
107 #define ALLOWED_CAPS \
108 "audio/x-raw-float, " \
109 "rate = (int) [ 1, MAX ], " \
110 "channels = (int) [ 1, MAX ], " \
111 "endianness = (int) BYTE_ORDER, " \
112 "width = (int) {32, 64}; " \
113 "audio/x-raw-int, " \
114 "channels = (int) [ 1, MAX ], " \
115 "rate = (int) [ 1, MAX ], " \
116 "endianness = (int) BYTE_ORDER, " \
117 "width = (int) 8, " \
118 "depth = (int) 8, " \
119 "signed = (bool) TRUE; " \
120 "audio/x-raw-int, " \
121 "channels = (int) [ 1, MAX ], " \
122 "rate = (int) [ 1, MAX ], " \
123 "endianness = (int) BYTE_ORDER, " \
124 "width = (int) 16, " \
125 "depth = (int) 16, " \
126 "signed = (bool) TRUE; " \
127 "audio/x-raw-int, " \
128 "channels = (int) [ 1, MAX ], " \
129 "rate = (int) [ 1, MAX ], " \
130 "endianness = (int) BYTE_ORDER, " \
131 "width = (int) 24, " \
132 "depth = (int) 24, " \
133 "signed = (bool) TRUE; " \
134 "audio/x-raw-int, " \
135 "channels = (int) [ 1, MAX ], " \
136 "rate = (int) [ 1, MAX ], " \
137 "endianness = (int) BYTE_ORDER, " \
138 "width = (int) 32, " \
139 "depth = (int) 32, " \
140 "signed = (bool) TRUE;" \
141 "audio/x-raw-int, " \
142 "channels = (int) [ 1, MAX ], " \
143 "rate = (int) [ 1, MAX ], " \
144 "endianness = (int) BYTE_ORDER, " \
145 "width = (int) 32, " \
146 "depth = (int) 24, " \
147 "signed = (bool) TRUE"
149 static void gst_volume_interface_init (GstImplementsInterfaceClass * klass);
150 static void gst_volume_mixer_init (GstMixerClass * iface);
152 #define _init_interfaces(type) \
154 static const GInterfaceInfo voliface_info = { \
155 (GInterfaceInitFunc) gst_volume_interface_init, \
159 static const GInterfaceInfo volmixer_info = { \
160 (GInterfaceInitFunc) gst_volume_mixer_init, \
164 static const GInterfaceInfo svol_info = { \
170 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
172 g_type_add_interface_static (type, GST_TYPE_MIXER, &volmixer_info); \
173 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_info); \
176 GST_BOILERPLATE_FULL (GstVolume, gst_volume, GstAudioFilter,
177 GST_TYPE_AUDIO_FILTER, _init_interfaces);
179 static void volume_set_property (GObject * object, guint prop_id,
180 const GValue * value, GParamSpec * pspec);
181 static void volume_get_property (GObject * object, guint prop_id,
182 GValue * value, GParamSpec * pspec);
184 static void volume_before_transform (GstBaseTransform * base,
186 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
188 static gboolean volume_stop (GstBaseTransform * base);
189 static gboolean volume_setup (GstAudioFilter * filter,
190 GstRingBufferSpec * format);
192 static void volume_process_double (GstVolume * self, gpointer bytes,
194 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
195 gdouble * volume, guint channels, guint n_bytes);
196 static void volume_process_float (GstVolume * self, gpointer bytes,
198 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
199 gdouble * volume, guint channels, guint n_bytes);
200 static void volume_process_int32 (GstVolume * self, gpointer bytes,
202 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
204 static void volume_process_controlled_int32_clamp (GstVolume * self,
205 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
206 static void volume_process_int24 (GstVolume * self, gpointer bytes,
208 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
210 static void volume_process_controlled_int24_clamp (GstVolume * self,
211 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
212 static void volume_process_int16 (GstVolume * self, gpointer bytes,
214 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
216 static void volume_process_controlled_int16_clamp (GstVolume * self,
217 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
218 static void volume_process_int8 (GstVolume * self, gpointer bytes,
220 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
222 static void volume_process_controlled_int8_clamp (GstVolume * self,
223 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
226 /* helper functions */
229 volume_choose_func (GstVolume * self)
231 self->process = NULL;
232 self->process_controlled = NULL;
234 if (GST_AUDIO_FILTER (self)->format.caps == NULL)
237 switch (GST_AUDIO_FILTER (self)->format.type) {
238 case GST_BUFTYPE_LINEAR:
239 switch (GST_AUDIO_FILTER (self)->format.width) {
241 /* only clamp if the gain is greater than 1.0
243 if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
244 self->process = volume_process_int32_clamp;
246 self->process = volume_process_int32;
248 self->process_controlled = volume_process_controlled_int32_clamp;
251 /* only clamp if the gain is greater than 1.0
253 if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
254 self->process = volume_process_int24_clamp;
256 self->process = volume_process_int24;
258 self->process_controlled = volume_process_controlled_int24_clamp;
261 /* only clamp if the gain is greater than 1.0
263 if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
264 self->process = volume_process_int16_clamp;
266 self->process = volume_process_int16;
268 self->process_controlled = volume_process_controlled_int16_clamp;
271 /* only clamp if the gain is greater than 1.0
273 if (self->current_vol_i8 > VOLUME_UNITY_INT8) {
274 self->process = volume_process_int8_clamp;
276 self->process = volume_process_int8;
278 self->process_controlled = volume_process_controlled_int8_clamp;
282 case GST_BUFTYPE_FLOAT:
283 switch (GST_AUDIO_FILTER (self)->format.width) {
285 self->process = volume_process_float;
286 self->process_controlled = volume_process_controlled_float;
289 self->process = volume_process_double;
290 self->process_controlled = volume_process_controlled_double;
298 return (self->process != NULL);
302 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
304 gboolean passthrough;
306 GstController *controller;
308 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
311 self->current_mute = TRUE;
312 self->current_volume = 0.0;
314 self->current_vol_i8 = 0;
315 self->current_vol_i16 = 0;
316 self->current_vol_i24 = 0;
317 self->current_vol_i32 = 0;
321 self->current_mute = FALSE;
322 self->current_volume = volume;
324 self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
325 self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
326 self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
327 self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
329 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
332 /* If a controller is used, never use passthrough mode
333 * because the property can change from 1.0 to something
334 * else in the middle of a buffer.
336 controller = gst_object_get_controller (G_OBJECT (self));
337 passthrough = passthrough && (controller == NULL);
339 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
341 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
343 res = self->negotiated = volume_choose_func (self);
348 /* Mixer interface */
351 gst_volume_interface_supported (GstImplementsInterface * iface, GType type)
353 return (type == GST_TYPE_MIXER || type == GST_TYPE_STREAM_VOLUME);
357 gst_volume_interface_init (GstImplementsInterfaceClass * klass)
359 klass->supported = gst_volume_interface_supported;
363 gst_volume_list_tracks (GstMixer * mixer)
365 GstVolume *self = GST_VOLUME (mixer);
367 g_return_val_if_fail (self != NULL, NULL);
368 g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
370 return self->tracklist;
374 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
376 GstVolume *self = GST_VOLUME (mixer);
378 g_return_if_fail (self != NULL);
379 g_return_if_fail (GST_IS_VOLUME (self));
381 GST_OBJECT_LOCK (self);
382 self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
383 GST_OBJECT_UNLOCK (self);
387 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
389 GstVolume *self = GST_VOLUME (mixer);
391 g_return_if_fail (self != NULL);
392 g_return_if_fail (GST_IS_VOLUME (self));
394 GST_OBJECT_LOCK (self);
395 volumes[0] = (gint) self->volume * VOLUME_STEPS;
396 GST_OBJECT_UNLOCK (self);
400 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
402 GstVolume *self = GST_VOLUME (mixer);
404 g_return_if_fail (self != NULL);
405 g_return_if_fail (GST_IS_VOLUME (self));
407 GST_OBJECT_LOCK (self);
409 GST_OBJECT_UNLOCK (self);
413 gst_volume_mixer_init (GstMixerClass * klass)
415 GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
417 /* default virtual functions */
418 klass->list_tracks = gst_volume_list_tracks;
419 klass->set_volume = gst_volume_set_volume;
420 klass->get_volume = gst_volume_get_volume;
421 klass->set_mute = gst_volume_set_mute;
427 gst_volume_dispose (GObject * object)
429 GstVolume *volume = GST_VOLUME (object);
431 if (volume->tracklist) {
432 if (volume->tracklist->data)
433 g_object_unref (volume->tracklist->data);
434 g_list_free (volume->tracklist);
435 volume->tracklist = NULL;
438 G_OBJECT_CLASS (parent_class)->dispose (object);
442 gst_volume_base_init (gpointer g_class)
444 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
445 GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (g_class);
448 gst_element_class_set_details_simple (element_class, "Volume",
449 "Filter/Effect/Audio",
450 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
452 caps = gst_caps_from_string (ALLOWED_CAPS);
453 gst_audio_filter_class_add_pad_templates (filter_class, caps);
454 gst_caps_unref (caps);
458 gst_volume_class_init (GstVolumeClass * klass)
460 GObjectClass *gobject_class;
461 GstBaseTransformClass *trans_class;
462 GstAudioFilterClass *filter_class;
464 gobject_class = (GObjectClass *) klass;
465 trans_class = (GstBaseTransformClass *) klass;
466 filter_class = (GstAudioFilterClass *) (klass);
468 gobject_class->set_property = volume_set_property;
469 gobject_class->get_property = volume_get_property;
470 gobject_class->dispose = gst_volume_dispose;
472 g_object_class_install_property (gobject_class, PROP_MUTE,
473 g_param_spec_boolean ("mute", "Mute", "mute channel",
475 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
477 g_object_class_install_property (gobject_class, PROP_VOLUME,
478 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
479 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
480 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
482 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
483 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
484 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
485 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
489 gst_volume_init (GstVolume * self, GstVolumeClass * g_class)
491 GstMixerTrack *track = NULL;
493 self->mute = DEFAULT_PROP_MUTE;;
494 self->volume = DEFAULT_PROP_VOLUME;
496 self->tracklist = NULL;
497 self->negotiated = FALSE;
499 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
501 if (GST_IS_MIXER_TRACK (track)) {
502 track->label = g_strdup ("volume");
503 track->num_channels = 1;
504 track->min_volume = 0;
505 track->max_volume = VOLUME_STEPS;
506 track->flags = GST_MIXER_TRACK_SOFTWARE;
507 self->tracklist = g_list_append (self->tracklist, track);
510 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
514 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
516 gdouble *data = (gdouble *) bytes;
517 guint num_samples = n_bytes / sizeof (gdouble);
519 orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
523 volume_process_controlled_double (GstVolume * self, gpointer bytes,
524 gdouble * volume, guint channels, guint n_bytes)
526 gdouble *data = (gdouble *) bytes;
527 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
532 orc_process_controlled_f64_1ch (data, volume, num_samples);
534 for (i = 0; i < num_samples; i++) {
536 for (j = 0; j < channels; j++) {
544 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
546 gfloat *data = (gfloat *) bytes;
547 guint num_samples = n_bytes / sizeof (gfloat);
549 orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
553 volume_process_controlled_float (GstVolume * self, gpointer bytes,
554 gdouble * volume, guint channels, guint n_bytes)
556 gfloat *data = (gfloat *) bytes;
557 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
562 orc_process_controlled_f32_1ch (data, volume, num_samples);
563 } else if (channels == 2) {
564 orc_process_controlled_f32_2ch (data, volume, num_samples);
566 for (i = 0; i < num_samples; i++) {
568 for (j = 0; j < channels; j++) {
576 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
578 gint32 *data = (gint32 *) bytes;
579 guint num_samples = n_bytes / sizeof (gint);
581 /* hard coded in volume.orc */
582 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
583 orc_process_int32 (data, self->current_vol_i32, num_samples);
587 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
589 gint32 *data = (gint32 *) bytes;
590 guint num_samples = n_bytes / sizeof (gint);
592 /* hard coded in volume.orc */
593 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
595 orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
599 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
600 gdouble * volume, guint channels, guint n_bytes)
602 gint32 *data = (gint32 *) bytes;
604 guint num_samples = n_bytes / (sizeof (gint32) * channels);
608 orc_process_controlled_int32_1ch (data, volume, num_samples);
610 for (i = 0; i < num_samples; i++) {
612 for (j = 0; j < channels; j++) {
614 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
620 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
621 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
623 #define write_unaligned_u24(_x,samp) \
625 *(_x)++ = samp & 0xFF; \
626 *(_x)++ = (samp >> 8) & 0xFF; \
627 *(_x)++ = (samp >> 16) & 0xFF; \
630 #else /* BIG ENDIAN */
631 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
632 #define write_unaligned_u24(_x,samp) \
634 *(_x)++ = (samp >> 16) & 0xFF; \
635 *(_x)++ = (samp >> 8) & 0xFF; \
636 *(_x)++ = samp & 0xFF; \
641 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
643 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
644 guint i, num_samples;
648 num_samples = n_bytes / (sizeof (gint8) * 3);
649 for (i = 0; i < num_samples; i++) {
650 samp = get_unaligned_i24 (data);
654 (((gint64) self->current_vol_i24 *
655 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
656 samp = (guint32) val;
658 /* write the value back into the stream */
659 write_unaligned_u24 (data, samp);
664 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
666 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
667 guint i, num_samples;
671 num_samples = n_bytes / (sizeof (gint8) * 3);
672 for (i = 0; i < num_samples; i++) {
673 samp = get_unaligned_i24 (data);
677 (((gint64) self->current_vol_i24 *
678 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
679 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
681 /* write the value back into the stream */
682 write_unaligned_u24 (data, samp);
687 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
688 gdouble * volume, guint channels, guint n_bytes)
690 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
692 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
695 for (i = 0; i < num_samples; i++) {
697 for (j = 0; j < channels; j++) {
698 val = get_unaligned_i24 (data) * vol;
699 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
700 write_unaligned_u24 (data, (gint32) val);
706 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
708 gint16 *data = (gint16 *) bytes;
709 guint num_samples = n_bytes / sizeof (gint16);
711 /* hard coded in volume.orc */
712 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
714 orc_process_int16 (data, self->current_vol_i16, num_samples);
718 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
720 gint16 *data = (gint16 *) bytes;
721 guint num_samples = n_bytes / sizeof (gint16);
723 /* hard coded in volume.orc */
724 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
726 orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
730 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
731 gdouble * volume, guint channels, guint n_bytes)
733 gint16 *data = (gint16 *) bytes;
735 guint num_samples = n_bytes / (sizeof (gint16) * channels);
739 orc_process_controlled_int16_1ch (data, volume, num_samples);
740 } else if (channels == 2) {
741 orc_process_controlled_int16_2ch (data, volume, num_samples);
743 for (i = 0; i < num_samples; i++) {
745 for (j = 0; j < channels; j++) {
747 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
754 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
756 gint8 *data = (gint8 *) bytes;
757 guint num_samples = n_bytes / sizeof (gint8);
759 /* hard coded in volume.orc */
760 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
762 orc_process_int8 (data, self->current_vol_i8, num_samples);
766 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
768 gint8 *data = (gint8 *) bytes;
769 guint num_samples = n_bytes / sizeof (gint8);
771 /* hard coded in volume.orc */
772 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
774 orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
778 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
779 gdouble * volume, guint channels, guint n_bytes)
781 gint8 *data = (gint8 *) bytes;
783 guint num_samples = n_bytes / (sizeof (gint8) * channels);
787 orc_process_controlled_int8_1ch (data, volume, num_samples);
788 } else if (channels == 2) {
789 orc_process_controlled_int8_2ch (data, volume, num_samples);
791 for (i = 0; i < num_samples; i++) {
793 for (j = 0; j < channels; j++) {
795 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
801 /* GstBaseTransform vmethod implementations */
803 /* get notified of caps and plug in the correct process function */
805 volume_setup (GstAudioFilter * filter, GstRingBufferSpec * format)
808 GstVolume *self = GST_VOLUME (filter);
812 GST_OBJECT_LOCK (self);
813 volume = self->volume;
815 GST_OBJECT_UNLOCK (self);
817 res = volume_update_volume (self, volume, mute);
819 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
820 ("Invalid incoming format"), (NULL));
822 self->negotiated = res;
828 volume_stop (GstBaseTransform * base)
830 GstVolume *self = GST_VOLUME (base);
832 g_free (self->volumes);
833 self->volumes = NULL;
834 self->volumes_count = 0;
836 g_free (self->mutes);
838 self->mutes_count = 0;
840 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
845 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
847 GstClockTime timestamp;
848 GstVolume *self = GST_VOLUME (base);
852 timestamp = GST_BUFFER_TIMESTAMP (buffer);
854 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
856 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
857 GST_TIME_ARGS (timestamp));
859 if (GST_CLOCK_TIME_IS_VALID (timestamp))
860 gst_object_sync_values (G_OBJECT (self), timestamp);
862 /* get latest values */
863 GST_OBJECT_LOCK (self);
864 volume = self->volume;
866 GST_OBJECT_UNLOCK (self);
868 if ((volume != self->current_volume) || (mute != self->current_mute)) {
869 /* the volume or mute was updated, update our internal state before
870 * we continue processing. */
871 volume_update_volume (self, volume, mute);
875 /* call the plugged-in process function for this instance
876 * needs to be done with this indirection since volume_transform is
877 * a class-global method
880 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
882 GstVolume *self = GST_VOLUME (base);
885 GstControlSource *mute_csource, *volume_csource;
887 if (G_UNLIKELY (!self->negotiated))
890 /* don't process data in passthrough-mode */
891 if (gst_base_transform_is_passthrough (base) ||
892 GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
895 data = GST_BUFFER_DATA (outbuf);
896 size = GST_BUFFER_SIZE (outbuf);
898 mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
899 volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
900 if (mute_csource || (volume_csource && !self->current_mute)) {
901 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
902 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
903 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
904 guint nsamples = size / (width * channels);
905 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
906 GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
907 gboolean use_mutes = FALSE;
909 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
911 if (self->mutes_count < nsamples && mute_csource) {
912 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
913 self->mutes_count = nsamples;
916 if (self->volumes_count < nsamples) {
917 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
918 self->volumes_count = nsamples;
922 GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
924 if (!gst_control_source_get_value_array (mute_csource, ts, &va))
925 goto controller_failure;
927 gst_object_unref (mute_csource);
931 g_free (self->mutes);
933 self->mutes_count = 0;
936 if (volume_csource) {
938 { "volume", nsamples, interval, (gpointer) self->volumes };
940 if (!gst_control_source_get_value_array (volume_csource, ts, &va))
941 goto controller_failure;
943 gst_object_unref (volume_csource);
944 volume_csource = NULL;
946 orc_memset_f64 (self->volumes, self->current_volume, nsamples);
950 orc_prepare_volumes (self->volumes, self->mutes, nsamples);
953 self->process_controlled (self, data, self->volumes, channels, size);
956 } else if (volume_csource) {
957 gst_object_unref (volume_csource);
960 if (self->current_volume == 0.0 || self->current_mute) {
961 orc_memset (data, 0, size);
962 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
963 } else if (self->current_volume != 1.0) {
964 self->process (self, data, size);
972 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
973 ("No format was negotiated"), (NULL));
974 return GST_FLOW_NOT_NEGOTIATED;
979 gst_object_unref (mute_csource);
981 gst_object_unref (volume_csource);
983 GST_ELEMENT_ERROR (self, CORE, FAILED,
984 ("Failed to get values from controller"), (NULL));
985 return GST_FLOW_ERROR;
990 volume_set_property (GObject * object, guint prop_id, const GValue * value,
993 GstVolume *self = GST_VOLUME (object);
997 GST_OBJECT_LOCK (self);
998 self->mute = g_value_get_boolean (value);
999 GST_OBJECT_UNLOCK (self);
1002 GST_OBJECT_LOCK (self);
1003 self->volume = g_value_get_double (value);
1004 GST_OBJECT_UNLOCK (self);
1007 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1013 volume_get_property (GObject * object, guint prop_id, GValue * value,
1016 GstVolume *self = GST_VOLUME (object);
1020 GST_OBJECT_LOCK (self);
1021 g_value_set_boolean (value, self->mute);
1022 GST_OBJECT_UNLOCK (self);
1025 GST_OBJECT_LOCK (self);
1026 g_value_set_double (value, self->volume);
1027 GST_OBJECT_UNLOCK (self);
1030 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1036 plugin_init (GstPlugin * plugin)
1038 gst_volume_orc_init ();
1040 /* initialize gst controller library */
1041 gst_controller_init (NULL, NULL);
1043 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
1045 /* ref class from a thread-safe context to work around missing bit of
1046 * thread-safety in GObject */
1047 g_type_class_ref (GST_TYPE_MIXER_TRACK);
1049 return gst_element_register (plugin, "volume", GST_RANK_NONE,
1053 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1056 "plugin for controlling audio volume",
1057 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);