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
46 #include <gst/base/gstbasetransform.h>
48 #include "audiopanorama.h"
50 #define GST_CAT_DEFAULT gst_audio_panorama_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 /* Filter signals and args */
69 METHOD_PSYCHOACOUSTIC = 0,
74 #define GST_TYPE_AUDIO_PANORAMA_METHOD (gst_audio_panorama_method_get_type ())
76 gst_audio_panorama_method_get_type (void)
78 static GType gtype = 0;
81 static const GEnumValue values[] = {
82 {METHOD_PSYCHOACOUSTIC, "Psychoacoustic Panning (default)",
84 {METHOD_SIMPLE, "Simple Panning", "simple"},
88 gtype = g_enum_register_static ("GstAudioPanoramaMethod", values);
93 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
96 GST_STATIC_CAPS ("audio/x-raw, "
97 "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
98 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
101 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
104 GST_STATIC_CAPS ("audio/x-raw, "
105 "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
106 "rate = (int) [ 1, MAX ], " "channels = (int) 2")
109 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
111 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
112 const GValue * value, GParamSpec * pspec);
113 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
114 GValue * value, GParamSpec * pspec);
116 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
117 GstCaps * caps, gsize * size);
118 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
119 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
120 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
121 GstCaps * incaps, GstCaps * outcaps);
123 static void gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter,
124 gint16 * idata, gint16 * odata, guint num_samples);
125 static void gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter,
126 gint16 * idata, gint16 * odata, guint num_samples);
127 static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
128 gfloat * idata, gfloat * odata, guint num_samples);
129 static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
130 gfloat * idata, gfloat * odata, guint num_samples);
132 static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
133 filter, gint16 * idata, gint16 * odata, guint num_samples);
134 static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
135 filter, gint16 * idata, gint16 * odata, guint num_samples);
136 static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
137 filter, gfloat * idata, gfloat * odata, guint num_samples);
138 static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
139 filter, gfloat * idata, gfloat * odata, guint num_samples);
141 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
142 GstBuffer * inbuf, GstBuffer * outbuf);
145 /* Table with processing functions: [channels][format][method] */
146 static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
148 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
149 (GstAudioPanoramaProcessFunc)
150 gst_audio_panorama_transform_m2s_int_simple},
151 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
152 (GstAudioPanoramaProcessFunc)
153 gst_audio_panorama_transform_m2s_float_simple}
156 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
157 (GstAudioPanoramaProcessFunc)
158 gst_audio_panorama_transform_s2s_int_simple},
159 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
160 (GstAudioPanoramaProcessFunc)
161 gst_audio_panorama_transform_s2s_float_simple}
165 /* GObject vmethod implementations */
168 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
170 GObjectClass *gobject_class;
171 GstElementClass *gstelement_class;
173 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
174 "audiopanorama element");
176 gobject_class = (GObjectClass *) klass;
177 gstelement_class = (GstElementClass *) klass;
179 gobject_class->set_property = gst_audio_panorama_set_property;
180 gobject_class->get_property = gst_audio_panorama_get_property;
182 g_object_class_install_property (gobject_class, PROP_PANORAMA,
183 g_param_spec_float ("panorama", "Panorama",
184 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
186 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
188 * GstAudioPanorama:method
190 * Panning method: psychoacoustic mode keeps the same perceived loudness,
191 * while simple mode just controls the volume of one channel. It's merely
192 * a matter of taste which method should be chosen.
196 g_object_class_install_property (gobject_class, PROP_METHOD,
197 g_param_spec_enum ("method", "Panning method",
198 "Psychoacoustic mode keeps same perceived loudness, "
199 "simple mode just controls volume of one channel.",
200 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 gst_element_class_set_details_simple (gstelement_class, "Stereo positioning",
204 "Filter/Effect/Audio",
205 "Positions audio streams in the stereo panorama",
206 "Stefan Kost <ensonic@users.sf.net>");
208 gst_element_class_add_pad_template (gstelement_class,
209 gst_static_pad_template_get (&src_template));
210 gst_element_class_add_pad_template (gstelement_class,
211 gst_static_pad_template_get (&sink_template));
213 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
214 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
215 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
216 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
217 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
218 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
219 GST_BASE_TRANSFORM_CLASS (klass)->transform =
220 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
224 gst_audio_panorama_init (GstAudioPanorama * filter)
227 filter->panorama = 0;
228 filter->method = METHOD_PSYCHOACOUSTIC;
229 gst_audio_info_init (&filter->info);
230 filter->process = NULL;
232 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
236 gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
239 gint channel_index, format_index, method_index;
240 const GstAudioFormatInfo *finfo = info->finfo;
242 /* set processing function */
243 channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
244 if (channel_index > 1 || channel_index < 0) {
245 filter->process = NULL;
249 format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
251 method_index = filter->method;
252 if (method_index >= NUM_METHODS || method_index < 0)
253 method_index = METHOD_PSYCHOACOUSTIC;
256 panorama_process_functions[channel_index][format_index][method_index];
261 gst_audio_panorama_set_property (GObject * object, guint prop_id,
262 const GValue * value, GParamSpec * pspec)
264 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
268 filter->panorama = g_value_get_float (value);
271 filter->method = g_value_get_enum (value);
272 gst_audio_panorama_set_process_function (filter, &filter->info);
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gst_audio_panorama_get_property (GObject * object, guint prop_id,
282 GValue * value, GParamSpec * pspec)
284 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
288 g_value_set_float (value, filter->panorama);
291 g_value_set_enum (value, filter->method);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 /* GstBaseTransform vmethod implementations */
302 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
309 if (!gst_audio_info_from_caps (&info, caps))
312 *size = GST_AUDIO_INFO_BPF (&info);
318 gst_audio_panorama_transform_caps (GstBaseTransform * base,
319 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
322 GstStructure *structure;
324 /* transform caps gives one single caps so we can just replace
325 * the channel property with our range. */
326 res = gst_caps_copy (caps);
327 structure = gst_caps_get_structure (res, 0);
328 if (direction == GST_PAD_SRC) {
329 GST_INFO ("allow 1-2 channels");
330 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
332 GST_INFO ("allow 2 channels");
333 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
340 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
343 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
346 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
347 if (!gst_audio_info_from_caps (&info, incaps))
350 GST_DEBUG ("try to process %d input with %d channels",
351 GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
353 if (!gst_audio_panorama_set_process_function (filter, &info))
362 GST_DEBUG ("invalid caps");
367 /* psychoacoustic processing functions */
369 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
370 gint16 * odata, guint num_samples)
381 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
383 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
386 for (i = 0; i < num_samples; i++) {
387 val = (gdouble) * idata++;
389 lval = (glong) (val * lpan);
390 rval = (glong) (val * rpan);
392 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
393 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
398 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
399 gint16 * odata, guint num_samples)
403 gdouble lival, rival;
404 gdouble lrpan, llpan, rrpan, rlpan;
412 if (filter->panorama > 0) {
413 rlpan = (gdouble) filter->panorama;
418 rrpan = (gdouble) (1.0 + filter->panorama);
424 for (i = 0; i < num_samples; i++) {
425 lival = (gdouble) * idata++;
426 rival = (gdouble) * idata++;
428 lval = lival * llpan + rival * lrpan;
429 rval = lival * rlpan + rival * rrpan;
431 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
432 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
437 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
438 gfloat * idata, gfloat * odata, guint num_samples)
448 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
450 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
453 for (i = 0; i < num_samples; i++) {
456 *odata++ = val * lpan;
457 *odata++ = val * rpan;
462 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
463 gfloat * idata, gfloat * odata, guint num_samples)
467 gdouble lrpan, llpan, rrpan, rlpan;
475 if (filter->panorama > 0) {
476 rlpan = (gdouble) filter->panorama;
481 rrpan = (gdouble) (1.0 + filter->panorama);
487 for (i = 0; i < num_samples; i++) {
491 *odata++ = lival * llpan + rival * lrpan;
492 *odata++ = lival * rlpan + rival * rrpan;
496 /* simple processing functions */
498 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
499 gint16 * idata, gint16 * odata, guint num_samples)
505 if (filter->panorama > 0.0) {
506 pan = 1.0 - filter->panorama;
507 for (i = 0; i < num_samples; i++) {
509 lval = (glong) ((gdouble) rval * pan);
511 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
512 *odata++ = (gint16) rval;
515 pan = 1.0 + filter->panorama;
516 for (i = 0; i < num_samples; i++) {
518 rval = (glong) ((gdouble) lval * pan);
520 *odata++ = (gint16) lval;
521 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
527 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
528 gint16 * idata, gint16 * odata, guint num_samples)
532 gdouble lival, rival, pan;
534 if (filter->panorama > 0.0) {
535 pan = 1.0 - filter->panorama;
536 for (i = 0; i < num_samples; i++) {
537 lival = (gdouble) * idata++;
538 rival = (gdouble) * idata++;
540 lval = (glong) (lival * pan);
541 rval = (glong) rival;
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++) {
549 lival = (gdouble) * idata++;
550 rival = (gdouble) * idata++;
552 lval = (glong) lival;
553 rval = (glong) (rival * pan);
555 *odata++ = (gint16) lval;
556 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
562 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
563 gfloat * idata, gfloat * odata, guint num_samples)
568 if (filter->panorama > 0.0) {
569 pan = 1.0 - filter->panorama;
570 for (i = 0; i < num_samples; i++) {
573 *odata++ = val * pan;
577 pan = 1.0 + filter->panorama;
578 for (i = 0; i < num_samples; i++) {
582 *odata++ = val * pan;
588 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
589 gfloat * idata, gfloat * odata, guint num_samples)
592 gfloat lival, rival, pan;
594 if (filter->panorama > 0.0) {
595 pan = 1.0 - filter->panorama;
596 for (i = 0; i < num_samples; i++) {
600 *odata++ = lival * pan;
604 pan = 1.0 + filter->panorama;
605 for (i = 0; i < num_samples; i++) {
610 *odata++ = rival * pan;
615 /* this function does the actual processing
618 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
621 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
622 GstClockTime timestamp, stream_time;
623 guint8 *indata, *outdata;
624 gsize insize, outsize;
626 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
628 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
630 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
631 GST_TIME_ARGS (timestamp));
633 if (GST_CLOCK_TIME_IS_VALID (stream_time))
634 gst_object_sync_values (GST_OBJECT (filter), stream_time);
636 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
637 outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
639 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
640 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
641 memset (outdata, 0, outsize);
643 guint num_samples = outsize / GST_AUDIO_INFO_BPF (&filter->info);
645 filter->process (filter, indata, outdata, num_samples);
648 gst_buffer_unmap (inbuf, indata, insize);
649 gst_buffer_unmap (outbuf, outdata, outsize);