Merge remote-tracking branch 'origin/0.10'
[platform/upstream/gstreamer.git] / libs / gst / controller / gstlfocontrolsource.c
1 /* GStreamer
2  *
3  * Copyright (C) 2007,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * gstlfocontrolsource.c: Control source that provides some periodic waveforms
6  *                        as control values.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstlfocontrolsource
26  * @short_description: LFO control source
27  *
28  * #GstLFOControlSource is a #GstControlSource, that provides several periodic waveforms
29  * as control values. It supports all fundamental, numeric GValue types as property.
30  *
31  * To use #GstLFOControlSource get a new instance by calling gst_lfo_control_source_new(),
32  * bind it to a #GParamSpec and set the relevant properties or use
33  * gst_lfo_control_source_set_waveform.
34  *
35  * All functions are MT-safe.
36  *
37  */
38
39 #include <glib-object.h>
40 #include <gst/gst.h>
41 #include <gst/gstcontrolsource.h>
42
43 #include "gstlfocontrolsource.h"
44
45 #include "gst/glib-compat-private.h"
46
47 #include <gst/math-compat.h>
48
49 #define GST_CAT_DEFAULT controller_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
51
52 struct _GstLFOControlSourcePrivate
53 {
54   GstLFOWaveform waveform;
55   gdouble frequency;
56   GstClockTime period;
57   GstClockTime timeshift;
58   gdouble amplitude;
59   gdouble offset;
60 };
61
62 /* FIXME: as % in C is not the modulo operator we need here for
63  * negative numbers implement our own. Are there better ways? */
64 static inline GstClockTime
65 _calculate_pos (GstClockTime timestamp, GstClockTime timeshift,
66     GstClockTime period)
67 {
68   while (timestamp < timeshift)
69     timestamp += period;
70
71   timestamp -= timeshift;
72
73   return timestamp % period;
74 }
75
76 static inline gdouble
77 _sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
78     GstClockTime timeshift, GstClockTime period, gdouble frequency,
79     GstClockTime timestamp)
80 {
81   gdouble pos =
82       gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
83   gdouble ret;
84
85   ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
86   ret *= amp;
87   ret += off;
88
89   return ret;
90 }
91
92 static gboolean
93 waveform_sine_get (GstLFOControlSource * self, GstClockTime timestamp,
94     gdouble * value)
95 {
96   GstLFOControlSourcePrivate *priv = self->priv;
97
98   gst_object_sync_values (GST_OBJECT (self), timestamp);
99   g_mutex_lock (&self->lock);
100   *value = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
101       priv->period, priv->frequency, timestamp);
102   g_mutex_unlock (&self->lock);
103   return TRUE;
104 }
105
106 static gboolean
107 waveform_sine_get_value_array (GstLFOControlSource * self,
108     GstClockTime timestamp, GstClockTime interval, guint n_values,
109     gdouble * values)
110 {
111   GstLFOControlSourcePrivate *priv = self->priv;
112   guint i;
113   GstClockTime ts = timestamp;
114
115   for (i = 0; i < n_values; i++) {
116     gst_object_sync_values (GST_OBJECT (self), ts);
117     g_mutex_lock (&self->lock);
118     *values = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
119         priv->period, priv->frequency, ts);
120     g_mutex_unlock (&self->lock);
121     ts += interval;
122     values++;
123   }
124   return TRUE;
125 }
126
127
128 static inline gdouble
129 _square_get (GstLFOControlSource * self, gdouble amp, gdouble off,
130     GstClockTime timeshift, GstClockTime period, gdouble frequency,
131     GstClockTime timestamp)
132 {
133   GstClockTime pos = _calculate_pos (timestamp, timeshift, period);
134   gdouble ret;
135
136   if (pos >= period / 2)
137     ret = amp;
138   else
139     ret = -amp;
140   ret += off;
141
142   return ret;
143 }
144
145 static gboolean
146 waveform_square_get (GstLFOControlSource * self, GstClockTime timestamp,
147     gdouble * value)
148 {
149   GstLFOControlSourcePrivate *priv = self->priv;
150
151   gst_object_sync_values (GST_OBJECT (self), timestamp);
152   g_mutex_lock (&self->lock);
153   *value = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
154       priv->period, priv->frequency, timestamp);
155   g_mutex_unlock (&self->lock);
156   return TRUE;
157 }
158
159 static gboolean
160 waveform_square_get_value_array (GstLFOControlSource * self,
161     GstClockTime timestamp, GstClockTime interval, guint n_values,
162     gdouble * values)
163 {
164   GstLFOControlSourcePrivate *priv = self->priv;
165   guint i;
166   GstClockTime ts = timestamp;
167
168   for (i = 0; i < n_values; i++) {
169     gst_object_sync_values (GST_OBJECT (self), ts);
170     g_mutex_lock (&self->lock);
171     *values = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
172         priv->period, priv->frequency, ts);
173     g_mutex_unlock (&self->lock);
174     ts += interval;
175     values++;
176   }
177   return TRUE;
178 }
179
180 static inline gdouble
181 _saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
182     GstClockTime timeshift, GstClockTime period, gdouble frequency,
183     GstClockTime timestamp)
184 {
185   gdouble pos =
186       gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
187   gdouble per = gst_guint64_to_gdouble (period);
188   gdouble ret;
189
190   ret = -((pos - per / 2.0) * ((2.0 * amp) / per));
191   ret += off;
192
193   return ret;
194 }
195
196 static gboolean
197 waveform_saw_get (GstLFOControlSource * self, GstClockTime timestamp,
198     gdouble * value)
199 {
200   GstLFOControlSourcePrivate *priv = self->priv;
201
202   gst_object_sync_values (GST_OBJECT (self), timestamp);
203   g_mutex_lock (&self->lock);
204   *value = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
205       priv->period, priv->frequency, timestamp);
206   g_mutex_unlock (&self->lock);
207   return TRUE;
208 }
209
210 static gboolean
211 waveform_saw_get_value_array (GstLFOControlSource * self,
212     GstClockTime timestamp, GstClockTime interval, guint n_values,
213     gdouble * values)
214 {
215   GstLFOControlSourcePrivate *priv = self->priv;
216   guint i;
217   GstClockTime ts = timestamp;
218
219   for (i = 0; i < n_values; i++) {
220     gst_object_sync_values (GST_OBJECT (self), ts);
221     g_mutex_lock (&self->lock);
222     *values = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
223         priv->period, priv->frequency, ts);
224     g_mutex_unlock (&self->lock);
225     ts += interval;
226     values++;
227   }
228   return TRUE;
229 }
230
231 static inline gdouble
232 _rsaw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
233     GstClockTime timeshift, GstClockTime period, gdouble frequency,
234     GstClockTime timestamp)
235 {
236   gdouble pos =
237       gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
238   gdouble per = gst_guint64_to_gdouble (period);
239   gdouble ret;
240
241   ret = (pos - per / 2.0) * ((2.0 * amp) / per);
242   ret += off;
243
244   return ret;
245 }
246
247 static gboolean
248 waveform_rsaw_get (GstLFOControlSource * self, GstClockTime timestamp,
249     gdouble * value)
250 {
251   GstLFOControlSourcePrivate *priv = self->priv;
252
253   gst_object_sync_values (GST_OBJECT (self), timestamp);
254   g_mutex_lock (&self->lock);
255   *value = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
256       priv->period, priv->frequency, timestamp);
257   g_mutex_unlock (&self->lock);
258   return TRUE;
259 }
260
261 static gboolean
262 waveform_rsaw_get_value_array (GstLFOControlSource * self,
263     GstClockTime timestamp, GstClockTime interval, guint n_values,
264     gdouble * values)
265 {
266   GstLFOControlSourcePrivate *priv = self->priv;
267   guint i;
268   GstClockTime ts = timestamp;
269
270   for (i = 0; i < n_values; i++) {
271     gst_object_sync_values (GST_OBJECT (self), ts);
272     g_mutex_lock (&self->lock);
273     *values = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
274         priv->period, priv->frequency, ts);
275     g_mutex_unlock (&self->lock);
276     ts += interval;
277     values++;
278   }
279   return TRUE;
280 }
281
282
283 static inline gdouble
284 _triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
285     GstClockTime timeshift, GstClockTime period, gdouble frequency,
286     GstClockTime timestamp)
287 {
288   gdouble pos =
289       gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
290   gdouble per = gst_guint64_to_gdouble (period);
291   gdouble ret;
292
293   if (pos <= 0.25 * per)
294     /* 1st quarter */
295     ret = pos * ((4.0 * amp) / per);
296   else if (pos <= 0.75 * per)
297     /* 2nd & 3rd quarter */
298     ret = -(pos - per / 2.0) * ((4.0 * amp) / per);
299   else
300     /* 4th quarter */
301     ret = -(per - pos) * ((4.0 * amp) / per);
302
303   ret += off;
304
305   return ret;
306 }
307
308 static gboolean
309 waveform_triangle_get (GstLFOControlSource * self, GstClockTime timestamp,
310     gdouble * value)
311 {
312   GstLFOControlSourcePrivate *priv = self->priv;
313
314   gst_object_sync_values (GST_OBJECT (self), timestamp);
315   g_mutex_lock (&self->lock);
316   *value = _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
317       priv->period, priv->frequency, timestamp);
318   g_mutex_unlock (&self->lock);
319   return TRUE;
320 }
321
322 static gboolean
323 waveform_triangle_get_value_array (GstLFOControlSource * self,
324     GstClockTime timestamp, GstClockTime interval, guint n_values,
325     gdouble * values)
326 {
327   GstLFOControlSourcePrivate *priv = self->priv;
328   guint i;
329   GstClockTime ts = timestamp;
330
331   for (i = 0; i < n_values; i++) {
332     gst_object_sync_values (GST_OBJECT (self), ts);
333     g_mutex_lock (&self->lock);
334     *values =
335         _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
336         priv->period, priv->frequency, ts);
337     g_mutex_unlock (&self->lock);
338     ts += interval;
339     values++;
340   }
341   return TRUE;
342 }
343
344 static struct
345 {
346   GstControlSourceGetValue get;
347   GstControlSourceGetValueArray get_value_array;
348 } waveforms[] = {
349   {
350   (GstControlSourceGetValue) waveform_sine_get,
351         (GstControlSourceGetValueArray) waveform_sine_get_value_array}, {
352   (GstControlSourceGetValue) waveform_square_get,
353         (GstControlSourceGetValueArray) waveform_square_get_value_array}, {
354   (GstControlSourceGetValue) waveform_saw_get,
355         (GstControlSourceGetValueArray) waveform_saw_get_value_array}, {
356   (GstControlSourceGetValue) waveform_rsaw_get,
357         (GstControlSourceGetValueArray) waveform_rsaw_get_value_array}, {
358   (GstControlSourceGetValue) waveform_triangle_get,
359         (GstControlSourceGetValueArray) waveform_triangle_get_value_array}
360 };
361
362 static const guint num_waveforms = G_N_ELEMENTS (waveforms);
363
364 enum
365 {
366   PROP_WAVEFORM = 1,
367   PROP_FREQUENCY,
368   PROP_TIMESHIFT,
369   PROP_AMPLITUDE,
370   PROP_OFFSET
371 };
372
373 GType
374 gst_lfo_waveform_get_type (void)
375 {
376   static gsize gtype = 0;
377   static const GEnumValue values[] = {
378     {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
379         "sine"},
380     {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
381         "square"},
382     {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
383         "saw"},
384     {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
385         "reverse-saw"},
386     {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
387         "triangle"},
388     {0, NULL, NULL}
389   };
390
391   if (g_once_init_enter (&gtype)) {
392     GType tmp = g_enum_register_static ("GstLFOWaveform", values);
393     g_once_init_leave (&gtype, tmp);
394   }
395
396   return (GType) gtype;
397 }
398
399 #define _do_init \
400   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "lfo control source", 0, "low frequency oscillator control source")
401
402 #define gst_lfo_control_source_parent_class parent_class
403 G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
404     GST_TYPE_CONTROL_SOURCE, _do_init);
405
406 static void
407 gst_lfo_control_source_reset (GstLFOControlSource * self)
408 {
409   GstControlSource *csource = GST_CONTROL_SOURCE (self);
410
411   csource->get_value = NULL;
412   csource->get_value_array = NULL;
413 }
414
415 /**
416  * gst_lfo_control_source_new:
417  *
418  * This returns a new, unbound #GstLFOControlSource.
419  *
420  * Returns: (transfer full): a new, unbound #GstLFOControlSource.
421  */
422 GstControlSource *
423 gst_lfo_control_source_new (void)
424 {
425   return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
426 }
427
428 static gboolean
429 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
430     GstLFOWaveform waveform)
431 {
432   GstControlSource *csource = GST_CONTROL_SOURCE (self);
433
434   if (waveform >= num_waveforms || (int) waveform < 0) {
435     GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
436     return FALSE;
437   }
438
439   csource->get_value = waveforms[waveform].get;
440   csource->get_value_array = waveforms[waveform].get_value_array;
441
442   self->priv->waveform = waveform;
443
444   return TRUE;
445 }
446
447 static void
448 gst_lfo_control_source_init (GstLFOControlSource * self)
449 {
450   self->priv =
451       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
452       GstLFOControlSourcePrivate);
453   self->priv->waveform = gst_lfo_control_source_set_waveform (self,
454       GST_LFO_WAVEFORM_SINE);
455   self->priv->frequency = 1.0;
456   self->priv->period = GST_SECOND / self->priv->frequency;
457   self->priv->timeshift = 0;
458
459   g_mutex_init (&self->lock);
460 }
461
462 static void
463 gst_lfo_control_source_finalize (GObject * obj)
464 {
465   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
466
467   gst_lfo_control_source_reset (self);
468   g_mutex_clear (&self->lock);
469
470   G_OBJECT_CLASS (parent_class)->finalize (obj);
471 }
472
473 static void
474 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
475     const GValue * value, GParamSpec * pspec)
476 {
477   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
478
479   switch (prop_id) {
480     case PROP_WAVEFORM:
481       g_mutex_lock (&self->lock);
482       gst_lfo_control_source_set_waveform (self,
483           (GstLFOWaveform) g_value_get_enum (value));
484       g_mutex_unlock (&self->lock);
485       break;
486     case PROP_FREQUENCY:{
487       gdouble frequency = g_value_get_double (value);
488
489       g_return_if_fail (frequency > 0
490           || ((GstClockTime) (GST_SECOND / frequency)) != 0);
491
492       g_mutex_lock (&self->lock);
493       self->priv->frequency = frequency;
494       self->priv->period = GST_SECOND / frequency;
495       g_mutex_unlock (&self->lock);
496       break;
497     }
498     case PROP_TIMESHIFT:
499       g_mutex_lock (&self->lock);
500       self->priv->timeshift = g_value_get_uint64 (value);
501       g_mutex_unlock (&self->lock);
502       break;
503     case PROP_AMPLITUDE:
504       g_mutex_lock (&self->lock);
505       self->priv->amplitude = g_value_get_double (value);
506       g_mutex_unlock (&self->lock);
507       break;
508     case PROP_OFFSET:
509       g_mutex_lock (&self->lock);
510       self->priv->offset = g_value_get_double (value);
511       g_mutex_unlock (&self->lock);
512       break;
513     default:
514       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515       break;
516   }
517 }
518
519 static void
520 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
521     GValue * value, GParamSpec * pspec)
522 {
523   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
524
525   switch (prop_id) {
526     case PROP_WAVEFORM:
527       g_value_set_enum (value, self->priv->waveform);
528       break;
529     case PROP_FREQUENCY:
530       g_value_set_double (value, self->priv->frequency);
531       break;
532     case PROP_TIMESHIFT:
533       g_value_set_uint64 (value, self->priv->timeshift);
534       break;
535     case PROP_AMPLITUDE:
536       g_value_set_double (value, self->priv->amplitude);
537       break;
538     case PROP_OFFSET:
539       g_value_set_double (value, self->priv->offset);
540       break;
541     default:
542       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543       break;
544   }
545 }
546
547 static void
548 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
549 {
550   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
551
552   g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
553
554   gobject_class->finalize = gst_lfo_control_source_finalize;
555   gobject_class->set_property = gst_lfo_control_source_set_property;
556   gobject_class->get_property = gst_lfo_control_source_get_property;
557
558   /**
559    * GstLFOControlSource:waveform
560    *
561    * Specifies the waveform that should be used for this #GstLFOControlSource.
562    */
563   g_object_class_install_property (gobject_class, PROP_WAVEFORM,
564       g_param_spec_enum ("waveform", "Waveform", "Waveform",
565           GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
566           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
567
568   /**
569    * GstLFOControlSource:frequency
570    *
571    * Specifies the frequency that should be used for the waveform
572    * of this #GstLFOControlSource. It should be large enough
573    * so that the period is longer than one nanosecond.
574    */
575   g_object_class_install_property (gobject_class, PROP_FREQUENCY,
576       g_param_spec_double ("frequency", "Frequency",
577           "Frequency of the waveform", 0.0, G_MAXDOUBLE, 1.0,
578           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
579
580   /**
581    * GstLFOControlSource:timeshift
582    *
583    * Specifies the timeshift to the right that should be used for the waveform
584    * of this #GstLFOControlSource in nanoseconds.
585    *
586    * To get a n nanosecond shift to the left use
587    * "(GST_SECOND / frequency) - n".
588    *
589    */
590   g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
591       g_param_spec_uint64 ("timeshift", "Timeshift",
592           "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
593           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
594
595   /**
596    * GstLFOControlSource:amplitude
597    *
598    * Specifies the amplitude for the waveform of this #GstLFOControlSource.
599    */
600   g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
601       g_param_spec_double ("amplitude", "Amplitude",
602           "Amplitude of the waveform", 0.0, 1.0, 1.0,
603           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
604
605   /**
606    * GstLFOControlSource:offset
607    *
608    * Specifies the value offset for the waveform of this #GstLFOControlSource.
609    */
610   g_object_class_install_property (gobject_class, PROP_OFFSET,
611       g_param_spec_double ("offset", "Offset", "Offset of the waveform",
612           0.0, 1.0, 1.0,
613           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
614 }