1 /* GStreamer
2 * Copyright (C) 2013 David Schleef <ds@schleef.org>
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., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19 /**
20 * SECTION:element-audiochannelmix
21 * @title: gstaudiochannelmix
22 *
23 * The audiochannelmix element mixes channels in stereo audio based on
24 * properties set on the element. The primary purpose is reconstruct
25 * equal left/right channels on an input stream that has audio in only
26 * one channel.
27 *
28 * ## Example launch line
29 * |[
30 * gst-launch-1.0 -v audiotestsrc ! audiochannelmix ! autoaudiosink
31 * ]|
32 *
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <gst/gst.h>
40 #include <gst/audio/gstaudiofilter.h>
41 #include "gstaudiochannelmix.h"
42 #include <math.h>
43
44 GST_DEBUG_CATEGORY_STATIC (gst_audio_channel_mix_debug_category);
45 #define GST_CAT_DEFAULT gst_audio_channel_mix_debug_category
46
47 /* prototypes */
48
49
50 static void gst_audio_channel_mix_set_property (GObject * object,
51 guint property_id, const GValue * value, GParamSpec * pspec);
52 static void gst_audio_channel_mix_get_property (GObject * object,
53 guint property_id, GValue * value, GParamSpec * pspec);
54
55 static gboolean gst_audio_channel_mix_setup (GstAudioFilter * filter,
56 const GstAudioInfo * info);
57 static GstFlowReturn gst_audio_channel_mix_transform_ip (GstBaseTransform *
58 trans, GstBuffer * buf);
59
60 enum
61 {
62 PROP_0,
63 PROP_LEFT_TO_LEFT,
64 PROP_LEFT_TO_RIGHT,
65 PROP_RIGHT_TO_LEFT,
66 PROP_RIGHT_TO_RIGHT
67 };
68
69 /* pad templates */
70
71 static GstStaticPadTemplate gst_audio_channel_mix_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
73 GST_PAD_SRC,
74 GST_PAD_ALWAYS,
75 GST_STATIC_CAPS ("audio/x-raw,format=S16LE,rate=[1,max],"
76 "channels=2,layout=interleaved")
77 );
78
79 static GstStaticPadTemplate gst_audio_channel_mix_sink_template =
80 GST_STATIC_PAD_TEMPLATE ("sink",
81 GST_PAD_SINK,
82 GST_PAD_ALWAYS,
83 GST_STATIC_CAPS ("audio/x-raw,format=S16LE,rate=[1,max],"
84 "channels=2,layout=interleaved")
85 );
86
87
88 /* class initialization */
89
90 G_DEFINE_TYPE_WITH_CODE (GstAudioChannelMix, gst_audio_channel_mix,
91 GST_TYPE_AUDIO_FILTER,
92 GST_DEBUG_CATEGORY_INIT (gst_audio_channel_mix_debug_category,
93 "audiochannelmix", 0, "debug category for audiochannelmix element"));
94 GST_ELEMENT_REGISTER_DEFINE (audiochannelmix, "audiochannelmix", GST_RANK_NONE,
95 GST_TYPE_AUDIO_CHANNEL_MIX);
96
97 static void
gst_audio_channel_mix_class_init(GstAudioChannelMixClass * klass)98 gst_audio_channel_mix_class_init (GstAudioChannelMixClass * klass)
99 {
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 GstBaseTransformClass *base_transform_class =
102 GST_BASE_TRANSFORM_CLASS (klass);
103 GstAudioFilterClass *audio_filter_class = GST_AUDIO_FILTER_CLASS (klass);
104
105 /* Setting up pads and setting metadata should be moved to
106 base_class_init if you intend to subclass this class. */
107 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
108 &gst_audio_channel_mix_src_template);
109 gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
110 &gst_audio_channel_mix_sink_template);
111
112 gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
113 "Simple stereo audio mixer", "Audio/Mixer", "Mixes left/right channels "
114 "of stereo audio", "David Schleef <ds@schleef.org>");
115
116 gobject_class->set_property = gst_audio_channel_mix_set_property;
117 gobject_class->get_property = gst_audio_channel_mix_get_property;
118 audio_filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_channel_mix_setup);
119 base_transform_class->transform_ip =
120 GST_DEBUG_FUNCPTR (gst_audio_channel_mix_transform_ip);
121
122 g_object_class_install_property (gobject_class, PROP_LEFT_TO_LEFT,
123 g_param_spec_double ("left-to-left", "Left to Left",
124 "Left channel to left channel gain",
125 -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
126 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127
128 g_object_class_install_property (gobject_class, PROP_LEFT_TO_RIGHT,
129 g_param_spec_double ("left-to-right", "Left to Right",
130 "Left channel to right channel gain",
131 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
132 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
133
134 g_object_class_install_property (gobject_class, PROP_RIGHT_TO_LEFT,
135 g_param_spec_double ("right-to-left", "Right to Left",
136 "Right channel to left channel gain",
137 -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
138 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
139
140 g_object_class_install_property (gobject_class, PROP_RIGHT_TO_RIGHT,
141 g_param_spec_double ("right-to-right", "Right to Right",
142 "Right channel to right channel gain",
143 -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
144 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145 }
146
147 static void
gst_audio_channel_mix_init(GstAudioChannelMix * audiochannelmix)148 gst_audio_channel_mix_init (GstAudioChannelMix * audiochannelmix)
149 {
150 audiochannelmix->left_to_left = 1.0;
151 audiochannelmix->left_to_right = 0.0;
152 audiochannelmix->right_to_left = 0.0;
153 audiochannelmix->right_to_right = 1.0;
154 }
155
156 void
gst_audio_channel_mix_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)157 gst_audio_channel_mix_set_property (GObject * object, guint property_id,
158 const GValue * value, GParamSpec * pspec)
159 {
160 GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);
161
162 GST_DEBUG_OBJECT (audiochannelmix, "set_property");
163
164 switch (property_id) {
165 case PROP_LEFT_TO_LEFT:
166 audiochannelmix->left_to_left = g_value_get_double (value);
167 break;
168 case PROP_LEFT_TO_RIGHT:
169 audiochannelmix->left_to_right = g_value_get_double (value);
170 break;
171 case PROP_RIGHT_TO_LEFT:
172 audiochannelmix->right_to_left = g_value_get_double (value);
173 break;
174 case PROP_RIGHT_TO_RIGHT:
175 audiochannelmix->right_to_right = g_value_get_double (value);
176 break;
177 default:
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
179 break;
180 }
181 }
182
183 void
gst_audio_channel_mix_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)184 gst_audio_channel_mix_get_property (GObject * object, guint property_id,
185 GValue * value, GParamSpec * pspec)
186 {
187 GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);
188
189 GST_DEBUG_OBJECT (audiochannelmix, "get_property");
190
191 switch (property_id) {
192 case PROP_LEFT_TO_LEFT:
193 g_value_set_double (value, audiochannelmix->left_to_left);
194 break;
195 case PROP_LEFT_TO_RIGHT:
196 g_value_set_double (value, audiochannelmix->left_to_right);
197 break;
198 case PROP_RIGHT_TO_LEFT:
199 g_value_set_double (value, audiochannelmix->right_to_left);
200 break;
201 case PROP_RIGHT_TO_RIGHT:
202 g_value_set_double (value, audiochannelmix->right_to_right);
203 break;
204 default:
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
206 break;
207 }
208 }
209
210 static gboolean
gst_audio_channel_mix_setup(GstAudioFilter * filter,const GstAudioInfo * info)211 gst_audio_channel_mix_setup (GstAudioFilter * filter, const GstAudioInfo * info)
212 {
213 #ifndef GST_DISABLE_GST_DEBUG
214 GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (filter);
215
216 GST_DEBUG_OBJECT (audiochannelmix, "setup");
217 #endif
218
219 return TRUE;
220 }
221
222 static GstFlowReturn
gst_audio_channel_mix_transform_ip(GstBaseTransform * trans,GstBuffer * buf)223 gst_audio_channel_mix_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
224 {
225 GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (trans);
226 int n;
227 GstMapInfo map;
228 int i;
229 double ll = audiochannelmix->left_to_left;
230 double lr = audiochannelmix->left_to_right;
231 double rl = audiochannelmix->right_to_left;
232 double rr = audiochannelmix->right_to_right;
233 int l, r;
234 gint16 *data;
235
236 GST_DEBUG_OBJECT (audiochannelmix, "transform_ip");
237
238 gst_buffer_map (buf, &map, GST_MAP_WRITE | GST_MAP_READ);
239
240 n = gst_buffer_get_size (buf) >> 2;
241 data = (gint16 *) map.data;
242 for (i = 0; i < n; i++) {
243 l = data[2 * i + 0];
244 r = data[2 * i + 1];
245 data[2 * i + 0] = CLAMP (rint (ll * l + rl * r), -32768, 32767);
246 data[2 * i + 1] = CLAMP (rint (lr * l + rr * r), -32768, 32767);
247 }
248
249 gst_buffer_unmap (buf, &map);
250
251 return GST_FLOW_OK;
252 }
253