3 * Copyright (C) 2007,2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * gstlfocontrolsource.c: Control source that provides some periodic waveforms
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.
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.
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., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gstlfocontrolsource
26 * @title: GstLFOControlSource
27 * @short_description: LFO control source
29 * #GstLFOControlSource is a #GstControlSource, that provides several periodic
30 * waveforms as control values.
32 * To use #GstLFOControlSource get a new instance by calling
33 * gst_lfo_control_source_new(), bind it to a #GParamSpec and set the relevant
36 * All functions are MT-safe.
41 #include <glib-object.h>
43 #include <gst/gstcontrolsource.h>
45 #include "gstlfocontrolsource.h"
47 #include "gst/glib-compat-private.h"
49 #include <gst/math-compat.h>
51 #define GST_CAT_DEFAULT controller_debug
52 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
54 struct _GstLFOControlSourcePrivate
56 GstLFOWaveform waveform;
59 GstClockTime timeshift;
64 /* FIXME: as % in C is not the modulo operator we need here for
65 * negative numbers implement our own. Are there better ways? */
66 static inline GstClockTime
67 _calculate_pos (GstClockTime timestamp, GstClockTime timeshift,
70 while (timestamp < timeshift)
73 timestamp -= timeshift;
75 return timestamp % period;
79 _sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
80 GstClockTime timeshift, GstClockTime period, gdouble frequency,
81 GstClockTime timestamp)
84 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
87 ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
95 waveform_sine_get (GstLFOControlSource * self, GstClockTime timestamp,
98 GstLFOControlSourcePrivate *priv = self->priv;
100 gst_object_sync_values (GST_OBJECT (self), timestamp);
101 g_mutex_lock (&self->lock);
102 *value = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
103 priv->period, priv->frequency, timestamp);
104 g_mutex_unlock (&self->lock);
109 waveform_sine_get_value_array (GstLFOControlSource * self,
110 GstClockTime timestamp, GstClockTime interval, guint n_values,
113 GstLFOControlSourcePrivate *priv = self->priv;
115 GstClockTime ts = timestamp;
117 for (i = 0; i < n_values; i++) {
118 gst_object_sync_values (GST_OBJECT (self), ts);
119 g_mutex_lock (&self->lock);
120 *values = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
121 priv->period, priv->frequency, ts);
122 g_mutex_unlock (&self->lock);
130 static inline gdouble
131 _square_get (GstLFOControlSource * self, gdouble amp, gdouble off,
132 GstClockTime timeshift, GstClockTime period, gdouble frequency,
133 GstClockTime timestamp)
135 GstClockTime pos = _calculate_pos (timestamp, timeshift, period);
138 if (pos >= period / 2)
148 waveform_square_get (GstLFOControlSource * self, GstClockTime timestamp,
151 GstLFOControlSourcePrivate *priv = self->priv;
153 gst_object_sync_values (GST_OBJECT (self), timestamp);
154 g_mutex_lock (&self->lock);
155 *value = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
156 priv->period, priv->frequency, timestamp);
157 g_mutex_unlock (&self->lock);
162 waveform_square_get_value_array (GstLFOControlSource * self,
163 GstClockTime timestamp, GstClockTime interval, guint n_values,
166 GstLFOControlSourcePrivate *priv = self->priv;
168 GstClockTime ts = timestamp;
170 for (i = 0; i < n_values; i++) {
171 gst_object_sync_values (GST_OBJECT (self), ts);
172 g_mutex_lock (&self->lock);
173 *values = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
174 priv->period, priv->frequency, ts);
175 g_mutex_unlock (&self->lock);
182 static inline gdouble
183 _saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
184 GstClockTime timeshift, GstClockTime period, gdouble frequency,
185 GstClockTime timestamp)
188 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
189 gdouble per = gst_guint64_to_gdouble (period);
192 ret = -((pos - per / 2.0) * ((2.0 * amp) / per));
199 waveform_saw_get (GstLFOControlSource * self, GstClockTime timestamp,
202 GstLFOControlSourcePrivate *priv = self->priv;
204 gst_object_sync_values (GST_OBJECT (self), timestamp);
205 g_mutex_lock (&self->lock);
206 *value = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
207 priv->period, priv->frequency, timestamp);
208 g_mutex_unlock (&self->lock);
213 waveform_saw_get_value_array (GstLFOControlSource * self,
214 GstClockTime timestamp, GstClockTime interval, guint n_values,
217 GstLFOControlSourcePrivate *priv = self->priv;
219 GstClockTime ts = timestamp;
221 for (i = 0; i < n_values; i++) {
222 gst_object_sync_values (GST_OBJECT (self), ts);
223 g_mutex_lock (&self->lock);
224 *values = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
225 priv->period, priv->frequency, ts);
226 g_mutex_unlock (&self->lock);
233 static inline gdouble
234 _rsaw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
235 GstClockTime timeshift, GstClockTime period, gdouble frequency,
236 GstClockTime timestamp)
239 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
240 gdouble per = gst_guint64_to_gdouble (period);
243 ret = (pos - per / 2.0) * ((2.0 * amp) / per);
250 waveform_rsaw_get (GstLFOControlSource * self, GstClockTime timestamp,
253 GstLFOControlSourcePrivate *priv = self->priv;
255 gst_object_sync_values (GST_OBJECT (self), timestamp);
256 g_mutex_lock (&self->lock);
257 *value = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
258 priv->period, priv->frequency, timestamp);
259 g_mutex_unlock (&self->lock);
264 waveform_rsaw_get_value_array (GstLFOControlSource * self,
265 GstClockTime timestamp, GstClockTime interval, guint n_values,
268 GstLFOControlSourcePrivate *priv = self->priv;
270 GstClockTime ts = timestamp;
272 for (i = 0; i < n_values; i++) {
273 gst_object_sync_values (GST_OBJECT (self), ts);
274 g_mutex_lock (&self->lock);
275 *values = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
276 priv->period, priv->frequency, ts);
277 g_mutex_unlock (&self->lock);
285 static inline gdouble
286 _triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
287 GstClockTime timeshift, GstClockTime period, gdouble frequency,
288 GstClockTime timestamp)
291 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
292 gdouble per = gst_guint64_to_gdouble (period);
295 if (pos <= 0.25 * per)
297 ret = pos * ((4.0 * amp) / per);
298 else if (pos <= 0.75 * per)
299 /* 2nd & 3rd quarter */
300 ret = -(pos - per / 2.0) * ((4.0 * amp) / per);
303 ret = -(per - pos) * ((4.0 * amp) / per);
311 waveform_triangle_get (GstLFOControlSource * self, GstClockTime timestamp,
314 GstLFOControlSourcePrivate *priv = self->priv;
316 gst_object_sync_values (GST_OBJECT (self), timestamp);
317 g_mutex_lock (&self->lock);
318 *value = _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
319 priv->period, priv->frequency, timestamp);
320 g_mutex_unlock (&self->lock);
325 waveform_triangle_get_value_array (GstLFOControlSource * self,
326 GstClockTime timestamp, GstClockTime interval, guint n_values,
329 GstLFOControlSourcePrivate *priv = self->priv;
331 GstClockTime ts = timestamp;
333 for (i = 0; i < n_values; i++) {
334 gst_object_sync_values (GST_OBJECT (self), ts);
335 g_mutex_lock (&self->lock);
337 _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
338 priv->period, priv->frequency, ts);
339 g_mutex_unlock (&self->lock);
348 GstControlSourceGetValue get;
349 GstControlSourceGetValueArray get_value_array;
352 (GstControlSourceGetValue) waveform_sine_get,
353 (GstControlSourceGetValueArray) waveform_sine_get_value_array}, {
354 (GstControlSourceGetValue) waveform_square_get,
355 (GstControlSourceGetValueArray) waveform_square_get_value_array}, {
356 (GstControlSourceGetValue) waveform_saw_get,
357 (GstControlSourceGetValueArray) waveform_saw_get_value_array}, {
358 (GstControlSourceGetValue) waveform_rsaw_get,
359 (GstControlSourceGetValueArray) waveform_rsaw_get_value_array}, {
360 (GstControlSourceGetValue) waveform_triangle_get,
361 (GstControlSourceGetValueArray) waveform_triangle_get_value_array}
364 static const guint num_waveforms = G_N_ELEMENTS (waveforms);
376 gst_lfo_waveform_get_type (void)
378 static gsize gtype = 0;
379 static const GEnumValue values[] = {
380 {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
382 {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
384 {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
386 {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
388 {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
393 if (g_once_init_enter (>ype)) {
394 GType tmp = g_enum_register_static ("GstLFOWaveform", values);
395 g_once_init_leave (>ype, tmp);
398 return (GType) gtype;
402 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "lfo control source", 0, "low frequency oscillator control source")
404 #define gst_lfo_control_source_parent_class parent_class
405 G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
406 GST_TYPE_CONTROL_SOURCE, _do_init);
409 gst_lfo_control_source_reset (GstLFOControlSource * self)
411 GstControlSource *csource = GST_CONTROL_SOURCE (self);
413 csource->get_value = NULL;
414 csource->get_value_array = NULL;
418 * gst_lfo_control_source_new:
420 * This returns a new, unbound #GstLFOControlSource.
422 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
425 gst_lfo_control_source_new (void)
427 return g_object_new (GST_TYPE_LFO_CONTROL_SOURCE, NULL);
431 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
432 GstLFOWaveform waveform)
434 GstControlSource *csource = GST_CONTROL_SOURCE (self);
436 if (waveform >= num_waveforms || (int) waveform < 0) {
437 GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
441 csource->get_value = waveforms[waveform].get;
442 csource->get_value_array = waveforms[waveform].get_value_array;
444 self->priv->waveform = waveform;
450 gst_lfo_control_source_init (GstLFOControlSource * self)
453 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
454 GstLFOControlSourcePrivate);
455 self->priv->waveform = gst_lfo_control_source_set_waveform (self,
456 GST_LFO_WAVEFORM_SINE);
457 self->priv->frequency = 1.0;
458 self->priv->amplitude = 1.0;
459 self->priv->period = GST_SECOND / self->priv->frequency;
460 self->priv->timeshift = 0;
462 g_mutex_init (&self->lock);
466 gst_lfo_control_source_finalize (GObject * obj)
468 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
470 gst_lfo_control_source_reset (self);
471 g_mutex_clear (&self->lock);
473 G_OBJECT_CLASS (parent_class)->finalize (obj);
477 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
478 const GValue * value, GParamSpec * pspec)
480 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
484 g_mutex_lock (&self->lock);
485 gst_lfo_control_source_set_waveform (self,
486 (GstLFOWaveform) g_value_get_enum (value));
487 g_mutex_unlock (&self->lock);
489 case PROP_FREQUENCY:{
490 gdouble frequency = g_value_get_double (value);
492 g_return_if_fail (((GstClockTime) (GST_SECOND / frequency)) != 0);
494 g_mutex_lock (&self->lock);
495 self->priv->frequency = frequency;
496 self->priv->period = GST_SECOND / frequency;
497 g_mutex_unlock (&self->lock);
501 g_mutex_lock (&self->lock);
502 self->priv->timeshift = g_value_get_uint64 (value);
503 g_mutex_unlock (&self->lock);
506 g_mutex_lock (&self->lock);
507 self->priv->amplitude = g_value_get_double (value);
508 g_mutex_unlock (&self->lock);
511 g_mutex_lock (&self->lock);
512 self->priv->offset = g_value_get_double (value);
513 g_mutex_unlock (&self->lock);
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
522 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
523 GValue * value, GParamSpec * pspec)
525 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
529 g_value_set_enum (value, self->priv->waveform);
532 g_value_set_double (value, self->priv->frequency);
535 g_value_set_uint64 (value, self->priv->timeshift);
538 g_value_set_double (value, self->priv->amplitude);
541 g_value_set_double (value, self->priv->offset);
544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
550 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
552 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
554 g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
556 gobject_class->finalize = gst_lfo_control_source_finalize;
557 gobject_class->set_property = gst_lfo_control_source_set_property;
558 gobject_class->get_property = gst_lfo_control_source_get_property;
561 * GstLFOControlSource:waveform:
563 * Specifies the waveform that should be used for this #GstLFOControlSource.
565 g_object_class_install_property (gobject_class, PROP_WAVEFORM,
566 g_param_spec_enum ("waveform", "Waveform", "Waveform",
567 GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
568 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 * GstLFOControlSource:frequency:
573 * Specifies the frequency that should be used for the waveform
574 * of this #GstLFOControlSource. It should be large enough
575 * so that the period is longer than one nanosecond.
577 g_object_class_install_property (gobject_class, PROP_FREQUENCY,
578 g_param_spec_double ("frequency", "Frequency",
579 "Frequency of the waveform", DBL_MIN, G_MAXDOUBLE, 1.0,
580 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
583 * GstLFOControlSource:timeshift:
585 * Specifies the timeshift to the right that should be used for the waveform
586 * of this #GstLFOControlSource in nanoseconds.
588 * To get a n nanosecond shift to the left use
589 * "(GST_SECOND / frequency) - n".
592 g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
593 g_param_spec_uint64 ("timeshift", "Timeshift",
594 "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
595 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
598 * GstLFOControlSource:amplitude:
600 * Specifies the amplitude for the waveform of this #GstLFOControlSource.
602 g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
603 g_param_spec_double ("amplitude", "Amplitude",
604 "Amplitude of the waveform", 0.0, 1.0, 1.0,
605 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
608 * GstLFOControlSource:offset:
610 * Specifies the value offset for the waveform of this #GstLFOControlSource.
612 g_object_class_install_property (gobject_class, PROP_OFFSET,
613 g_param_spec_double ("offset", "Offset", "Offset of the waveform",
615 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));