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 G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
403     GST_TYPE_CONTROL_SOURCE, _do_init);
404
405 static void
406 gst_lfo_control_source_reset (GstLFOControlSource * self)
407 {
408   GstControlSource *csource = GST_CONTROL_SOURCE (self);
409
410   csource->get_value = NULL;
411   csource->get_value_array = NULL;
412 }
413
414 /**
415  * gst_lfo_control_source_new:
416  *
417  * This returns a new, unbound #GstLFOControlSource.
418  *
419  * Returns: (transfer full): a new, unbound #GstLFOControlSource.
420  */
421 GstControlSource *
422 gst_lfo_control_source_new (void)
423 {
424   return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
425 }
426
427 static gboolean
428 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
429     GstLFOWaveform waveform)
430 {
431   GstControlSource *csource = GST_CONTROL_SOURCE (self);
432
433   if (waveform >= num_waveforms || (int) waveform < 0) {
434     GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
435     return FALSE;
436   }
437
438   csource->get_value = waveforms[waveform].get;
439   csource->get_value_array = waveforms[waveform].get_value_array;
440
441   self->priv->waveform = waveform;
442
443   return TRUE;
444 }
445
446 static void
447 gst_lfo_control_source_init (GstLFOControlSource * self)
448 {
449   self->priv =
450       G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
451       GstLFOControlSourcePrivate);
452   self->priv->waveform = gst_lfo_control_source_set_waveform (self,
453       GST_LFO_WAVEFORM_SINE);
454   self->priv->frequency = 1.0;
455   self->priv->period = GST_SECOND / self->priv->frequency;
456   self->priv->timeshift = 0;
457
458   g_mutex_init (&self->lock);
459 }
460
461 static void
462 gst_lfo_control_source_finalize (GObject * obj)
463 {
464   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
465
466   gst_lfo_control_source_reset (self);
467   g_mutex_clear (&self->lock);
468
469   G_OBJECT_CLASS (gst_lfo_control_source_parent_class)->finalize (obj);
470 }
471
472 static void
473 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
474     const GValue * value, GParamSpec * pspec)
475 {
476   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
477
478   switch (prop_id) {
479     case PROP_WAVEFORM:
480       g_mutex_lock (&self->lock);
481       gst_lfo_control_source_set_waveform (self,
482           (GstLFOWaveform) g_value_get_enum (value));
483       g_mutex_unlock (&self->lock);
484       break;
485     case PROP_FREQUENCY:{
486       gdouble frequency = g_value_get_double (value);
487
488       g_return_if_fail (frequency > 0
489           || ((GstClockTime) (GST_SECOND / frequency)) != 0);
490
491       g_mutex_lock (&self->lock);
492       self->priv->frequency = frequency;
493       self->priv->period = GST_SECOND / frequency;
494       g_mutex_unlock (&self->lock);
495       break;
496     }
497     case PROP_TIMESHIFT:
498       g_mutex_lock (&self->lock);
499       self->priv->timeshift = g_value_get_uint64 (value);
500       g_mutex_unlock (&self->lock);
501       break;
502     case PROP_AMPLITUDE:
503       g_mutex_lock (&self->lock);
504       self->priv->amplitude = g_value_get_double (value);
505       g_mutex_unlock (&self->lock);
506       break;
507     case PROP_OFFSET:
508       g_mutex_lock (&self->lock);
509       self->priv->offset = g_value_get_double (value);
510       g_mutex_unlock (&self->lock);
511       break;
512     default:
513       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
514       break;
515   }
516 }
517
518 static void
519 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
520     GValue * value, GParamSpec * pspec)
521 {
522   GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
523
524   switch (prop_id) {
525     case PROP_WAVEFORM:
526       g_value_set_enum (value, self->priv->waveform);
527       break;
528     case PROP_FREQUENCY:
529       g_value_set_double (value, self->priv->frequency);
530       break;
531     case PROP_TIMESHIFT:
532       g_value_set_uint64 (value, self->priv->timeshift);
533       break;
534     case PROP_AMPLITUDE:
535       g_value_set_double (value, self->priv->amplitude);
536       break;
537     case PROP_OFFSET:
538       g_value_set_double (value, self->priv->offset);
539       break;
540     default:
541       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
542       break;
543   }
544 }
545
546 static void
547 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
548 {
549   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
550
551   g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
552
553   gobject_class->finalize = gst_lfo_control_source_finalize;
554   gobject_class->set_property = gst_lfo_control_source_set_property;
555   gobject_class->get_property = gst_lfo_control_source_get_property;
556
557   /**
558    * GstLFOControlSource:waveform
559    *
560    * Specifies the waveform that should be used for this #GstLFOControlSource.
561    */
562   g_object_class_install_property (gobject_class, PROP_WAVEFORM,
563       g_param_spec_enum ("waveform", "Waveform", "Waveform",
564           GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
565           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566
567   /**
568    * GstLFOControlSource:frequency
569    *
570    * Specifies the frequency that should be used for the waveform
571    * of this #GstLFOControlSource. It should be large enough
572    * so that the period is longer than one nanosecond.
573    */
574   g_object_class_install_property (gobject_class, PROP_FREQUENCY,
575       g_param_spec_double ("frequency", "Frequency",
576           "Frequency of the waveform", 0.0, G_MAXDOUBLE, 1.0,
577           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
578
579   /**
580    * GstLFOControlSource:timeshift
581    *
582    * Specifies the timeshift to the right that should be used for the waveform
583    * of this #GstLFOControlSource in nanoseconds.
584    *
585    * To get a n nanosecond shift to the left use
586    * "(GST_SECOND / frequency) - n".
587    *
588    */
589   g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
590       g_param_spec_uint64 ("timeshift", "Timeshift",
591           "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
592           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
593
594   /**
595    * GstLFOControlSource:amplitude
596    *
597    * Specifies the amplitude for the waveform of this #GstLFOControlSource.
598    */
599   g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
600       g_param_spec_double ("amplitude", "Amplitude",
601           "Amplitude of the waveform", 0.0, 1.0, 1.0,
602           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
603
604   /**
605    * GstLFOControlSource:offset
606    *
607    * Specifies the value offset for the waveform of this #GstLFOControlSource.
608    */
609   g_object_class_install_property (gobject_class, PROP_OFFSET,
610       g_param_spec_double ("offset", "Offset", "Offset of the waveform",
611           0.0, 1.0, 1.0,
612           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
613 }