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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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-1.0 audiotestsrc wave=saw ! audiopanorama panorama=-1.00 ! alsasink
32 * gst-launch-1.0 filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiopanorama panorama=-1.00 ! alsasink
33 * gst-launch-1.0 audiotestsrc wave=saw ! audioconvert ! audiopanorama panorama=-1.00 ! audioconvert ! alsasink
34 * gst-launch-1.0 audiotestsrc wave=saw ! audioconvert ! audiopanorama method=simple panorama=-0.50 ! audioconvert ! alsasink
46 #include <gst/base/gstbasetransform.h>
49 #include <orc/orcfunctions.h>
51 #define orc_memset memset
54 #include "audiopanorama.h"
55 #include "audiopanoramaorc.h"
57 #define GST_CAT_DEFAULT gst_audio_panorama_debug
58 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
60 /* Filter signals and args */
68 #define GST_TYPE_AUDIO_PANORAMA_METHOD (gst_audio_panorama_method_get_type ())
70 gst_audio_panorama_method_get_type (void)
72 static GType gtype = 0;
75 static const GEnumValue values[] = {
76 {METHOD_PSYCHOACOUSTIC, "Psychoacoustic Panning (default)",
78 {METHOD_SIMPLE, "Simple Panning", "simple"},
82 gtype = g_enum_register_static ("GstAudioPanoramaMethod", values);
87 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
90 GST_STATIC_CAPS ("audio/x-raw, "
91 "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
92 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ], "
93 "layout = (string) interleaved")
96 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
99 GST_STATIC_CAPS ("audio/x-raw, "
100 "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
101 "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
102 "layout = (string) interleaved")
105 G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
107 static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
112 static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
113 GstCaps * caps, gsize * size);
114 static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
115 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
116 static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
117 GstCaps * incaps, GstCaps * outcaps);
119 static void gst_audio_panorama_m2s_int (gfloat pan,
120 gint16 * idata, gint16 * odata, guint num_samples);
121 static void gst_audio_panorama_s2s_int (gfloat pan,
122 gint16 * idata, gint16 * odata, guint num_samples);
123 static void gst_audio_panorama_m2s_float (gfloat pan,
124 gfloat * idata, gfloat * odata, guint num_samples);
125 static void gst_audio_panorama_s2s_float (gfloat pan,
126 gfloat * idata, gfloat * odata, guint num_samples);
128 static void gst_audio_panorama_m2s_int_simple (gfloat pan,
129 gint16 * idata, gint16 * odata, guint num_samples);
130 static void gst_audio_panorama_s2s_int_simple (gfloat pan,
131 gint16 * idata, gint16 * odata, guint num_samples);
132 static void gst_audio_panorama_m2s_float_simple (gfloat pan,
133 gfloat * idata, gfloat * odata, guint num_samples);
134 static void gst_audio_panorama_s2s_float_simple (gfloat pan,
135 gfloat * idata, gfloat * odata, guint num_samples);
137 static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
138 GstBuffer * inbuf, GstBuffer * outbuf);
141 /* Table with processing functions: [channels][format][method] */
142 static const GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
145 (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_int,
146 (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_int_simple},
148 (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_float,
149 (GstAudioPanoramaProcessFunc) gst_audio_panorama_m2s_float_simple}
153 (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_int,
154 (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_int_simple},
156 (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_float,
157 (GstAudioPanoramaProcessFunc) gst_audio_panorama_s2s_float_simple}
161 /* GObject vmethod implementations */
164 gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
166 GObjectClass *gobject_class;
167 GstElementClass *gstelement_class;
169 GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
170 "audiopanorama element");
172 gobject_class = (GObjectClass *) klass;
173 gstelement_class = (GstElementClass *) klass;
175 gobject_class->set_property = gst_audio_panorama_set_property;
176 gobject_class->get_property = gst_audio_panorama_get_property;
178 g_object_class_install_property (gobject_class, PROP_PANORAMA,
179 g_param_spec_float ("panorama", "Panorama",
180 "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
182 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
184 * GstAudioPanorama:method:
186 * Panning method: psychoacoustic mode keeps the same perceived loudness,
187 * while simple mode just controls the volume of one channel. It's merely
188 * a matter of taste which method should be chosen.
190 g_object_class_install_property (gobject_class, PROP_METHOD,
191 g_param_spec_enum ("method", "Panning method",
192 "Psychoacoustic mode keeps same perceived loudness, "
193 "simple mode just controls volume of one channel.",
194 GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 gst_element_class_set_static_metadata (gstelement_class, "Stereo positioning",
198 "Filter/Effect/Audio",
199 "Positions audio streams in the stereo panorama",
200 "Stefan Kost <ensonic@users.sf.net>");
202 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
203 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
205 GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
206 GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
207 GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
208 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
209 GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
210 GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
211 GST_BASE_TRANSFORM_CLASS (klass)->transform =
212 GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
216 gst_audio_panorama_init (GstAudioPanorama * filter)
219 filter->panorama = 0;
220 filter->method = METHOD_PSYCHOACOUSTIC;
221 gst_audio_info_init (&filter->info);
222 filter->process = NULL;
224 gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
228 gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
231 gint channel_index, format_index, method_index;
232 const GstAudioFormatInfo *finfo = info->finfo;
234 /* set processing function */
235 channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
236 if (channel_index > 1 || channel_index < 0) {
237 filter->process = NULL;
241 format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
242 method_index = filter->method;
245 panorama_process_functions[channel_index][format_index][method_index];
250 gst_audio_panorama_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec)
253 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
257 filter->panorama = g_value_get_float (value);
260 filter->method = g_value_get_enum (value);
261 gst_audio_panorama_set_process_function (filter, &filter->info);
264 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 gst_audio_panorama_get_property (GObject * object, guint prop_id,
271 GValue * value, GParamSpec * pspec)
273 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);
277 g_value_set_float (value, filter->panorama);
280 g_value_set_enum (value, filter->method);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 /* GstBaseTransform vmethod implementations */
291 gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
298 if (!gst_audio_info_from_caps (&info, caps))
301 *size = GST_AUDIO_INFO_BPF (&info);
307 gst_audio_panorama_transform_caps (GstBaseTransform * base,
308 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
311 GstStructure *structure;
314 /* replace the channel property with our range. */
315 res = gst_caps_copy (caps);
316 for (i = 0; i < gst_caps_get_size (res); i++) {
317 structure = gst_caps_get_structure (res, i);
318 if (direction == GST_PAD_SRC) {
319 GST_INFO_OBJECT (base, "[%d] allow 1-2 channels", i);
320 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
322 GST_INFO_OBJECT (base, "[%d] allow 2 channels", i);
323 gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
325 gst_structure_remove_field (structure, "channel-mask");
327 GST_DEBUG_OBJECT (base, "transformed %" GST_PTR_FORMAT, res);
330 GstCaps *intersection;
332 GST_DEBUG_OBJECT (base, "Using filter caps %" GST_PTR_FORMAT, filter);
334 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
335 gst_caps_unref (res);
337 GST_DEBUG_OBJECT (base, "Intersection %" GST_PTR_FORMAT, res);
344 gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
347 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
350 /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
351 if (!gst_audio_info_from_caps (&info, incaps))
354 GST_DEBUG ("try to process %d input with %d channels",
355 GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
357 if (!gst_audio_panorama_set_process_function (filter, &info))
366 GST_DEBUG ("invalid caps");
371 /* psychoacoustic processing functions */
373 /* mono to stereo panning
378 * FIXME: we should use -3db (1/sqtr(2)) for 50:50
381 gst_audio_panorama_m2s_int (gfloat pan, gint16 * idata, gint16 * odata, guint n)
383 gfloat r = (pan + 1.0) / 2.0;
384 audiopanoramam_orc_process_s16_ch1_psy (odata, idata, 1.0 - r, r, n);
388 gst_audio_panorama_m2s_float (gfloat pan, gfloat * idata,
389 gfloat * odata, guint n)
391 gfloat r = (pan + 1.0) / 2.0;
392 audiopanoramam_orc_process_f32_ch1_psy (odata, idata, 1.0 - r, r, n);
403 gst_audio_panorama_s2s_int (gfloat pan, gint16 * idata, gint16 * odata, guint n)
406 audiopanoramam_orc_process_s16_ch2_none (odata, idata, n);
407 } else if (pan > 0.0) {
409 gfloat ll = 1.0 - rl;
410 audiopanoramam_orc_process_s16_ch2_psy_right (odata, idata, ll, rl, n);
412 gfloat rr = 1.0 + pan;
413 gfloat lr = 1.0 - rr;
414 audiopanoramam_orc_process_s16_ch2_psy_left (odata, idata, lr, rr, n);
419 gst_audio_panorama_s2s_float (gfloat pan, gfloat * idata,
420 gfloat * odata, guint n)
423 audiopanoramam_orc_process_f32_ch2_none (odata, idata, n);
424 } else if (pan > 0.0) {
426 gfloat ll = 1.0 - rl;
427 audiopanoramam_orc_process_f32_ch2_psy_right (odata, idata, ll, rl, n);
429 gfloat rr = 1.0 + pan;
430 gfloat lr = 1.0 - rr;
431 audiopanoramam_orc_process_f32_ch2_psy_left (odata, idata, lr, rr, n);
435 /* simple processing functions */
438 gst_audio_panorama_m2s_int_simple (gfloat pan, gint16 * idata,
439 gint16 * odata, guint n)
442 audiopanoramam_orc_process_s16_ch1_none (odata, idata, n);
443 } else if (pan > 0.0) {
444 gfloat lpan = 1.0 - pan;
445 audiopanoramam_orc_process_s16_ch1_sim_left (odata, idata, lpan, n);
447 gfloat rpan = 1.0 + pan;
448 audiopanoramam_orc_process_s16_ch1_sim_right (odata, idata, rpan, n);
453 gst_audio_panorama_s2s_int_simple (gfloat pan, gint16 * idata,
454 gint16 * odata, guint n)
457 audiopanoramam_orc_process_s16_ch2_none (odata, idata, n);
458 } else if (pan > 0.0) {
459 gfloat lpan = 1.0 - pan;
460 audiopanoramam_orc_process_s16_ch2_sim_left (odata, idata, lpan, n);
462 gfloat rpan = 1.0 + pan;
463 audiopanoramam_orc_process_s16_ch2_sim_right (odata, idata, rpan, n);
468 gst_audio_panorama_m2s_float_simple (gfloat pan, gfloat * idata,
469 gfloat * odata, guint n)
472 audiopanoramam_orc_process_f32_ch1_none (odata, idata, n);
473 } else if (pan > 0.0) {
474 gfloat lpan = 1.0 - pan;
475 audiopanoramam_orc_process_f32_ch1_sim_left (odata, idata, lpan, n);
477 gfloat rpan = 1.0 + pan;
478 audiopanoramam_orc_process_f32_ch1_sim_right (odata, idata, rpan, n);
483 gst_audio_panorama_s2s_float_simple (gfloat pan, gfloat * idata,
484 gfloat * odata, guint n)
487 audiopanoramam_orc_process_f32_ch2_none (odata, idata, n);
488 } else if (pan > 0.0) {
489 gfloat lpan = 1.0 - pan;
490 audiopanoramam_orc_process_f32_ch2_sim_left (odata, idata, lpan, n);
492 gfloat rpan = 1.0 + pan;
493 audiopanoramam_orc_process_f32_ch2_sim_right (odata, idata, rpan, n);
497 /* this function does the actual processing
500 gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
503 GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
505 GstMapInfo inmap, outmap;
507 ts = gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME,
508 GST_BUFFER_TIMESTAMP (inbuf));
510 if (GST_CLOCK_TIME_IS_VALID (ts)) {
511 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT, GST_TIME_ARGS (ts));
512 gst_object_sync_values (GST_OBJECT (filter), ts);
515 gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
517 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
518 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
519 orc_memset (outmap.data, 0, outmap.size);
521 /* output is always stereo, input is mono or stereo,
522 * and info describes input format */
523 guint num_samples = outmap.size / (2 * GST_AUDIO_INFO_BPS (&filter->info));
525 gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
526 filter->process (filter->panorama, inmap.data, outmap.data, num_samples);
527 gst_buffer_unmap (inbuf, &inmap);
530 gst_buffer_unmap (outbuf, &outmap);