aboutsummaryrefslogtreecommitdiff
path: root/libs/gst/controller/gstlfocontrolsource.c
blob: 819518db65dd2193041be98e3b3e2fa97d1610a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* GStreamer
 *
 * Copyright (C) 2007,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gstlfocontrolsource
 * @short_description: LFO control source
 *
 * #GstLFOControlSource is a #GstControlSource, that provides several periodic waveforms
 * as control values. It supports all fundamental, numeric GValue types as property.
 *
 * To use #GstLFOControlSource get a new instance by calling gst_lfo_control_source_new(),
 * bind it to a #GParamSpec and set the relevant properties or use
 * gst_lfo_control_source_set_waveform.
 *
 * All functions are MT-safe.
 *
 */

#include <glib-object.h>
#include <gst/gst.h>
#include <gst/gstcontrolsource.h>

#include "gstlfocontrolsource.h"

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

#include <gst/math-compat.h>

#define GST_CAT_DEFAULT controller_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

struct _GstLFOControlSourcePrivate
{
  GstLFOWaveform waveform;
  gdouble frequency;
  GstClockTime period;
  GstClockTime timeshift;
  gdouble amplitude;
  gdouble offset;
};

/* 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;
}

static inline gdouble
_sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble ret;

  ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
  ret *= amp;
  ret += off;

  return ret;
}

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

  gst_object_sync_values (GST_OBJECT (self), timestamp);
  g_mutex_lock (&self->lock);
  *value = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
  g_mutex_unlock (&self->lock);
  return TRUE;
}

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++) {
    gst_object_sync_values (GST_OBJECT (self), ts);
    g_mutex_lock (&self->lock);
    *values = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
    g_mutex_unlock (&self->lock);
    ts += interval;
    values++;
  }
  return TRUE;
}


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;
}

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

  gst_object_sync_values (GST_OBJECT (self), timestamp);
  g_mutex_lock (&self->lock);
  *value = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
  g_mutex_unlock (&self->lock);
  return TRUE;
}

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++) {
    gst_object_sync_values (GST_OBJECT (self), ts);
    g_mutex_lock (&self->lock);
    *values = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
    g_mutex_unlock (&self->lock);
    ts += interval;
    values++;
  }
  return TRUE;
}

static inline gdouble
_saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble per = gst_guint64_to_gdouble (period);
  gdouble ret;

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

  return ret;
}

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

  gst_object_sync_values (GST_OBJECT (self), timestamp);
  g_mutex_lock (&self->lock);
  *value = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
  g_mutex_unlock (&self->lock);
  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++) {
    gst_object_sync_values (GST_OBJECT (self), ts);
    g_mutex_lock (&self->lock);
    *values = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
    g_mutex_unlock (&self->lock);
    ts += interval;
    values++;
  }
  return TRUE;
}

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

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

  return ret;
}

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

  gst_object_sync_values (GST_OBJECT (self), timestamp);
  g_mutex_lock (&self->lock);
  *value = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
  g_mutex_unlock (&self->lock);
  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++) {
    gst_object_sync_values (GST_OBJECT (self), ts);
    g_mutex_lock (&self->lock);
    *values = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
    g_mutex_unlock (&self->lock);
    ts += interval;
    values++;
  }
  return TRUE;
}


static inline gdouble
_triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
    GstClockTime timeshift, GstClockTime period, gdouble frequency,
    GstClockTime timestamp)
{
  gdouble pos =
      gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
  gdouble per = gst_guint64_to_gdouble (period);
  gdouble ret;

  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);
  else
    /* 4th quarter */
    ret = -(per - pos) * ((4.0 * amp) / per);

  ret += off;

  return ret;
}

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

  gst_object_sync_values (GST_OBJECT (self), timestamp);
  g_mutex_lock (&self->lock);
  *value = _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
      priv->period, priv->frequency, timestamp);
  g_mutex_unlock (&self->lock);
  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++) {
    gst_object_sync_values (GST_OBJECT (self), ts);
    g_mutex_lock (&self->lock);
    *values =
        _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
        priv->period, priv->frequency, ts);
    g_mutex_unlock (&self->lock);
    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}
};

static const guint num_waveforms = G_N_ELEMENTS (waveforms);

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

