gstlfocontrolsource.c 16.8 KB
Newer Older
1 2
/* GStreamer
 *
3
 * Copyright (C) 2007,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * gstlfocontrolsource.c: Control source that provides some periodic waveforms
 *                        as control values.
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
20 21
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
22 23 24 25
 */

/**
 * SECTION:gstlfocontrolsource
26
 * @title: GstLFOControlSource
27 28
 * @short_description: LFO control source
 *
29 30
 * #GstLFOControlSource is a #GstControlSource, that provides several periodic
 * waveforms as control values.
31
 *
32 33 34
 * To use #GstLFOControlSource get a new instance by calling
 * gst_lfo_control_source_new(), bind it to a #GParamSpec and set the relevant
 * properties.
35 36 37
 *
 * All functions are MT-safe.
 */
38 39 40
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
41

42 43
#include <float.h>

44 45
#include <glib-object.h>
#include <gst/gst.h>
46
#include <gst/gstcontrolsource.h>
47 48 49

#include "gstlfocontrolsource.h"

50 51
#include "gst/glib-compat-private.h"

52
#include <gst/math-compat.h>
53

54 55 56
#define GST_CAT_DEFAULT controller_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

57 58 59 60 61 62 63 64 65
struct _GstLFOControlSourcePrivate
{
  GstLFOWaveform waveform;
  gdouble frequency;
  GstClockTime period;
  GstClockTime timeshift;
  gdouble amplitude;
  gdouble offset;
};
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* FIXME: as % in C is not the modulo operator we need here for
 * negative numbers implement our own. Are there better ways? */
static inline GstClockTime
_calculate_pos (GstClockTime timestamp, GstClockTime timeshift,
    GstClockTime period)
{
  while (timestamp < timeshift)
    timestamp += period;

  timestamp -= timeshift;

  return timestamp % period;
}

81 82 83 84 85
static inline gdouble
_sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
86 87
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
88 89
  gdouble ret;

90
  ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
91 92 93 94
  ret *= amp;
  ret += off;

  return ret;
95 96
}

97 98 99 100 101 102
static gboolean
waveform_sine_get (GstLFOControlSource * self, GstClockTime timestamp,
    gdouble * value)
{
  GstLFOControlSourcePrivate *priv = self->priv;

103
  gst_object_sync_values (GST_OBJECT (self), timestamp);
Wim Taymans's avatar
Wim Taymans committed
104
  g_mutex_lock (&self->lock);
105 106
  *value = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
Wim Taymans's avatar
Wim Taymans committed
107
  g_mutex_unlock (&self->lock);
108 109
  return TRUE;
}
110

111 112 113 114 115 116 117 118 119 120
static gboolean
waveform_sine_get_value_array (GstLFOControlSource * self,
    GstClockTime timestamp, GstClockTime interval, guint n_values,
    gdouble * values)
{
  GstLFOControlSourcePrivate *priv = self->priv;
  guint i;
  GstClockTime ts = timestamp;

  for (i = 0; i < n_values; i++) {
121
    gst_object_sync_values (GST_OBJECT (self), ts);
Wim Taymans's avatar
Wim Taymans committed
122
    g_mutex_lock (&self->lock);
123 124
    *values = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
Wim Taymans's avatar
Wim Taymans committed
125
    g_mutex_unlock (&self->lock);
126 127 128 129
    ts += interval;
    values++;
  }
  return TRUE;
130 131 132
}


133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static inline gdouble
_square_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
  GstClockTime pos = _calculate_pos (timestamp, timeshift, period);
  gdouble ret;

  if (pos >= period / 2)
    ret = amp;
  else
    ret = -amp;
  ret += off;

  return ret;
148 149
}

150 151 152 153 154 155
static gboolean
waveform_square_get (GstLFOControlSource * self, GstClockTime timestamp,
    gdouble * value)
{
  GstLFOControlSourcePrivate *priv = self->priv;

156
  gst_object_sync_values (GST_OBJECT (self), timestamp);
Wim Taymans's avatar
Wim Taymans committed
157
  g_mutex_lock (&self->lock);
158 159
  *value = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
Wim Taymans's avatar
Wim Taymans committed
160
  g_mutex_unlock (&self->lock);
161 162
  return TRUE;
}
163

164 165 166 167 168 169 170 171 172 173
static gboolean
waveform_square_get_value_array (GstLFOControlSource * self,
    GstClockTime timestamp, GstClockTime interval, guint n_values,
    gdouble * values)
{
  GstLFOControlSourcePrivate *priv = self->priv;
  guint i;
  GstClockTime ts = timestamp;

  for (i = 0; i < n_values; i++) {
174
    gst_object_sync_values (GST_OBJECT (self), ts);
Wim Taymans's avatar
Wim Taymans committed
175
    g_mutex_lock (&self->lock);
176 177
    *values = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
Wim Taymans's avatar
Wim Taymans committed
178
    g_mutex_unlock (&self->lock);
179 180 181 182
    ts += interval;
    values++;
  }
  return TRUE;
183 184
}

