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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:gstlfocontrolsource
26 * @short_description: LFO control source
28 * #GstLFOControlSource is a #GstControlSource, that provides several periodic waveforms
29 * as control values. It supports all fundamental, numeric GValue types as property.
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.
35 * All functions are MT-safe.
39 #include <glib-object.h>
41 #include <gst/gstcontrolsource.h>
43 #include "gstlfocontrolsource.h"
45 #include "gst/glib-compat-private.h"
47 #include <gst/math-compat.h>
49 #define GST_CAT_DEFAULT controller_debug
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 struct _GstLFOControlSourcePrivate
54 GstLFOWaveform waveform;
57 GstClockTime timeshift;
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,
68 while (timestamp < timeshift)
71 timestamp -= timeshift;
73 return timestamp % period;
77 _sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
78 GstClockTime timeshift, GstClockTime period, gdouble frequency,
79 GstClockTime timestamp)
82 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
85 ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
93 waveform_sine_get (GstLFOControlSource * self, GstClockTime timestamp,
96 GstLFOControlSourcePrivate *priv = self->priv;
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);
107 waveform_sine_get_value_array (GstLFOControlSource * self,
108 GstClockTime timestamp, GstClockTime interval, guint n_values,
111 GstLFOControlSourcePrivate *priv = self->priv;
113 GstClockTime ts = timestamp;
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);
128 static inline gdouble
129 _square_get (GstLFOControlSource * self, gdouble amp, gdouble off,
130 GstClockTime timeshift, GstClockTime period, gdouble frequency,
131 GstClockTime timestamp)
133 GstClockTime pos = _calculate_pos (timestamp, timeshift, period);
136 if (pos >= period / 2)
146 waveform_square_get (GstLFOControlSource * self, GstClockTime timestamp,
149 GstLFOControlSourcePrivate *priv = self->priv;
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);
160 waveform_square_get_value_array (GstLFOControlSource * self,
161 GstClockTime timestamp, GstClockTime interval, guint n_values,
164 GstLFOControlSourcePrivate *priv = self->priv;
166 GstClockTime ts = timestamp;
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);
180 static inline gdouble
181 _saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
182 GstClockTime timeshift, GstClockTime period, gdouble frequency,
183 GstClockTime timestamp)
186 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
187 gdouble per = gst_guint64_to_gdouble (period);
190 ret = -((pos - per / 2.0) * ((2.0 * amp) / per));
197 waveform_saw_get (GstLFOControlSource * self, GstClockTime timestamp,
200 GstLFOControlSourcePrivate *priv = self->priv;
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);
211 waveform_saw_get_value_array (GstLFOControlSource * self,
212 GstClockTime timestamp, GstClockTime interval, guint n_values,
215 GstLFOControlSourcePrivate *priv = self->priv;
217 GstClockTime ts = timestamp;
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);
231 static inline gdouble
232 _rsaw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
233 GstClockTime timeshift, GstClockTime period, gdouble frequency,
234 GstClockTime timestamp)
237 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
238 gdouble per = gst_guint64_to_gdouble (period);
241 ret = (pos - per / 2.0) * ((2.0 * amp) / per);
248 waveform_rsaw_get (GstLFOControlSource * self, GstClockTime timestamp,
251 GstLFOControlSourcePrivate *priv = self->priv;
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);
262 waveform_rsaw_get_value_array (GstLFOControlSource * self,
263 GstClockTime timestamp, GstClockTime interval, guint n_values,
266 GstLFOControlSourcePrivate *priv = self->priv;
268 GstClockTime ts = timestamp;
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);
283 static inline gdouble
284 _triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
285 GstClockTime timeshift, GstClockTime period, gdouble frequency,
286 GstClockTime timestamp)
289 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
290 gdouble per = gst_guint64_to_gdouble (period);
293 if (pos <= 0.25 * per)
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);
301 ret = -(per - pos) * ((4.0 * amp) / per);
309 waveform_triangle_get (GstLFOControlSource * self, GstClockTime timestamp,
312 GstLFOControlSourcePrivate *priv = self->priv;
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);
323 waveform_triangle_get_value_array (GstLFOControlSource * self,
324 GstClockTime timestamp, GstClockTime interval, guint n_values,
327 GstLFOControlSourcePrivate *priv = self->priv;
329 GstClockTime ts = timestamp;
331 for (i = 0; i < n_values; i++) {
332 gst_object_sync_values (GST_OBJECT (self), ts);
333 g_mutex_lock (&self->lock);
335 _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
336 priv->period, priv->frequency, ts);
337 g_mutex_unlock (&self->lock);
346 GstControlSourceGetValue get;
347 GstControlSourceGetValueArray get_value_array;
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}
362 static const guint num_waveforms = G_N_ELEMENTS (waveforms);
374 gst_lfo_waveform_get_type (void)
376 static gsize gtype = 0;
377 static const GEnumValue values[] = {
378 {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
380 {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
382 {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
384 {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
386 {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
391 if (g_once_init_enter (>ype)) {
392 GType tmp = g_enum_register_static ("GstLFOWaveform", values);
393 g_once_init_leave (>ype, tmp);
396 return (GType) gtype;
400 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "lfo control source", 0, "low frequency oscillator control source")
402 G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
403 GST_TYPE_CONTROL_SOURCE, _do_init);
406 gst_lfo_control_source_reset (GstLFOControlSource * self)
408 GstControlSource *csource = GST_CONTROL_SOURCE (self);
410 csource->get_value = NULL;
411 csource->get_value_array = NULL;
415 * gst_lfo_control_source_new:
417 * This returns a new, unbound #GstLFOControlSource.
419 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
422 gst_lfo_control_source_new (void)
424 return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
428 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
429 GstLFOWaveform waveform)
431 GstControlSource *csource = GST_CONTROL_SOURCE (self);
433 if (waveform >= num_waveforms || (int) waveform < 0) {
434 GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
438 csource->get_value = waveforms[waveform].get;
439 csource->get_value_array = waveforms[waveform].get_value_array;
441 self->priv->waveform = waveform;
447 gst_lfo_control_source_init (GstLFOControlSource * self)
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;
458 g_mutex_init (&self->lock);
462 gst_lfo_control_source_finalize (GObject * obj)
464 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
466 gst_lfo_control_source_reset (self);
467 g_mutex_clear (&self->lock);
469 G_OBJECT_CLASS (gst_lfo_control_source_parent_class)->finalize (obj);
473 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
474 const GValue * value, GParamSpec * pspec)
476 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
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);
485 case PROP_FREQUENCY:{
486 gdouble frequency = g_value_get_double (value);
488 g_return_if_fail (frequency > 0
489 || ((GstClockTime) (GST_SECOND / frequency)) != 0);
491 g_mutex_lock (&self->lock);
492 self->priv->frequency = frequency;
493 self->priv->period = GST_SECOND / frequency;
494 g_mutex_unlock (&self->lock);
498 g_mutex_lock (&self->lock);
499 self->priv->timeshift = g_value_get_uint64 (value);
500 g_mutex_unlock (&self->lock);
503 g_mutex_lock (&self->lock);
504 self->priv->amplitude = g_value_get_double (value);
505 g_mutex_unlock (&self->lock);
508 g_mutex_lock (&self->lock);
509 self->priv->offset = g_value_get_double (value);
510 g_mutex_unlock (&self->lock);
513 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
519 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
520 GValue * value, GParamSpec * pspec)
522 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
526 g_value_set_enum (value, self->priv->waveform);
529 g_value_set_double (value, self->priv->frequency);
532 g_value_set_uint64 (value, self->priv->timeshift);
535 g_value_set_double (value, self->priv->amplitude);
538 g_value_set_double (value, self->priv->offset);
541 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
549 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
551 g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
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;
558 * GstLFOControlSource:waveform
560 * Specifies the waveform that should be used for this #GstLFOControlSource.
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));
568 * GstLFOControlSource:frequency
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.
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));
580 * GstLFOControlSource:timeshift
582 * Specifies the timeshift to the right that should be used for the waveform
583 * of this #GstLFOControlSource in nanoseconds.
585 * To get a n nanosecond shift to the left use
586 * "(GST_SECOND / frequency) - n".
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));
595 * GstLFOControlSource:amplitude
597 * Specifies the amplitude for the waveform of this #GstLFOControlSource.
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));
605 * GstLFOControlSource:offset
607 * Specifies the value offset for the waveform of this #GstLFOControlSource.
609 g_object_class_install_property (gobject_class, PROP_OFFSET,
610 g_param_spec_double ("offset", "Offset", "Offset of the waveform",
612 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));