audiopanorama.c 18.9 KB
Newer Older
1 2 3
/*
 * GStreamer
 * Copyright (C) 2006 Stefan Kost <ensonic@users.sf.net>
4
 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
23
 * SECTION:element-audiopanorama
24
 *
25 26
 * Stereo panorama effect with controllable pan position. One can choose between the default psychoacoustic panning method,
 * which keeps the same perceived loudness, and a simple panning method that just controls the volume on one channel.
27 28
 *
 * <refsect2>
29
 * <title>Example launch line</title>
30
 * |[
31 32 33
 * gst-launch audiotestsrc wave=saw ! audiopanorama panorama=-1.00 ! alsasink
 * gst-launch filesrc location="melo1.ogg" ! oggdemux ! vorbisdec ! audioconvert ! audiopanorama panorama=-1.00 ! alsasink
 * 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
35
 * ]|
36 37 38 39 40 41 42
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Stefan Sauer's avatar
Stefan Sauer committed
43 44
#include <string.h>

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <gst/gst.h>
#include <gst/base/gstbasetransform.h>

#include "audiopanorama.h"

#define GST_CAT_DEFAULT gst_audio_panorama_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

/* Filter signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  PROP_0,
63 64
  PROP_PANORAMA,
  PROP_METHOD
65 66
};

67 68
enum
{
69 70 71
  METHOD_PSYCHOACOUSTIC = 0,
  METHOD_SIMPLE,
  NUM_METHODS
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
};

#define GST_TYPE_AUDIO_PANORAMA_METHOD (gst_audio_panorama_method_get_type ())
static GType
gst_audio_panorama_method_get_type (void)
{
  static GType gtype = 0;

  if (gtype == 0) {
    static const GEnumValue values[] = {
      {METHOD_PSYCHOACOUSTIC, "Psychoacoustic Panning (default)",
          "psychoacoustic"},
      {METHOD_SIMPLE, "Simple Panning", "simple"},
      {0, NULL, NULL}
    };

    gtype = g_enum_register_static ("GstAudioPanoramaMethod", values);
  }
  return gtype;
}

93 94 95
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
96
    GST_STATIC_CAPS ("audio/x-raw, "
97
        "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
98 99 100
        "rate = (int) [ 1, MAX ], " "channels = (int) 1, "
        "layout = (string) interleaved;"
        "audio/x-raw, "
101
        "format = (string) { " GST_AUDIO_NE (F32) ", " GST_AUDIO_NE (S16) "}, "
102 103
        "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
        "layout = (string) interleaved, " "channel-mask = (bitmask) 0x3")
104 105 106 107 108
    );

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
109 110
    GST_STATIC_CAPS ("audio/x-raw, "
        "format = (string) { " GST_AUDIO_NE (S32) ", " GST_AUDIO_NE (S16) "}, "
111 112
        "rate = (int) [ 1, MAX ], " "channels = (int) 2, "
        "layout = (string) interleaved, " "channel-mask = (bitmask)0x3")
113 114
    );

Wim Taymans's avatar
Wim Taymans committed
115
G_DEFINE_TYPE (GstAudioPanorama, gst_audio_panorama, GST_TYPE_BASE_TRANSFORM);
116 117 118 119 120 121 122

static void gst_audio_panorama_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_audio_panorama_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static gboolean gst_audio_panorama_get_unit_size (GstBaseTransform * base,
Wim Taymans's avatar
Wim Taymans committed
123
    GstCaps * caps, gsize * size);
124
static GstCaps *gst_audio_panorama_transform_caps (GstBaseTransform * base,
Wim Taymans's avatar
Wim Taymans committed
125
    GstPadDirection direction, GstCaps * caps, GstCaps * filter);
126 127 128
static gboolean gst_audio_panorama_set_caps (GstBaseTransform * base,
    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 134 135 136
static void gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples);
static void gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples);
137

138 139 140 141 142 143 144 145 146
static void gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama *
    filter, gint16 * idata, gint16 * odata, guint num_samples);
static void gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama *
    filter, gint16 * idata, gint16 * odata, guint num_samples);
static void gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama *
    filter, gfloat * idata, gfloat * odata, guint num_samples);
static void gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama *
    filter, gfloat * idata, gfloat * odata, guint num_samples);

147 148 149
static GstFlowReturn gst_audio_panorama_transform (GstBaseTransform * base,
    GstBuffer * inbuf, GstBuffer * outbuf);

150 151 152 153 154

/* Table with processing functions: [channels][format][method] */
static GstAudioPanoramaProcessFunc panorama_process_functions[2][2][2] = {
  {
        {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_int,
155 156
              (GstAudioPanoramaProcessFunc)
            gst_audio_panorama_transform_m2s_int_simple},
157
        {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_m2s_float,
158 159
              (GstAudioPanoramaProcessFunc)
            gst_audio_panorama_transform_m2s_float_simple}
160 161 162
      },
  {
        {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_int,
163 164
              (GstAudioPanoramaProcessFunc)
            gst_audio_panorama_transform_s2s_int_simple},
165
        {(GstAudioPanoramaProcessFunc) gst_audio_panorama_transform_s2s_float,
166 167
              (GstAudioPanoramaProcessFunc)
            gst_audio_panorama_transform_s2s_float_simple}
168 169 170
      }
};