GType
gst_lfo_waveform_get_type (void)
{
  static gsize gtype = 0;
  static const GEnumValue values[] = {
    {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
        "sine"},
    {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
        "square"},
    {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
        "saw"},
    {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
        "reverse-saw"},
    {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
        "triangle"},
    {0, NULL, NULL}
  };

  if (g_once_init_enter (&gtype)) {
    GType tmp = g_enum_register_static ("GstLFOWaveform", values);
    g_once_init_leave (&gtype, tmp);
  }

  return (GType) gtype;
}

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

#define gst_lfo_control_source_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
    GST_TYPE_CONTROL_SOURCE, _do_init);

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.
 *
 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
 */
GstControlSource *
gst_lfo_control_source_new (void)
{
  return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
}

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

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

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

  self->priv->waveform = waveform;

  return TRUE;
}

static void
gst_lfo_control_source_init (GstLFOControlSource * self)
{
  self->priv =
      G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
      GstLFOControlSourcePrivate);
  self->priv->waveform = gst_lfo_control_source_set_waveform (self,
      GST_LFO_WAVEFORM_SINE);
  self->priv->frequency = 1.0;
  self->priv->period = GST_SECOND / self->priv->frequency;
  self->priv->timeshift = 0;

  g_mutex_init (&self->lock);
}

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

  gst_lfo_control_source_reset (self);
  g_mutex_clear (&self->lock);

  G_OBJECT_CLASS (parent_class)->finalize (obj);
}

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:
      g_mutex_lock (&self->lock);
      gst_lfo_control_source_set_waveform (self,
          (GstLFOWaveform) g_value_get_enum (value));
      g_mutex_unlock (&self->lock);
      break;
    case PROP_FREQUENCY:{
      gdouble frequency = g_value_get_double (value);

      g_return_if_fail (frequency > 0
          || ((GstClockTime) (GST_SECOND / frequency)) != 0);

      g_mutex_lock (&self->lock);
      self->priv->frequency = frequency;
      self->priv->period = GST_SECOND / frequency;
      g_mutex_unlock (&self->lock);
      break;
    }
    case PROP_TIMESHIFT:
      g_mutex_lock (&self->lock);
      self->priv->timeshift = g_value_get_uint64 (value);
      g_mutex_unlock (&self->lock);
      break;
    case PROP_AMPLITUDE:
      g_mutex_lock (&self->lock);
      self->priv->amplitude = g_value_get_double (value);
      g_mutex_unlock (&self->lock);
      break;
    case PROP_OFFSET:
      g_mutex_lock (&self->lock);
      self->priv->offset = g_value_get_double (value);
      g_mutex_unlock (&self->lock);
      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:
      g_value_set_double (value, self->priv->amplitude);
      break;
    case PROP_OFFSET:
      g_value_set_double (value, self->priv->offset);
      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);

  g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));

  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;

  /**
   * GstLFOControlSource:waveform
   *
   * Specifies the waveform that should be used for this #GstLFOControlSource.
   */
  g_object_class_install_property (gobject_class, PROP_WAVEFORM,
      g_param_spec_enum ("waveform", "Waveform", "Waveform",
          GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
   * GstLFOControlSource:frequency
   *
   * 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.
   */
  g_object_class_install_property (gobject_class, PROP_FREQUENCY,
      g_param_spec_double ("frequency", "Frequency",
          "Frequency of the waveform", 0.0, G_MAXDOUBLE, 1.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));

  /**
   * GstLFOControlSource:timeshift
   *
   * 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".
   *
   */
  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,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));

  /**
   * GstLFOControlSource:amplitude
   *
   * Specifies the amplitude for the waveform of this #GstLFOControlSource.
   */
  g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
      g_param_spec_double ("amplitude", "Amplitude",
          "Amplitude of the waveform", 0.0, 1.0, 1.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));

  /**
   * GstLFOControlSource:offset
   *
   * Specifies the value offset for the waveform of this #GstLFOControlSource.
   */
  g_object_class_install_property (gobject_class, PROP_OFFSET,
      g_param_spec_double ("offset", "Offset", "Offset of the waveform",
          0.0, 1.0, 1.0,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
}