185 186 187 188 189
static inline gdouble
_saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
190 191 192
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble per = gst_guint64_to_gdouble (period);
193 194
  gdouble ret;

195
  ret = -((pos - per / 2.0) * ((2.0 * amp) / per));
196
  ret += off;
197

198 199 200 201 202 203 204 205 206
  return ret;
}

static gboolean
waveform_saw_get (GstLFOControlSource * self, GstClockTime timestamp,
    gdouble * value)
{
  GstLFOControlSourcePrivate *priv = self->priv;

207
  gst_object_sync_values (GST_OBJECT (self), timestamp);
Wim Taymans's avatar
Wim Taymans committed
208
  g_mutex_lock (&self->lock);
209 210
  *value = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
Wim Taymans's avatar
Wim Taymans committed
211
  g_mutex_unlock (&self->lock);
212 213 214 215 216 217 218 219 220 221 222 223 224
  return TRUE;
}

static gboolean
waveform_saw_get_value_array (GstLFOControlSource * self,
    GstClockTime timestamp, GstClockTime interval, guint n_values,
    gdouble * values)
{
  GstLFOControlSourcePrivate *priv = self->priv;
  guint i;
  GstClockTime ts = timestamp;

  for (i = 0; i < n_values; i++) {
225
    gst_object_sync_values (GST_OBJECT (self), ts);
Wim Taymans's avatar
Wim Taymans committed
226
    g_mutex_lock (&self->lock);
227 228
    *values = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
Wim Taymans's avatar
Wim Taymans committed
229
    g_mutex_unlock (&self->lock);
230 231 232 233 234 235 236 237 238 239 240
    ts += interval;
    values++;
  }
  return TRUE;
}

static inline gdouble
_rsaw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
241 242 243
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble per = gst_guint64_to_gdouble (period);
244 245
  gdouble ret;

246
  ret = (pos - per / 2.0) * ((2.0 * amp) / per);
247 248 249 250 251 252 253 254 255 256 257
  ret += off;

  return ret;
}

static gboolean
waveform_rsaw_get (GstLFOControlSource * self, GstClockTime timestamp,
    gdouble * value)
{
  GstLFOControlSourcePrivate *priv = self->priv;

258
  gst_object_sync_values (GST_OBJECT (self), timestamp);
Wim Taymans's avatar
Wim Taymans committed
259
  g_mutex_lock (&self->lock);
260 261
  *value = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
Wim Taymans's avatar
Wim Taymans committed
262
  g_mutex_unlock (&self->lock);
263 264 265 266 267 268 269 270 271 272 273 274 275
  return TRUE;
}

static gboolean
waveform_rsaw_get_value_array (GstLFOControlSource * self,
    GstClockTime timestamp, GstClockTime interval, guint n_values,
    gdouble * values)
{
  GstLFOControlSourcePrivate *priv = self->priv;
  guint i;
  GstClockTime ts = timestamp;

  for (i = 0; i < n_values; i++) {
276
    gst_object_sync_values (GST_OBJECT (self), ts);
Wim Taymans's avatar
Wim Taymans committed
277
    g_mutex_lock (&self->lock);
278 279
    *values = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
Wim Taymans's avatar
Wim Taymans committed
280
    g_mutex_unlock (&self->lock);
281 282 283 284
    ts += interval;
    values++;
  }
  return TRUE;
285 286 287
}


288 289 290 291 292
static inline gdouble
_triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
293 294 295
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble per = gst_guint64_to_gdouble (period);
296 297
  gdouble ret;

298 299 300 301 302 303
  if (pos <= 0.25 * per)
    /* 1st quarter */
    ret = pos * ((4.0 * amp) / per);
  else if (pos <= 0.75 * per)
    /* 2nd & 3rd quarter */
    ret = -(pos - per / 2.0) * ((4.0 * amp) / per);
304
  else
305 306
    /* 4th quarter */
    ret = -(per - pos) * ((4.0 * amp) / per);
307 308 309 310 311 312 313 314 315 316 317 318

  ret += off;

  return ret;
}

static gboolean
waveform_triangle_get (GstLFOControlSource * self, GstClockTime timestamp,
    gdouble * value)
{
  GstLFOControlSourcePrivate *priv = self->priv;

319
  gst_object_sync_values (GST_OBJECT (self), timestamp);
Wim Taymans's avatar
Wim Taymans committed
320
  g_mutex_lock (&self->lock);
321 322
  *value = _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
Wim Taymans's avatar
Wim Taymans committed
323
  g_mutex_unlock (&self->lock);
324 325 326 327 328 329 330 331 332 333 334 335 336
  return TRUE;
}

