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 _init_interfaces(type) \
147 static const GInterfaceInfo voliface_info = { \
148 (GInterfaceInitFunc) gst_volume_interface_init, \
152 static const GInterfaceInfo volmixer_info = { \
153 (GInterfaceInitFunc) gst_volume_mixer_init, \
157 static const GInterfaceInfo svol_info = { \
163 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE, \
165 g_type_add_interface_static (type, GST_TYPE_MIXER, &volmixer_info); \
166 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_info); \
169 GST_BOILERPLATE_FULL (GstVolume, gst_volume, GstAudioFilter,
170 GST_TYPE_AUDIO_FILTER, _init_interfaces);
172 static void volume_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec);
174 static void volume_get_property (GObject * object, guint prop_id,
175 GValue * value, GParamSpec * pspec);
177 static void volume_before_transform (GstBaseTransform * base,
179 static GstFlowReturn volume_transform_ip (GstBaseTransform * base,
181 static gboolean volume_stop (GstBaseTransform * base);
182 static gboolean volume_setup (GstAudioFilter * filter,
183 GstRingBufferSpec * format);
185 static void volume_process_double (GstVolume * self, gpointer bytes,
187 static void volume_process_controlled_double (GstVolume * self, gpointer bytes,
188 gdouble * volume, guint channels, guint n_bytes);
189 static void volume_process_float (GstVolume * self, gpointer bytes,
191 static void volume_process_controlled_float (GstVolume * self, gpointer bytes,
192 gdouble * volume, guint channels, guint n_bytes);
193 static void volume_process_int32 (GstVolume * self, gpointer bytes,
195 static void volume_process_int32_clamp (GstVolume * self, gpointer bytes,
197 static void volume_process_controlled_int32_clamp (GstVolume * self,
198 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
199 static void volume_process_int24 (GstVolume * self, gpointer bytes,
201 static void volume_process_int24_clamp (GstVolume * self, gpointer bytes,
203 static void volume_process_controlled_int24_clamp (GstVolume * self,
204 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
205 static void volume_process_int16 (GstVolume * self, gpointer bytes,
207 static void volume_process_int16_clamp (GstVolume * self, gpointer bytes,
209 static void volume_process_controlled_int16_clamp (GstVolume * self,
210 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
211 static void volume_process_int8 (GstVolume * self, gpointer bytes,
213 static void volume_process_int8_clamp (GstVolume * self, gpointer bytes,
215 static void volume_process_controlled_int8_clamp (GstVolume * self,
216 gpointer bytes, gdouble * volume, guint channels, guint n_bytes);
219 /* helper functions */
222 volume_choose_func (GstVolume * self)
224 self->process = NULL;
225 self->process_controlled = NULL;
227 if (GST_AUDIO_FILTER (self)->format.caps == NULL)
230 switch (GST_AUDIO_FILTER (self)->format.type) {
231 case GST_BUFTYPE_LINEAR:
232 switch (GST_AUDIO_FILTER (self)->format.width) {
234 /* only clamp if the gain is greater than 1.0
236 if (self->current_vol_i32 > VOLUME_UNITY_INT32) {
237 self->process = volume_process_int32_clamp;
239 self->process = volume_process_int32;
241 self->process_controlled = volume_process_controlled_int32_clamp;
244 /* only clamp if the gain is greater than 1.0
246 if (self->current_vol_i24 > VOLUME_UNITY_INT24) {
247 self->process = volume_process_int24_clamp;
249 self->process = volume_process_int24;
251 self->process_controlled = volume_process_controlled_int24_clamp;
254 /* only clamp if the gain is greater than 1.0
256 if (self->current_vol_i16 > VOLUME_UNITY_INT16) {
257 self->process = volume_process_int16_clamp;
259 self->process = volume_process_int16;
261 self->process_controlled = volume_process_controlled_int16_clamp;
264 /* only clamp if the gain is greater than 1.0
266 if (self->current_vol_i16 > VOLUME_UNITY_INT8) {
267 self->process = volume_process_int8_clamp;
269 self->process = volume_process_int8;
271 self->process_controlled = volume_process_controlled_int8_clamp;
275 case GST_BUFTYPE_FLOAT:
276 switch (GST_AUDIO_FILTER (self)->format.width) {
278 self->process = volume_process_float;
279 self->process_controlled = volume_process_controlled_float;
282 self->process = volume_process_double;
283 self->process_controlled = volume_process_controlled_double;
291 return (self->process != NULL);
295 volume_update_volume (GstVolume * self, gfloat volume, gboolean mute)
297 gboolean passthrough;
299 GstController *controller;
301 GST_DEBUG_OBJECT (self, "configure mute %d, volume %f", mute, volume);
304 self->current_mute = TRUE;
305 self->current_volume = 0.0;
307 self->current_vol_i8 = 0;
308 self->current_vol_i16 = 0;
309 self->current_vol_i24 = 0;
310 self->current_vol_i32 = 0;
314 self->current_mute = FALSE;
315 self->current_volume = volume;
317 self->current_vol_i8 = volume * VOLUME_UNITY_INT8;
318 self->current_vol_i16 = volume * VOLUME_UNITY_INT16;
319 self->current_vol_i24 = volume * VOLUME_UNITY_INT24;
320 self->current_vol_i32 = volume * VOLUME_UNITY_INT32;
322 passthrough = (self->current_vol_i16 == VOLUME_UNITY_INT16);
325 /* If a controller is used, never use passthrough mode
326 * because the property can change from 1.0 to something
327 * else in the middle of a buffer.
329 controller = gst_object_get_controller (G_OBJECT (self));
330 passthrough = passthrough && (controller == NULL);
332 GST_DEBUG_OBJECT (self, "set passthrough %d", passthrough);
334 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (self), passthrough);
336 res = self->negotiated = volume_choose_func (self);
341 /* Mixer interface */
344 gst_volume_interface_supported (GstImplementsInterface * iface, GType type)
346 return (type == GST_TYPE_MIXER || type == GST_TYPE_STREAM_VOLUME);
350 gst_volume_interface_init (GstImplementsInterfaceClass * klass)
352 klass->supported = gst_volume_interface_supported;
356 gst_volume_list_tracks (GstMixer * mixer)
358 GstVolume *self = GST_VOLUME (mixer);
360 g_return_val_if_fail (self != NULL, NULL);
361 g_return_val_if_fail (GST_IS_VOLUME (self), NULL);
363 return self->tracklist;
367 gst_volume_set_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
369 GstVolume *self = GST_VOLUME (mixer);
371 g_return_if_fail (self != NULL);
372 g_return_if_fail (GST_IS_VOLUME (self));
374 GST_OBJECT_LOCK (self);
375 self->volume = (gfloat) volumes[0] / VOLUME_STEPS;
376 GST_OBJECT_UNLOCK (self);
380 gst_volume_get_volume (GstMixer * mixer, GstMixerTrack * track, gint * volumes)
382 GstVolume *self = GST_VOLUME (mixer);
384 g_return_if_fail (self != NULL);
385 g_return_if_fail (GST_IS_VOLUME (self));
387 GST_OBJECT_LOCK (self);
388 volumes[0] = (gint) self->volume * VOLUME_STEPS;
389 GST_OBJECT_UNLOCK (self);
393 gst_volume_set_mute (GstMixer * mixer, GstMixerTrack * track, gboolean mute)
395 GstVolume *self = GST_VOLUME (mixer);
397 g_return_if_fail (self != NULL);
398 g_return_if_fail (GST_IS_VOLUME (self));
400 GST_OBJECT_LOCK (self);
402 GST_OBJECT_UNLOCK (self);
406 gst_volume_mixer_init (GstMixerClass * klass)
408 GST_MIXER_TYPE (klass) = GST_MIXER_SOFTWARE;
410 /* default virtual functions */
411 klass->list_tracks = gst_volume_list_tracks;
412 klass->set_volume = gst_volume_set_volume;
413 klass->get_volume = gst_volume_get_volume;
414 klass->set_mute = gst_volume_set_mute;
420 gst_volume_dispose (GObject * object)
422 GstVolume *volume = GST_VOLUME (object);
424 if (volume->tracklist) {
425 if (volume->tracklist->data)
426 g_object_unref (volume->tracklist->data);
427 g_list_free (volume->tracklist);
428 volume->tracklist = NULL;
431 G_OBJECT_CLASS (parent_class)->dispose (object);
435 gst_volume_base_init (gpointer g_class)
437 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
438 GstAudioFilterClass *filter_class = GST_AUDIO_FILTER_CLASS (g_class);
441 gst_element_class_set_details_simple (element_class, "Volume",
442 "Filter/Effect/Audio",
443 "Set volume on audio/raw streams", "Andy Wingo <wingo@pobox.com>");
445 caps = gst_caps_from_string (ALLOWED_CAPS);
446 gst_audio_filter_class_add_pad_templates (filter_class, caps);
447 gst_caps_unref (caps);
451 gst_volume_class_init (GstVolumeClass * klass)
453 GObjectClass *gobject_class;
454 GstBaseTransformClass *trans_class;
455 GstAudioFilterClass *filter_class;
457 gobject_class = (GObjectClass *) klass;
458 trans_class = (GstBaseTransformClass *) klass;
459 filter_class = (GstAudioFilterClass *) (klass);
461 gobject_class->set_property = volume_set_property;
462 gobject_class->get_property = volume_get_property;
463 gobject_class->dispose = gst_volume_dispose;
465 g_object_class_install_property (gobject_class, PROP_MUTE,
466 g_param_spec_boolean ("mute", "Mute", "mute channel",
468 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
470 g_object_class_install_property (gobject_class, PROP_VOLUME,
471 g_param_spec_double ("volume", "Volume", "volume factor, 1.0=100%",
472 0.0, VOLUME_MAX_DOUBLE, DEFAULT_PROP_VOLUME,
473 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
475 trans_class->before_transform = GST_DEBUG_FUNCPTR (volume_before_transform);
476 trans_class->transform_ip = GST_DEBUG_FUNCPTR (volume_transform_ip);
477 trans_class->stop = GST_DEBUG_FUNCPTR (volume_stop);
478 filter_class->setup = GST_DEBUG_FUNCPTR (volume_setup);
482 gst_volume_init (GstVolume * self, GstVolumeClass * g_class)
484 GstMixerTrack *track = NULL;
486 self->mute = DEFAULT_PROP_MUTE;;
487 self->volume = DEFAULT_PROP_VOLUME;
489 self->tracklist = NULL;
490 self->negotiated = FALSE;
492 track = g_object_new (GST_TYPE_MIXER_TRACK, NULL);
494 if (GST_IS_MIXER_TRACK (track)) {
495 track->label = g_strdup ("volume");
496 track->num_channels = 1;
497 track->min_volume = 0;
498 track->max_volume = VOLUME_STEPS;
499 track->flags = GST_MIXER_TRACK_SOFTWARE;
500 self->tracklist = g_list_append (self->tracklist, track);
503 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (self), TRUE);
507 volume_process_double (GstVolume * self, gpointer bytes, guint n_bytes)
509 gdouble *data = (gdouble *) bytes;
510 guint num_samples = n_bytes / sizeof (gdouble);
512 orc_scalarmultiply_f64_ns (data, self->current_volume, num_samples);
516 volume_process_controlled_double (GstVolume * self, gpointer bytes,
517 gdouble * volume, guint channels, guint n_bytes)
519 gdouble *data = (gdouble *) bytes;
520 guint num_samples = n_bytes / (sizeof (gdouble) * channels);
525 orc_process_controlled_f64_1ch (data, volume, num_samples);
527 for (i = 0; i < num_samples; i++) {
529 for (j = 0; j < channels; j++) {
537 volume_process_float (GstVolume * self, gpointer bytes, guint n_bytes)
539 gfloat *data = (gfloat *) bytes;
540 guint num_samples = n_bytes / sizeof (gfloat);
542 orc_scalarmultiply_f32_ns (data, self->current_volume, num_samples);
546 volume_process_controlled_float (GstVolume * self, gpointer bytes,
547 gdouble * volume, guint channels, guint n_bytes)
549 gfloat *data = (gfloat *) bytes;
550 guint num_samples = n_bytes / (sizeof (gfloat) * channels);
555 orc_process_controlled_f32_1ch (data, volume, num_samples);
556 } else if (channels == 2) {
557 orc_process_controlled_f32_2ch (data, volume, num_samples);
559 for (i = 0; i < num_samples; i++) {
561 for (j = 0; j < channels; j++) {
569 volume_process_int32 (GstVolume * self, gpointer bytes, guint n_bytes)
571 gint32 *data = (gint32 *) bytes;
572 guint num_samples = n_bytes / sizeof (gint);
574 /* hard coded in volume.orc */
575 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
576 orc_process_int32 (data, self->current_vol_i32, num_samples);
580 volume_process_int32_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
582 gint32 *data = (gint32 *) bytes;
583 guint num_samples = n_bytes / sizeof (gint);
585 /* hard coded in volume.orc */
586 g_assert (VOLUME_UNITY_INT32_BIT_SHIFT == 27);
588 orc_process_int32_clamp (data, self->current_vol_i32, num_samples);
592 volume_process_controlled_int32_clamp (GstVolume * self, gpointer bytes,
593 gdouble * volume, guint channels, guint n_bytes)
595 gint32 *data = (gint32 *) bytes;
597 guint num_samples = n_bytes / (sizeof (gint32) * channels);
601 orc_process_controlled_int32_1ch (data, volume, num_samples);
603 for (i = 0; i < num_samples; i++) {
605 for (j = 0; j < channels; j++) {
607 *data++ = (gint32) CLAMP (val, VOLUME_MIN_INT32, VOLUME_MAX_INT32);
613 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
614 #define get_unaligned_i24(_x) ( (((guint8*)_x)[0]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[2]) << 16) )
616 #define write_unaligned_u24(_x,samp) \
618 *(_x)++ = samp & 0xFF; \
619 *(_x)++ = (samp >> 8) & 0xFF; \
620 *(_x)++ = (samp >> 16) & 0xFF; \
623 #else /* BIG ENDIAN */
624 #define get_unaligned_i24(_x) ( (((guint8*)_x)[2]) | ((((guint8*)_x)[1]) << 8) | ((((gint8*)_x)[0]) << 16) )
625 #define write_unaligned_u24(_x,samp) \
627 *(_x)++ = (samp >> 16) & 0xFF; \
628 *(_x)++ = (samp >> 8) & 0xFF; \
629 *(_x)++ = samp & 0xFF; \
634 volume_process_int24 (GstVolume * self, gpointer bytes, guint n_bytes)
636 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
637 guint i, num_samples;
641 num_samples = n_bytes / (sizeof (gint8) * 3);
642 for (i = 0; i < num_samples; i++) {
643 samp = get_unaligned_i24 (data);
647 (((gint64) self->current_vol_i24 *
648 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
649 samp = (guint32) val;
651 /* write the value back into the stream */
652 write_unaligned_u24 (data, samp);
657 volume_process_int24_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
659 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
660 guint i, num_samples;
664 num_samples = n_bytes / (sizeof (gint8) * 3);
665 for (i = 0; i < num_samples; i++) {
666 samp = get_unaligned_i24 (data);
670 (((gint64) self->current_vol_i24 *
671 val) >> VOLUME_UNITY_INT24_BIT_SHIFT);
672 samp = (guint32) CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
674 /* write the value back into the stream */
675 write_unaligned_u24 (data, samp);
680 volume_process_controlled_int24_clamp (GstVolume * self, gpointer bytes,
681 gdouble * volume, guint channels, guint n_bytes)
683 gint8 *data = (gint8 *) bytes; /* treat the data as a byte stream */
685 guint num_samples = n_bytes / (sizeof (gint8) * 3 * channels);
688 for (i = 0; i < num_samples; i++) {
690 for (j = 0; j < channels; j++) {
691 val = get_unaligned_i24 (data) * vol;
692 val = CLAMP (val, VOLUME_MIN_INT24, VOLUME_MAX_INT24);
693 write_unaligned_u24 (data, (gint32) val);
699 volume_process_int16 (GstVolume * self, gpointer bytes, guint n_bytes)
701 gint16 *data = (gint16 *) bytes;
702 guint num_samples = n_bytes / sizeof (gint16);
704 /* hard coded in volume.orc */
705 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
707 orc_process_int16 (data, self->current_vol_i16, num_samples);
711 volume_process_int16_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
713 gint16 *data = (gint16 *) bytes;
714 guint num_samples = n_bytes / sizeof (gint16);
716 /* hard coded in volume.orc */
717 g_assert (VOLUME_UNITY_INT16_BIT_SHIFT == 13);
719 orc_process_int16_clamp (data, self->current_vol_i16, num_samples);
723 volume_process_controlled_int16_clamp (GstVolume * self, gpointer bytes,
724 gdouble * volume, guint channels, guint n_bytes)
726 gint16 *data = (gint16 *) bytes;
728 guint num_samples = n_bytes / (sizeof (gint16) * channels);
732 orc_process_controlled_int16_1ch (data, volume, num_samples);
733 } else if (channels == 2) {
734 orc_process_controlled_int16_2ch (data, volume, num_samples);
736 for (i = 0; i < num_samples; i++) {
738 for (j = 0; j < channels; j++) {
740 *data++ = (gint16) CLAMP (val, VOLUME_MIN_INT16, VOLUME_MAX_INT16);
747 volume_process_int8 (GstVolume * self, gpointer bytes, guint n_bytes)
749 gint8 *data = (gint8 *) bytes;
750 guint num_samples = n_bytes / sizeof (gint8);
752 /* hard coded in volume.orc */
753 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
755 orc_process_int8 (data, self->current_vol_i8, num_samples);
759 volume_process_int8_clamp (GstVolume * self, gpointer bytes, guint n_bytes)
761 gint8 *data = (gint8 *) bytes;
762 guint num_samples = n_bytes / sizeof (gint8);
764 /* hard coded in volume.orc */
765 g_assert (VOLUME_UNITY_INT8_BIT_SHIFT == 5);
767 orc_process_int8_clamp (data, self->current_vol_i8, num_samples);
771 volume_process_controlled_int8_clamp (GstVolume * self, gpointer bytes,
772 gdouble * volume, guint channels, guint n_bytes)
774 gint8 *data = (gint8 *) bytes;
776 guint num_samples = n_bytes / (sizeof (gint8) * channels);
780 orc_process_controlled_int8_1ch (data, volume, num_samples);
781 } else if (channels == 2) {
782 orc_process_controlled_int8_2ch (data, volume, num_samples);
784 for (i = 0; i < num_samples; i++) {
786 for (j = 0; j < channels; j++) {
788 *data++ = (gint8) CLAMP (val, VOLUME_MIN_INT8, VOLUME_MAX_INT8);
794 /* GstBaseTransform vmethod implementations */
796 /* get notified of caps and plug in the correct process function */
798 volume_setup (GstAudioFilter * filter, GstRingBufferSpec * format)
801 GstVolume *self = GST_VOLUME (filter);
805 GST_OBJECT_LOCK (self);
806 volume = self->volume;
808 GST_OBJECT_UNLOCK (self);
810 res = volume_update_volume (self, volume, mute);
812 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
813 ("Invalid incoming format"), (NULL));
815 self->negotiated = res;
821 volume_stop (GstBaseTransform * base)
823 GstVolume *self = GST_VOLUME (base);
825 g_free (self->volumes);
826 self->volumes = NULL;
827 self->volumes_count = 0;
829 g_free (self->mutes);
831 self->mutes_count = 0;
833 return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_TRANSFORM_CLASS, stop, (base),
838 volume_before_transform (GstBaseTransform * base, GstBuffer * buffer)
840 GstClockTime timestamp;
841 GstVolume *self = GST_VOLUME (base);
845 timestamp = GST_BUFFER_TIMESTAMP (buffer);
847 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
849 GST_DEBUG_OBJECT (base, "sync to %" GST_TIME_FORMAT,
850 GST_TIME_ARGS (timestamp));
852 if (GST_CLOCK_TIME_IS_VALID (timestamp))
853 gst_object_sync_values (G_OBJECT (self), timestamp);
855 /* get latest values */
856 GST_OBJECT_LOCK (self);
857 volume = self->volume;
859 GST_OBJECT_UNLOCK (self);
861 if ((volume != self->current_volume) || (mute != self->current_mute)) {
862 /* the volume or mute was updated, update our internal state before
863 * we continue processing. */
864 volume_update_volume (self, volume, mute);
868 /* call the plugged-in process function for this instance
869 * needs to be done with this indirection since volume_transform is
870 * a class-global method
873 volume_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
875 GstVolume *self = GST_VOLUME (base);
878 GstControlSource *mute_csource, *volume_csource;
880 if (G_UNLIKELY (!self->negotiated))
883 /* don't process data in passthrough-mode */
884 if (gst_base_transform_is_passthrough (base) ||
885 GST_BUFFER_FLAG_IS_SET (outbuf, GST_BUFFER_FLAG_GAP))
888 data = GST_BUFFER_DATA (outbuf);
889 size = GST_BUFFER_SIZE (outbuf);
891 mute_csource = gst_object_get_control_source (G_OBJECT (self), "mute");
892 volume_csource = gst_object_get_control_source (G_OBJECT (self), "volume");
893 if (mute_csource || (volume_csource && !self->current_mute)) {
894 gint rate = GST_AUDIO_FILTER_CAST (self)->format.rate;
895 gint width = GST_AUDIO_FILTER_CAST (self)->format.width / 8;
896 gint channels = GST_AUDIO_FILTER_CAST (self)->format.channels;
897 guint nsamples = size / (width * channels);
898 GstClockTime interval = gst_util_uint64_scale_int (1, GST_SECOND, rate);
899 GstClockTime ts = GST_BUFFER_TIMESTAMP (outbuf);
901 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, ts);
903 if (self->mutes_count < nsamples && mute_csource) {
904 self->mutes = g_realloc (self->mutes, sizeof (gboolean) * nsamples);
905 self->mutes_count = nsamples;
908 if (self->volumes_count < nsamples) {
909 self->volumes = g_realloc (self->volumes, sizeof (gdouble) * nsamples);
910 self->volumes_count = nsamples;
914 GstValueArray va = { "mute", nsamples, interval, (gpointer) self->mutes };
916 if (!gst_control_source_get_value_array (mute_csource, ts, &va))
917 goto controller_failure;
919 gst_object_unref (mute_csource);
922 g_free (self->mutes);
924 self->mutes_count = 0;
927 if (volume_csource) {
929 { "volume", nsamples, interval, (gpointer) self->volumes };
931 if (!gst_control_source_get_value_array (volume_csource, ts, &va))
932 goto controller_failure;
934 gst_object_unref (volume_csource);
935 volume_csource = NULL;
937 orc_memset_f64 (self->volumes, self->current_volume, nsamples);
941 orc_prepare_volumes (self->volumes, self->mutes, nsamples);
944 self->process_controlled (self, data, self->volumes, channels, size);
947 } else if (volume_csource) {
948 gst_object_unref (volume_csource);
951 if (self->current_volume == 0.0 || self->current_mute) {
952 orc_memset (data, 0, size);
953 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
954 } else if (self->current_volume != 1.0) {
955 self->process (self, data, size);
963 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
964 ("No format was negotiated"), (NULL));
965 return GST_FLOW_NOT_NEGOTIATED;
970 gst_object_unref (mute_csource);
972 gst_object_unref (volume_csource);
974 GST_ELEMENT_ERROR (self, CORE, FAILED,
975 ("Failed to get values from controller"), (NULL));
976 return GST_FLOW_ERROR;
981 volume_set_property (GObject * object, guint prop_id, const GValue * value,
984 GstVolume *self = GST_VOLUME (object);
988 GST_OBJECT_LOCK (self);
989 self->mute = g_value_get_boolean (value);
990 GST_OBJECT_UNLOCK (self);
993 GST_OBJECT_LOCK (self);
994 self->volume = g_value_get_double (value);
995 GST_OBJECT_UNLOCK (self);
998 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1004 volume_get_property (GObject * object, guint prop_id, GValue * value,
1007 GstVolume *self = GST_VOLUME (object);
1011 GST_OBJECT_LOCK (self);
1012 g_value_set_boolean (value, self->mute);
1013 GST_OBJECT_UNLOCK (self);
1016 GST_OBJECT_LOCK (self);
1017 g_value_set_double (value, self->volume);
1018 GST_OBJECT_UNLOCK (self);
1021 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1027 plugin_init (GstPlugin * plugin)
1029 gst_volume_orc_init ();
1031 /* initialize gst controller library */
1032 gst_controller_init (NULL, NULL);
1034 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "volume", 0, "Volume gain");
1036 /* ref class from a thread-safe context to work around missing bit of
1037 * thread-safety in GObject */
1038 g_type_class_ref (GST_TYPE_MIXER_TRACK);
1040 return gst_element_register (plugin, "volume", GST_RANK_NONE,
1044 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1047 "plugin for controlling audio volume",
1048 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);