171 172 173 174 175 176
/* GObject vmethod implementations */

static void
gst_audio_panorama_class_init (GstAudioPanoramaClass * klass)
{
  GObjectClass *gobject_class;
Wim Taymans's avatar
Wim Taymans committed
177 178 179 180
  GstElementClass *gstelement_class;

  GST_DEBUG_CATEGORY_INIT (gst_audio_panorama_debug, "audiopanorama", 0,
      "audiopanorama element");
181 182

  gobject_class = (GObjectClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
183 184
  gstelement_class = (GstElementClass *) klass;

185 186 187 188
  gobject_class->set_property = gst_audio_panorama_set_property;
  gobject_class->get_property = gst_audio_panorama_get_property;

  g_object_class_install_property (gobject_class, PROP_PANORAMA,
189 190
      g_param_spec_float ("panorama", "Panorama",
          "Position in stereo panorama (-1.0 left -> 1.0 right)", -1.0, 1.0,
191 192
          0.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
193 194 195 196
  /**
   * GstAudioPanorama:method
   *
   * Panning method: psychoacoustic mode keeps the same perceived loudness,
197 198
   * while simple mode just controls the volume of one channel. It's merely
   * a matter of taste which method should be chosen. 
199 200 201 202 203 204 205 206
   *
   * Since: 0.10.6
   **/
  g_object_class_install_property (gobject_class, PROP_METHOD,
      g_param_spec_enum ("method", "Panning method",
          "Psychoacoustic mode keeps same perceived loudness, "
          "simple mode just controls volume of one channel.",
          GST_TYPE_AUDIO_PANORAMA_METHOD, METHOD_PSYCHOACOUSTIC,
207
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208

Wim Taymans's avatar
Wim Taymans committed
209 210 211 212 213 214 215 216 217 218
  gst_element_class_set_details_simple (gstelement_class, "Stereo positioning",
      "Filter/Effect/Audio",
      "Positions audio streams in the stereo panorama",
      "Stefan Kost <ensonic@users.sf.net>");

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&src_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&sink_template));

219 220 221 222 223 224 225 226 227 228 229
  GST_BASE_TRANSFORM_CLASS (klass)->get_unit_size =
      GST_DEBUG_FUNCPTR (gst_audio_panorama_get_unit_size);
  GST_BASE_TRANSFORM_CLASS (klass)->transform_caps =
      GST_DEBUG_FUNCPTR (gst_audio_panorama_transform_caps);
  GST_BASE_TRANSFORM_CLASS (klass)->set_caps =
      GST_DEBUG_FUNCPTR (gst_audio_panorama_set_caps);
  GST_BASE_TRANSFORM_CLASS (klass)->transform =
      GST_DEBUG_FUNCPTR (gst_audio_panorama_transform);
}

static void
Wim Taymans's avatar
Wim Taymans committed
230
gst_audio_panorama_init (GstAudioPanorama * filter)
231
{
232

233
  filter->panorama = 0;
234
  filter->method = METHOD_PSYCHOACOUSTIC;
Wim Taymans's avatar
Wim Taymans committed
235
  gst_audio_info_init (&filter->info);
236
  filter->process = NULL;
237 238

  gst_base_transform_set_gap_aware (GST_BASE_TRANSFORM (filter), TRUE);
239 240 241
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
242 243
gst_audio_panorama_set_process_function (GstAudioPanorama * filter,
    GstAudioInfo * info)
244
{
245
  gint channel_index, format_index, method_index;
Wim Taymans's avatar
Wim Taymans committed
246
  const GstAudioFormatInfo *finfo = info->finfo;
247 248

  /* set processing function */
Wim Taymans's avatar
Wim Taymans committed
249
  channel_index = GST_AUDIO_INFO_CHANNELS (info) - 1;
250 251 252
  if (channel_index > 1 || channel_index < 0) {
    filter->process = NULL;
    return FALSE;
253 254
  }

Wim Taymans's avatar
Wim Taymans committed
255
  format_index = GST_AUDIO_FORMAT_INFO_IS_FLOAT (finfo) ? 1 : 0;
256 257 258 259 260 261 262 263

  method_index = filter->method;
  if (method_index >= NUM_METHODS || method_index < 0)
    method_index = METHOD_PSYCHOACOUSTIC;

  filter->process =
      panorama_process_functions[channel_index][format_index][method_index];
  return TRUE;
264 265 266 267 268 269 270 271 272 273
}

static void
gst_audio_panorama_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);

  switch (prop_id) {
    case PROP_PANORAMA:
274
      filter->panorama = g_value_get_float (value);
275
      break;
276 277
    case PROP_METHOD:
      filter->method = g_value_get_enum (value);
Wim Taymans's avatar
Wim Taymans committed
278
      gst_audio_panorama_set_process_function (filter, &filter->info);
279
      break;
280 281 282 283 284 285 286 287 288 289 290 291 292 293
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_audio_panorama_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstAudioPanorama *filter = GST_AUDIO_PANORAMA (object);

  switch (prop_id) {
    case PROP_PANORAMA:
294
      g_value_set_float (value, filter->panorama);
295
      break;
296 297 298
    case PROP_METHOD:
      g_value_set_enum (value, filter->method);
      break;
299 300 301 302 303 304 305 306 307 308
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

/* GstBaseTransform vmethod implementations */

static gboolean
gst_audio_panorama_get_unit_size (GstBaseTransform * base, GstCaps * caps,
Wim Taymans's avatar
Wim Taymans committed
309
    gsize * size)
310
{
311
  GstAudioInfo info;
312 313 314

  g_assert (size);

315 316
  if (!gst_audio_info_from_caps (&info, caps))
    return FALSE;
317

318
  *size = GST_AUDIO_INFO_BPF (&info);
319

320
  return TRUE;
321 322 323 324
}

static GstCaps *
gst_audio_panorama_transform_caps (GstBaseTransform * base,
Wim Taymans's avatar
Wim Taymans committed
325
    GstPadDirection direction, GstCaps * caps, GstCaps * filter)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
  GstCaps *res;
  GstStructure *structure;

  /* transform caps gives one single caps so we can just replace
   * the channel property with our range. */
  res = gst_caps_copy (caps);
  structure = gst_caps_get_structure (res, 0);
  if (direction == GST_PAD_SRC) {
    GST_INFO ("allow 1-2 channels");
    gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
  } else {
    GST_INFO ("allow 2 channels");
    gst_structure_set (structure, "channels", G_TYPE_INT, 2, NULL);
  }

  return res;
}

static gboolean
gst_audio_panorama_set_caps (GstBaseTransform * base, GstCaps * incaps,
    GstCaps * outcaps)
{
  GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
Wim Taymans's avatar
Wim Taymans committed
350
  GstAudioInfo info;
351 352

  /*GST_INFO ("incaps are %" GST_PTR_FORMAT, incaps); */
Wim Taymans's avatar
Wim Taymans committed
353 354
  if (!gst_audio_info_from_caps (&info, incaps))
    goto no_format;
355

Wim Taymans's avatar
Wim Taymans committed
356 357
  GST_DEBUG ("try to process %d input with %d channels",
      GST_AUDIO_INFO_FORMAT (&info), GST_AUDIO_INFO_CHANNELS (&info));
358

Wim Taymans's avatar
Wim Taymans committed
359 360
  if (!gst_audio_panorama_set_process_function (filter, &info))
    goto no_format;
361

Wim Taymans's avatar
Wim Taymans committed
362
  filter->info = info;
363

Wim Taymans's avatar
Wim Taymans committed
364
  return TRUE;
365

Wim Taymans's avatar
Wim Taymans committed
366 367 368 369 370
no_format:
  {
    GST_DEBUG ("invalid caps");
    return FALSE;
  }
371 372
}

373
/* psychoacoustic processing functions */
374
static void
375
gst_audio_panorama_transform_m2s_int (GstAudioPanorama * filter, gint16 * idata,
376 377 378 379 380 381 382
    gint16 * odata, guint num_samples)
{
  guint i;
  gdouble val;
  glong lval, rval;
  gdouble rpan, lpan;

383
  /* pan:  -1.0  0.0  1.0
384 385
   * lpan:  1.0  0.5  0.0  
   * rpan:  0.0  0.5  1.0
386 387
   *
   * FIXME: we should use -3db (1/sqtr(2)) for 50:50
388
   */
389
  rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
390 391 392 393 394 395 396 397 398 399 400 401 402 403
  lpan = 1.0 - rpan;

  for (i = 0; i < num_samples; i++) {
    val = (gdouble) * idata++;

    lval = (glong) (val * lpan);
    rval = (glong) (val * rpan);

    *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
    *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
  }
}

static void
404
gst_audio_panorama_transform_s2s_int (GstAudioPanorama * filter, gint16 * idata,
405 406 407 408 409 410 411
    gint16 * odata, guint num_samples)
{
  guint i;
  glong lval, rval;
  gdouble lival, rival;
  gdouble lrpan, llpan, rrpan, rlpan;

412
  /* pan:  -1.0  0.0  1.0
413 414 415 416 417 418
   * llpan: 1.0  1.0  0.0
   * lrpan: 1.0  0.0  0.0
   * rrpan: 0.0  1.0  1.0
   * rlpan: 0.0  0.0  1.0
   */
  if (filter->panorama > 0) {
419
    rlpan = (gdouble) filter->panorama;
420 421 422 423
    llpan = 1.0 - rlpan;
    lrpan = 0.0;
    rrpan = 1.0;
  } else {
424
    rrpan = (gdouble) (1.0 + filter->panorama);
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    lrpan = 1.0 - rrpan;
    rlpan = 0.0;
    llpan = 1.0;
  }

  for (i = 0; i < num_samples; i++) {
    lival = (gdouble) * idata++;
    rival = (gdouble) * idata++;

    lval = lival * llpan + rival * lrpan;
    rval = lival * rlpan + rival * rrpan;

    *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
    *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
  }
}

442 443 444 445 446 447 448 449
static void
gst_audio_panorama_transform_m2s_float (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples)
{
  guint i;
  gfloat val;
  gdouble rpan, lpan;

450
  /* pan:  -1.0  0.0  1.0
451 452 453 454 455
   * lpan:  1.0  0.5  0.0  
   * rpan:  0.0  0.5  1.0
   *
   * FIXME: we should use -3db (1/sqtr(2)) for 50:50
   */
456
  rpan = (gdouble) (filter->panorama + 1.0) / 2.0;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  lpan = 1.0 - rpan;

  for (i = 0; i < num_samples; i++) {
    val = *idata++;

    *odata++ = val * lpan;
    *odata++ = val * rpan;
  }
}

static void
gst_audio_panorama_transform_s2s_float (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples)
{
  guint i;
  gfloat lival, rival;
  gdouble lrpan, llpan, rrpan, rlpan;

475
  /* pan:  -1.0  0.0  1.0
476 477 478 479 480 481
   * llpan: 1.0  1.0  0.0
   * lrpan: 1.0  0.0  0.0
   * rrpan: 0.0  1.0  1.0
   * rlpan: 0.0  0.0  1.0
   */
  if (filter->panorama > 0) {
482
    rlpan = (gdouble) filter->panorama;
483 484 485 486
    llpan = 1.0 - rlpan;
    lrpan = 0.0;
    rrpan = 1.0;
  } else {
487
    rrpan = (gdouble) (1.0 + filter->panorama);
488 489 490 491 492 493 494 495 496 497 498 499 500 501
    lrpan = 1.0 - rrpan;
    rlpan = 0.0;
    llpan = 1.0;
  }

  for (i = 0; i < num_samples; i++) {
    lival = *idata++;
    rival = *idata++;

    *odata++ = lival * llpan + rival * lrpan;
    *odata++ = lival * rlpan + rival * rrpan;
  }
}

502 503 504 505 506 507
/* simple processing functions */
static void
gst_audio_panorama_transform_m2s_int_simple (GstAudioPanorama * filter,
    gint16 * idata, gint16 * odata, guint num_samples)
{
  guint i;
508
  gdouble pan;
509 510
  glong lval, rval;

511 512 513 514 515
  if (filter->panorama > 0.0) {
    pan = 1.0 - filter->panorama;
    for (i = 0; i < num_samples; i++) {
      rval = *idata++;
      lval = (glong) ((gdouble) rval * pan);
516

517 518
      *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
      *odata++ = (gint16) rval;
519
    }
520 521 522 523 524
  } else {
    pan = 1.0 + filter->panorama;
    for (i = 0; i < num_samples; i++) {
      lval = *idata++;
      rval = (glong) ((gdouble) lval * pan);
525

526 527 528
      *odata++ = (gint16) lval;
      *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
    }
529 530 531 532 533 534 535 536 537
  }
}

static void
gst_audio_panorama_transform_s2s_int_simple (GstAudioPanorama * filter,
    gint16 * idata, gint16 * odata, guint num_samples)
{
  guint i;
  glong lval, rval;
538
  gdouble lival, rival, pan;
539

540 541 542 543 544
  if (filter->panorama > 0.0) {
    pan = 1.0 - filter->panorama;
    for (i = 0; i < num_samples; i++) {
      lival = (gdouble) * idata++;
      rival = (gdouble) * idata++;
545

546
      lval = (glong) (lival * pan);
547
      rval = (glong) rival;
548 549 550

      *odata++ = (gint16) CLAMP (lval, G_MININT16, G_MAXINT16);
      *odata++ = (gint16) rval;
551
    }
552 553 554 555 556
  } else {
    pan = 1.0 + filter->panorama;
    for (i = 0; i < num_samples; i++) {
      lival = (gdouble) * idata++;
      rival = (gdouble) * idata++;
557

558 559 560 561 562 563
      lval = (glong) lival;
      rval = (glong) (rival * pan);

      *odata++ = (gint16) lval;
      *odata++ = (gint16) CLAMP (rval, G_MININT16, G_MAXINT16);
    }
564 565 566 567 568 569 570 571
  }
}

static void
gst_audio_panorama_transform_m2s_float_simple (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples)
{
  guint i;
572
  gfloat val, pan;
573

574 575 576 577
  if (filter->panorama > 0.0) {
    pan = 1.0 - filter->panorama;
    for (i = 0; i < num_samples; i++) {
      val = *idata++;
578

579
      *odata++ = val * pan;
580
      *odata++ = val;
581 582 583 584 585 586
    }
  } else {
    pan = 1.0 + filter->panorama;
    for (i = 0; i < num_samples; i++) {
      val = *idata++;

587
      *odata++ = val;
588
      *odata++ = val * pan;
589 590 591 592 593 594 595 596 597
    }
  }
}

static void
gst_audio_panorama_transform_s2s_float_simple (GstAudioPanorama * filter,
    gfloat * idata, gfloat * odata, guint num_samples)
{
  guint i;
598
  gfloat lival, rival, pan;
599

600 601 602 603 604
  if (filter->panorama > 0.0) {
    pan = 1.0 - filter->panorama;
    for (i = 0; i < num_samples; i++) {
      lival = *idata++;
      rival = *idata++;
605

606
      *odata++ = lival * pan;
607
      *odata++ = rival;
608 609 610 611 612 613 614
    }
  } else {
    pan = 1.0 + filter->panorama;
    for (i = 0; i < num_samples; i++) {
      lival = *idata++;
      rival = *idata++;

615
      *odata++ = lival;
616
      *odata++ = rival * pan;
617 618 619 620
    }
  }
}

621 622 623 624 625 626 627
/* this function does the actual processing
 */
static GstFlowReturn
gst_audio_panorama_transform (GstBaseTransform * base, GstBuffer * inbuf,
    GstBuffer * outbuf)
{
  GstAudioPanorama *filter = GST_AUDIO_PANORAMA (base);
628
  GstClockTime timestamp, stream_time;
Wim Taymans's avatar
Wim Taymans committed
629
  GstMapInfo inmap, outmap;
630

631 632 633 634 635 636 637 638
  timestamp = GST_BUFFER_TIMESTAMP (inbuf);
  stream_time =
      gst_segment_to_stream_time (&base->segment, GST_FORMAT_TIME, timestamp);

  GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
      GST_TIME_ARGS (timestamp));

  if (GST_CLOCK_TIME_IS_VALID (stream_time))
639
    gst_object_sync_values (GST_OBJECT (filter), stream_time);
640

Wim Taymans's avatar
Wim Taymans committed
641 642
  gst_buffer_map (inbuf, &inmap, GST_MAP_READ);
  gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE);
Wim Taymans's avatar
Wim Taymans committed
643

644 645
  if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_GAP))) {
    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_GAP);
Wim Taymans's avatar
Wim Taymans committed
646
    memset (outmap.data, 0, outmap.size);
Wim Taymans's avatar
Wim Taymans committed
647
  } else {
648 649 650
    /* output always stereo, input mono or stereo,
     * and info describes input format */
    guint num_samples = outmap.size / (2 * GST_AUDIO_INFO_BPS (&filter->info));
Wim Taymans's avatar
Wim Taymans committed
651

Wim Taymans's avatar
Wim Taymans committed
652
    filter->process (filter, inmap.data, outmap.data, num_samples);
653 654
  }

Wim Taymans's avatar
Wim Taymans committed
655 656
  gst_buffer_unmap (inbuf, &inmap);
  gst_buffer_unmap (outbuf, &outmap);
657 658 659

  return GST_FLOW_OK;
}