static gboolean
waveform_triangle_get_value_array (GstLFOControlSource * self,
    GstClockTime timestamp, GstClockTime interval, guint n_values,
    gdouble * values)
{
  GstLFOControlSourcePrivate *priv = self->priv;
  guint i;
  GstClockTime ts = timestamp;

  for (i = 0; i < n_values; i++) {
337
    gst_object_sync_values (GST_OBJECT (self), ts);
Wim Taymans's avatar
Wim Taymans committed
338
    g_mutex_lock (&self->lock);
339 340 341
    *values =
        _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
Wim Taymans's avatar
Wim Taymans committed
342
    g_mutex_unlock (&self->lock);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    ts += interval;
    values++;
  }
  return TRUE;
}

static struct
{
  GstControlSourceGetValue get;
  GstControlSourceGetValueArray get_value_array;
} waveforms[] = {
  {
  (GstControlSourceGetValue) waveform_sine_get,
        (GstControlSourceGetValueArray) waveform_sine_get_value_array}, {
  (GstControlSourceGetValue) waveform_square_get,
        (GstControlSourceGetValueArray) waveform_square_get_value_array}, {
  (GstControlSourceGetValue) waveform_saw_get,
        (GstControlSourceGetValueArray) waveform_saw_get_value_array}, {
  (GstControlSourceGetValue) waveform_rsaw_get,
        (GstControlSourceGetValueArray) waveform_rsaw_get_value_array}, {
  (GstControlSourceGetValue) waveform_triangle_get,
        (GstControlSourceGetValueArray) waveform_triangle_get_value_array}
365 366
};

367
static const guint num_waveforms = G_N_ELEMENTS (waveforms);
368 369 370 371 372 373 374 375 376 377

enum
{
  PROP_WAVEFORM = 1,
  PROP_FREQUENCY,
  PROP_TIMESHIFT,
  PROP_AMPLITUDE,
  PROP_OFFSET
};

378 379 380
#define _do_init \
  GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "lfo control source", 0, "low frequency oscillator control source")

381
#define gst_lfo_control_source_parent_class parent_class
382
G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
383
    GST_TYPE_CONTROL_SOURCE, G_ADD_PRIVATE (GstLFOControlSource) _do_init);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

static void
gst_lfo_control_source_reset (GstLFOControlSource * self)
{
  GstControlSource *csource = GST_CONTROL_SOURCE (self);

  csource->get_value = NULL;
  csource->get_value_array = NULL;
}

/**
 * gst_lfo_control_source_new:
 *
 * This returns a new, unbound #GstLFOControlSource.
 *
399
 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
400
 */
401
GstControlSource *
402
gst_lfo_control_source_new (void)
403
{
404 405 406 407 408 409
  GstControlSource *csource = g_object_new (GST_TYPE_LFO_CONTROL_SOURCE, NULL);

  /* Clear floating flag */
  gst_object_ref_sink (csource);

  return csource;
410 411 412 413 414 415 416 417
}

static gboolean
gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
    GstLFOWaveform waveform)
{
  GstControlSource *csource = GST_CONTROL_SOURCE (self);

418
  if (waveform >= num_waveforms || (int) waveform < 0) {
419 420 421 422
    GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
    return FALSE;
  }

423 424
  csource->get_value = waveforms[waveform].get;
  csource->get_value_array = waveforms[waveform].get_value_array;
425

426
  self->priv->waveform = waveform;
427

428
  return TRUE;
429 430 431 432 433
}

static void
gst_lfo_control_source_init (GstLFOControlSource * self)
{
434
  self->priv = gst_lfo_control_source_get_instance_private (self);
435 436
  self->priv->waveform = gst_lfo_control_source_set_waveform (self,
      GST_LFO_WAVEFORM_SINE);
437
  self->priv->frequency = 1.0;
438
  self->priv->amplitude = 1.0;
439 440 441
  self->priv->period = GST_SECOND / self->priv->frequency;
  self->priv->timeshift = 0;

Wim Taymans's avatar
Wim Taymans committed
442
  g_mutex_init (&self->lock);
443 444 445 446 447 448 449 450
}

static void
gst_lfo_control_source_finalize (GObject * obj)
{
  GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);

  gst_lfo_control_source_reset (self);
Wim Taymans's avatar
Wim Taymans committed
451
  g_mutex_clear (&self->lock);
452

453
  G_OBJECT_CLASS (parent_class)->finalize (obj);
454 455 456 457 458 459 460 461 462 463
}

