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 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
122 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
128 GstCaps * caps, gsize * size);
129 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
130 GstPadDirection direction, GstCaps * caps);
131 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
132 GstCaps * incaps, GstCaps * outcaps);
134 static void gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter,
135 gint16 * idata, gint16 * odata, guint num_samples);
136 static void gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter,
137 gint16 * idata, gint16 * odata, guint num_samples);
138 static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
139 gfloat * idata, gfloat * odata, guint num_samples);
140 static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
141 gfloat * idata, gfloat * odata, guint num_samples);
143 static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
144 filter, gint16 * idata, gint16 * odata, guint num_samples);
145 static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
146 filter, gint16 * idata, gint16 * odata, guint num_samples);
147 static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
148 filter, gfloat * idata, gfloat * odata, guint num_samples);
149 static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
150 filter, gfloat * idata, gfloat * odata, guint num_samples);
152 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
153 GstBuffer * inbuf, GstBuffer * outbuf);
156 /* Table with processing functions: [channels][format][method] */
157 static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
159 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
160 (GstAudioPanoramaProcessFunc)
161 gst_audio_panorama_transform_m2s_int_simple},
162 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
163 (GstAudioPanoramaProcessFunc)
164 gst_audio_panorama_transform_m2s_float_simple}
167 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
168 (GstAudioPanoramaProcessFunc)
169 gst_audio_panorama_transform_s2s_int_simple},
170 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
171 (GstAudioPanoramaProcessFunc)
172 gst_audio_panorama_transform_s2s_float_simple}
176 /* GObject vmethod implementations */
179 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
181 GObjectClass *gobject_class;
182 GstElementClass *gstelement_class;
184 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
185 "audiopanorama element");
187 gobject_class = (GObjectClass *) klass;
188 gstelement_class = (GstElementClass *) klass;
190 gobject_class->set_property = gst_audio_panorama_set_property;
191 gobject_class->get_property = gst_audio_panorama_get_property;
193 g_object_class_install_property (gobject_class, PROP_PANORAMA,
194 g_param_spec_float ("panorama", "Panorama",
195 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
197 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
199 * GstAudioPanorama:method
201 * Panning method: psychoacoustic mode keeps the same perceived loudness,
202 * while simple mode just controls the volume of one channel. It's merely
203 * a matter of taste which method should be chosen.
207 g_object_class_install_property (gobject_class, PROP_METHOD,
208 g_param_spec_enum ("method", "Panning method",
209 "Psychoacoustic mode keeps same perceived loudness, "
210 "simple mode just controls volume of one channel.",
211 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 gst_element_class_set_details_simple (gstelement_class, "Stereo positioning",
215 "Filter/Effect/Audio",
216 "Positions audio streams in the stereo panorama",
217 "Stefan Kost <ensonic@users.sf.net>");
219 gst_element_class_add_pad_template (gstelement_class,
220 gst_static_pad_template_get (&src_template));
221 gst_element_class_add_pad_template (gstelement_class,
222 gst_static_pad_template_get (&sink_template));
224 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
225 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
226 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
227 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
228 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
229 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
230 GST_BASE_TRANSFORM_CLASS (klass)->transform =
231 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
235 gst_audio_panorama_init (GstAudioPanorama * filter)
238 filter->panorama = 0;
239 filter->method = METHOD_PSYCHOACOUSTIC;
241 filter->channels = 0;
242 filter->format_float = FALSE;
243 filter->process = NULL;
245 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
249 gst_audio_panorama_set_process_function (GstAudioPanorama * filter)
251 gint channel_index, format_index, method_index;
253 /* set processing function */
254 channel_index = filter->channels - 1;
255 if (channel_index > 1 || channel_index < 0) {
256 filter->process = NULL;
260 format_index = (filter->format_float) ? 1 : 0;
262 method_index = filter->method;
263 if (method_index >= NUM_METHODS || method_index < 0)
264 method_index = METHOD_PSYCHOACOUSTIC;
267 panorama_process_functions[channel_index][format_index][method_index];
272 gst_audio_panorama_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec)
275 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
279 filter->panorama = g_value_get_float (value);
282 filter->method = g_value_get_enum (value);
283 gst_audio_panorama_set_process_function (filter);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 gst_audio_panorama_get_property (GObject * object, guint prop_id,
293 GValue * value, GParamSpec * pspec)
295 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
299 g_value_set_float (value, filter->panorama);
302 g_value_set_enum (value, filter->method);
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 /* GstBaseTransform vmethod implementations */
313 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
316 gint width, channels;
317 GstStructure *structure;
322 /* this works for both float and int */
323 structure = gst_caps_get_structure (caps, 0);
324 ret = gst_structure_get_int (structure, "width", &width);
325 ret &= gst_structure_get_int (structure, "channels", &channels);
327 *size = width * channels / 8;
333 gst_audio_panorama_transform_caps (GstBaseTransform * base,
334 GstPadDirection direction, GstCaps * caps)
337 GstStructure *structure;
339 /* transform caps gives one single caps so we can just replace
340 * the channel property with our range. */
341 res = gst_caps_copy (caps);
342 structure = gst_caps_get_structure (res, 0);
343 if (direction == GST_PAD_SRC) {
344 GST_INFO ("allow 1-2 channels");
345 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
347 GST_INFO ("allow 2 channels");
348 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
355 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
358 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
359 const GstStructure *structure;
364 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
366 structure = gst_caps_get_structure (incaps, 0);
367 ret = gst_structure_get_int (structure, "channels", &filter->channels);
371 ret = gst_structure_get_int (structure, "width", &width);
374 filter->width = width / 8;
376 fmt = gst_structure_get_name (structure);
377 if (!strcmp (fmt, "audio/x-raw-int"))
378 filter->format_float = FALSE;
380 filter->format_float = TRUE;
382 GST_DEBUG ("try to process %s input with %d channels", fmt, filter->channels);
384 ret = gst_audio_panorama_set_process_function (filter);
387 GST_WARNING ("can't process input with %d channels", filter->channels);
392 GST_DEBUG ("no channels in caps");
395 GST_DEBUG ("no width in caps");
399 /* psychoacoustic processing functions */
401 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
402 gint16 * odata, guint num_samples)
413 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
415 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
418 for (i = 0; i < num_samples; i++) {
419 val = (gdouble) * idata++;
421 lval = (glong) (val * lpan);
422 rval = (glong) (val * rpan);
424 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
425 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
430 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
431 gint16 * odata, guint num_samples)
435 gdouble lival, rival;
436 gdouble lrpan, llpan, rrpan, rlpan;
444 if (filter->panorama > 0) {
445 rlpan = (gdouble) filter->panorama;
450 rrpan = (gdouble) (1.0 + filter->panorama);
456 for (i = 0; i < num_samples; i++) {
457 lival = (gdouble) * idata++;
458 rival = (gdouble) * idata++;
460 lval = lival * llpan + rival * lrpan;
461 rval = lival * rlpan + rival * rrpan;
463 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
464 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
469 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
470 gfloat * idata, gfloat * odata, guint num_samples)
480 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
482 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
485 for (i = 0; i < num_samples; i++) {
488 *odata++ = val * lpan;
489 *odata++ = val * rpan;
494 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
495 gfloat * idata, gfloat * odata, guint num_samples)
499 gdouble lrpan, llpan, rrpan, rlpan;
507 if (filter->panorama > 0) {
508 rlpan = (gdouble) filter->panorama;
513 rrpan = (gdouble) (1.0 + filter->panorama);
519 for (i = 0; i < num_samples; i++) {
523 *odata++ = lival * llpan + rival * lrpan;
524 *odata++ = lival * rlpan + rival * rrpan;
528 /* simple processing functions */
530 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
531 gint16 * idata, gint16 * odata, guint num_samples)
537 if (filter->panorama > 0.0) {
538 pan = 1.0 - filter->panorama;
539 for (i = 0; i < num_samples; i++) {
541 lval = (glong) ((gdouble) rval * pan);
543 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
544 *odata++ = (gint16) rval;
547 pan = 1.0 + filter->panorama;
548 for (i = 0; i < num_samples; i++) {
550 rval = (glong) ((gdouble) lval * pan);
552 *odata++ = (gint16) lval;
553 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
559 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
560 gint16 * idata, gint16 * odata, guint num_samples)
564 gdouble lival, rival, pan;
566 if (filter->panorama > 0.0) {
567 pan = 1.0 - filter->panorama;
568 for (i = 0; i < num_samples; i++) {
569 lival = (gdouble) * idata++;
570 rival = (gdouble) * idata++;
572 lval = (glong) (lival * pan);
573 rval = (glong) rival;
575 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
576 *odata++ = (gint16) rval;
579 pan = 1.0 + filter->panorama;
580 for (i = 0; i < num_samples; i++) {
581 lival = (gdouble) * idata++;
582 rival = (gdouble) * idata++;
584 lval = (glong) lival;
585 rval = (glong) (rival * pan);
587 *odata++ = (gint16) lval;
588 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
594 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
595 gfloat * idata, gfloat * odata, guint num_samples)
600 if (filter->panorama > 0.0) {
601 pan = 1.0 - filter->panorama;
602 for (i = 0; i < num_samples; i++) {
605 *odata++ = val * pan;
609 pan = 1.0 + filter->panorama;
610 for (i = 0; i < num_samples; i++) {
614 *odata++ = val * pan;
620 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
621 gfloat * idata, gfloat * odata, guint num_samples)
624 gfloat lival, rival, pan;
626 if (filter->panorama > 0.0) {
627 pan = 1.0 - filter->panorama;
628 for (i = 0; i < num_samples; i++) {
632 *odata++ = lival * pan;
636 pan = 1.0 + filter->panorama;
637 for (i = 0; i < num_samples; i++) {
642 *odata++ = rival * pan;
647 /* this function does the actual processing
650 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
653 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
654 GstClockTime timestamp, stream_time;
655 guint8 *indata, *outdata;
656 gsize insize, outsize;
658 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
660 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
662 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
663 GST_TIME_ARGS (timestamp));
665 if (GST_CLOCK_TIME_IS_VALID (stream_time))
666 gst_object_sync_values (G_OBJECT (filter), stream_time);
668 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
669 outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
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 (outdata, 0, outsize);
675 guint num_samples = outsize / (2 * filter->width);
677 filter->process (filter, indata, outdata, num_samples);
680 gst_buffer_unmap (inbuf, indata, insize);
681 gst_buffer_unmap (outbuf, outdata, outsize);