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_static_pad_template (element_class, &src_template);
188 gst_element_class_add_static_pad_template (element_class,
190 gst_element_class_set_details_simple (element_class, "Stereo positioning",
191 "Filter/Effect/Audio",
192 "Positions audio streams in the stereo panorama",
193 "Stefan Kost <ensonic@users.sf.net>");
197 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
199 GObjectClass *gobject_class;
201 gobject_class = (GObjectClass *) klass;
202 gobject_class->set_property = gst_audio_panorama_set_property;
203 gobject_class->get_property = gst_audio_panorama_get_property;
205 g_object_class_install_property (gobject_class, PROP_PANORAMA,
206 g_param_spec_float ("panorama", "Panorama",
207 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
209 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
211 * GstAudioPanorama:method
213 * Panning method: psychoacoustic mode keeps the same perceived loudness,
214 * while simple mode just controls the volume of one channel. It's merely
215 * a matter of taste which method should be chosen.
219 g_object_class_install_property (gobject_class, PROP_METHOD,
220 g_param_spec_enum ("method", "Panning method",
221 "Psychoacoustic mode keeps same perceived loudness, "
222 "simple mode just controls volume of one channel.",
223 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
227 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
228 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
229 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
230 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
231 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
232 GST_BASE_TRANSFORM_CLASS (klass)->transform =
233 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
237 gst_audio_panorama_init (GstAudioPanorama * filter,
238 GstAudioPanoramaClass * klass)
241 filter->panorama = 0;
242 filter->method = METHOD_PSYCHOACOUSTIC;
244 filter->channels = 0;
245 filter->format_float = FALSE;
246 filter->process = NULL;
248 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
252 gst_audio_panorama_set_process_function (GstAudioPanorama * filter)
254 gint channel_index, format_index, method_index;
256 /* set processing function */
257 channel_index = filter->channels - 1;
258 if (channel_index > 1 || channel_index < 0) {
259 filter->process = NULL;
263 format_index = (filter->format_float) ? 1 : 0;
265 method_index = filter->method;
266 if (method_index >= NUM_METHODS || method_index < 0)
267 method_index = METHOD_PSYCHOACOUSTIC;
270 panorama_process_functions[channel_index][format_index][method_index];
275 gst_audio_panorama_set_property (GObject * object, guint prop_id,
276 const GValue * value, GParamSpec * pspec)
278 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
282 filter->panorama = g_value_get_float (value);
285 filter->method = g_value_get_enum (value);
286 gst_audio_panorama_set_process_function (filter);
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gst_audio_panorama_get_property (GObject * object, guint prop_id,
296 GValue * value, GParamSpec * pspec)
298 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
302 g_value_set_float (value, filter->panorama);
305 g_value_set_enum (value, filter->method);
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 /* GstBaseTransform vmethod implementations */
316 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
319 gint width, channels;
320 GstStructure *structure;
325 /* this works for both float and int */
326 structure = gst_caps_get_structure (caps, 0);
327 ret = gst_structure_get_int (structure, "width", &width);
328 ret &= gst_structure_get_int (structure, "channels", &channels);
330 *size = width * channels / 8;
336 gst_audio_panorama_transform_caps (GstBaseTransform * base,
337 GstPadDirection direction, GstCaps * caps)
340 GstStructure *structure;
342 /* transform caps gives one single caps so we can just replace
343 * the channel property with our range. */
344 res = gst_caps_copy (caps);
345 structure = gst_caps_get_structure (res, 0);
346 if (direction == GST_PAD_SRC) {
347 GST_INFO ("allow 1-2 channels");
348 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
350 GST_INFO ("allow 2 channels");
351 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
358 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
361 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
362 const GstStructure *structure;
367 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
369 structure = gst_caps_get_structure (incaps, 0);
370 ret = gst_structure_get_int (structure, "channels", &filter->channels);
374 ret = gst_structure_get_int (structure, "width", &width);
377 filter->width = width / 8;
379 fmt = gst_structure_get_name (structure);
380 if (!strcmp (fmt, "audio/x-raw-int"))
381 filter->format_float = FALSE;
383 filter->format_float = TRUE;
385 GST_DEBUG ("try to process %s input with %d channels", fmt, filter->channels);
387 ret = gst_audio_panorama_set_process_function (filter);
390 GST_WARNING ("can't process input with %d channels", filter->channels);
395 GST_DEBUG ("no channels in caps");
398 GST_DEBUG ("no width in caps");
402 /* psychoacoustic processing functions */
404 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
405 gint16 * odata, guint num_samples)
416 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
418 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
421 for (i = 0; i < num_samples; i++) {
422 val = (gdouble) * idata++;
424 lval = (glong) (val * lpan);
425 rval = (glong) (val * rpan);
427 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
428 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
433 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
434 gint16 * odata, guint num_samples)
438 gdouble lival, rival;
439 gdouble lrpan, llpan, rrpan, rlpan;
447 if (filter->panorama > 0) {
448 rlpan = (gdouble) filter->panorama;
453 rrpan = (gdouble) (1.0 + filter->panorama);
459 for (i = 0; i < num_samples; i++) {
460 lival = (gdouble) * idata++;
461 rival = (gdouble) * idata++;
463 lval = lival * llpan + rival * lrpan;
464 rval = lival * rlpan + rival * rrpan;
466 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
467 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
472 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
473 gfloat * idata, gfloat * odata, guint num_samples)
483 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
485 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
488 for (i = 0; i < num_samples; i++) {
491 *odata++ = val * lpan;
492 *odata++ = val * rpan;
497 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
498 gfloat * idata, gfloat * odata, guint num_samples)
502 gdouble lrpan, llpan, rrpan, rlpan;
510 if (filter->panorama > 0) {
511 rlpan = (gdouble) filter->panorama;
516 rrpan = (gdouble) (1.0 + filter->panorama);
522 for (i = 0; i < num_samples; i++) {
526 *odata++ = lival * llpan + rival * lrpan;
527 *odata++ = lival * rlpan + rival * rrpan;
531 /* simple processing functions */
533 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
534 gint16 * idata, gint16 * odata, guint num_samples)
540 if (filter->panorama > 0.0) {
541 pan = 1.0 - filter->panorama;
542 for (i = 0; i < num_samples; i++) {
544 lval = (glong) ((gdouble) rval * pan);
546 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
547 *odata++ = (gint16) rval;
550 pan = 1.0 + filter->panorama;
551 for (i = 0; i < num_samples; i++) {
553 rval = (glong) ((gdouble) lval * pan);
555 *odata++ = (gint16) lval;
556 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
562 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
563 gint16 * idata, gint16 * odata, guint num_samples)
567 gdouble lival, rival, pan;
569 if (filter->panorama > 0.0) {
570 pan = 1.0 - filter->panorama;
571 for (i = 0; i < num_samples; i++) {
572 lival = (gdouble) * idata++;
573 rival = (gdouble) * idata++;
575 lval = (glong) (lival * pan);
576 rval = (glong) rival;
578 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
579 *odata++ = (gint16) rval;
582 pan = 1.0 + filter->panorama;
583 for (i = 0; i < num_samples; i++) {
584 lival = (gdouble) * idata++;
585 rival = (gdouble) * idata++;
587 lval = (glong) lival;
588 rval = (glong) (rival * pan);
590 *odata++ = (gint16) lval;
591 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
597 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
598 gfloat * idata, gfloat * odata, guint num_samples)
603 if (filter->panorama > 0.0) {
604 pan = 1.0 - filter->panorama;
605 for (i = 0; i < num_samples; i++) {
608 *odata++ = val * pan;
612 pan = 1.0 + filter->panorama;
613 for (i = 0; i < num_samples; i++) {
617 *odata++ = val * pan;
623 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
624 gfloat * idata, gfloat * odata, guint num_samples)
627 gfloat lival, rival, pan;
629 if (filter->panorama > 0.0) {
630 pan = 1.0 - filter->panorama;
631 for (i = 0; i < num_samples; i++) {
635 *odata++ = lival * pan;
639 pan = 1.0 + filter->panorama;
640 for (i = 0; i < num_samples; i++) {
645 *odata++ = rival * pan;
650 /* this function does the actual processing
653 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
656 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
657 guint num_samples = GST_BUFFER_SIZE (outbuf) / (2 * filter->width);
658 GstClockTime timestamp, stream_time;
660 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
662 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
664 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
665 GST_TIME_ARGS (timestamp));
667 if (GST_CLOCK_TIME_IS_VALID (stream_time))
668 gst_object_sync_values (G_OBJECT (filter), stream_time);
670 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
671 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
672 memset (GST_BUFFER_DATA (outbuf), 0, GST_BUFFER_SIZE (outbuf));
676 filter->process (filter, GST_BUFFER_DATA (inbuf),
677 GST_BUFFER_DATA (outbuf), num_samples);