3 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
4 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-audiopanorama
25 * Stereo panorama effect with controllable pan position. One can choose between the default psychoacoustic panning method,
26 * which keeps the same perceived loudness, and a simple panning method that just controls the volume on one channel.
29 * <title>Example launch line</title>
31 * gst-launch audiotestsrc wave=saw ! audiopanorama panorama=-1.00 ! alsasink
32 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiopanorama panorama=-1.00 ! alsasink
33 * gst-launch audiotestsrc wave=saw ! audioconvert ! audiopanorama panorama=-1.00 ! audioconvert ! alsasink
34 * gst-launch audiotestsrc wave=saw ! audioconvert ! audiopanorama method=simple panorama=-0.50 ! audioconvert ! alsasink
44 #include <gst/base/gstbasetransform.h>
45 #include <gst/controller/gstcontroller.h>
47 #include "audiopanorama.h"
49 #define GST_CAT_DEFAULT gst_audio_panorama_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 /* Filter signals and args */
68 METHOD_PSYCHOACOUSTIC = 0,
73 #define GST_TYPE_AUDIO_PANORAMA_METHOD (gst_audio_panorama_method_get_type ())
75 gst_audio_panorama_method_get_type (void)
77 static GType gtype = 0;
80 static const GEnumValue values[] = {
81 {METHOD_PSYCHOACOUSTIC, "Psychoacoustic Panning (default)",
83 {METHOD_SIMPLE, "Simple Panning", "simple"},
87 gtype = g_enum_register_static ("GstAudioPanoramaMethod", values);
92 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
95 GST_STATIC_CAPS ("audio/x-raw-float, "
96 "rate = (int) [ 1, MAX ], "
97 "channels = (int) [ 1, 2 ], "
98 "endianness = (int) BYTE_ORDER, " "width = (int) 32; "
100 "rate = (int) [ 1, MAX ], "
101 "channels = (int) [ 1, 2 ], "
102 "endianness = (int) BYTE_ORDER, "
103 "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
106 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_STATIC_CAPS ("audio/x-raw-float, "
110 "rate = (int) [ 1, MAX ], "
111 "channels = (int) 2, "
112 "endianness = (int) BYTE_ORDER, " "width = (int) 32; "
114 "rate = (int) [ 1, MAX ], "
115 "channels = (int) 2, "
116 "endianness = (int) BYTE_ORDER, "
117 "width = (int) 16, " "depth = (int) 16, " "signed = (boolean) true")
120 #define DEBUG_INIT(bla) \
121 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0, "audiopanorama element");
123 GST_BOILERPLATE_FULL (GstAudioPanorama, gst_audio_panorama, GstBaseTransform,
124 GST_TYPE_BASE_TRANSFORM, DEBUG_INIT);
126 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
127 const GValue * value, GParamSpec * pspec);
128 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
129 GValue * value, GParamSpec * pspec);
131 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
132 GstCaps * caps, guint * size);
133 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
134 GstPadDirection direction, GstCaps * caps);
135 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
136 GstCaps * incaps, GstCaps * outcaps);
138 static void gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter,
139 gint16 * idata, gint16 * odata, guint num_samples);
140 static void gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter,
141 gint16 * idata, gint16 * odata, guint num_samples);
142 static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
143 gfloat * idata, gfloat * odata, guint num_samples);
144 static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
145 gfloat * idata, gfloat * odata, guint num_samples);
147 static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
148 filter, gint16 * idata, gint16 * odata, guint num_samples);
149 static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
150 filter, gint16 * idata, gint16 * odata, guint num_samples);
151 static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
152 filter, gfloat * idata, gfloat * odata, guint num_samples);
153 static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
154 filter, gfloat * idata, gfloat * odata, guint num_samples);
156 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
157 GstBuffer * inbuf, GstBuffer * outbuf);
160 /* Table with processing functions: [channels][format][method] */
161 static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
163 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
164 (GstAudioPanoramaProcessFunc)
165 gst_audio_panorama_transform_m2s_int_simple},
166 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
167 (GstAudioPanoramaProcessFunc)
168 gst_audio_panorama_transform_m2s_float_simple}
171 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
172 (GstAudioPanoramaProcessFunc)
173 gst_audio_panorama_transform_s2s_int_simple},
174 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
175 (GstAudioPanoramaProcessFunc)
176 gst_audio_panorama_transform_s2s_float_simple}
180 /* GObject vmethod implementations */
183 gst_audio_panorama_base_init (gpointer klass)
185 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
187 gst_element_class_add_pad_template (element_class,
188 gst_static_pad_template_get (&src_template));
189 gst_element_class_add_pad_template (element_class,
190 gst_static_pad_template_get (&sink_template));
191 gst_element_class_set_details_simple (element_class, "Stereo positioning",
192 "Filter/Effect/Audio",
193 "Positions audio streams in the stereo panorama",
194 "Stefan Kost <ensonic@users.sf.net>");
198 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
200 GObjectClass *gobject_class;
202 gobject_class = (GObjectClass *) klass;
203 gobject_class->set_property = gst_audio_panorama_set_property;
204 gobject_class->get_property = gst_audio_panorama_get_property;
206 g_object_class_install_property (gobject_class, PROP_PANORAMA,
207 g_param_spec_float ("panorama", "Panorama",
208 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
210 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
212 * GstAudioPanorama:method
214 * Panning method: psychoacoustic mode keeps the same perceived loudness,
215 * while simple mode just controls the volume of one channel. It's merely
216 * a matter of taste which method should be chosen.
220 g_object_class_install_property (gobject_class, PROP_METHOD,
221 g_param_spec_enum ("method", "Panning method",
222 "Psychoacoustic mode keeps same perceived loudness, "
223 "simple mode just controls volume of one channel.",
224 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
228 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
229 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
230 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
231 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
232 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
233 GST_BASE_TRANSFORM_CLASS (klass)->transform =
234 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
238 gst_audio_panorama_init (GstAudioPanorama * filter,
239 GstAudioPanoramaClass * klass)
242 filter->panorama = 0;
243 filter->method = METHOD_PSYCHOACOUSTIC;
245 filter->channels = 0;
246 filter->format_float = FALSE;
247 filter->process = NULL;
249 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
253 gst_audio_panorama_set_process_function (GstAudioPanorama * filter)
255 gint channel_index, format_index, method_index;
257 /* set processing function */
258 channel_index = filter->channels - 1;
259 if (channel_index > 1 || channel_index < 0) {
260 filter->process = NULL;
264 format_index = (filter->format_float) ? 1 : 0;
266 method_index = filter->method;
267 if (method_index >= NUM_METHODS || method_index < 0)
268 method_index = METHOD_PSYCHOACOUSTIC;
271 panorama_process_functions[channel_index][format_index][method_index];
276 gst_audio_panorama_set_property (GObject * object, guint prop_id,
277 const GValue * value, GParamSpec * pspec)
279 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
283 filter->panorama = g_value_get_float (value);
286 filter->method = g_value_get_enum (value);
287 gst_audio_panorama_set_process_function (filter);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 gst_audio_panorama_get_property (GObject * object, guint prop_id,
297 GValue * value, GParamSpec * pspec)
299 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
303 g_value_set_float (value, filter->panorama);
306 g_value_set_enum (value, filter->method);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 /* GstBaseTransform vmethod implementations */
317 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
320 gint width, channels;
321 GstStructure *structure;
326 /* this works for both float and int */
327 structure = gst_caps_get_structure (caps, 0);
328 ret = gst_structure_get_int (structure, "width", &width);
329 ret &= gst_structure_get_int (structure, "channels", &channels);
331 *size = width * channels / 8;
337 gst_audio_panorama_transform_caps (GstBaseTransform * base,
338 GstPadDirection direction, GstCaps * caps)
341 GstStructure *structure;
343 /* transform caps gives one single caps so we can just replace
344 * the channel property with our range. */
345 res = gst_caps_copy (caps);
346 structure = gst_caps_get_structure (res, 0);
347 if (direction == GST_PAD_SRC) {
348 GST_INFO ("allow 1-2 channels");
349 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
351 GST_INFO ("allow 2 channels");
352 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
359 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
362 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
363 const GstStructure *structure;
368 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
370 structure = gst_caps_get_structure (incaps, 0);
371 ret = gst_structure_get_int (structure, "channels", &filter->channels);
375 ret = gst_structure_get_int (structure, "width", &width);
378 filter->width = width / 8;
380 fmt = gst_structure_get_name (structure);
381 if (!strcmp (fmt, "audio/x-raw-int"))
382 filter->format_float = FALSE;
384 filter->format_float = TRUE;
386 GST_DEBUG ("try to process %s input with %d channels", fmt, filter->channels);
388 ret = gst_audio_panorama_set_process_function (filter);
391 GST_WARNING ("can't process input with %d channels", filter->channels);
396 GST_DEBUG ("no channels in caps");
399 GST_DEBUG ("no width in caps");
403 /* psychoacoustic processing functions */
405 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
406 gint16 * odata, guint num_samples)
417 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
419 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
422 for (i = 0; i < num_samples; i++) {
423 val = (gdouble) * idata++;
425 lval = (glong) (val * lpan);
426 rval = (glong) (val * rpan);
428 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
429 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
434 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
435 gint16 * odata, guint num_samples)
439 gdouble lival, rival;
440 gdouble lrpan, llpan, rrpan, rlpan;
448 if (filter->panorama > 0) {
449 rlpan = (gdouble) filter->panorama;
454 rrpan = (gdouble) (1.0 + filter->panorama);
460 for (i = 0; i < num_samples; i++) {
461 lival = (gdouble) * idata++;
462 rival = (gdouble) * idata++;
464 lval = lival * llpan + rival * lrpan;
465 rval = lival * rlpan + rival * rrpan;
467 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
468 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
473 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
474 gfloat * idata, gfloat * odata, guint num_samples)
484 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
486 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
489 for (i = 0; i < num_samples; i++) {
492 *odata++ = val * lpan;
493 *odata++ = val * rpan;
498 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
499 gfloat * idata, gfloat * odata, guint num_samples)
503 gdouble lrpan, llpan, rrpan, rlpan;
511 if (filter->panorama > 0) {
512 rlpan = (gdouble) filter->panorama;
517 rrpan = (gdouble) (1.0 + filter->panorama);
523 for (i = 0; i < num_samples; i++) {
527 *odata++ = lival * llpan + rival * lrpan;
528 *odata++ = lival * rlpan + rival * rrpan;
532 /* simple processing functions */
534 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
535 gint16 * idata, gint16 * odata, guint num_samples)
541 if (filter->panorama > 0.0) {
542 pan = 1.0 - filter->panorama;
543 for (i = 0; i < num_samples; i++) {
545 lval = (glong) ((gdouble) rval * pan);
547 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
548 *odata++ = (gint16) rval;
551 pan = 1.0 + filter->panorama;
552 for (i = 0; i < num_samples; i++) {
554 rval = (glong) ((gdouble) lval * pan);
556 *odata++ = (gint16) lval;
557 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
563 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
564 gint16 * idata, gint16 * odata, guint num_samples)
568 gdouble lival, rival, pan;
570 if (filter->panorama > 0.0) {
571 pan = 1.0 - filter->panorama;
572 for (i = 0; i < num_samples; i++) {
573 lival = (gdouble) * idata++;
574 rival = (gdouble) * idata++;
576 lval = (glong) (lival * pan);
577 rval = (glong) rival;
579 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
580 *odata++ = (gint16) rval;
583 pan = 1.0 + filter->panorama;
584 for (i = 0; i < num_samples; i++) {
585 lival = (gdouble) * idata++;
586 rival = (gdouble) * idata++;
588 lval = (glong) lival;
589 rval = (glong) (rival * pan);
591 *odata++ = (gint16) lval;
592 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
598 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
599 gfloat * idata, gfloat * odata, guint num_samples)
604 if (filter->panorama > 0.0) {
605 pan = 1.0 - filter->panorama;
606 for (i = 0; i < num_samples; i++) {
609 *odata++ = val * pan;
613 pan = 1.0 + filter->panorama;
614 for (i = 0; i < num_samples; i++) {
618 *odata++ = val * pan;
624 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
625 gfloat * idata, gfloat * odata, guint num_samples)
628 gfloat lival, rival, pan;
630 if (filter->panorama > 0.0) {
631 pan = 1.0 - filter->panorama;
632 for (i = 0; i < num_samples; i++) {
636 *odata++ = lival * pan;
640 pan = 1.0 + filter->panorama;
641 for (i = 0; i < num_samples; i++) {
646 *odata++ = rival * pan;
651 /* this function does the actual processing
654 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
657 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
658 guint num_samples = GST_BUFFER_SIZE (outbuf) / (2 * filter->width);
659 GstClockTime timestamp, stream_time;
661 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
663 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
665 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
666 GST_TIME_ARGS (timestamp));
668 if (GST_CLOCK_TIME_IS_VALID (stream_time))
669 gst_object_sync_values (G_OBJECT (filter), stream_time);
671 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
672 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
673 memset (GST_BUFFER_DATA (outbuf), 0, GST_BUFFER_SIZE (outbuf));
677 filter->process (filter, GST_BUFFER_DATA (inbuf),
678 GST_BUFFER_DATA (outbuf), num_samples);