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 * @short_description: LFO control source
28 * #GstLFOControlSource is a #GstControlSource, that provides several periodic
29 * waveforms as control values.
31 * To use #GstLFOControlSource get a new instance by calling
32 * gst_lfo_control_source_new(), bind it to a #GParamSpec and set the relevant
35 * All functions are MT-safe.
40 #include <glib-object.h>
42 #include <gst/gstcontrolsource.h>
44 #include "gstlfocontrolsource.h"
46 #include "gst/glib-compat-private.h"
48 #include <gst/math-compat.h>
50 #define GST_CAT_DEFAULT controller_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 struct _GstLFOControlSourcePrivate
55 GstLFOWaveform waveform;
58 GstClockTime timeshift;
63 /* FIXME: as % in C is not the modulo operator we need here for
64 * negative numbers implement our own. Are there better ways? */
65 static inline GstClockTime
66 _calculate_pos (GstClockTime timestamp, GstClockTime timeshift,
69 while (timestamp < timeshift)
72 timestamp -= timeshift;
74 return timestamp % period;
78 _sine_get (GstLFOControlSource * self, gdouble amp, gdouble off,
79 GstClockTime timeshift, GstClockTime period, gdouble frequency,
80 GstClockTime timestamp)
83 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
86 ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * pos);
94 waveform_sine_get (GstLFOControlSource * self, GstClockTime timestamp,
97 GstLFOControlSourcePrivate *priv = self->priv;
99 gst_object_sync_values (GST_OBJECT (self), timestamp);
100 g_mutex_lock (&self->lock);
101 *value = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
102 priv->period, priv->frequency, timestamp);
103 g_mutex_unlock (&self->lock);
108 waveform_sine_get_value_array (GstLFOControlSource * self,
109 GstClockTime timestamp, GstClockTime interval, guint n_values,
112 GstLFOControlSourcePrivate *priv = self->priv;
114 GstClockTime ts = timestamp;
116 for (i = 0; i < n_values; i++) {
117 gst_object_sync_values (GST_OBJECT (self), ts);
118 g_mutex_lock (&self->lock);
119 *values = _sine_get (self, priv->amplitude, priv->offset, priv->timeshift,
120 priv->period, priv->frequency, ts);
121 g_mutex_unlock (&self->lock);
129 static inline gdouble
130 _square_get (GstLFOControlSource * self, gdouble amp, gdouble off,
131 GstClockTime timeshift, GstClockTime period, gdouble frequency,
132 GstClockTime timestamp)
134 GstClockTime pos = _calculate_pos (timestamp, timeshift, period);
137 if (pos >= period / 2)
147 waveform_square_get (GstLFOControlSource * self, GstClockTime timestamp,
150 GstLFOControlSourcePrivate *priv = self->priv;
152 gst_object_sync_values (GST_OBJECT (self), timestamp);
153 g_mutex_lock (&self->lock);
154 *value = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
155 priv->period, priv->frequency, timestamp);
156 g_mutex_unlock (&self->lock);
161 waveform_square_get_value_array (GstLFOControlSource * self,
162 GstClockTime timestamp, GstClockTime interval, guint n_values,
165 GstLFOControlSourcePrivate *priv = self->priv;
167 GstClockTime ts = timestamp;
169 for (i = 0; i < n_values; i++) {
170 gst_object_sync_values (GST_OBJECT (self), ts);
171 g_mutex_lock (&self->lock);
172 *values = _square_get (self, priv->amplitude, priv->offset, priv->timeshift,
173 priv->period, priv->frequency, ts);
174 g_mutex_unlock (&self->lock);
181 static inline gdouble
182 _saw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
183 GstClockTime timeshift, GstClockTime period, gdouble frequency,
184 GstClockTime timestamp)
187 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
188 gdouble per = gst_guint64_to_gdouble (period);
191 ret = -((pos - per / 2.0) * ((2.0 * amp) / per));
198 waveform_saw_get (GstLFOControlSource * self, GstClockTime timestamp,
201 GstLFOControlSourcePrivate *priv = self->priv;
203 gst_object_sync_values (GST_OBJECT (self), timestamp);
204 g_mutex_lock (&self->lock);
205 *value = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
206 priv->period, priv->frequency, timestamp);
207 g_mutex_unlock (&self->lock);
212 waveform_saw_get_value_array (GstLFOControlSource * self,
213 GstClockTime timestamp, GstClockTime interval, guint n_values,
216 GstLFOControlSourcePrivate *priv = self->priv;
218 GstClockTime ts = timestamp;
220 for (i = 0; i < n_values; i++) {
221 gst_object_sync_values (GST_OBJECT (self), ts);
222 g_mutex_lock (&self->lock);
223 *values = _saw_get (self, priv->amplitude, priv->offset, priv->timeshift,
224 priv->period, priv->frequency, ts);
225 g_mutex_unlock (&self->lock);
232 static inline gdouble
233 _rsaw_get (GstLFOControlSource * self, gdouble amp, gdouble off,
234 GstClockTime timeshift, GstClockTime period, gdouble frequency,
235 GstClockTime timestamp)
238 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
239 gdouble per = gst_guint64_to_gdouble (period);
242 ret = (pos - per / 2.0) * ((2.0 * amp) / per);
249 waveform_rsaw_get (GstLFOControlSource * self, GstClockTime timestamp,
252 GstLFOControlSourcePrivate *priv = self->priv;
254 gst_object_sync_values (GST_OBJECT (self), timestamp);
255 g_mutex_lock (&self->lock);
256 *value = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
257 priv->period, priv->frequency, timestamp);
258 g_mutex_unlock (&self->lock);
263 waveform_rsaw_get_value_array (GstLFOControlSource * self,
264 GstClockTime timestamp, GstClockTime interval, guint n_values,
267 GstLFOControlSourcePrivate *priv = self->priv;
269 GstClockTime ts = timestamp;
271 for (i = 0; i < n_values; i++) {
272 gst_object_sync_values (GST_OBJECT (self), ts);
273 g_mutex_lock (&self->lock);
274 *values = _rsaw_get (self, priv->amplitude, priv->offset, priv->timeshift,
275 priv->period, priv->frequency, ts);
276 g_mutex_unlock (&self->lock);
284 static inline gdouble
285 _triangle_get (GstLFOControlSource * self, gdouble amp, gdouble off,
286 GstClockTime timeshift, GstClockTime period, gdouble frequency,
287 GstClockTime timestamp)
290 gst_guint64_to_gdouble (_calculate_pos (timestamp, timeshift, period));
291 gdouble per = gst_guint64_to_gdouble (period);
294 if (pos <= 0.25 * per)
296 ret = pos * ((4.0 * amp) / per);
297 else if (pos <= 0.75 * per)
298 /* 2nd & 3rd quarter */
299 ret = -(pos - per / 2.0) * ((4.0 * amp) / per);
302 ret = -(per - pos) * ((4.0 * amp) / per);
310 waveform_triangle_get (GstLFOControlSource * self, GstClockTime timestamp,
313 GstLFOControlSourcePrivate *priv = self->priv;
315 gst_object_sync_values (GST_OBJECT (self), timestamp);
316 g_mutex_lock (&self->lock);
317 *value = _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
318 priv->period, priv->frequency, timestamp);
319 g_mutex_unlock (&self->lock);
324 waveform_triangle_get_value_array (GstLFOControlSource * self,
325 GstClockTime timestamp, GstClockTime interval, guint n_values,
328 GstLFOControlSourcePrivate *priv = self->priv;
330 GstClockTime ts = timestamp;
332 for (i = 0; i < n_values; i++) {
333 gst_object_sync_values (GST_OBJECT (self), ts);
334 g_mutex_lock (&self->lock);
336 _triangle_get (self, priv->amplitude, priv->offset, priv->timeshift,
337 priv->period, priv->frequency, ts);
338 g_mutex_unlock (&self->lock);
347 GstControlSourceGetValue get;
348 GstControlSourceGetValueArray get_value_array;
351 (GstControlSourceGetValue) waveform_sine_get,
352 (GstControlSourceGetValueArray) waveform_sine_get_value_array}, {
353 (GstControlSourceGetValue) waveform_square_get,
354 (GstControlSourceGetValueArray) waveform_square_get_value_array}, {
355 (GstControlSourceGetValue) waveform_saw_get,
356 (GstControlSourceGetValueArray) waveform_saw_get_value_array}, {
357 (GstControlSourceGetValue) waveform_rsaw_get,
358 (GstControlSourceGetValueArray) waveform_rsaw_get_value_array}, {
359 (GstControlSourceGetValue) waveform_triangle_get,
360 (GstControlSourceGetValueArray) waveform_triangle_get_value_array}
363 static const guint num_waveforms = G_N_ELEMENTS (waveforms);
375 gst_lfo_waveform_get_type (void)
377 static gsize gtype = 0;
378 static const GEnumValue values[] = {
379 {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
381 {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
383 {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
385 {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
387 {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
392 if (g_once_init_enter (>ype)) {
393 GType tmp = g_enum_register_static ("GstLFOWaveform", values);
394 g_once_init_leave (>ype, tmp);
397 return (GType) gtype;
401 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "lfo control source", 0, "low frequency oscillator control source")
403 #define gst_lfo_control_source_parent_class parent_class
404 G_DEFINE_TYPE_WITH_CODE (GstLFOControlSource, gst_lfo_control_source,
405 GST_TYPE_CONTROL_SOURCE, _do_init);
408 gst_lfo_control_source_reset (GstLFOControlSource * self)
410 GstControlSource *csource = GST_CONTROL_SOURCE (self);
412 csource->get_value = NULL;
413 csource->get_value_array = NULL;
417 * gst_lfo_control_source_new:
419 * This returns a new, unbound #GstLFOControlSource.
421 * Returns: (transfer full): a new, unbound #GstLFOControlSource.
424 gst_lfo_control_source_new (void)
426 return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
430 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
431 GstLFOWaveform waveform)
433 GstControlSource *csource = GST_CONTROL_SOURCE (self);
435 if (waveform >= num_waveforms || (int) waveform < 0) {
436 GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
440 csource->get_value = waveforms[waveform].get;
441 csource->get_value_array = waveforms[waveform].get_value_array;
443 self->priv->waveform = waveform;
449 gst_lfo_control_source_init (GstLFOControlSource * self)
452 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
453 GstLFOControlSourcePrivate);
454 self->priv->waveform = gst_lfo_control_source_set_waveform (self,
455 GST_LFO_WAVEFORM_SINE);
456 self->priv->frequency = 1.0;
457 self->priv->amplitude = 1.0;
458 self->priv->period = GST_SECOND / self->priv->frequency;
459 self->priv->timeshift = 0;
461 g_mutex_init (&self->lock);
465 gst_lfo_control_source_finalize (GObject * obj)
467 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
469 gst_lfo_control_source_reset (self);
470 g_mutex_clear (&self->lock);
472 G_OBJECT_CLASS (parent_class)->finalize (obj);
476 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
477 const GValue * value, GParamSpec * pspec)
479 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
483 g_mutex_lock (&self->lock);
484 gst_lfo_control_source_set_waveform (self,
485 (GstLFOWaveform) g_value_get_enum (value));
486 g_mutex_unlock (&self->lock);
488 case PROP_FREQUENCY:{
489 gdouble frequency = g_value_get_double (value);
491 g_return_if_fail (((GstClockTime) (GST_SECOND / frequency)) != 0);
493 g_mutex_lock (&self->lock);
494 self->priv->frequency = frequency;
495 self->priv->period = GST_SECOND / frequency;
496 g_mutex_unlock (&self->lock);
500 g_mutex_lock (&self->lock);
501 self->priv->timeshift = g_value_get_uint64 (value);
502 g_mutex_unlock (&self->lock);
505 g_mutex_lock (&self->lock);
506 self->priv->amplitude = g_value_get_double (value);
507 g_mutex_unlock (&self->lock);
510 g_mutex_lock (&self->lock);
511 self->priv->offset = g_value_get_double (value);
512 g_mutex_unlock (&self->lock);
515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
522 GValue * value, GParamSpec * pspec)
524 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
528 g_value_set_enum (value, self->priv->waveform);
531 g_value_set_double (value, self->priv->frequency);
534 g_value_set_uint64 (value, self->priv->timeshift);
537 g_value_set_double (value, self->priv->amplitude);
540 g_value_set_double (value, self->priv->offset);
543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
549 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
551 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
553 g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
555 gobject_class->finalize = gst_lfo_control_source_finalize;
556 gobject_class->set_property = gst_lfo_control_source_set_property;
557 gobject_class->get_property = gst_lfo_control_source_get_property;
560 * GstLFOControlSource:waveform:
562 * Specifies the waveform that should be used for this #GstLFOControlSource.
564 g_object_class_install_property (gobject_class, PROP_WAVEFORM,
565 g_param_spec_enum ("waveform", "Waveform", "Waveform",
566 GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 * GstLFOControlSource:frequency:
572 * Specifies the frequency that should be used for the waveform
573 * of this #GstLFOControlSource. It should be large enough
574 * so that the period is longer than one nanosecond.
576 g_object_class_install_property (gobject_class, PROP_FREQUENCY,
577 g_param_spec_double ("frequency", "Frequency",
578 "Frequency of the waveform", DBL_MIN, G_MAXDOUBLE, 1.0,
579 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
582 * GstLFOControlSource:timeshift:
584 * Specifies the timeshift to the right that should be used for the waveform
585 * of this #GstLFOControlSource in nanoseconds.
587 * To get a n nanosecond shift to the left use
588 * "(GST_SECOND / frequency) - n".
591 g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
592 g_param_spec_uint64 ("timeshift", "Timeshift",
593 "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
594 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
597 * GstLFOControlSource:amplitude:
599 * Specifies the amplitude for the waveform of this #GstLFOControlSource.
601 g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
602 g_param_spec_double ("amplitude", "Amplitude",
603 "Amplitude of the waveform", 0.0, 1.0, 1.0,
604 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
607 * GstLFOControlSource:offset:
609 * Specifies the value offset for the waveform of this #GstLFOControlSource.
611 g_object_class_install_property (gobject_class, PROP_OFFSET,
612 g_param_spec_double ("offset", "Offset", "Offset of the waveform",
614 G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));