Branch data Line data Source code
1 : : /* GStreamer Mixer
2 : : * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 : : *
4 : : * This library is free software; you can redistribute it and/or
5 : : * modify it under the terms of the GNU Library General Public
6 : : * License as published by the Free Software Foundation; either
7 : : * version 2 of the License, or (at your option) any later version.
8 : : *
9 : : * This library is distributed in the hope that it will be useful,
10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : : * Library General Public License for more details.
13 : : *
14 : : * You should have received a copy of the GNU Library General Public
15 : : * License along with this library; if not, write to the
16 : : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : : * Boston, MA 02111-1307, USA.
18 : : */
19 : :
20 : : /**
21 : : * SECTION:gststreamvolume
22 : : * @short_description: Interface for elements that provide a stream volume
23 : : *
24 : : * <refsect2>
25 : : * <para>
26 : : * This interface is implemented by elements that provide a stream volume. Examples for
27 : : * such elements are #volume and #playbin2.
28 : : * </para>
29 : : * <para>
30 : : * Applications can use this interface to get or set the current stream volume. For this
31 : : * the "volume" #GObject property can be used or the helper functions gst_stream_volume_set_volume()
32 : : * and gst_stream_volume_get_volume(). This volume is always a linear factor, i.e. 0.0 is muted
33 : : * 1.0 is 100%. For showing the volume in a GUI it might make sense to convert it to
34 : : * a different format by using gst_stream_volume_convert_volume(). Volume sliders should usually
35 : : * use a cubic volume.
36 : : *
37 : : * Separate from the volume the stream can also be muted by the "mute" #GObject property or
38 : : * gst_stream_volume_set_mute() and gst_stream_volume_get_mute().
39 : : * </para>
40 : : * <para>
41 : : * Elements that provide some kind of stream volume should implement the "volume" and
42 : : * "mute" #GObject properties and handle setting and getting of them properly.
43 : : * The volume property is defined to be a linear volume factor.
44 : : * </para>
45 : : * </refsect2>
46 : : */
47 : :
48 : : #ifdef HAVE_CONFIG_H
49 : : #include "config.h"
50 : : #endif
51 : :
52 : : #include "streamvolume.h"
53 : : #include <math.h>
54 : :
55 : : static void
56 : 92 : gst_stream_volume_class_init (GstStreamVolumeInterface * iface)
57 : : {
58 : 92 : g_object_interface_install_property (iface,
59 : : g_param_spec_double ("volume",
60 : : "Volume",
61 : : "Linear volume factor, 1.0=100%",
62 : : 0.0, G_MAXDOUBLE, 1.0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
63 : 92 : g_object_interface_install_property (iface,
64 : : g_param_spec_boolean ("mute",
65 : : "Mute",
66 : : "Mute the audio channel without changing the volume",
67 : : FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
68 : 92 : }
69 : :
70 : : GType
71 : 174 : gst_stream_volume_get_type (void)
72 : : {
73 : : static volatile gsize type = 0;
74 [ + + ]: 174 : if (g_once_init_enter (&type)) {
75 : : GType tmp;
76 : : static const GTypeInfo info = {
77 : : sizeof (GstStreamVolumeInterface),
78 : : NULL, /* base_init */
79 : : NULL, /* base_finalize */
80 : : (GClassInitFunc) gst_stream_volume_class_init, /* class_init */
81 : : NULL, /* class_finalize */
82 : : NULL, /* class_data */
83 : : 0,
84 : : 0, /* n_preallocs */
85 : : NULL /* instance_init */
86 : : };
87 : 133 : tmp = g_type_register_static (G_TYPE_INTERFACE,
88 : : "GstStreamVolume", &info, 0);
89 : 133 : g_type_interface_add_prerequisite (tmp, G_TYPE_OBJECT);
90 : :
91 : 133 : g_once_init_leave (&type, tmp);
92 : : }
93 : 174 : return type;
94 : : }
95 : :
96 : : /**
97 : : * gst_stream_volume_get_volume:
98 : : * @volume: #GstStreamVolume that should be used
99 : : * @format: #GstStreamVolumeFormat which should be returned
100 : : *
101 : : * Returns: The current stream volume as linear factor
102 : : *
103 : : * Since: 0.10.25
104 : : */
105 : : gdouble
106 : 3 : gst_stream_volume_get_volume (GstStreamVolume * volume,
107 : : GstStreamVolumeFormat format)
108 : : {
109 : : gdouble val;
110 : :
111 [ - + ]: 3 : g_return_val_if_fail (GST_IS_STREAM_VOLUME (volume), 1.0);
112 : :
113 : 3 : g_object_get (volume, "volume", &val, NULL);
114 [ - + ]: 3 : if (format != GST_STREAM_VOLUME_FORMAT_LINEAR)
115 : 0 : val =
116 : 0 : gst_stream_volume_convert_volume (GST_STREAM_VOLUME_FORMAT_LINEAR,
117 : : format, val);
118 : 3 : return val;
119 : : }
120 : :
121 : : /**
122 : : * gst_stream_volume_set_volume:
123 : : * @volume: #GstStreamVolume that should be used
124 : : * @format: #GstStreamVolumeFormat of @val
125 : : * @val: Linear volume factor that should be set
126 : : *
127 : : * Since: 0.10.25
128 : : */
129 : : void
130 : 1 : gst_stream_volume_set_volume (GstStreamVolume * volume,
131 : : GstStreamVolumeFormat format, gdouble val)
132 : : {
133 [ - + ]: 2 : g_return_if_fail (GST_IS_STREAM_VOLUME (volume));
134 : :
135 [ - + ]: 1 : if (format != GST_STREAM_VOLUME_FORMAT_LINEAR)
136 : 0 : val =
137 : : gst_stream_volume_convert_volume (format,
138 : : GST_STREAM_VOLUME_FORMAT_LINEAR, val);
139 : 1 : g_object_set (volume, "volume", val, NULL);
140 : : }
141 : :
142 : : /**
143 : : * gst_stream_volume_get_mute:
144 : : * @volume: #GstStreamVolume that should be used
145 : : *
146 : : * Returns: Returns %TRUE if the stream is muted
147 : : *
148 : : * Since: 0.10.25
149 : : */
150 : : gboolean
151 : 0 : gst_stream_volume_get_mute (GstStreamVolume * volume)
152 : : {
153 : : gboolean val;
154 : :
155 [ # # ]: 0 : g_return_val_if_fail (GST_IS_STREAM_VOLUME (volume), FALSE);
156 : :
157 : 0 : g_object_get (volume, "mute", &val, NULL);
158 : 0 : return val;
159 : : }
160 : :
161 : : /**
162 : : * gst_stream_volume_set_mute:
163 : : * @volume: #GstStreamVolume that should be used
164 : : * @mute: Mute state that should be set
165 : : *
166 : : * Since: 0.10.25
167 : : */
168 : : void
169 : 0 : gst_stream_volume_set_mute (GstStreamVolume * volume, gboolean mute)
170 : : {
171 [ # # ]: 0 : g_return_if_fail (GST_IS_STREAM_VOLUME (volume));
172 : :
173 : 0 : g_object_set (volume, "mute", mute, NULL);
174 : : }
175 : :
176 : : /**
177 : : * gst_stream_volume_convert_volume:
178 : : * @from: #GstStreamVolumeFormat to convert from
179 : : * @to: #GstStreamVolumeFormat to convert to
180 : : * @val: Volume in @from format that should be converted
181 : : *
182 : : * Returns: the converted volume
183 : : *
184 : : * Since: 0.10.25
185 : : */
186 : : gdouble
187 : 0 : gst_stream_volume_convert_volume (GstStreamVolumeFormat from,
188 : : GstStreamVolumeFormat to, gdouble val)
189 : : {
190 [ # # # # ]: 0 : switch (from) {
191 : : case GST_STREAM_VOLUME_FORMAT_LINEAR:
192 [ # # ]: 0 : g_return_val_if_fail (val >= 0.0, 0.0);
193 [ # # # # ]: 0 : switch (to) {
194 : : case GST_STREAM_VOLUME_FORMAT_LINEAR:
195 : 0 : return val;
196 : : case GST_STREAM_VOLUME_FORMAT_CUBIC:
197 : 0 : return pow (val, 1 / 3.0);
198 : : case GST_STREAM_VOLUME_FORMAT_DB:
199 : 0 : return 20.0 * log10 (val);
200 : : }
201 : 0 : break;
202 : : case GST_STREAM_VOLUME_FORMAT_CUBIC:
203 [ # # ]: 0 : g_return_val_if_fail (val >= 0.0, 0.0);
204 [ # # # # ]: 0 : switch (to) {
205 : : case GST_STREAM_VOLUME_FORMAT_LINEAR:
206 : 0 : return val * val * val;
207 : : case GST_STREAM_VOLUME_FORMAT_CUBIC:
208 : 0 : return val;
209 : : case GST_STREAM_VOLUME_FORMAT_DB:
210 : 0 : return 3.0 * 20.0 * log10 (val);
211 : : }
212 : 0 : break;
213 : : case GST_STREAM_VOLUME_FORMAT_DB:
214 [ # # # # ]: 0 : switch (to) {
215 : : case GST_STREAM_VOLUME_FORMAT_LINEAR:
216 : 0 : return pow (10.0, val / 20.0);
217 : : case GST_STREAM_VOLUME_FORMAT_CUBIC:
218 : 0 : return pow (10.0, val / (3.0 * 20.0));
219 : : case GST_STREAM_VOLUME_FORMAT_DB:
220 : 0 : return val;
221 : : }
222 : 0 : break;
223 : : }
224 : 0 : g_return_val_if_reached (0.0);
225 : : }
|