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 #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);
407 gst_lfo_control_source_reset (GstLFOControlSource * self)
409 GstControlSource *csource = GST_CONTROL_SOURCE (self);
411 csource->get_value = NULL;
412 csource->get_value_array = NULL;
416 * gst_lfo_control_source_new:
418 * This returns a new, unbound #GstLFOControlSource.
420 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
423 gst_lfo_control_source_new (void)
425 return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
429 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
430 GstLFOWaveform waveform)
432 GstControlSource *csource = GST_CONTROL_SOURCE (self);
434 if (waveform >= num_waveforms || (int) waveform < 0) {
435 GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
439 csource->get_value = waveforms[waveform].get;
440 csource->get_value_array = waveforms[waveform].get_value_array;
442 self->priv->waveform = waveform;
448 gst_lfo_control_source_init (GstLFOControlSource * self)
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;
459 g_mutex_init (&self->lock);
463 gst_lfo_control_source_finalize (GObject * obj)
465 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
467 gst_lfo_control_source_reset (self);
468 g_mutex_clear (&self->lock);
470 G_OBJECT_CLASS (parent_class)->finalize (obj);
474 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
475 const GValue * value, GParamSpec * pspec)
477 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
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);
486 case PROP_FREQUENCY:{
487 gdouble frequency = g_value_get_double (value);
489 g_return_if_fail (frequency > 0
490 || ((GstClockTime) (GST_SECOND / frequency)) != 0);
492 g_mutex_lock (&self->lock);
493 self->priv->frequency = frequency;
494 self->priv->period = GST_SECOND / frequency;
495 g_mutex_unlock (&self->lock);
499 g_mutex_lock (&self->lock);
500 self->priv->timeshift = g_value_get_uint64 (value);
501 g_mutex_unlock (&self->lock);
504 g_mutex_lock (&self->lock);
505 self->priv->amplitude = g_value_get_double (value);
506 g_mutex_unlock (&self->lock);
509 g_mutex_lock (&self->lock);
510 self->priv->offset = g_value_get_double (value);
511 g_mutex_unlock (&self->lock);
514 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
520 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
521 GValue * value, GParamSpec * pspec)
523 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
527 g_value_set_enum (value, self->priv->waveform);
530 g_value_set_double (value, self->priv->frequency);
533 g_value_set_uint64 (value, self->priv->timeshift);
536 g_value_set_double (value, self->priv->amplitude);
539 g_value_set_double (value, self->priv->offset);
542 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
548 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
550 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
552 g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
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;
559 * GstLFOControlSource:waveform
561 * Specifies the waveform that should be used for this #GstLFOControlSource.
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));
569 * GstLFOControlSource:frequency
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.
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));
581 * GstLFOControlSource:timeshift
583 * Specifies the timeshift to the right that should be used for the waveform
584 * of this #GstLFOControlSource in nanoseconds.
586 * To get a n nanosecond shift to the left use
587 * "(GST_SECOND / frequency) - n".
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));
596 * GstLFOControlSource:amplitude
598 * Specifies the amplitude for the waveform of this #GstLFOControlSource.
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));
606 * GstLFOControlSource:offset
608 * Specifies the value offset for the waveform of this #GstLFOControlSource.
610 g_object_class_install_property (gobject_class, PROP_OFFSET,
611 g_param_spec_double ("offset", "Offset", "Offset of the waveform",
613 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));