• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /* This effect is borrowed from xmms-0.6.1, though I mangled it so badly in
21  * the process of copying it over that the xmms people probably won't want
22  * any credit for it ;-)
23  */
24 /**
25  * SECTION:element-stereo
26  * @title: stereo
27  *
28  * Create a wide stereo effect.
29  *
30  * ## Example pipelines
31  * |[
32  * gst-launch-1.0 -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! stereo ! audioconvert ! audioresample ! alsasink
33  * ]| Play an Ogg/Vorbis file.
34  *
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 #include "gststereo.h"
41 
42 #include <gst/gst.h>
43 #include <gst/base/gstbasetransform.h>
44 #include <gst/audio/audio.h>
45 #include <gst/audio/gstaudiofilter.h>
46 
47 #define ALLOWED_CAPS \
48     "audio/x-raw,"                     \
49     " format = "GST_AUDIO_NE (S16) "," \
50     " rate = (int) [ 1, MAX ],"        \
51     " channels = (int) 2"
52 
53 /* Stereo signals and args */
54 enum
55 {
56   /* FILL ME */
57   LAST_SIGNAL
58 };
59 
60 enum
61 {
62   PROP_0,
63   PROP_ACTIVE,
64   PROP_STEREO
65 };
66 
67 static void gst_stereo_set_property (GObject * object, guint prop_id,
68     const GValue * value, GParamSpec * pspec);
69 static void gst_stereo_get_property (GObject * object, guint prop_id,
70     GValue * value, GParamSpec * pspec);
71 
72 static GstFlowReturn gst_stereo_transform_ip (GstBaseTransform * base,
73     GstBuffer * outbuf);
74 
75 G_DEFINE_TYPE (GstStereo, gst_stereo, GST_TYPE_AUDIO_FILTER);
76 GST_ELEMENT_REGISTER_DEFINE (stereo, "stereo", GST_RANK_NONE, GST_TYPE_STEREO);
77 
78 static void
gst_stereo_class_init(GstStereoClass * klass)79 gst_stereo_class_init (GstStereoClass * klass)
80 {
81   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
83   GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
84   GstAudioFilterClass *audiofilter_class = GST_AUDIO_FILTER_CLASS (klass);
85   GstCaps *caps;
86 
87   gst_element_class_set_static_metadata (element_class, "Stereo effect",
88       "Filter/Effect/Audio",
89       "Muck with the stereo signal to enhance its 'stereo-ness'",
90       "Erik Walthinsen <omega@cse.ogi.edu>");
91 
92   caps = gst_caps_from_string (ALLOWED_CAPS);
93   gst_audio_filter_class_add_pad_templates (audiofilter_class, caps);
94   gst_caps_unref (caps);
95 
96   gobject_class->set_property = gst_stereo_set_property;
97   gobject_class->get_property = gst_stereo_get_property;
98 
99   g_object_class_install_property (gobject_class, PROP_ACTIVE,
100       g_param_spec_boolean ("active", "active", "active",
101           TRUE,
102           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
103 
104   g_object_class_install_property (gobject_class, PROP_STEREO,
105       g_param_spec_float ("stereo", "stereo", "stereo",
106           0.0, 1.0, 0.1f,
107           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
108 
109   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_stereo_transform_ip);
110 }
111 
112 static void
gst_stereo_init(GstStereo * stereo)113 gst_stereo_init (GstStereo * stereo)
114 {
115   stereo->active = TRUE;
116   stereo->stereo = 0.1f;
117 }
118 
119 static GstFlowReturn
gst_stereo_transform_ip(GstBaseTransform * base,GstBuffer * outbuf)120 gst_stereo_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
121 {
122   GstStereo *stereo = GST_STEREO (base);
123   gint samples;
124   gint i;
125   gdouble avg, ldiff, rdiff, tmp;
126   gdouble mul = stereo->stereo;
127   gint16 *data;
128   GstMapInfo info;
129 
130   if (!gst_buffer_map (outbuf, &info, GST_MAP_READWRITE))
131     return GST_FLOW_ERROR;
132 
133   data = (gint16 *) info.data;
134   samples = info.size / 2;
135 
136   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (outbuf)))
137     gst_object_sync_values (GST_OBJECT (stereo), GST_BUFFER_TIMESTAMP (outbuf));
138 
139   if (stereo->active) {
140     for (i = 0; i < samples / 2; i += 2) {
141       avg = (data[i] + data[i + 1]) / 2;
142       ldiff = data[i] - avg;
143       rdiff = data[i + 1] - avg;
144 
145       tmp = avg + ldiff * mul;
146       if (tmp < -32768)
147         tmp = -32768;
148       if (tmp > 32767)
149         tmp = 32767;
150       data[i] = tmp;
151 
152       tmp = avg + rdiff * mul;
153       if (tmp < -32768)
154         tmp = -32768;
155       if (tmp > 32767)
156         tmp = 32767;
157       data[i + 1] = tmp;
158     }
159   }
160 
161   gst_buffer_unmap (outbuf, &info);
162 
163   return GST_FLOW_OK;
164 }
165 
166 static void
gst_stereo_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)167 gst_stereo_set_property (GObject * object, guint prop_id, const GValue * value,
168     GParamSpec * pspec)
169 {
170   GstStereo *stereo = GST_STEREO (object);
171 
172   switch (prop_id) {
173     case PROP_ACTIVE:
174       stereo->active = g_value_get_boolean (value);
175       break;
176     case PROP_STEREO:
177       stereo->stereo = g_value_get_float (value) * 10.0;
178       break;
179     default:
180       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181       break;
182   }
183 }
184 
185 static void
gst_stereo_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)186 gst_stereo_get_property (GObject * object, guint prop_id, GValue * value,
187     GParamSpec * pspec)
188 {
189   GstStereo *stereo = GST_STEREO (object);
190 
191   switch (prop_id) {
192     case PROP_ACTIVE:
193       g_value_set_boolean (value, stereo->active);
194       break;
195     case PROP_STEREO:
196       g_value_set_float (value, stereo->stereo / 10.0);
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202 }
203