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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
108 #define ALLOWED_CAPS \
109 GST_AUDIO_CAPS_MAKE ("{ F32_LE, F64_LE, S8, S16_LE, S24_3LE, S32_LE }")
111 #define ALLOWED_CAPS \
112 GST_AUDIO_CAPS_MAKE ("{ F32_BE, F64_BE, S8, S16_BE, S24_3BE, S32_BE }")
115 static void gst_volume_mixer_init (GstMixerClass * iface);
117 #define gst_volume_parent_class parent_class
118 G_DEFINE_TYPE_WITH_CODE (GstVolume, gst_volume,
119 GST_TYPE_AUDIO_FILTER,
120 G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_volume_mixer_init);
121 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
123 static void volume_set_property (GObject * object, guint prop_id,
124 const GValue * value, GParamSpec * pspec);
125 static void volume_get_property (GObject * object, guint prop_id,
126 GValue * value, GParamSpec * pspec);
128 static void volume_before_transform (GstBaseTransform * base,
130 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
132 static gboolean volume_stop (GstBaseTransform * base);
133 static gboolean volume_setup (GstAudioFilter * filter,
134 const GstAudioInfo * info);
136 static void volume_process_double (GstVolume * self, gpointer bytes,
138 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
139 gdouble * volume, guint channels, guint n_bytes);
140 static void volume_process_float (GstVolume * self, gpointer bytes,
142 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
143 gdouble * volume, guint channels, guint n_bytes);
144 static void volume_process_int32 (GstVolume * self, gpointer bytes,
146 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
148 static void volume_process_controlled_int32_clamp (GstVolume * self,
149 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
150 static void volume_process_int24 (GstVolume * self, gpointer bytes,
152 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
154 static void volume_process_controlled_int24_clamp (GstVolume * self,
155 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
156 static void volume_process_int16 (GstVolume * self, gpointer bytes,
158 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
160 static void volume_process_controlled_int16_clamp (GstVolume * self,
161 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
162 static void volume_process_int8 (GstVolume * self, gpointer bytes,
164 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
166 static void volume_process_controlled_int8_clamp (GstVolume * self,
167 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
170 /* helper functions */
173 volume_choose_func (GstVolume * self)
175 GstAudioFilter *filter = GST_AUDIO_FILTER (self);
176 GstAudioFormat format;
178 self->process = NULL;
179 self->process_controlled = NULL;
181 format = GST_AUDIO_FORMAT_INFO_FORMAT (filter->info.finfo);
183 if (format == GST_AUDIO_FORMAT_UNKNOWN)
187 case GST_AUDIO_FORMAT_S32:
188 /* only clamp if the gain is greater than 1.0 */
189 if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
190 self->process = volume_process_int32_clamp;
192 self->process = volume_process_int32;
194 self->process_controlled = volume_process_controlled_int32_clamp;
196 case GST_AUDIO_FORMAT_S24_3:
197 /* only clamp if the gain is greater than 1.0 */
198 if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
199 self->process = volume_process_int24_clamp;
201 self->process = volume_process_int24;
203 self->process_controlled = volume_process_controlled_int24_clamp;
205 case GST_AUDIO_FORMAT_S16:
206 /* only clamp if the gain is greater than 1.0 */
207 if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
208 self->process = volume_process_int16_clamp;
210 self->process = volume_process_int16;
212 self->process_controlled = volume_process_controlled_int16_clamp;
214 case GST_AUDIO_FORMAT_S8:
215 /* only clamp if the gain is greater than 1.0 */
216 if (self->current_vol_i8 > VOLUME_UNITY_INT8) {
217 self->process = volume_process_int8_clamp;
219 self->process = volume_process_int8;
221 self->process_controlled = volume_process_controlled_int8_clamp;
223 case GST_AUDIO_FORMAT_F32:
224 self->process = volume_process_float;
225 self->process_controlled = volume_process_controlled_float;
227 case GST_AUDIO_FORMAT_F64:
228 self->process = volume_process_double;
229 self->process_controlled = volume_process_controlled_double;
235 return (self->process != NULL);
239 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
241 gboolean passthrough;
243 GstController *controller;
245 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
248 self->current_mute = TRUE;
249 self->current_volume = 0.0;
251 self->current_vol_i8 = 0;
252 self->current_vol_i16 = 0;
253 self->current_vol_i24 = 0;
254 self->current_vol_i32 = 0;
258 self->current_mute = FALSE;
259 self->current_volume = volume;
261 self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
262 self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
263 self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
264 self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
266 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
269 /* If a controller is used, never use passthrough mode
270 * because the property can change from 1.0 to something
271 * else in the middle of a buffer.
273 controller = gst_object_get_controller (G_OBJECT (self));
274 passthrough = passthrough && (controller == NULL);
276 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
278 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
280 res = self->negotiated = volume_choose_func (self);
285 /* Mixer interface */
288 gst_volume_list_tracks (GstMixer * mixer)
290 GstVolume *self = GST_VOLUME (mixer);
292 g_return_val_if_fail (self != NULL, NULL);
293 g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
295 return self->tracklist;
299 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
301 GstVolume *self = GST_VOLUME (mixer);
303 g_return_if_fail (self != NULL);
304 g_return_if_fail (GST_IS_VOLUME (self));
306 GST_OBJECT_LOCK (self);
307 self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
308 GST_OBJECT_UNLOCK (self);
312 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
314 GstVolume *self = GST_VOLUME (mixer);
316 g_return_if_fail (self != NULL);
317 g_return_if_fail (GST_IS_VOLUME (self));
319 GST_OBJECT_LOCK (self);
320 volumes[0] = (gint) self->volume * VOLUME_STEPS;
321 GST_OBJECT_UNLOCK (self);
325 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
327 GstVolume *self = GST_VOLUME (mixer);
329 g_return_if_fail (self != NULL);
330 g_return_if_fail (GST_IS_VOLUME (self));
332 GST_OBJECT_LOCK (self);
334 GST_OBJECT_UNLOCK (self);
338 gst_volume_mixer_init (GstMixerClass * klass)
340 GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
342 /* default virtual functions */
343 klass->list_tracks = gst_volume_list_tracks;
344 klass->set_volume = gst_volume_set_volume;
345 klass->get_volume = gst_volume_get_volume;
346 klass->set_mute = gst_volume_set_mute;
352 gst_volume_dispose (GObject * object)
354 GstVolume *volume = GST_VOLUME (object);
356 if (volume->tracklist) {
357 if (volume->tracklist->data)
358 g_object_unref (volume->tracklist->data);
359 g_list_free (volume->tracklist);
360 volume->tracklist = NULL;
363 G_OBJECT_CLASS (parent_class)->dispose (object);
367 gst_volume_class_init (GstVolumeClass * klass)
369 GObjectClass *gobject_class;
370 GstElementClass *element_class;
371 GstBaseTransformClass *trans_class;
372 GstAudioFilterClass *filter_class;
375 gobject_class = (GObjectClass *) klass;
376 element_class = (GstElementClass *) klass;
377 trans_class = (GstBaseTransformClass *) klass;
378 filter_class = (GstAudioFilterClass *) (klass);
380 gobject_class->set_property = volume_set_property;
381 gobject_class->get_property = volume_get_property;
382 gobject_class->dispose = gst_volume_dispose;
384 g_object_class_install_property (gobject_class, PROP_MUTE,
385 g_param_spec_boolean ("mute", "Mute", "mute channel",
387 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_VOLUME,
390 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
391 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
392 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
394 gst_element_class_set_details_simple (element_class, "Volume",
395 "Filter/Effect/Audio",
396 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
398 caps = gst_caps_from_string (ALLOWED_CAPS);
399 gst_audio_filter_class_add_pad_templates (filter_class, caps);
400 gst_caps_unref (caps);
402 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
403 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
404 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
405 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
409 gst_volume_init (GstVolume * self)
411 GstMixerTrack *track = NULL;
413 self->mute = DEFAULT_PROP_MUTE;;
414 self->volume = DEFAULT_PROP_VOLUME;
416 self->tracklist = NULL;
417 self->negotiated = FALSE;
419 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
421 if (GST_IS_MIXER_TRACK (track)) {
422 track->label = g_strdup ("volume");
423 track->num_channels = 1;
424 track->min_volume = 0;
425 track->max_volume = VOLUME_STEPS;
426 track->flags = GST_MIXER_TRACK_SOFTWARE;
427 self->tracklist = g_list_append (self->tracklist, track);
430 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
434 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
436 gdouble *data = (gdouble *) bytes;
437 guint num_samples = n_bytes / sizeof (gdouble);
439 orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
443 volume_process_controlled_double (GstVolume * self, gpointer bytes,
444 gdouble * volume, guint channels, guint n_bytes)
446 gdouble *data = (gdouble *) bytes;
447 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
452 orc_process_controlled_f64_1ch (data, volume, num_samples);
454 for (i = 0; i < num_samples; i++) {
456 for (j = 0; j < channels; j++) {
464 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
466 gfloat *data = (gfloat *) bytes;
467 guint num_samples = n_bytes / sizeof (gfloat);
469 orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
473 volume_process_controlled_float (GstVolume * self, gpointer bytes,
474 gdouble * volume, guint channels, guint n_bytes)
476 gfloat *data = (gfloat *) bytes;
477 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
482 orc_process_controlled_f32_1ch (data, volume, num_samples);
483 } else if (channels == 2) {
484 orc_process_controlled_f32_2ch (data, volume, num_samples);
486 for (i = 0; i < num_samples; i++) {
488 for (j = 0; j < channels; j++) {
496 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
498 gint32 *data = (gint32 *) bytes;
499 guint num_samples = n_bytes / sizeof (gint);
501 /* hard coded in volume.orc */
502 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
503 orc_process_int32 (data, self->current_vol_i32, num_samples);
507 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
509 gint32 *data = (gint32 *) bytes;
510 guint num_samples = n_bytes / sizeof (gint);
512 /* hard coded in volume.orc */
513 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
515 orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
519 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
520 gdouble * volume, guint channels, guint n_bytes)
522 gint32 *data = (gint32 *) bytes;
524 guint num_samples = n_bytes / (sizeof (gint32) * channels);
528 orc_process_controlled_int32_1ch (data, volume, num_samples);
530 for (i = 0; i < num_samples; i++) {
532 for (j = 0; j < channels; j++) {
534 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
540 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
541 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
543 #define write_unaligned_u24(_x,samp) \
545 *(_x)++ = samp & 0xFF; \
546 *(_x)++ = (samp >> 8) & 0xFF; \
547 *(_x)++ = (samp >> 16) & 0xFF; \
550 #else /* BIG ENDIAN */
551 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
552 #define write_unaligned_u24(_x,samp) \
554 *(_x)++ = (samp >> 16) & 0xFF; \
555 *(_x)++ = (samp >> 8) & 0xFF; \
556 *(_x)++ = samp & 0xFF; \
561 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
563 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
564 guint i, num_samples;
568 num_samples = n_bytes / (sizeof (gint8) * 3);
569 for (i = 0; i < num_samples; i++) {
570 samp = get_unaligned_i24 (data);
574 (((gint64) self->current_vol_i24 *
575 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
576 samp = (guint32) val;
578 /* write the value back into the stream */
579 write_unaligned_u24 (data, samp);
584 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
586 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
587 guint i, num_samples;
591 num_samples = n_bytes / (sizeof (gint8) * 3);
592 for (i = 0; i < num_samples; i++) {
593 samp = get_unaligned_i24 (data);
597 (((gint64) self->current_vol_i24 *
598 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
599 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
601 /* write the value back into the stream */
602 write_unaligned_u24 (data, samp);
607 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
608 gdouble * volume, guint channels, guint n_bytes)
610 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
612 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
615 for (i = 0; i < num_samples; i++) {
617 for (j = 0; j < channels; j++) {
618 val = get_unaligned_i24 (data) * vol;
619 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
620 write_unaligned_u24 (data, (gint32) val);
626 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
628 gint16 *data = (gint16 *) bytes;
629 guint num_samples = n_bytes / sizeof (gint16);
631 /* hard coded in volume.orc */
632 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
634 orc_process_int16 (data, self->current_vol_i16, num_samples);
638 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
640 gint16 *data = (gint16 *) bytes;
641 guint num_samples = n_bytes / sizeof (gint16);
643 /* hard coded in volume.orc */
644 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
646 orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
650 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
651 gdouble * volume, guint channels, guint n_bytes)
653 gint16 *data = (gint16 *) bytes;
655 guint num_samples = n_bytes / (sizeof (gint16) * channels);
659 orc_process_controlled_int16_1ch (data, volume, num_samples);
660 } else if (channels == 2) {
661 orc_process_controlled_int16_2ch (data, volume, num_samples);
663 for (i = 0; i < num_samples; i++) {
665 for (j = 0; j < channels; j++) {
667 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
674 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
676 gint8 *data = (gint8 *) bytes;
677 guint num_samples = n_bytes / sizeof (gint8);
679 /* hard coded in volume.orc */
680 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
682 orc_process_int8 (data, self->current_vol_i8, num_samples);
686 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
688 gint8 *data = (gint8 *) bytes;
689 guint num_samples = n_bytes / sizeof (gint8);
691 /* hard coded in volume.orc */
692 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
694 orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
698 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
699 gdouble * volume, guint channels, guint n_bytes)
701 gint8 *data = (gint8 *) bytes;
703 guint num_samples = n_bytes / (sizeof (gint8) * channels);
707 orc_process_controlled_int8_1ch (data, volume, num_samples);
708 } else if (channels == 2) {
709 orc_process_controlled_int8_2ch (data, volume, num_samples);
711 for (i = 0; i < num_samples; i++) {
713 for (j = 0; j < channels; j++) {
715 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
721 /* GstBaseTransform vmethod implementations */
723 /* get notified of caps and plug in the correct process function */
725 volume_setup (GstAudioFilter * filter, const GstAudioInfo * info)
728 GstVolume *self = GST_VOLUME (filter);
732 GST_OBJECT_LOCK (self);
733 volume = self->volume;
735 GST_OBJECT_UNLOCK (self);
737 res = volume_update_volume (self, volume, mute);
739 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
740 ("Invalid incoming format"), (NULL));
742 self->negotiated = res;
748 volume_stop (GstBaseTransform * base)
750 GstVolume *self = GST_VOLUME (base);
752 g_free (self->volumes);
753 self->volumes = NULL;
754 self->volumes_count = 0;
756 g_free (self->mutes);
758 self->mutes_count = 0;
760 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
765 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
767 GstClockTime timestamp;
768 GstVolume *self = GST_VOLUME (base);
772 timestamp = GST_BUFFER_TIMESTAMP (buffer);
774 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
776 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
777 GST_TIME_ARGS (timestamp));
779 if (GST_CLOCK_TIME_IS_VALID (timestamp))
780 gst_object_sync_values (G_OBJECT (self), timestamp);
782 /* get latest values */
783 GST_OBJECT_LOCK (self);
784 volume = self->volume;
786 GST_OBJECT_UNLOCK (self);
788 if ((volume != self->current_volume) || (mute != self->current_mute)) {
789 /* the volume or mute was updated, update our internal state before
790 * we continue processing. */
791 volume_update_volume (self, volume, mute);
795 /* call the plugged-in process function for this instance
796 * needs to be done with this indirection since volume_transform is
797 * a class-global method
800 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
802 GstAudioFilter *filter = GST_AUDIO_FILTER_CAST (base);
803 GstVolume *self = GST_VOLUME (base);
806 GstControlSource *mute_csource, *volume_csource;
808 if (G_UNLIKELY (!self->negotiated))
811 /* don't process data in passthrough-mode */
812 if (gst_base_transform_is_passthrough (base) ||
813 GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
816 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
818 mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
819 volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
821 if (mute_csource || (volume_csource && !self->current_mute)) {
822 gint rate = GST_AUDIO_INFO_RATE (&filter->info);
823 gint width = GST_AUDIO_FORMAT_INFO_WIDTH (filter->info.finfo) / 8;
824 gint channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
825 guint nsamples = size / (width * channels);
826 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
827 GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
828 gboolean use_mutes = FALSE;
830 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
832 if (self->mutes_count < nsamples && mute_csource) {
833 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
834 self->mutes_count = nsamples;
837 if (self->volumes_count < nsamples) {
838 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
839 self->volumes_count = nsamples;
843 GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
845 if (!gst_control_source_get_value_array (mute_csource, ts, &va))
846 goto controller_failure;
848 gst_object_unref (mute_csource);
852 g_free (self->mutes);
854 self->mutes_count = 0;
857 if (volume_csource) {
859 { "volume", nsamples, interval, (gpointer) self->volumes };
861 if (!gst_control_source_get_value_array (volume_csource, ts, &va))
862 goto controller_failure;
864 gst_object_unref (volume_csource);
865 volume_csource = NULL;
867 orc_memset_f64 (self->volumes, self->current_volume, nsamples);
871 orc_prepare_volumes (self->volumes, self->mutes, nsamples);
874 self->process_controlled (self, data, self->volumes, channels, size);
877 } else if (volume_csource) {
878 gst_object_unref (volume_csource);
881 if (self->current_volume == 0.0 || self->current_mute) {
882 orc_memset (data, 0, size);
883 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
884 } else if (self->current_volume != 1.0) {
885 self->process (self, data, size);
887 gst_buffer_unmap (outbuf, data, size);
894 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
895 ("No format was negotiated"), (NULL));
896 return GST_FLOW_NOT_NEGOTIATED;
901 gst_object_unref (mute_csource);
903 gst_object_unref (volume_csource);
905 GST_ELEMENT_ERROR (self, CORE, FAILED,
906 ("Failed to get values from controller"), (NULL));
907 gst_buffer_unmap (outbuf, data, size);
908 return GST_FLOW_ERROR;
913 volume_set_property (GObject * object, guint prop_id, const GValue * value,
916 GstVolume *self = GST_VOLUME (object);
920 GST_OBJECT_LOCK (self);
921 self->mute = g_value_get_boolean (value);
922 GST_OBJECT_UNLOCK (self);
925 GST_OBJECT_LOCK (self);
926 self->volume = g_value_get_double (value);
927 GST_OBJECT_UNLOCK (self);
930 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
936 volume_get_property (GObject * object, guint prop_id, GValue * value,
939 GstVolume *self = GST_VOLUME (object);
943 GST_OBJECT_LOCK (self);
944 g_value_set_boolean (value, self->mute);
945 GST_OBJECT_UNLOCK (self);
948 GST_OBJECT_LOCK (self);
949 g_value_set_double (value, self->volume);
950 GST_OBJECT_UNLOCK (self);
953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
959 plugin_init (GstPlugin * plugin)
961 gst_volume_orc_init ();
963 /* initialize gst controller library */
964 gst_controller_init (NULL, NULL);
966 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
968 /* ref class from a thread-safe context to work around missing bit of
969 * thread-safety in GObject */
970 g_type_class_ref (GST_TYPE_MIXER_TRACK);
972 return gst_element_register (plugin, "volume", GST_RANK_NONE,
976 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
979 "plugin for controlling audio volume",
980 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);