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/audio/mixer.h>
49 #include <gst/audio/gstaudiofilter.h>
52 #include <orc/orcfunctions.h>
54 #define orc_memset memset
57 #include "gstvolumeorc.h"
58 #include "gstvolume.h"
60 /* some defines for audio processing */
61 /* the volume factor is a range from 0.0 to (arbitrary) VOLUME_MAX_DOUBLE = 10.0
62 * we map 1.0 to VOLUME_UNITY_INT*
64 #define VOLUME_UNITY_INT8 8 /* internal int for unity 2^(8-5) */
65 #define VOLUME_UNITY_INT8_BIT_SHIFT 3 /* number of bits to shift for unity */
66 #define VOLUME_UNITY_INT16 2048 /* internal int for unity 2^(16-5) */
67 #define VOLUME_UNITY_INT16_BIT_SHIFT 11 /* number of bits to shift for unity */
68 #define VOLUME_UNITY_INT24 524288 /* internal int for unity 2^(24-5) */
69 #define VOLUME_UNITY_INT24_BIT_SHIFT 19 /* number of bits to shift for unity */
70 #define VOLUME_UNITY_INT32 134217728 /* internal int for unity 2^(32-5) */
71 #define VOLUME_UNITY_INT32_BIT_SHIFT 27
72 #define VOLUME_MAX_DOUBLE 10.0
73 #define VOLUME_MAX_INT8 G_MAXINT8
74 #define VOLUME_MIN_INT8 G_MININT8
75 #define VOLUME_MAX_INT16 G_MAXINT16
76 #define VOLUME_MIN_INT16 G_MININT16
77 #define VOLUME_MAX_INT24 8388607
78 #define VOLUME_MIN_INT24 -8388608
79 #define VOLUME_MAX_INT32 G_MAXINT32
80 #define VOLUME_MIN_INT32 G_MININT32
82 /* number of steps we use for the mixer interface to go from 0.0 to 1.0 */
83 # define VOLUME_STEPS 100
85 #define GST_CAT_DEFAULT gst_volume_debug
86 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
88 /* Filter signals and args */
95 #define DEFAULT_PROP_MUTE FALSE
96 #define DEFAULT_PROP_VOLUME 1.0
105 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
106 #define ALLOWED_CAPS \
107 GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S8, S16LE, S24LE, S32LE }") \
108 ", layout = (string) interleaved"
110 #define ALLOWED_CAPS \
111 GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S8, S16BE, S24BE, S32BE }") \
112 ", layout = (string) { interleaved, non-interleaved }"
115 static void gst_volume_mixer_init (GstMixerInterface * 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:
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;
244 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
247 self->current_mute = TRUE;
248 self->current_volume = 0.0;
250 self->current_vol_i8 = 0;
251 self->current_vol_i16 = 0;
252 self->current_vol_i24 = 0;
253 self->current_vol_i32 = 0;
257 self->current_mute = FALSE;
258 self->current_volume = volume;
260 self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
261 self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
262 self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
263 self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
265 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
268 /* If a controller is used, never use passthrough mode
269 * because the property can change from 1.0 to something
270 * else in the middle of a buffer.
272 passthrough &= !gst_object_has_active_control_bindings (GST_OBJECT (self));
274 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
276 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
278 res = self->negotiated = volume_choose_func (self);
283 /* Mixer interface */
286 gst_volume_list_tracks (GstMixer * mixer)
288 GstVolume *self = GST_VOLUME (mixer);
290 g_return_val_if_fail (self != NULL, NULL);
291 g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
293 return self->tracklist;
297 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
299 GstVolume *self = GST_VOLUME (mixer);
301 g_return_if_fail (self != NULL);
302 g_return_if_fail (GST_IS_VOLUME (self));
304 GST_OBJECT_LOCK (self);
305 self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
306 GST_OBJECT_UNLOCK (self);
310 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
312 GstVolume *self = GST_VOLUME (mixer);
314 g_return_if_fail (self != NULL);
315 g_return_if_fail (GST_IS_VOLUME (self));
317 GST_OBJECT_LOCK (self);
318 volumes[0] = (gint) self->volume * VOLUME_STEPS;
319 GST_OBJECT_UNLOCK (self);
323 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
325 GstVolume *self = GST_VOLUME (mixer);
327 g_return_if_fail (self != NULL);
328 g_return_if_fail (GST_IS_VOLUME (self));
330 GST_OBJECT_LOCK (self);
332 GST_OBJECT_UNLOCK (self);
336 gst_volume_mixer_init (GstMixerInterface * iface)
338 GST_MIXER_TYPE (iface) = GST_MIXER_SOFTWARE;
340 /* default virtual functions */
341 iface->list_tracks = gst_volume_list_tracks;
342 iface->set_volume = gst_volume_set_volume;
343 iface->get_volume = gst_volume_get_volume;
344 iface->set_mute = gst_volume_set_mute;
350 gst_volume_dispose (GObject * object)
352 GstVolume *volume = GST_VOLUME (object);
354 if (volume->tracklist) {
355 if (volume->tracklist->data)
356 g_object_unref (volume->tracklist->data);
357 g_list_free (volume->tracklist);
358 volume->tracklist = NULL;
361 G_OBJECT_CLASS (parent_class)->dispose (object);
365 gst_volume_class_init (GstVolumeClass * klass)
367 GObjectClass *gobject_class;
368 GstElementClass *element_class;
369 GstBaseTransformClass *trans_class;
370 GstAudioFilterClass *filter_class;
373 gobject_class = (GObjectClass *) klass;
374 element_class = (GstElementClass *) klass;
375 trans_class = (GstBaseTransformClass *) klass;
376 filter_class = (GstAudioFilterClass *) (klass);
378 gobject_class->set_property = volume_set_property;
379 gobject_class->get_property = volume_get_property;
380 gobject_class->dispose = gst_volume_dispose;
382 g_object_class_install_property (gobject_class, PROP_MUTE,
383 g_param_spec_boolean ("mute", "Mute", "mute channel",
385 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_VOLUME,
388 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
389 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
390 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
392 gst_element_class_set_details_simple (element_class, "Volume",
393 "Filter/Effect/Audio",
394 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
396 caps = gst_caps_from_string (ALLOWED_CAPS);
397 gst_audio_filter_class_add_pad_templates (filter_class, caps);
398 gst_caps_unref (caps);
400 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
401 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
402 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
403 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
407 gst_volume_init (GstVolume * self)
409 GstMixerTrack *track = NULL;
411 self->mute = DEFAULT_PROP_MUTE;;
412 self->volume = DEFAULT_PROP_VOLUME;
414 self->tracklist = NULL;
415 self->negotiated = FALSE;
417 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
419 if (GST_IS_MIXER_TRACK (track)) {
420 track->label = g_strdup ("volume");
421 track->num_channels = 1;
422 track->min_volume = 0;
423 track->max_volume = VOLUME_STEPS;
424 track->flags = GST_MIXER_TRACK_SOFTWARE;
425 self->tracklist = g_list_append (self->tracklist, track);
428 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
432 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
434 gdouble *data = (gdouble *) bytes;
435 guint num_samples = n_bytes / sizeof (gdouble);
437 orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
441 volume_process_controlled_double (GstVolume * self, gpointer bytes,
442 gdouble * volume, guint channels, guint n_bytes)
444 gdouble *data = (gdouble *) bytes;
445 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
450 orc_process_controlled_f64_1ch (data, volume, num_samples);
452 for (i = 0; i < num_samples; i++) {
454 for (j = 0; j < channels; j++) {
462 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
464 gfloat *data = (gfloat *) bytes;
465 guint num_samples = n_bytes / sizeof (gfloat);
467 orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
471 volume_process_controlled_float (GstVolume * self, gpointer bytes,
472 gdouble * volume, guint channels, guint n_bytes)
474 gfloat *data = (gfloat *) bytes;
475 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
480 orc_process_controlled_f32_1ch (data, volume, num_samples);
481 } else if (channels == 2) {
482 orc_process_controlled_f32_2ch (data, volume, num_samples);
484 for (i = 0; i < num_samples; i++) {
486 for (j = 0; j < channels; j++) {
494 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
496 gint32 *data = (gint32 *) bytes;
497 guint num_samples = n_bytes / sizeof (gint);
499 /* hard coded in volume.orc */
500 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
501 orc_process_int32 (data, self->current_vol_i32, num_samples);
505 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
507 gint32 *data = (gint32 *) bytes;
508 guint num_samples = n_bytes / sizeof (gint);
510 /* hard coded in volume.orc */
511 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
513 orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
517 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
518 gdouble * volume, guint channels, guint n_bytes)
520 gint32 *data = (gint32 *) bytes;
522 guint num_samples = n_bytes / (sizeof (gint32) * channels);
526 orc_process_controlled_int32_1ch (data, volume, num_samples);
528 for (i = 0; i < num_samples; i++) {
530 for (j = 0; j < channels; j++) {
532 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
538 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
539 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
541 #define write_unaligned_u24(_x,samp) \
543 *(_x)++ = samp & 0xFF; \
544 *(_x)++ = (samp >> 8) & 0xFF; \
545 *(_x)++ = (samp >> 16) & 0xFF; \
548 #else /* BIG ENDIAN */
549 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
550 #define write_unaligned_u24(_x,samp) \
552 *(_x)++ = (samp >> 16) & 0xFF; \
553 *(_x)++ = (samp >> 8) & 0xFF; \
554 *(_x)++ = samp & 0xFF; \
559 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
561 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
562 guint i, num_samples;
566 num_samples = n_bytes / (sizeof (gint8) * 3);
567 for (i = 0; i < num_samples; i++) {
568 samp = get_unaligned_i24 (data);
572 (((gint64) self->current_vol_i24 *
573 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
574 samp = (guint32) val;
576 /* write the value back into the stream */
577 write_unaligned_u24 (data, samp);
582 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
584 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
585 guint i, num_samples;
589 num_samples = n_bytes / (sizeof (gint8) * 3);
590 for (i = 0; i < num_samples; i++) {
591 samp = get_unaligned_i24 (data);
595 (((gint64) self->current_vol_i24 *
596 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
597 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
599 /* write the value back into the stream */
600 write_unaligned_u24 (data, samp);
605 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
606 gdouble * volume, guint channels, guint n_bytes)
608 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
610 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
613 for (i = 0; i < num_samples; i++) {
615 for (j = 0; j < channels; j++) {
616 val = get_unaligned_i24 (data) * vol;
617 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
618 write_unaligned_u24 (data, (gint32) val);
624 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
626 gint16 *data = (gint16 *) bytes;
627 guint num_samples = n_bytes / sizeof (gint16);
629 /* hard coded in volume.orc */
630 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
632 orc_process_int16 (data, self->current_vol_i16, num_samples);
636 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
638 gint16 *data = (gint16 *) bytes;
639 guint num_samples = n_bytes / sizeof (gint16);
641 /* hard coded in volume.orc */
642 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 11);
644 orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
648 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
649 gdouble * volume, guint channels, guint n_bytes)
651 gint16 *data = (gint16 *) bytes;
653 guint num_samples = n_bytes / (sizeof (gint16) * channels);
657 orc_process_controlled_int16_1ch (data, volume, num_samples);
658 } else if (channels == 2) {
659 orc_process_controlled_int16_2ch (data, volume, num_samples);
661 for (i = 0; i < num_samples; i++) {
663 for (j = 0; j < channels; j++) {
665 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
672 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
674 gint8 *data = (gint8 *) bytes;
675 guint num_samples = n_bytes / sizeof (gint8);
677 /* hard coded in volume.orc */
678 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
680 orc_process_int8 (data, self->current_vol_i8, num_samples);
684 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
686 gint8 *data = (gint8 *) bytes;
687 guint num_samples = n_bytes / sizeof (gint8);
689 /* hard coded in volume.orc */
690 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 3);
692 orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
696 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
697 gdouble * volume, guint channels, guint n_bytes)
699 gint8 *data = (gint8 *) bytes;
701 guint num_samples = n_bytes / (sizeof (gint8) * channels);
705 orc_process_controlled_int8_1ch (data, volume, num_samples);
706 } else if (channels == 2) {
707 orc_process_controlled_int8_2ch (data, volume, num_samples);
709 for (i = 0; i < num_samples; i++) {
711 for (j = 0; j < channels; j++) {
713 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
719 /* GstBaseTransform vmethod implementations */
721 /* get notified of caps and plug in the correct process function */
723 volume_setup (GstAudioFilter * filter, const GstAudioInfo * info)
726 GstVolume *self = GST_VOLUME (filter);
730 GST_OBJECT_LOCK (self);
731 volume = self->volume;
733 GST_OBJECT_UNLOCK (self);
735 res = volume_update_volume (self, volume, mute);
737 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
738 ("Invalid incoming format"), (NULL));
740 self->negotiated = res;
746 volume_stop (GstBaseTransform * base)
748 GstVolume *self = GST_VOLUME (base);
750 g_free (self->volumes);
751 self->volumes = NULL;
752 self->volumes_count = 0;
754 g_free (self->mutes);
756 self->mutes_count = 0;
758 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
763 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
765 GstClockTime timestamp;
766 GstVolume *self = GST_VOLUME (base);
770 timestamp = GST_BUFFER_TIMESTAMP (buffer);
772 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
774 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
775 GST_TIME_ARGS (timestamp));
777 if (GST_CLOCK_TIME_IS_VALID (timestamp))
778 gst_object_sync_values (GST_OBJECT (self), timestamp);
780 /* get latest values */
781 GST_OBJECT_LOCK (self);
782 volume = self->volume;
784 GST_OBJECT_UNLOCK (self);
786 if ((volume != self->current_volume) || (mute != self->current_mute)) {
787 /* the volume or mute was updated, update our internal state before
788 * we continue processing. */
789 volume_update_volume (self, volume, mute);
793 /* call the plugged-in process function for this instance
794 * needs to be done with this indirection since volume_transform is
795 * a class-global method
798 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
800 GstAudioFilter *filter = GST_AUDIO_FILTER_CAST (base);
801 GstVolume *self = GST_VOLUME (base);
804 GstControlBinding *mute_cb, *volume_cb;
806 if (G_UNLIKELY (!self->negotiated))
809 /* don't process data in passthrough-mode */
810 if (gst_base_transform_is_passthrough (base) ||
811 GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
814 data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READWRITE);
816 mute_cb = gst_object_get_control_binding (GST_OBJECT (self), "mute");
817 volume_cb = gst_object_get_control_binding (GST_OBJECT (self), "volume");
819 if (mute_cb || (volume_cb && !self->current_mute)) {
820 gint rate = GST_AUDIO_INFO_RATE (&filter->info);
821 gint width = GST_AUDIO_FORMAT_INFO_WIDTH (filter->info.finfo) / 8;
822 gint channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
823 guint nsamples = size / (width * channels);
824 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
825 GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
826 gboolean use_mutes = FALSE;
828 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
830 if (self->mutes_count < nsamples && mute_cb) {
831 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
832 self->mutes_count = nsamples;
835 if (self->volumes_count < nsamples) {
836 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
837 self->volumes_count = nsamples;
841 if (!gst_control_binding_get_value_array (mute_cb, ts, interval,
842 nsamples, (gpointer) self->mutes))
843 goto controller_failure;
845 gst_object_replace ((GstObject **) & mute_cb, NULL);
848 g_free (self->mutes);
850 self->mutes_count = 0;
854 if (!gst_control_binding_get_value_array (volume_cb, ts, interval,
855 nsamples, (gpointer) self->volumes))
856 goto controller_failure;
858 gst_object_replace ((GstObject **) & volume_cb, NULL);
860 orc_memset_f64 (self->volumes, self->current_volume, nsamples);
864 orc_prepare_volumes (self->volumes, self->mutes, nsamples);
867 self->process_controlled (self, data, self->volumes, channels, size);
870 } else if (volume_cb) {
871 gst_object_unref (volume_cb);
874 if (self->current_volume == 0.0 || self->current_mute) {
875 orc_memset (data, 0, size);
876 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
877 } else if (self->current_volume != 1.0) {
878 self->process (self, data, size);
880 gst_buffer_unmap (outbuf, data, size);
887 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
888 ("No format was negotiated"), (NULL));
889 return GST_FLOW_NOT_NEGOTIATED;
894 gst_object_unref (mute_cb);
896 gst_object_unref (volume_cb);
898 GST_ELEMENT_ERROR (self, CORE, FAILED,
899 ("Failed to get values from controller"), (NULL));
900 gst_buffer_unmap (outbuf, data, size);
901 return GST_FLOW_ERROR;
906 volume_set_property (GObject * object, guint prop_id, const GValue * value,
909 GstVolume *self = GST_VOLUME (object);
913 GST_OBJECT_LOCK (self);
914 self->mute = g_value_get_boolean (value);
915 GST_OBJECT_UNLOCK (self);
918 GST_OBJECT_LOCK (self);
919 self->volume = g_value_get_double (value);
920 GST_OBJECT_UNLOCK (self);
923 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
929 volume_get_property (GObject * object, guint prop_id, GValue * value,
932 GstVolume *self = GST_VOLUME (object);
936 GST_OBJECT_LOCK (self);
937 g_value_set_boolean (value, self->mute);
938 GST_OBJECT_UNLOCK (self);
941 GST_OBJECT_LOCK (self);
942 g_value_set_double (value, self->volume);
943 GST_OBJECT_UNLOCK (self);
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
952 plugin_init (GstPlugin * plugin)
954 gst_volume_orc_init ();
956 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
958 /* ref class from a thread-safe context to work around missing bit of
959 * thread-safety in GObject */
960 g_type_class_ref (GST_TYPE_MIXER_TRACK);
962 return gst_element_register (plugin, "volume", GST_RANK_NONE,
966 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
969 "plugin for controlling audio volume",
970 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);