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 32 /* internal int for unity 2^(8-3) */
67 #define VOLUME_UNITY_INT8_BIT_SHIFT 5 /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT16 8192 /* internal int for unity 2^(16-3) */
69 #define VOLUME_UNITY_INT16_BIT_SHIFT 13 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT24 2097152 /* internal int for unity 2^(24-3) */
71 #define VOLUME_UNITY_INT24_BIT_SHIFT 21 /* 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"
142 static void gst_volume_interface_init (GstImplementsInterfaceClass * klass);
143 static void gst_volume_mixer_init (GstMixerClass * iface);
145 #define gst_volume_parent_class parent_class
146 G_DEFINE_TYPE_WITH_CODE (GstVolume, gst_volume,
147 GST_TYPE_AUDIO_FILTER, G_IMPLEMENT_INTERFACE (GST_TYPE_IMPLEMENTS_INTERFACE,
148 gst_volume_interface_init);
149 G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_volume_mixer_init);
150 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
152 static void volume_set_property (GObject * object, guint prop_id,
153 const GValue * value, GParamSpec * pspec);
154 static void volume_get_property (GObject * object, guint prop_id,
155 GValue * value, GParamSpec * pspec);
157 static void volume_before_transform (GstBaseTransform * base,
159 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
161 static gboolean volume_stop (GstBaseTransform * base);
162 static gboolean volume_setup (GstAudioFilter * filter,
163 GstRingBufferSpec * format);
165 static void volume_process_double (GstVolume * self, gpointer bytes,
167 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
168 gdouble * volume, guint channels, guint n_bytes);
169 static void volume_process_float (GstVolume * self, gpointer bytes,
171 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
172 gdouble * volume, guint channels, guint n_bytes);
173 static void volume_process_int32 (GstVolume * self, gpointer bytes,
175 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
177 static void volume_process_controlled_int32_clamp (GstVolume * self,
178 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
179 static void volume_process_int24 (GstVolume * self, gpointer bytes,
181 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
183 static void volume_process_controlled_int24_clamp (GstVolume * self,
184 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
185 static void volume_process_int16 (GstVolume * self, gpointer bytes,
187 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
189 static void volume_process_controlled_int16_clamp (GstVolume * self,
190 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
191 static void volume_process_int8 (GstVolume * self, gpointer bytes,
193 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
195 static void volume_process_controlled_int8_clamp (GstVolume * self,
196 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
199 /* helper functions */
202 volume_choose_func (GstVolume * self)
204 self->process = NULL;
205 self->process_controlled = NULL;
207 if (GST_AUDIO_FILTER (self)->format.caps == NULL)
210 switch (GST_AUDIO_FILTER (self)->format.type) {
211 case GST_BUFTYPE_LINEAR:
212 switch (GST_AUDIO_FILTER (self)->format.width) {
214 /* only clamp if the gain is greater than 1.0
216 if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
217 self->process = volume_process_int32_clamp;
219 self->process = volume_process_int32;
221 self->process_controlled = volume_process_controlled_int32_clamp;
224 /* only clamp if the gain is greater than 1.0
226 if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
227 self->process = volume_process_int24_clamp;
229 self->process = volume_process_int24;
231 self->process_controlled = volume_process_controlled_int24_clamp;
234 /* only clamp if the gain is greater than 1.0
236 if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
237 self->process = volume_process_int16_clamp;
239 self->process = volume_process_int16;
241 self->process_controlled = volume_process_controlled_int16_clamp;
244 /* only clamp if the gain is greater than 1.0
246 if (self->current_vol_i16 > VOLUME_UNITY_INT8) {
247 self->process = volume_process_int8_clamp;
249 self->process = volume_process_int8;
251 self->process_controlled = volume_process_controlled_int8_clamp;
255 case GST_BUFTYPE_FLOAT:
256 switch (GST_AUDIO_FILTER (self)->format.width) {
258 self->process = volume_process_float;
259 self->process_controlled = volume_process_controlled_float;
262 self->process = volume_process_double;
263 self->process_controlled = volume_process_controlled_double;
271 return (self->process != NULL);
275 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
277 gboolean passthrough;
279 GstController *controller;
281 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
284 self->current_mute = TRUE;
285 self->current_volume = 0.0;
287 self->current_vol_i8 = 0;
288 self->current_vol_i16 = 0;
289 self->current_vol_i24 = 0;
290 self->current_vol_i32 = 0;
294 self->current_mute = FALSE;
295 self->current_volume = volume;
297 self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
298 self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
299 self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
300 self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
302 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
305 /* If a controller is used, never use passthrough mode
306 * because the property can change from 1.0 to something
307 * else in the middle of a buffer.
309 controller = gst_object_get_controller (G_OBJECT (self));
310 passthrough = passthrough && (controller == NULL);
312 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
314 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
316 res = self->negotiated = volume_choose_func (self);
321 /* Mixer interface */
324 gst_volume_interface_supported (GstImplementsInterface * iface, GType type)
326 return (type == GST_TYPE_MIXER || type == GST_TYPE_STREAM_VOLUME);
330 gst_volume_interface_init (GstImplementsInterfaceClass * klass)
332 klass->supported = gst_volume_interface_supported;
336 gst_volume_list_tracks (GstMixer * mixer)
338 GstVolume *self = GST_VOLUME (mixer);
340 g_return_val_if_fail (self != NULL, NULL);
341 g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
343 return self->tracklist;
347 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
349 GstVolume *self = GST_VOLUME (mixer);
351 g_return_if_fail (self != NULL);
352 g_return_if_fail (GST_IS_VOLUME (self));
354 GST_OBJECT_LOCK (self);
355 self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
356 GST_OBJECT_UNLOCK (self);
360 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
362 GstVolume *self = GST_VOLUME (mixer);
364 g_return_if_fail (self != NULL);
365 g_return_if_fail (GST_IS_VOLUME (self));
367 GST_OBJECT_LOCK (self);
368 volumes[0] = (gint) self->volume * VOLUME_STEPS;
369 GST_OBJECT_UNLOCK (self);
373 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
375 GstVolume *self = GST_VOLUME (mixer);
377 g_return_if_fail (self != NULL);
378 g_return_if_fail (GST_IS_VOLUME (self));
380 GST_OBJECT_LOCK (self);
382 GST_OBJECT_UNLOCK (self);
386 gst_volume_mixer_init (GstMixerClass * klass)
388 GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
390 /* default virtual functions */
391 klass->list_tracks = gst_volume_list_tracks;
392 klass->set_volume = gst_volume_set_volume;
393 klass->get_volume = gst_volume_get_volume;
394 klass->set_mute = gst_volume_set_mute;
400 gst_volume_dispose (GObject * object)
402 GstVolume *volume = GST_VOLUME (object);
404 if (volume->tracklist) {
405 if (volume->tracklist->data)
406 g_object_unref (volume->tracklist->data);
407 g_list_free (volume->tracklist);
408 volume->tracklist = NULL;
411 G_OBJECT_CLASS (parent_class)->dispose (object);
415 gst_volume_class_init (GstVolumeClass * klass)
417 GObjectClass *gobject_class;
418 GstElementClass *element_class;
419 GstBaseTransformClass *trans_class;
420 GstAudioFilterClass *filter_class;
423 gobject_class = (GObjectClass *) klass;
424 element_class = (GstElementClass *) klass;
425 trans_class = (GstBaseTransformClass *) klass;
426 filter_class = (GstAudioFilterClass *) (klass);
428 gobject_class->set_property = volume_set_property;
429 gobject_class->get_property = volume_get_property;
430 gobject_class->dispose = gst_volume_dispose;
432 g_object_class_install_property (gobject_class, PROP_MUTE,
433 g_param_spec_boolean ("mute", "Mute", "mute channel",
435 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_VOLUME,
438 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
439 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
440 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
442 gst_element_class_set_details_simple (element_class, "Volume",
443 "Filter/Effect/Audio",
444 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
446 caps = gst_caps_from_string (ALLOWED_CAPS);
447 gst_audio_filter_class_add_pad_templates (filter_class, caps);
448 gst_caps_unref (caps);
450 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
451 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
452 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
453 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
457 gst_volume_init (GstVolume * self)
459 GstMixerTrack *track = NULL;
461 self->mute = DEFAULT_PROP_MUTE;;
462 self->volume = DEFAULT_PROP_VOLUME;
464 self->tracklist = NULL;
465 self->negotiated = FALSE;
467 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
469 if (GST_IS_MIXER_TRACK (track)) {
470 track->label = g_strdup ("volume");
471 track->num_channels = 1;
472 track->min_volume = 0;
473 track->max_volume = VOLUME_STEPS;
474 track->flags = GST_MIXER_TRACK_SOFTWARE;
475 self->tracklist = g_list_append (self->tracklist, track);
478 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
482 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
484 gdouble *data = (gdouble *) bytes;
485 guint num_samples = n_bytes / sizeof (gdouble);
487 orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
491 volume_process_controlled_double (GstVolume * self, gpointer bytes,
492 gdouble * volume, guint channels, guint n_bytes)
494 gdouble *data = (gdouble *) bytes;
495 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
500 orc_process_controlled_f64_1ch (data, volume, num_samples);
502 for (i = 0; i < num_samples; i++) {
504 for (j = 0; j < channels; j++) {
512 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
514 gfloat *data = (gfloat *) bytes;
515 guint num_samples = n_bytes / sizeof (gfloat);
517 orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
521 volume_process_controlled_float (GstVolume * self, gpointer bytes,
522 gdouble * volume, guint channels, guint n_bytes)
524 gfloat *data = (gfloat *) bytes;
525 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
530 orc_process_controlled_f32_1ch (data, volume, num_samples);
531 } else if (channels == 2) {
532 orc_process_controlled_f32_2ch (data, volume, num_samples);
534 for (i = 0; i < num_samples; i++) {
536 for (j = 0; j < channels; j++) {
544 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
546 gint32 *data = (gint32 *) bytes;
547 guint num_samples = n_bytes / sizeof (gint);
549 /* hard coded in volume.orc */
550 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
551 orc_process_int32 (data, self->current_vol_i32, num_samples);
555 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
557 gint32 *data = (gint32 *) bytes;
558 guint num_samples = n_bytes / sizeof (gint);
560 /* hard coded in volume.orc */
561 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
563 orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
567 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
568 gdouble * volume, guint channels, guint n_bytes)
570 gint32 *data = (gint32 *) bytes;
572 guint num_samples = n_bytes / (sizeof (gint32) * channels);
576 orc_process_controlled_int32_1ch (data, volume, num_samples);
578 for (i = 0; i < num_samples; i++) {
580 for (j = 0; j < channels; j++) {
582 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
588 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
589 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
591 #define write_unaligned_u24(_x,samp) \
593 *(_x)++ = samp & 0xFF; \
594 *(_x)++ = (samp >> 8) & 0xFF; \
595 *(_x)++ = (samp >> 16) & 0xFF; \
598 #else /* BIG ENDIAN */
599 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
600 #define write_unaligned_u24(_x,samp) \
602 *(_x)++ = (samp >> 16) & 0xFF; \
603 *(_x)++ = (samp >> 8) & 0xFF; \
604 *(_x)++ = samp & 0xFF; \
609 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
611 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
612 guint i, num_samples;
616 num_samples = n_bytes / (sizeof (gint8) * 3);
617 for (i = 0; i < num_samples; i++) {
618 samp = get_unaligned_i24 (data);
622 (((gint64) self->current_vol_i24 *
623 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
624 samp = (guint32) val;
626 /* write the value back into the stream */
627 write_unaligned_u24 (data, samp);
632 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
634 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
635 guint i, num_samples;
639 num_samples = n_bytes / (sizeof (gint8) * 3);
640 for (i = 0; i < num_samples; i++) {
641 samp = get_unaligned_i24 (data);
645 (((gint64) self->current_vol_i24 *
646 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
647 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
649 /* write the value back into the stream */
650 write_unaligned_u24 (data, samp);
655 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
656 gdouble * volume, guint channels, guint n_bytes)
658 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
660 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
663 for (i = 0; i < num_samples; i++) {
665 for (j = 0; j < channels; j++) {
666 val = get_unaligned_i24 (data) * vol;
667 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
668 write_unaligned_u24 (data, (gint32) val);
674 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
676 gint16 *data = (gint16 *) bytes;
677 guint num_samples = n_bytes / sizeof (gint16);
679 /* hard coded in volume.orc */
680 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
682 orc_process_int16 (data, self->current_vol_i16, num_samples);
686 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
688 gint16 *data = (gint16 *) bytes;
689 guint num_samples = n_bytes / sizeof (gint16);
691 /* hard coded in volume.orc */
692 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
694 orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
698 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
699 gdouble * volume, guint channels, guint n_bytes)
701 gint16 *data = (gint16 *) bytes;
703 guint num_samples = n_bytes / (sizeof (gint16) * channels);
707 orc_process_controlled_int16_1ch (data, volume, num_samples);
708 } else if (channels == 2) {
709 orc_process_controlled_int16_2ch (data, volume, num_samples);
711 for (i = 0; i < num_samples; i++) {
713 for (j = 0; j < channels; j++) {
715 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
722 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
724 gint8 *data = (gint8 *) bytes;
725 guint num_samples = n_bytes / sizeof (gint8);
727 /* hard coded in volume.orc */
728 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
730 orc_process_int8 (data, self->current_vol_i8, num_samples);
734 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
736 gint8 *data = (gint8 *) bytes;
737 guint num_samples = n_bytes / sizeof (gint8);
739 /* hard coded in volume.orc */
740 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
742 orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
746 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
747 gdouble * volume, guint channels, guint n_bytes)
749 gint8 *data = (gint8 *) bytes;
751 guint num_samples = n_bytes / (sizeof (gint8) * channels);
755 orc_process_controlled_int8_1ch (data, volume, num_samples);
756 } else if (channels == 2) {
757 orc_process_controlled_int8_2ch (data, volume, num_samples);
759 for (i = 0; i < num_samples; i++) {
761 for (j = 0; j < channels; j++) {
763 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
769 /* GstBaseTransform vmethod implementations */
771 /* get notified of caps and plug in the correct process function */
773 volume_setup (GstAudioFilter * filter, GstRingBufferSpec * format)
776 GstVolume *self = GST_VOLUME (filter);
780 GST_OBJECT_LOCK (self);
781 volume = self->volume;
783 GST_OBJECT_UNLOCK (self);
785 res = volume_update_volume (self, volume, mute);
787 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
788 ("Invalid incoming format"), (NULL));
790 self->negotiated = res;
796 volume_stop (GstBaseTransform * base)
798 GstVolume *self = GST_VOLUME (base);
800 g_free (self->volumes);
801 self->volumes = NULL;
802 self->volumes_count = 0;
804 g_free (self->mutes);
806 self->mutes_count = 0;
808 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
813 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
815 GstClockTime timestamp;
816 GstVolume *self = GST_VOLUME (base);
820 timestamp = GST_BUFFER_TIMESTAMP (buffer);
822 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
824 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
825 GST_TIME_ARGS (timestamp));
827 if (GST_CLOCK_TIME_IS_VALID (timestamp))
828 gst_object_sync_values (G_OBJECT (self), timestamp);
830 /* get latest values */
831 GST_OBJECT_LOCK (self);
832 volume = self->volume;
834 GST_OBJECT_UNLOCK (self);
836 if ((volume != self->current_volume) || (mute != self->current_mute)) {
837 /* the volume or mute was updated, update our internal state before
838 * we continue processing. */
839 volume_update_volume (self, volume, mute);
843 /* call the plugged-in process function for this instance
844 * needs to be done with this indirection since volume_transform is
845 * a class-global method
848 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
850 GstVolume *self = GST_VOLUME (base);
853 GstControlSource *mute_csource, *volume_csource;
855 if (G_UNLIKELY (!self->negotiated))
858 /* don't process data in passthrough-mode */
859 if (gst_base_transform_is_passthrough (base) ||
860 GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
863 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
865 mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
866 volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
867 if (mute_csource || (volume_csource && !self->current_mute)) {
868 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
869 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
870 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
871 guint nsamples = size / (width * channels);
872 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
873 GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
874 gboolean use_mutes = FALSE;
876 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
878 if (self->mutes_count < nsamples && mute_csource) {
879 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
880 self->mutes_count = nsamples;
883 if (self->volumes_count < nsamples) {
884 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
885 self->volumes_count = nsamples;
889 GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
891 if (!gst_control_source_get_value_array (mute_csource, ts, &va))
892 goto controller_failure;
894 gst_object_unref (mute_csource);
898 g_free (self->mutes);
900 self->mutes_count = 0;
903 if (volume_csource) {
905 { "volume", nsamples, interval, (gpointer) self->volumes };
907 if (!gst_control_source_get_value_array (volume_csource, ts, &va))
908 goto controller_failure;
910 gst_object_unref (volume_csource);
911 volume_csource = NULL;
913 orc_memset_f64 (self->volumes, self->current_volume, nsamples);
917 orc_prepare_volumes (self->volumes, self->mutes, nsamples);
920 self->process_controlled (self, data, self->volumes, channels, size);
923 } else if (volume_csource) {
924 gst_object_unref (volume_csource);
927 if (self->current_volume == 0.0 || self->current_mute) {
928 orc_memset (data, 0, size);
929 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
930 } else if (self->current_volume != 1.0) {
931 self->process (self, data, size);
933 gst_buffer_unmap (outbuf, data, size);
940 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
941 ("No format was negotiated"), (NULL));
942 return GST_FLOW_NOT_NEGOTIATED;
947 gst_object_unref (mute_csource);
949 gst_object_unref (volume_csource);
951 GST_ELEMENT_ERROR (self, CORE, FAILED,
952 ("Failed to get values from controller"), (NULL));
953 gst_buffer_unmap (outbuf, data, size);
954 return GST_FLOW_ERROR;
959 volume_set_property (GObject * object, guint prop_id, const GValue * value,
962 GstVolume *self = GST_VOLUME (object);
966 GST_OBJECT_LOCK (self);
967 self->mute = g_value_get_boolean (value);
968 GST_OBJECT_UNLOCK (self);
971 GST_OBJECT_LOCK (self);
972 self->volume = g_value_get_double (value);
973 GST_OBJECT_UNLOCK (self);
976 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
982 volume_get_property (GObject * object, guint prop_id, GValue * value,
985 GstVolume *self = GST_VOLUME (object);
989 GST_OBJECT_LOCK (self);
990 g_value_set_boolean (value, self->mute);
991 GST_OBJECT_UNLOCK (self);
994 GST_OBJECT_LOCK (self);
995 g_value_set_double (value, self->volume);
996 GST_OBJECT_UNLOCK (self);
999 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1005 plugin_init (GstPlugin * plugin)
1007 gst_volume_orc_init ();
1009 /* initialize gst controller library */
1010 gst_controller_init (NULL, NULL);
1012 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
1014 /* ref class from a thread-safe context to work around missing bit of
1015 * thread-safety in GObject */
1016 g_type_class_ref (GST_TYPE_MIXER_TRACK);
1018 return gst_element_register (plugin, "volume", GST_RANK_NONE,
1022 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1025 "plugin for controlling audio volume",
1026 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);