static void
gst_lfo_control_source_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);

  switch (prop_id) {
    case PROP_WAVEFORM:
Wim Taymans's avatar
Wim Taymans committed
464
      g_mutex_lock (&self->lock);
465 466
      gst_lfo_control_source_set_waveform (self,
          (GstLFOWaveform) g_value_get_enum (value));
Wim Taymans's avatar
Wim Taymans committed
467
      g_mutex_unlock (&self->lock);
468 469 470 471
      break;
    case PROP_FREQUENCY:{
      gdouble frequency = g_value_get_double (value);

472
      g_return_if_fail (((GstClockTime) (GST_SECOND / frequency)) != 0);
473

Wim Taymans's avatar
Wim Taymans committed
474
      g_mutex_lock (&self->lock);
475 476
      self->priv->frequency = frequency;
      self->priv->period = GST_SECOND / frequency;
Wim Taymans's avatar
Wim Taymans committed
477
      g_mutex_unlock (&self->lock);
478 479 480
      break;
    }
    case PROP_TIMESHIFT:
Wim Taymans's avatar
Wim Taymans committed
481
      g_mutex_lock (&self->lock);
482
      self->priv->timeshift = g_value_get_uint64 (value);
Wim Taymans's avatar
Wim Taymans committed
483
      g_mutex_unlock (&self->lock);
484
      break;
485
    case PROP_AMPLITUDE:
Wim Taymans's avatar
Wim Taymans committed
486
      g_mutex_lock (&self->lock);
487
      self->priv->amplitude = g_value_get_double (value);
Wim Taymans's avatar
Wim Taymans committed
488
      g_mutex_unlock (&self->lock);
489
      break;
490
    case PROP_OFFSET:
Wim Taymans's avatar
Wim Taymans committed
491
      g_mutex_lock (&self->lock);
492
      self->priv->offset = g_value_get_double (value);
Wim Taymans's avatar
Wim Taymans committed
493
      g_mutex_unlock (&self->lock);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_lfo_control_source_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);

  switch (prop_id) {
    case PROP_WAVEFORM:
      g_value_set_enum (value, self->priv->waveform);
      break;
    case PROP_FREQUENCY:
      g_value_set_double (value, self->priv->frequency);
      break;
    case PROP_TIMESHIFT:
      g_value_set_uint64 (value, self->priv->timeshift);
      break;
    case PROP_AMPLITUDE:
518
      g_value_set_double (value, self->priv->amplitude);
519 520
      break;
    case PROP_OFFSET:
521
      g_value_set_double (value, self->priv->offset);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gst_lfo_control_source_finalize;
  gobject_class->set_property = gst_lfo_control_source_set_property;
  gobject_class->get_property = gst_lfo_control_source_get_property;

  /**
539
   * GstLFOControlSource:waveform:
540 541
   *
   * Specifies the waveform that should be used for this #GstLFOControlSource.
542
   */
543 544
  g_object_class_install_property (gobject_class, PROP_WAVEFORM,
      g_param_spec_enum ("waveform", "Waveform", "Waveform",
545 546
          GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 548

  /**
549
   * GstLFOControlSource:frequency:
550 551 552 553
   *
   * Specifies the frequency that should be used for the waveform
   * of this #GstLFOControlSource. It should be large enough
   * so that the period is longer than one nanosecond.
554
   */
555 556
  g_object_class_install_property (gobject_class, PROP_FREQUENCY,
      g_param_spec_double ("frequency", "Frequency",
557
          "Frequency of the waveform", DBL_MIN, G_MAXDOUBLE, 1.0,
558
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
559 560

  /**
561
   * GstLFOControlSource:timeshift:
562 563 564 565 566 567 568
   *
   * Specifies the timeshift to the right that should be used for the waveform
   * of this #GstLFOControlSource in nanoseconds.
   *
   * To get a n nanosecond shift to the left use
   * "(GST_SECOND / frequency) - n".
   *
569
   */
570 571 572
  g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
      g_param_spec_uint64 ("timeshift", "Timeshift",
          "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
573
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
574 575

  /**
576
   * GstLFOControlSource:amplitude:
577 578
   *
   * Specifies the amplitude for the waveform of this #GstLFOControlSource.
579
   */
580
  g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
581
      g_param_spec_double ("amplitude", "Amplitude",
582 583
          "Amplitude of the waveform", 0.0, 1.0, 1.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
584 585

  /**
586
   * GstLFOControlSource:offset:
587
   *
588 589
   * Specifies the value offset for the waveform of this #GstLFOControlSource.
   */
590
  g_object_class_install_property (gobject_class, PROP_OFFSET,
591
      g_param_spec_double ("offset", "Offset", "Offset of the waveform",
592 593
          0.0, 1.0, 1.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
594
}