• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-gstaudiochannelmix
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 
95 static void
gst_audio_channel_mix_class_init(GstAudioChannelMixClass * klass)96 gst_audio_channel_mix_class_init (GstAudioChannelMixClass * klass)
97 {
98   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99   GstBaseTransformClass *base_transform_class =
100       GST_BASE_TRANSFORM_CLASS (klass);
101   GstAudioFilterClass *audio_filter_class = GST_AUDIO_FILTER_CLASS (klass);
102 
103   /* Setting up pads and setting metadata should be moved to
104      base_class_init if you intend to subclass this class. */
105   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
106       &gst_audio_channel_mix_src_template);
107   gst_element_class_add_static_pad_template (GST_ELEMENT_CLASS (klass),
108       &gst_audio_channel_mix_sink_template);
109 
110   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
111       "Simple stereo audio mixer", "Audio/Mixer", "Mixes left/right channels "
112       "of stereo audio", "David Schleef <ds@schleef.org>");
113 
114   gobject_class->set_property = gst_audio_channel_mix_set_property;
115   gobject_class->get_property = gst_audio_channel_mix_get_property;
116   audio_filter_class->setup = GST_DEBUG_FUNCPTR (gst_audio_channel_mix_setup);
117   base_transform_class->transform_ip =
118       GST_DEBUG_FUNCPTR (gst_audio_channel_mix_transform_ip);
119 
120   g_object_class_install_property (gobject_class, PROP_LEFT_TO_LEFT,
121       g_param_spec_double ("left-to-left", "Left to Left",
122           "Left channel to left channel gain",
123           -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
124           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125 
126   g_object_class_install_property (gobject_class, PROP_LEFT_TO_RIGHT,
127       g_param_spec_double ("left-to-right", "Left to Right",
128           "Left channel to right channel gain",
129           -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
130           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
131 
132   g_object_class_install_property (gobject_class, PROP_RIGHT_TO_LEFT,
133       g_param_spec_double ("right-to-left", "Right to Left",
134           "Right channel to left channel gain",
135           -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
136           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 
138   g_object_class_install_property (gobject_class, PROP_RIGHT_TO_RIGHT,
139       g_param_spec_double ("right-to-right", "Right to Right",
140           "Right channel to right channel gain",
141           -G_MAXDOUBLE, G_MAXDOUBLE, 1.0,
142           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
143 }
144 
145 static void
gst_audio_channel_mix_init(GstAudioChannelMix * audiochannelmix)146 gst_audio_channel_mix_init (GstAudioChannelMix * audiochannelmix)
147 {
148   audiochannelmix->left_to_left = 1.0;
149   audiochannelmix->left_to_right = 0.0;
150   audiochannelmix->right_to_left = 0.0;
151   audiochannelmix->right_to_right = 1.0;
152 }
153 
154 void
gst_audio_channel_mix_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)155 gst_audio_channel_mix_set_property (GObject * object, guint property_id,
156     const GValue * value, GParamSpec * pspec)
157 {
158   GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);
159 
160   GST_DEBUG_OBJECT (audiochannelmix, "set_property");
161 
162   switch (property_id) {
163     case PROP_LEFT_TO_LEFT:
164       audiochannelmix->left_to_left = g_value_get_double (value);
165       break;
166     case PROP_LEFT_TO_RIGHT:
167       audiochannelmix->left_to_right = g_value_get_double (value);
168       break;
169     case PROP_RIGHT_TO_LEFT:
170       audiochannelmix->right_to_left = g_value_get_double (value);
171       break;
172     case PROP_RIGHT_TO_RIGHT:
173       audiochannelmix->right_to_right = g_value_get_double (value);
174       break;
175     default:
176       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
177       break;
178   }
179 }
180 
181 void
gst_audio_channel_mix_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)182 gst_audio_channel_mix_get_property (GObject * object, guint property_id,
183     GValue * value, GParamSpec * pspec)
184 {
185   GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (object);
186 
187   GST_DEBUG_OBJECT (audiochannelmix, "get_property");
188 
189   switch (property_id) {
190     case PROP_LEFT_TO_LEFT:
191       g_value_set_double (value, audiochannelmix->left_to_left);
192       break;
193     case PROP_LEFT_TO_RIGHT:
194       g_value_set_double (value, audiochannelmix->left_to_right);
195       break;
196     case PROP_RIGHT_TO_LEFT:
197       g_value_set_double (value, audiochannelmix->right_to_left);
198       break;
199     case PROP_RIGHT_TO_RIGHT:
200       g_value_set_double (value, audiochannelmix->right_to_right);
201       break;
202     default:
203       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
204       break;
205   }
206 }
207 
208 static gboolean
gst_audio_channel_mix_setup(GstAudioFilter * filter,const GstAudioInfo * info)209 gst_audio_channel_mix_setup (GstAudioFilter * filter, const GstAudioInfo * info)
210 {
211 #ifndef GST_DISABLE_GST_DEBUG
212   GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (filter);
213 
214   GST_DEBUG_OBJECT (audiochannelmix, "setup");
215 #endif
216 
217   return TRUE;
218 }
219 
220 static GstFlowReturn
gst_audio_channel_mix_transform_ip(GstBaseTransform * trans,GstBuffer * buf)221 gst_audio_channel_mix_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
222 {
223   GstAudioChannelMix *audiochannelmix = GST_AUDIO_CHANNEL_MIX (trans);
224   int n;
225   GstMapInfo map;
226   int i;
227   double ll = audiochannelmix->left_to_left;
228   double lr = audiochannelmix->left_to_right;
229   double rl = audiochannelmix->right_to_left;
230   double rr = audiochannelmix->right_to_right;
231   int l, r;
232   gint16 *data;
233 
234   GST_DEBUG_OBJECT (audiochannelmix, "transform_ip");
235 
236   gst_buffer_map (buf, &map, GST_MAP_WRITE | GST_MAP_READ);
237 
238   n = gst_buffer_get_size (buf) >> 2;
239   data = (gint16 *) map.data;
240   for (i = 0; i < n; i++) {
241     l = data[2 * i + 0];
242     r = data[2 * i + 1];
243     data[2 * i + 0] = CLAMP (rint (ll * l + rl * r), -32768, 32767);
244     data[2 * i + 1] = CLAMP (rint (lr * l + rr * r), -32768, 32767);
245   }
246 
247   gst_buffer_unmap (buf, &map);
248 
249   return GST_FLOW_OK;
250 }
251