Explicitly define float constants as float
[platform/upstream/gst-plugins-good.git] / gst / audiofx / gststereo.c
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  *
27  * Create a wide stereo effect.
28  *
29  * <refsect2>
30  * <title>Example pipelines</title>
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  * </refsect2>
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
77 static void
78 gst_stereo_class_init (GstStereoClass * klass)
79 {
80   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82   GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass);
83   GstAudioFilterClass *audiofilter_class = GST_AUDIO_FILTER_CLASS (klass);
84   GstCaps *caps;
85
86   gst_element_class_set_static_metadata (element_class, "Stereo effect",
87       "Filter/Effect/Audio",
88       "Muck with the stereo signal to enhance its 'stereo-ness'",
89       "Erik Walthinsen <omega@cse.ogi.edu>");
90
91   caps = gst_caps_from_string (ALLOWED_CAPS);
92   gst_audio_filter_class_add_pad_templates (audiofilter_class, caps);
93   gst_caps_unref (caps);
94
95   gobject_class->set_property = gst_stereo_set_property;
96   gobject_class->get_property = gst_stereo_get_property;
97
98   g_object_class_install_property (gobject_class, PROP_ACTIVE,
99       g_param_spec_boolean ("active", "active", "active",
100           TRUE,
101           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
102
103   g_object_class_install_property (gobject_class, PROP_STEREO,
104       g_param_spec_float ("stereo", "stereo", "stereo",
105           0.0, 1.0, 0.1f,
106           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
107
108   trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_stereo_transform_ip);
109 }
110
111 static void
112 gst_stereo_init (GstStereo * stereo)
113 {
114   stereo->active = TRUE;
115   stereo->stereo = 0.1f;
116 }
117
118 static GstFlowReturn
119 gst_stereo_transform_ip (GstBaseTransform * base, GstBuffer * outbuf)
120 {
121   GstStereo *stereo = GST_STEREO (base);
122   gint samples;
123   gint i;
124   gdouble avg, ldiff, rdiff, tmp;
125   gdouble mul = stereo->stereo;
126   gint16 *data;
127   GstMapInfo info;
128
129   if (!gst_buffer_map (outbuf, &info, GST_MAP_READWRITE))
130     return GST_FLOW_ERROR;
131
132   data = (gint16 *) info.data;
133   samples = info.size / 2;
134
135   if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (outbuf)))
136     gst_object_sync_values (GST_OBJECT (stereo), GST_BUFFER_TIMESTAMP (outbuf));
137
138   if (stereo->active) {
139     for (i = 0; i < samples / 2; i += 2) {
140       avg = (data[i] + data[i + 1]) / 2;
141       ldiff = data[i] - avg;
142       rdiff = data[i + 1] - avg;
143
144       tmp = avg + ldiff * mul;
145       if (tmp < -32768)
146         tmp = -32768;
147       if (tmp > 32767)
148         tmp = 32767;
149       data[i] = tmp;
150
151       tmp = avg + rdiff * mul;
152       if (tmp < -32768)
153         tmp = -32768;
154       if (tmp > 32767)
155         tmp = 32767;
156       data[i + 1] = tmp;
157     }
158   }
159
160   gst_buffer_unmap (outbuf, &info);
161
162   return GST_FLOW_OK;
163 }
164
165 static void
166 gst_stereo_set_property (GObject * object, guint prop_id, const GValue * value,
167     GParamSpec * pspec)
168 {
169   GstStereo *stereo = GST_STEREO (object);
170
171   switch (prop_id) {
172     case PROP_ACTIVE:
173       stereo->active = g_value_get_boolean (value);
174       break;
175     case PROP_STEREO:
176       stereo->stereo = g_value_get_float (value) * 10.0;
177       break;
178     default:
179       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180       break;
181   }
182 }
183
184 static void
185 gst_stereo_get_property (GObject * object, guint prop_id, GValue * value,
186     GParamSpec * pspec)
187 {
188   GstStereo *stereo = GST_STEREO (object);
189
190   switch (prop_id) {
191     case PROP_ACTIVE:
192       g_value_set_boolean (value, stereo->active);
193       break;
194     case PROP_STEREO:
195       g_value_set_float (value, stereo->stereo / 10.0);
196       break;
197     default:
198       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199       break;
200   }
201 }
202
203 static gboolean
204 plugin_init (GstPlugin * plugin)
205 {
206   return gst_element_register (plugin, "stereo", GST_RANK_NONE,
207       GST_TYPE_STEREO);
208 }
209
210 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
211     GST_VERSION_MINOR,
212     stereo,
213     "Muck with the stereo signal, enhance it's 'stereo-ness'",
214     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)