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, "
96 "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
97 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
100 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
103 GST_STATIC_CAPS ("audio/x-raw, "
104 "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
105 "rate = (int) [ 1, MAX ], " "channels = (int) 2")
108 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
110 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
111 const GValue * value, GParamSpec * pspec);
112 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
113 GValue * value, GParamSpec * pspec);
115 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
116 GstCaps * caps, gsize * size);
117 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
118 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
119 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
120 GstCaps * incaps, GstCaps * outcaps);
122 static void gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter,
123 gint16 * idata, gint16 * odata, guint num_samples);
124 static void gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter,
125 gint16 * idata, gint16 * odata, guint num_samples);
126 static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
127 gfloat * idata, gfloat * odata, guint num_samples);
128 static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
129 gfloat * idata, gfloat * odata, guint num_samples);
131 static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
132 filter, gint16 * idata, gint16 * odata, guint num_samples);
133 static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
134 filter, gint16 * idata, gint16 * odata, guint num_samples);
135 static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
136 filter, gfloat * idata, gfloat * odata, guint num_samples);
137 static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
138 filter, gfloat * idata, gfloat * odata, guint num_samples);
140 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
141 GstBuffer * inbuf, GstBuffer * outbuf);
144 /* Table with processing functions: [channels][format][method] */
145 static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
147 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
148 (GstAudioPanoramaProcessFunc)
149 gst_audio_panorama_transform_m2s_int_simple},
150 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
151 (GstAudioPanoramaProcessFunc)
152 gst_audio_panorama_transform_m2s_float_simple}
155 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
156 (GstAudioPanoramaProcessFunc)
157 gst_audio_panorama_transform_s2s_int_simple},
158 {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
159 (GstAudioPanoramaProcessFunc)
160 gst_audio_panorama_transform_s2s_float_simple}
164 /* GObject vmethod implementations */
167 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
169 GObjectClass *gobject_class;
170 GstElementClass *gstelement_class;
172 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
173 "audiopanorama element");
175 gobject_class = (GObjectClass *) klass;
176 gstelement_class = (GstElementClass *) klass;
178 gobject_class->set_property = gst_audio_panorama_set_property;
179 gobject_class->get_property = gst_audio_panorama_get_property;
181 g_object_class_install_property (gobject_class, PROP_PANORAMA,
182 g_param_spec_float ("panorama", "Panorama",
183 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
185 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
187 * GstAudioPanorama:method
189 * Panning method: psychoacoustic mode keeps the same perceived loudness,
190 * while simple mode just controls the volume of one channel. It's merely
191 * a matter of taste which method should be chosen.
195 g_object_class_install_property (gobject_class, PROP_METHOD,
196 g_param_spec_enum ("method", "Panning method",
197 "Psychoacoustic mode keeps same perceived loudness, "
198 "simple mode just controls volume of one channel.",
199 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202 gst_element_class_set_details_simple (gstelement_class, "Stereo positioning",
203 "Filter/Effect/Audio",
204 "Positions audio streams in the stereo panorama",
205 "Stefan Kost <ensonic@users.sf.net>");
207 gst_element_class_add_pad_template (gstelement_class,
208 gst_static_pad_template_get (&src_template));
209 gst_element_class_add_pad_template (gstelement_class,
210 gst_static_pad_template_get (&sink_template));
212 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
213 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
214 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
215 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
216 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
217 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
218 GST_BASE_TRANSFORM_CLASS (klass)->transform =
219 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
223 gst_audio_panorama_init (GstAudioPanorama * filter)
226 filter->panorama = 0;
227 filter->method = METHOD_PSYCHOACOUSTIC;
228 gst_audio_info_init (&filter->info);
229 filter->process = NULL;
231 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
235 gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
238 gint channel_index, format_index, method_index;
239 const GstAudioFormatInfo *finfo = info->finfo;
241 /* set processing function */
242 channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
243 if (channel_index > 1 || channel_index < 0) {
244 filter->process = NULL;
248 format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
250 method_index = filter->method;
251 if (method_index >= NUM_METHODS || method_index < 0)
252 method_index = METHOD_PSYCHOACOUSTIC;
255 panorama_process_functions[channel_index][format_index][method_index];
260 gst_audio_panorama_set_property (GObject * object, guint prop_id,
261 const GValue * value, GParamSpec * pspec)
263 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
267 filter->panorama = g_value_get_float (value);
270 filter->method = g_value_get_enum (value);
271 gst_audio_panorama_set_process_function (filter, &filter->info);
274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280 gst_audio_panorama_get_property (GObject * object, guint prop_id,
281 GValue * value, GParamSpec * pspec)
283 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
287 g_value_set_float (value, filter->panorama);
290 g_value_set_enum (value, filter->method);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 /* GstBaseTransform vmethod implementations */
301 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
304 gint width, channels;
305 GstStructure *structure;
310 /* this works for both float and int */
311 structure = gst_caps_get_structure (caps, 0);
312 ret = gst_structure_get_int (structure, "width", &width);
313 ret &= gst_structure_get_int (structure, "channels", &channels);
315 *size = width * channels / 8;
321 gst_audio_panorama_transform_caps (GstBaseTransform * base,
322 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
325 GstStructure *structure;
327 /* transform caps gives one single caps so we can just replace
328 * the channel property with our range. */
329 res = gst_caps_copy (caps);
330 structure = gst_caps_get_structure (res, 0);
331 if (direction == GST_PAD_SRC) {
332 GST_INFO ("allow 1-2 channels");
333 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
335 GST_INFO ("allow 2 channels");
336 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
343 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
346 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
349 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
350 if (!gst_audio_info_from_caps (&info, incaps))
353 GST_DEBUG ("try to process %d input with %d channels",
354 GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
356 if (!gst_audio_panorama_set_process_function (filter, &info))
365 GST_DEBUG ("invalid caps");
370 /* psychoacoustic processing functions */
372 gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
373 gint16 * odata, guint num_samples)
384 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
386 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
389 for (i = 0; i < num_samples; i++) {
390 val = (gdouble) * idata++;
392 lval = (glong) (val * lpan);
393 rval = (glong) (val * rpan);
395 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
396 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
401 gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
402 gint16 * odata, guint num_samples)
406 gdouble lival, rival;
407 gdouble lrpan, llpan, rrpan, rlpan;
415 if (filter->panorama > 0) {
416 rlpan = (gdouble) filter->panorama;
421 rrpan = (gdouble) (1.0 + filter->panorama);
427 for (i = 0; i < num_samples; i++) {
428 lival = (gdouble) * idata++;
429 rival = (gdouble) * idata++;
431 lval = lival * llpan + rival * lrpan;
432 rval = lival * rlpan + rival * rrpan;
434 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
435 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
440 gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
441 gfloat * idata, gfloat * odata, guint num_samples)
451 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
453 rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
456 for (i = 0; i < num_samples; i++) {
459 *odata++ = val * lpan;
460 *odata++ = val * rpan;
465 gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
466 gfloat * idata, gfloat * odata, guint num_samples)
470 gdouble lrpan, llpan, rrpan, rlpan;
478 if (filter->panorama > 0) {
479 rlpan = (gdouble) filter->panorama;
484 rrpan = (gdouble) (1.0 + filter->panorama);
490 for (i = 0; i < num_samples; i++) {
494 *odata++ = lival * llpan + rival * lrpan;
495 *odata++ = lival * rlpan + rival * rrpan;
499 /* simple processing functions */
501 gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
502 gint16 * idata, gint16 * odata, guint num_samples)
508 if (filter->panorama > 0.0) {
509 pan = 1.0 - filter->panorama;
510 for (i = 0; i < num_samples; i++) {
512 lval = (glong) ((gdouble) rval * pan);
514 *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
515 *odata++ = (gint16) rval;
518 pan = 1.0 + filter->panorama;
519 for (i = 0; i < num_samples; i++) {
521 rval = (glong) ((gdouble) lval * pan);
523 *odata++ = (gint16) lval;
524 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
530 gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
531 gint16 * idata, gint16 * odata, guint num_samples)
535 gdouble lival, rival, pan;
537 if (filter->panorama > 0.0) {
538 pan = 1.0 - filter->panorama;
539 for (i = 0; i < num_samples; i++) {
540 lival = (gdouble) * idata++;
541 rival = (gdouble) * idata++;
543 lval = (glong) (lival * pan);
544 rval = (glong) rival;
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++) {
552 lival = (gdouble) * idata++;
553 rival = (gdouble) * idata++;
555 lval = (glong) lival;
556 rval = (glong) (rival * pan);
558 *odata++ = (gint16) lval;
559 *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
565 gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
566 gfloat * idata, gfloat * odata, guint num_samples)
571 if (filter->panorama > 0.0) {
572 pan = 1.0 - filter->panorama;
573 for (i = 0; i < num_samples; i++) {
576 *odata++ = val * pan;
580 pan = 1.0 + filter->panorama;
581 for (i = 0; i < num_samples; i++) {
585 *odata++ = val * pan;
591 gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
592 gfloat * idata, gfloat * odata, guint num_samples)
595 gfloat lival, rival, pan;
597 if (filter->panorama > 0.0) {
598 pan = 1.0 - filter->panorama;
599 for (i = 0; i < num_samples; i++) {
603 *odata++ = lival * pan;
607 pan = 1.0 + filter->panorama;
608 for (i = 0; i < num_samples; i++) {
613 *odata++ = rival * pan;
618 /* this function does the actual processing
621 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
624 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
625 GstClockTime timestamp, stream_time;
626 guint8 *indata, *outdata;
627 gsize insize, outsize;
629 timestamp = GST_BUFFER_TIMESTAMP (inbuf);
631 gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);
633 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
634 GST_TIME_ARGS (timestamp));
636 if (GST_CLOCK_TIME_IS_VALID (stream_time))
637 gst_object_sync_values (G_OBJECT (filter), stream_time);
639 indata = gst_buffer_map (inbuf, &insize, NULL, GST_MAP_READ);
640 outdata = gst_buffer_map (outbuf, &outsize, NULL, GST_MAP_WRITE);
642 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
643 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
644 memset (outdata, 0, outsize);
646 guint num_samples = outsize / GST_AUDIO_INFO_BPF (&filter->info);
648 filter->process (filter, indata, outdata, num_samples);
651 gst_buffer_unmap (inbuf, indata, insize);
652 gst_buffer_unmap (outbuf, outdata, outsize);