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 (F32) ", " GST_AUDIO_NE (S16) "}, "
98 "rate = (int) [ 1, MAX ], " "channels = (int) 1, "
99 "layout = (string) interleaved;"
101 "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
102 "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
103 "layout = (string) interleaved, " "channel-mask = (bitmask) 0x3")
106 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_STATIC_CAPS ("audio/x-raw, "
110 "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
111 "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
112 "layout = (string) interleaved, " "channel-mask = (bitmask)0x3")
115 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
117 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
118 const GValue * value, GParamSpec * pspec);
119 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
120 GValue * value, GParamSpec * pspec);
122 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
123 GstCaps * caps, gsize * size);
124 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
125 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
126 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
127 GstCaps * incaps, GstCaps * outcaps);
129 static void gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter,
130 gint16 * idata, gint16 * odata, guint num_samples);
131 static void gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter,
132 gint16 * idata, gint16 * odata, guint num_samples);
133 static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
134 gfloat * idata, gfloat * odata, guint num_samples);
135 static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
136 gfloat * idata, gfloat * odata, guint num_samples);
138 static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
139 filter, gint16 * idata, gint16 * odata, guint num_samples);
140 static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
141 filter, gint16 * idata, gint16 * odata, guint num_samples);
142 static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
143 filter, gfloat * idata, gfloat * odata, guint num_samples);
144 static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
145 filter, gfloat * idata, gfloat * odata, guint num_samples);
147 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
148 GstBuffer * inbuf, GstBuffer * outbuf);
151 /* Table with processing functions: [channels][format][method] */
152 static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
154 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
155 (GstAudioPanoramaProcessFunc)
156 gst_audio_panorama_transform_m2s_int_simple},
157 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
158 (GstAudioPanoramaProcessFunc)
159 gst_audio_panorama_transform_m2s_float_simple}
162 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
163 (GstAudioPanoramaProcessFunc)
164 gst_audio_panorama_transform_s2s_int_simple},
165 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
166 (GstAudioPanoramaProcessFunc)
167 gst_audio_panorama_transform_s2s_float_simple}
171 /* GObject vmethod implementations */
174 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
176 GObjectClass *gobject_class;
177 GstElementClass *gstelement_class;
179 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
180 "audiopanorama element");
182 gobject_class = (GObjectClass *) klass;
183 gstelement_class = (GstElementClass *) klass;
185 gobject_class->set_property = gst_audio_panorama_set_property;
186 gobject_class->get_property = gst_audio_panorama_get_property;
188 g_object_class_install_property (gobject_class, PROP_PANORAMA,
189 g_param_spec_float ("panorama", "Panorama",
190 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
192 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
194 * GstAudioPanorama:method
196 * Panning method: psychoacoustic mode keeps the same perceived loudness,
197 * while simple mode just controls the volume of one channel. It's merely
198 * a matter of taste which method should be chosen.
202 g_object_class_install_property (gobject_class, PROP_METHOD,
203 g_param_spec_enum ("method", "Panning method",
204 "Psychoacoustic mode keeps same perceived loudness, "
205 "simple mode just controls volume of one channel.",
206 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 gst_element_class_set_details_simple (gstelement_class, "Stereo positioning",
210 "Filter/Effect/Audio",
211 "Positions audio streams in the stereo panorama",
212 "Stefan Kost <ensonic@users.sf.net>");
214 gst_element_class_add_pad_template (gstelement_class,
215 gst_static_pad_template_get (&src_template));
216 gst_element_class_add_pad_template (gstelement_class,
217 gst_static_pad_template_get (&sink_template));
219 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
220 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
221 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
222 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
223 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
224 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
225 GST_BASE_TRANSFORM_CLASS (klass)->transform =
226 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
230 gst_audio_panorama_init (GstAudioPanorama * filter)
233 filter->panorama = 0;
234 filter->method = METHOD_PSYCHOACOUSTIC;
235 gst_audio_info_init (&filter->info);
236 filter->process = NULL;
238 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
242 gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
245 gint channel_index, format_index, method_index;
246 const GstAudioFormatInfo *finfo = info->finfo;
248 /* set processing function */
249 channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
250 if (channel_index > 1 || channel_index < 0) {
251 filter->process = NULL;
255 format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
257 method_index = filter->method;
258 if (method_index >= NUM_METHODS || method_index < 0)
259 method_index = METHOD_PSYCHOACOUSTIC;
262 panorama_process_functions[channel_index][format_index][method_index];
267 gst_audio_panorama_set_property (GObject * object, guint prop_id,
268 const GValue * value, GParamSpec * pspec)
270 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
274 filter->panorama = g_value_get_float (value);
277 filter->method = g_value_get_enum (value);
278 gst_audio_panorama_set_process_function (filter, &filter->info);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gst_audio_panorama_get_property (GObject * object, guint prop_id,
288 GValue * value, GParamSpec * pspec)
290 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
294 g_value_set_float (value, filter->panorama);
297 g_value_set_enum (value, filter->method);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 /* GstBaseTransform vmethod implementations */
308 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
315 if (!gst_audio_info_from_caps (&info, caps))
318 *size = GST_AUDIO_INFO_BPF (&info);
324 gst_audio_panorama_transform_caps (GstBaseTransform * base,
325 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
328 GstStructure *structure;
330 /* transform caps gives one single caps so we can just replace
331 * the channel property with our range. */
332 res = gst_caps_copy (caps);
333 structure = gst_caps_get_structure (res, 0);
334 if (direction == GST_PAD_SRC) {
335 GST_INFO ("allow 1-2 channels");
336 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
338 GST_INFO ("allow 2 channels");
339 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
346 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
349 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
352 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
353 if (!gst_audio_info_from_caps (&info, incaps))
356 GST_DEBUG ("try to process %d input with %d channels",
357 GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
359 if (!gst_audio_panorama_set_process_function (filter, &info))
368 GST_DEBUG ("invalid caps");
373 /* psychoacoustic processing functions */
375 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
376 gint16 * odata, guint num_samples)
387 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
389 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
392 for (i = 0; i < num_samples; i++) {
393 val = (gdouble) * idata++;
395 lval = (glong) (val * lpan);
396 rval = (glong) (val * rpan);
398 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
399 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
404 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
405 gint16 * odata, guint num_samples)
409 gdouble lival, rival;
410 gdouble lrpan, llpan, rrpan, rlpan;
418 if (filter->panorama > 0) {
419 rlpan = (gdouble) filter->panorama;
424 rrpan = (gdouble) (1.0 + filter->panorama);
430 for (i = 0; i < num_samples; i++) {
431 lival = (gdouble) * idata++;
432 rival = (gdouble) * idata++;
434 lval = lival * llpan + rival * lrpan;
435 rval = lival * rlpan + rival * rrpan;
437 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
438 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
443 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
444 gfloat * idata, gfloat * odata, guint num_samples)
454 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
456 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
459 for (i = 0; i < num_samples; i++) {
462 *odata++ = val * lpan;
463 *odata++ = val * rpan;
468 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
469 gfloat * idata, gfloat * odata, guint num_samples)
473 gdouble lrpan, llpan, rrpan, rlpan;
481 if (filter->panorama > 0) {
482 rlpan = (gdouble) filter->panorama;
487 rrpan = (gdouble) (1.0 + filter->panorama);
493 for (i = 0; i < num_samples; i++) {
497 *odata++ = lival * llpan + rival * lrpan;
498 *odata++ = lival * rlpan + rival * rrpan;
502 /* simple processing functions */
504 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
505 gint16 * idata, gint16 * odata, guint num_samples)
511 if (filter->panorama > 0.0) {
512 pan = 1.0 - filter->panorama;
513 for (i = 0; i < num_samples; i++) {
515 lval = (glong) ((gdouble) rval * pan);
517 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
518 *odata++ = (gint16) rval;
521 pan = 1.0 + filter->panorama;
522 for (i = 0; i < num_samples; i++) {
524 rval = (glong) ((gdouble) lval * pan);
526 *odata++ = (gint16) lval;
527 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
533 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
534 gint16 * idata, gint16 * odata, guint num_samples)
538 gdouble lival, rival, pan;
540 if (filter->panorama > 0.0) {
541 pan = 1.0 - filter->panorama;
542 for (i = 0; i < num_samples; i++) {
543 lival = (gdouble) * idata++;
544 rival = (gdouble) * idata++;
546 lval = (glong) (lival * pan);
547 rval = (glong) rival;
549 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
550 *odata++ = (gint16) rval;
553 pan = 1.0 + filter->panorama;
554 for (i = 0; i < num_samples; i++) {
555 lival = (gdouble) * idata++;
556 rival = (gdouble) * idata++;
558 lval = (glong) lival;
559 rval = (glong) (rival * pan);
561 *odata++ = (gint16) lval;
562 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
568 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
569 gfloat * idata, gfloat * odata, guint num_samples)
574 if (filter->panorama > 0.0) {
575 pan = 1.0 - filter->panorama;
576 for (i = 0; i < num_samples; i++) {
579 *odata++ = val * pan;
583 pan = 1.0 + filter->panorama;
584 for (i = 0; i < num_samples; i++) {
588 *odata++ = val * pan;
594 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
595 gfloat * idata, gfloat * odata, guint num_samples)
598 gfloat lival, rival, pan;
600 if (filter->panorama > 0.0) {
601 pan = 1.0 - filter->panorama;
602 for (i = 0; i < num_samples; i++) {
606 *odata++ = lival * pan;
610 pan = 1.0 + filter->panorama;
611 for (i = 0; i < num_samples; i++) {
616 *odata++ = rival * pan;
621 /* this function does the actual processing
624 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
627 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
628 GstClockTime timestamp, stream_time;
629 GstMapInfo inmap, outmap;
631 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
633 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
635 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
636 GST_TIME_ARGS (timestamp));
638 if (GST_CLOCK_TIME_IS_VALID (stream_time))
639 gst_object_sync_values (GST_OBJECT (filter), stream_time);
641 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
642 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
644 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
645 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
646 memset (outmap.data, 0, outmap.size);
648 /* output always stereo, input mono or stereo,
649 * and info describes input format */
650 guint num_samples = outmap.size / (2 * GST_AUDIO_INFO_BPS (&filter->info));
652 filter->process (filter, inmap.data, outmap.data, num_samples);
655 gst_buffer_unmap (inbuf, &inmap);
656 gst_buffer_unmap (outbuf, &outmap);