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>
42 #include "gstcontrolsource.h"
43 #include "gstlfocontrolsource.h"
44 #include "gstlfocontrolsourceprivate.h"
46 #include "gst/glib-compat-private.h"
48 #include <gst/math-compat.h>
52 /* FIXME: as % in C is not the modulo operator we need here for
53 * negative numbers implement our own. Are there better ways? */
54 static inline GstClockTime
55 _calculate_pos (GstClockTime timestamp, GstClockTime timeshift,
58 while (timestamp < timeshift)
61 timestamp -= timeshift;
63 return timestamp % period;
66 #define DEFINE_SINE(type,round,convert) \
67 static inline g##type \
68 _sine_get_##type (GstLFOControlSource *self, g##type max, g##type min, gdouble amp, gdouble off, GstClockTime timeshift, GstClockTime period, gdouble frequency, GstClockTime timestamp) \
71 GstClockTime pos = _calculate_pos (timestamp, timeshift, period); \
73 ret = sin (2.0 * M_PI * (frequency / GST_SECOND) * gst_guint64_to_gdouble (pos)); \
80 return (g##type) CLAMP (ret, convert (min), convert (max)); \
84 waveform_sine_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \
87 g##type ret, max, min; \
88 gdouble amp, off, frequency; \
89 GstClockTime timeshift, period; \
91 g_mutex_lock (self->lock); \
92 max = g_value_get_##type (&self->priv->maximum_value); \
93 min = g_value_get_##type (&self->priv->minimum_value); \
94 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
95 off = convert (g_value_get_##type (&self->priv->offset)); \
96 timeshift = self->priv->timeshift; \
97 period = self->priv->period; \
98 frequency = self->priv->frequency; \
100 ret = _sine_get_##type (self, max, min, amp, off, timeshift, period, frequency, timestamp); \
101 g_value_set_##type (value, ret); \
102 g_mutex_unlock (self->lock); \
107 waveform_sine_get_##type##_value_array (GstLFOControlSource *self, \
108 GstClockTime timestamp, GstValueArray * value_array) \
111 GstClockTime ts = timestamp; \
112 g##type *values = (g##type *) value_array->values; \
114 gdouble amp, off, frequency; \
115 GstClockTime timeshift, period; \
117 g_mutex_lock (self->lock); \
118 max = g_value_get_##type (&self->priv->maximum_value); \
119 min = g_value_get_##type (&self->priv->minimum_value); \
120 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
121 off = convert (g_value_get_##type (&self->priv->offset)); \
122 timeshift = self->priv->timeshift; \
123 period = self->priv->period; \
124 frequency = self->priv->frequency; \
126 for(i = 0; i < value_array->nbsamples; i++) { \
127 *values = _sine_get_##type (self, max, min, amp, off, timeshift, period, frequency, ts); \
128 ts += value_array->sample_interval; \
131 g_mutex_unlock (self->lock); \
135 DEFINE_SINE (int, TRUE, EMPTY);
136 DEFINE_SINE (uint, TRUE, EMPTY);
137 DEFINE_SINE (long, TRUE, EMPTY);
138 DEFINE_SINE (ulong, TRUE, EMPTY);
139 DEFINE_SINE (int64, TRUE, EMPTY);
140 DEFINE_SINE (uint64, TRUE, gst_guint64_to_gdouble);
141 DEFINE_SINE (float, FALSE, EMPTY);
142 DEFINE_SINE (double, FALSE, EMPTY);
144 static GstWaveformImplementation waveform_sine = {
145 (GstControlSourceGetValue) waveform_sine_get_int,
146 (GstControlSourceGetValueArray) waveform_sine_get_int_value_array,
147 (GstControlSourceGetValue) waveform_sine_get_uint,
148 (GstControlSourceGetValueArray) waveform_sine_get_uint_value_array,
149 (GstControlSourceGetValue) waveform_sine_get_long,
150 (GstControlSourceGetValueArray) waveform_sine_get_long_value_array,
151 (GstControlSourceGetValue) waveform_sine_get_ulong,
152 (GstControlSourceGetValueArray) waveform_sine_get_ulong_value_array,
153 (GstControlSourceGetValue) waveform_sine_get_int64,
154 (GstControlSourceGetValueArray) waveform_sine_get_int64_value_array,
155 (GstControlSourceGetValue) waveform_sine_get_uint64,
156 (GstControlSourceGetValueArray) waveform_sine_get_uint64_value_array,
157 (GstControlSourceGetValue) waveform_sine_get_float,
158 (GstControlSourceGetValueArray) waveform_sine_get_float_value_array,
159 (GstControlSourceGetValue) waveform_sine_get_double,
160 (GstControlSourceGetValueArray) waveform_sine_get_double_value_array
163 #define DEFINE_SQUARE(type,round, convert) \
165 static inline g##type \
166 _square_get_##type (GstLFOControlSource *self, g##type max, g##type min, gdouble amp, gdouble off, GstClockTime timeshift, GstClockTime period, gdouble frequency, GstClockTime timestamp) \
168 GstClockTime pos = _calculate_pos (timestamp, timeshift, period); \
171 if (pos >= period / 2) \
181 return (g##type) CLAMP (ret, convert (min), convert (max)); \
185 waveform_square_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \
188 g##type ret, max, min; \
189 gdouble amp, off, frequency; \
190 GstClockTime timeshift, period; \
192 g_mutex_lock (self->lock); \
193 max = g_value_get_##type (&self->priv->maximum_value); \
194 min = g_value_get_##type (&self->priv->minimum_value); \
195 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
196 off = convert (g_value_get_##type (&self->priv->offset)); \
197 timeshift = self->priv->timeshift; \
198 period = self->priv->period; \
199 frequency = self->priv->frequency; \
201 ret = _square_get_##type (self, max, min, amp, off, timeshift, period, frequency, timestamp); \
202 g_value_set_##type (value, ret); \
203 g_mutex_unlock (self->lock); \
208 waveform_square_get_##type##_value_array (GstLFOControlSource *self, \
209 GstClockTime timestamp, GstValueArray * value_array) \
212 GstClockTime ts = timestamp; \
213 g##type *values = (g##type *) value_array->values; \
215 gdouble amp, off, frequency; \
216 GstClockTime timeshift, period; \
218 g_mutex_lock (self->lock); \
219 max = g_value_get_##type (&self->priv->maximum_value); \
220 min = g_value_get_##type (&self->priv->minimum_value); \
221 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
222 off = convert (g_value_get_##type (&self->priv->offset)); \
223 timeshift = self->priv->timeshift; \
224 period = self->priv->period; \
225 frequency = self->priv->frequency; \
227 for(i = 0; i < value_array->nbsamples; i++) { \
228 *values = _square_get_##type (self, max, min, amp, off, timeshift, period, frequency, ts); \
229 ts += value_array->sample_interval; \
232 g_mutex_unlock (self->lock); \
236 DEFINE_SQUARE (int, TRUE, EMPTY);
237 DEFINE_SQUARE (uint, TRUE, EMPTY);
238 DEFINE_SQUARE (long, TRUE, EMPTY);
239 DEFINE_SQUARE (ulong, TRUE, EMPTY);
240 DEFINE_SQUARE (int64, TRUE, EMPTY);
241 DEFINE_SQUARE (uint64, TRUE, gst_guint64_to_gdouble);
242 DEFINE_SQUARE (float, FALSE, EMPTY);
243 DEFINE_SQUARE (double, FALSE, EMPTY);
245 static GstWaveformImplementation waveform_square = {
246 (GstControlSourceGetValue) waveform_square_get_int,
247 (GstControlSourceGetValueArray) waveform_square_get_int_value_array,
248 (GstControlSourceGetValue) waveform_square_get_uint,
249 (GstControlSourceGetValueArray) waveform_square_get_uint_value_array,
250 (GstControlSourceGetValue) waveform_square_get_long,
251 (GstControlSourceGetValueArray) waveform_square_get_long_value_array,
252 (GstControlSourceGetValue) waveform_square_get_ulong,
253 (GstControlSourceGetValueArray) waveform_square_get_ulong_value_array,
254 (GstControlSourceGetValue) waveform_square_get_int64,
255 (GstControlSourceGetValueArray) waveform_square_get_int64_value_array,
256 (GstControlSourceGetValue) waveform_square_get_uint64,
257 (GstControlSourceGetValueArray) waveform_square_get_uint64_value_array,
258 (GstControlSourceGetValue) waveform_square_get_float,
259 (GstControlSourceGetValueArray) waveform_square_get_float_value_array,
260 (GstControlSourceGetValue) waveform_square_get_double,
261 (GstControlSourceGetValueArray) waveform_square_get_double_value_array
264 #define DEFINE_SAW(type,round,convert) \
266 static inline g##type \
267 _saw_get_##type (GstLFOControlSource *self, g##type max, g##type min, gdouble amp, gdouble off, GstClockTime timeshift, GstClockTime period, gdouble frequency, GstClockTime timestamp) \
269 GstClockTime pos = _calculate_pos (timestamp, timeshift, period); \
272 ret = - ((gst_guint64_to_gdouble (pos) - gst_guint64_to_gdouble (period) / 2.0) * ((2.0 * amp) / gst_guint64_to_gdouble (period)));\
279 return (g##type) CLAMP (ret, convert (min), convert (max)); \
283 waveform_saw_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \
286 g##type ret, max, min; \
287 gdouble amp, off, frequency; \
288 GstClockTime timeshift, period; \
290 g_mutex_lock (self->lock); \
291 max = g_value_get_##type (&self->priv->maximum_value); \
292 min = g_value_get_##type (&self->priv->minimum_value); \
293 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
294 off = convert (g_value_get_##type (&self->priv->offset)); \
295 timeshift = self->priv->timeshift; \
296 period = self->priv->period; \
297 frequency = self->priv->frequency; \
299 ret = _saw_get_##type (self, max, min, amp, off, timeshift, period, frequency, timestamp); \
300 g_value_set_##type (value, ret); \
301 g_mutex_unlock (self->lock); \
306 waveform_saw_get_##type##_value_array (GstLFOControlSource *self, \
307 GstClockTime timestamp, GstValueArray * value_array) \
310 GstClockTime ts = timestamp; \
311 g##type *values = (g##type *) value_array->values; \
313 gdouble amp, off, frequency; \
314 GstClockTime timeshift, period; \
316 g_mutex_lock (self->lock); \
317 max = g_value_get_##type (&self->priv->maximum_value); \
318 min = g_value_get_##type (&self->priv->minimum_value); \
319 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
320 off = convert (g_value_get_##type (&self->priv->offset)); \
321 timeshift = self->priv->timeshift; \
322 period = self->priv->period; \
323 frequency = self->priv->frequency; \
325 for(i = 0; i < value_array->nbsamples; i++) { \
326 *values = _saw_get_##type (self, max, min, amp, off, timeshift, period, frequency, ts); \
327 ts += value_array->sample_interval; \
330 g_mutex_unlock (self->lock); \
334 DEFINE_SAW (int, TRUE, EMPTY);
335 DEFINE_SAW (uint, TRUE, EMPTY);
336 DEFINE_SAW (long, TRUE, EMPTY);
337 DEFINE_SAW (ulong, TRUE, EMPTY);
338 DEFINE_SAW (int64, TRUE, EMPTY);
339 DEFINE_SAW (uint64, TRUE, gst_guint64_to_gdouble);
340 DEFINE_SAW (float, FALSE, EMPTY);
341 DEFINE_SAW (double, FALSE, EMPTY);
343 static GstWaveformImplementation waveform_saw = {
344 (GstControlSourceGetValue) waveform_saw_get_int,
345 (GstControlSourceGetValueArray) waveform_saw_get_int_value_array,
346 (GstControlSourceGetValue) waveform_saw_get_uint,
347 (GstControlSourceGetValueArray) waveform_saw_get_uint_value_array,
348 (GstControlSourceGetValue) waveform_saw_get_long,
349 (GstControlSourceGetValueArray) waveform_saw_get_long_value_array,
350 (GstControlSourceGetValue) waveform_saw_get_ulong,
351 (GstControlSourceGetValueArray) waveform_saw_get_ulong_value_array,
352 (GstControlSourceGetValue) waveform_saw_get_int64,
353 (GstControlSourceGetValueArray) waveform_saw_get_int64_value_array,
354 (GstControlSourceGetValue) waveform_saw_get_uint64,
355 (GstControlSourceGetValueArray) waveform_saw_get_uint64_value_array,
356 (GstControlSourceGetValue) waveform_saw_get_float,
357 (GstControlSourceGetValueArray) waveform_saw_get_float_value_array,
358 (GstControlSourceGetValue) waveform_saw_get_double,
359 (GstControlSourceGetValueArray) waveform_saw_get_double_value_array
362 #define DEFINE_RSAW(type,round,convert) \
364 static inline g##type \
365 _rsaw_get_##type (GstLFOControlSource *self, g##type max, g##type min, gdouble amp, gdouble off, GstClockTime timeshift, GstClockTime period, gdouble frequency, GstClockTime timestamp) \
367 GstClockTime pos = _calculate_pos (timestamp, timeshift, period); \
370 ret = ((gst_guint64_to_gdouble (pos) - gst_guint64_to_gdouble (period) / 2.0) * ((2.0 * amp) / gst_guint64_to_gdouble (period)));\
377 return (g##type) CLAMP (ret, convert (min), convert (max)); \
381 waveform_rsaw_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \
384 g##type ret, max, min; \
385 gdouble amp, off, frequency; \
386 GstClockTime timeshift, period; \
388 g_mutex_lock (self->lock); \
389 max = g_value_get_##type (&self->priv->maximum_value); \
390 min = g_value_get_##type (&self->priv->minimum_value); \
391 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
392 off = convert (g_value_get_##type (&self->priv->offset)); \
393 timeshift = self->priv->timeshift; \
394 period = self->priv->period; \
395 frequency = self->priv->frequency; \
397 ret = _rsaw_get_##type (self, max, min, amp, off, timeshift, period, frequency, timestamp); \
398 g_value_set_##type (value, ret); \
399 g_mutex_unlock (self->lock); \
404 waveform_rsaw_get_##type##_value_array (GstLFOControlSource *self, \
405 GstClockTime timestamp, GstValueArray * value_array) \
408 GstClockTime ts = timestamp; \
409 g##type *values = (g##type *) value_array->values; \
411 gdouble amp, off, frequency; \
412 GstClockTime timeshift, period; \
414 g_mutex_lock (self->lock); \
415 max = g_value_get_##type (&self->priv->maximum_value); \
416 min = g_value_get_##type (&self->priv->minimum_value); \
417 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
418 off = convert (g_value_get_##type (&self->priv->offset)); \
419 timeshift = self->priv->timeshift; \
420 period = self->priv->period; \
421 frequency = self->priv->frequency; \
423 for(i = 0; i < value_array->nbsamples; i++) { \
424 *values = _rsaw_get_##type (self, max, min, amp, off, timeshift, period, frequency, ts); \
425 ts += value_array->sample_interval; \
428 g_mutex_unlock (self->lock); \
432 DEFINE_RSAW (int, TRUE, EMPTY);
433 DEFINE_RSAW (uint, TRUE, EMPTY);
434 DEFINE_RSAW (long, TRUE, EMPTY);
435 DEFINE_RSAW (ulong, TRUE, EMPTY);
436 DEFINE_RSAW (int64, TRUE, EMPTY);
437 DEFINE_RSAW (uint64, TRUE, gst_guint64_to_gdouble);
438 DEFINE_RSAW (float, FALSE, EMPTY);
439 DEFINE_RSAW (double, FALSE, EMPTY);
441 static GstWaveformImplementation waveform_rsaw = {
442 (GstControlSourceGetValue) waveform_rsaw_get_int,
443 (GstControlSourceGetValueArray) waveform_rsaw_get_int_value_array,
444 (GstControlSourceGetValue) waveform_rsaw_get_uint,
445 (GstControlSourceGetValueArray) waveform_rsaw_get_uint_value_array,
446 (GstControlSourceGetValue) waveform_rsaw_get_long,
447 (GstControlSourceGetValueArray) waveform_rsaw_get_long_value_array,
448 (GstControlSourceGetValue) waveform_rsaw_get_ulong,
449 (GstControlSourceGetValueArray) waveform_rsaw_get_ulong_value_array,
450 (GstControlSourceGetValue) waveform_rsaw_get_int64,
451 (GstControlSourceGetValueArray) waveform_rsaw_get_int64_value_array,
452 (GstControlSourceGetValue) waveform_rsaw_get_uint64,
453 (GstControlSourceGetValueArray) waveform_rsaw_get_uint64_value_array,
454 (GstControlSourceGetValue) waveform_rsaw_get_float,
455 (GstControlSourceGetValueArray) waveform_rsaw_get_float_value_array,
456 (GstControlSourceGetValue) waveform_rsaw_get_double,
457 (GstControlSourceGetValueArray) waveform_rsaw_get_double_value_array
460 #define DEFINE_TRIANGLE(type,round,convert) \
462 static inline g##type \
463 _triangle_get_##type (GstLFOControlSource *self, g##type max, g##type min, gdouble amp, gdouble off, GstClockTime timeshift, GstClockTime period, gdouble frequency, GstClockTime timestamp) \
465 GstClockTime pos = _calculate_pos (timestamp, timeshift, period); \
468 if (gst_guint64_to_gdouble (pos) <= gst_guint64_to_gdouble (period) / 4.0) \
469 ret = gst_guint64_to_gdouble (pos) * ((4.0 * amp) / gst_guint64_to_gdouble (period)); \
470 else if (gst_guint64_to_gdouble (pos) <= (3.0 * gst_guint64_to_gdouble (period)) / 4.0) \
471 ret = -(gst_guint64_to_gdouble (pos) - gst_guint64_to_gdouble (period) / 2.0) * ((4.0 * amp) / gst_guint64_to_gdouble (period)); \
473 ret = gst_guint64_to_gdouble (period) - gst_guint64_to_gdouble (pos) * ((4.0 * amp) / gst_guint64_to_gdouble (period)); \
480 return (g##type) CLAMP (ret, convert (min), convert (max)); \
484 waveform_triangle_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \
487 g##type ret, max, min; \
488 gdouble amp, off, frequency; \
489 GstClockTime timeshift, period; \
491 g_mutex_lock (self->lock); \
492 max = g_value_get_##type (&self->priv->maximum_value); \
493 min = g_value_get_##type (&self->priv->minimum_value); \
494 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
495 off = convert (g_value_get_##type (&self->priv->offset)); \
496 timeshift = self->priv->timeshift; \
497 period = self->priv->period; \
498 frequency = self->priv->frequency; \
500 ret = _triangle_get_##type (self, max, min, amp, off, timeshift, period, frequency, timestamp); \
501 g_value_set_##type (value, ret); \
502 g_mutex_unlock (self->lock); \
507 waveform_triangle_get_##type##_value_array (GstLFOControlSource *self, \
508 GstClockTime timestamp, GstValueArray * value_array) \
511 GstClockTime ts = timestamp; \
512 g##type *values = (g##type *) value_array->values; \
514 gdouble amp, off, frequency; \
515 GstClockTime timeshift, period; \
517 g_mutex_lock (self->lock); \
518 max = g_value_get_##type (&self->priv->maximum_value); \
519 min = g_value_get_##type (&self->priv->minimum_value); \
520 amp = convert (g_value_get_##type (&self->priv->amplitude)); \
521 off = convert (g_value_get_##type (&self->priv->offset)); \
522 timeshift = self->priv->timeshift; \
523 period = self->priv->period; \
524 frequency = self->priv->frequency; \
526 for(i = 0; i < value_array->nbsamples; i++) { \
527 *values = _triangle_get_##type (self, max, min, amp, off, timeshift, period, frequency, ts); \
528 ts += value_array->sample_interval; \
531 g_mutex_unlock (self->lock); \
535 DEFINE_TRIANGLE (int, TRUE, EMPTY);
536 DEFINE_TRIANGLE (uint, TRUE, EMPTY);
537 DEFINE_TRIANGLE (long, TRUE, EMPTY);
538 DEFINE_TRIANGLE (ulong, TRUE, EMPTY);
539 DEFINE_TRIANGLE (int64, TRUE, EMPTY);
540 DEFINE_TRIANGLE (uint64, TRUE, gst_guint64_to_gdouble);
541 DEFINE_TRIANGLE (float, FALSE, EMPTY);
542 DEFINE_TRIANGLE (double, FALSE, EMPTY);
544 static GstWaveformImplementation waveform_triangle = {
545 (GstControlSourceGetValue) waveform_triangle_get_int,
546 (GstControlSourceGetValueArray) waveform_triangle_get_int_value_array,
547 (GstControlSourceGetValue) waveform_triangle_get_uint,
548 (GstControlSourceGetValueArray) waveform_triangle_get_uint_value_array,
549 (GstControlSourceGetValue) waveform_triangle_get_long,
550 (GstControlSourceGetValueArray) waveform_triangle_get_long_value_array,
551 (GstControlSourceGetValue) waveform_triangle_get_ulong,
552 (GstControlSourceGetValueArray) waveform_triangle_get_ulong_value_array,
553 (GstControlSourceGetValue) waveform_triangle_get_int64,
554 (GstControlSourceGetValueArray) waveform_triangle_get_int64_value_array,
555 (GstControlSourceGetValue) waveform_triangle_get_uint64,
556 (GstControlSourceGetValueArray) waveform_triangle_get_uint64_value_array,
557 (GstControlSourceGetValue) waveform_triangle_get_float,
558 (GstControlSourceGetValueArray) waveform_triangle_get_float_value_array,
559 (GstControlSourceGetValue) waveform_triangle_get_double,
560 (GstControlSourceGetValueArray) waveform_triangle_get_double_value_array
563 static GstWaveformImplementation *waveforms[] = {
571 static const guint num_waveforms = G_N_ELEMENTS (waveforms);
583 gst_lfo_waveform_get_type (void)
585 static gsize gtype = 0;
586 static const GEnumValue values[] = {
587 {GST_LFO_WAVEFORM_SINE, "GST_LFO_WAVEFORM_SINE",
589 {GST_LFO_WAVEFORM_SQUARE, "GST_LFO_WAVEFORM_SQUARE",
591 {GST_LFO_WAVEFORM_SAW, "GST_LFO_WAVEFORM_SAW",
593 {GST_LFO_WAVEFORM_REVERSE_SAW, "GST_LFO_WAVEFORM_REVERSE_SAW",
595 {GST_LFO_WAVEFORM_TRIANGLE, "GST_LFO_WAVEFORM_TRIANGLE",
600 if (g_once_init_enter (>ype)) {
601 GType tmp = g_enum_register_static ("GstLFOWaveform", values);
602 g_once_init_leave (>ype, tmp);
605 return (GType) gtype;
608 G_DEFINE_TYPE (GstLFOControlSource, gst_lfo_control_source,
609 GST_TYPE_CONTROL_SOURCE);
611 static GObjectClass *parent_class = NULL;
614 gst_lfo_control_source_reset (GstLFOControlSource * self)
616 GstControlSource *csource = GST_CONTROL_SOURCE (self);
618 csource->get_value = NULL;
619 csource->get_value_array = NULL;
621 self->priv->type = self->priv->base = G_TYPE_INVALID;
623 if (G_IS_VALUE (&self->priv->minimum_value))
624 g_value_unset (&self->priv->minimum_value);
625 if (G_IS_VALUE (&self->priv->maximum_value))
626 g_value_unset (&self->priv->maximum_value);
628 if (G_IS_VALUE (&self->priv->amplitude))
629 g_value_unset (&self->priv->amplitude);
630 if (G_IS_VALUE (&self->priv->offset))
631 g_value_unset (&self->priv->offset);
635 * gst_lfo_control_source_new:
637 * This returns a new, unbound #GstLFOControlSource.
639 * Returns: a new, unbound #GstLFOControlSource.
641 GstLFOControlSource *
642 gst_lfo_control_source_new (void)
644 return g_object_newv (GST_TYPE_LFO_CONTROL_SOURCE, 0, NULL);
648 gst_lfo_control_source_set_waveform (GstLFOControlSource * self,
649 GstLFOWaveform waveform)
651 GstControlSource *csource = GST_CONTROL_SOURCE (self);
654 if (waveform >= num_waveforms || (int) waveform < 0) {
655 GST_WARNING ("waveform %d invalid or not implemented yet", waveform);
659 if (self->priv->base == G_TYPE_INVALID) {
660 GST_WARNING ("not bound to a property yet");
664 switch (self->priv->base) {
666 csource->get_value = waveforms[waveform]->get_int;
667 csource->get_value_array = waveforms[waveform]->get_int_value_array;
670 csource->get_value = waveforms[waveform]->get_uint;
671 csource->get_value_array = waveforms[waveform]->get_uint_value_array;
675 csource->get_value = waveforms[waveform]->get_long;
676 csource->get_value_array = waveforms[waveform]->get_long_value_array;
680 csource->get_value = waveforms[waveform]->get_ulong;
681 csource->get_value_array = waveforms[waveform]->get_ulong_value_array;
685 csource->get_value = waveforms[waveform]->get_int64;
686 csource->get_value_array = waveforms[waveform]->get_int64_value_array;
690 csource->get_value = waveforms[waveform]->get_uint64;
691 csource->get_value_array = waveforms[waveform]->get_uint64_value_array;
695 csource->get_value = waveforms[waveform]->get_float;
696 csource->get_value_array = waveforms[waveform]->get_float_value_array;
700 csource->get_value = waveforms[waveform]->get_double;
701 csource->get_value_array = waveforms[waveform]->get_double_value_array;
710 self->priv->waveform = waveform;
712 GST_WARNING ("incomplete implementation for type '%s'",
713 GST_STR_NULL (g_type_name (self->priv->type)));
719 gst_lfo_control_source_bind (GstControlSource * source, GParamSpec * pspec)
722 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (source);
725 /* get the fundamental base type */
726 self->priv->type = base = type = G_PARAM_SPEC_VALUE_TYPE (pspec);
727 while ((type = g_type_parent (type)))
730 self->priv->base = base;
732 type = self->priv->type;
736 GParamSpecInt *tpspec = G_PARAM_SPEC_INT (pspec);
738 g_value_init (&self->priv->minimum_value, type);
739 g_value_set_int (&self->priv->minimum_value, tpspec->minimum);
740 g_value_init (&self->priv->maximum_value, type);
741 g_value_set_int (&self->priv->maximum_value, tpspec->maximum);
743 if (!G_IS_VALUE (&self->priv->amplitude)) {
744 g_value_init (&self->priv->amplitude, type);
745 g_value_set_int (&self->priv->amplitude, 0);
748 if (!G_IS_VALUE (&self->priv->offset)) {
749 g_value_init (&self->priv->offset, type);
750 g_value_set_int (&self->priv->offset, tpspec->default_value);
755 GParamSpecUInt *tpspec = G_PARAM_SPEC_UINT (pspec);
757 g_value_init (&self->priv->minimum_value, type);
758 g_value_set_uint (&self->priv->minimum_value, tpspec->minimum);
759 g_value_init (&self->priv->maximum_value, type);
760 g_value_set_uint (&self->priv->maximum_value, tpspec->maximum);
762 if (!G_IS_VALUE (&self->priv->amplitude)) {
763 g_value_init (&self->priv->amplitude, type);
764 g_value_set_uint (&self->priv->amplitude, 0);
767 if (!G_IS_VALUE (&self->priv->offset)) {
768 g_value_init (&self->priv->offset, type);
769 g_value_set_uint (&self->priv->offset, tpspec->default_value);
774 GParamSpecLong *tpspec = G_PARAM_SPEC_LONG (pspec);
776 g_value_init (&self->priv->minimum_value, type);
777 g_value_set_long (&self->priv->minimum_value, tpspec->minimum);
778 g_value_init (&self->priv->maximum_value, type);
779 g_value_set_long (&self->priv->maximum_value, tpspec->maximum);
780 if (!G_IS_VALUE (&self->priv->amplitude)) {
781 g_value_init (&self->priv->amplitude, type);
782 g_value_set_long (&self->priv->amplitude, 0);
785 if (!G_IS_VALUE (&self->priv->offset)) {
786 g_value_init (&self->priv->offset, type);
787 g_value_set_long (&self->priv->offset, tpspec->default_value);
792 GParamSpecULong *tpspec = G_PARAM_SPEC_ULONG (pspec);
794 g_value_init (&self->priv->minimum_value, type);
795 g_value_set_ulong (&self->priv->minimum_value, tpspec->minimum);
796 g_value_init (&self->priv->maximum_value, type);
797 g_value_set_ulong (&self->priv->maximum_value, tpspec->maximum);
798 if (!G_IS_VALUE (&self->priv->amplitude)) {
799 g_value_init (&self->priv->amplitude, type);
800 g_value_set_ulong (&self->priv->amplitude, 0);
803 if (!G_IS_VALUE (&self->priv->offset)) {
804 g_value_init (&self->priv->offset, type);
805 g_value_set_ulong (&self->priv->offset, tpspec->default_value);
810 GParamSpecInt64 *tpspec = G_PARAM_SPEC_INT64 (pspec);
812 g_value_init (&self->priv->minimum_value, type);
813 g_value_set_int64 (&self->priv->minimum_value, tpspec->minimum);
814 g_value_init (&self->priv->maximum_value, type);
815 g_value_set_int64 (&self->priv->maximum_value, tpspec->maximum);
816 if (!G_IS_VALUE (&self->priv->amplitude)) {
817 g_value_init (&self->priv->amplitude, type);
818 g_value_set_int64 (&self->priv->amplitude, 0);
821 if (!G_IS_VALUE (&self->priv->offset)) {
822 g_value_init (&self->priv->offset, type);
823 g_value_set_int64 (&self->priv->offset, tpspec->default_value);
828 GParamSpecUInt64 *tpspec = G_PARAM_SPEC_UINT64 (pspec);
830 g_value_init (&self->priv->minimum_value, type);
831 g_value_set_uint64 (&self->priv->minimum_value, tpspec->minimum);
832 g_value_init (&self->priv->maximum_value, type);
833 g_value_set_uint64 (&self->priv->maximum_value, tpspec->maximum);
834 if (!G_IS_VALUE (&self->priv->amplitude)) {
835 g_value_init (&self->priv->amplitude, type);
836 g_value_set_uint64 (&self->priv->amplitude, 0);
839 if (!G_IS_VALUE (&self->priv->offset)) {
840 g_value_init (&self->priv->offset, type);
841 g_value_set_uint64 (&self->priv->offset, tpspec->default_value);
846 GParamSpecFloat *tpspec = G_PARAM_SPEC_FLOAT (pspec);
848 g_value_init (&self->priv->minimum_value, type);
849 g_value_set_float (&self->priv->minimum_value, tpspec->minimum);
850 g_value_init (&self->priv->maximum_value, type);
851 g_value_set_float (&self->priv->maximum_value, tpspec->maximum);
852 if (!G_IS_VALUE (&self->priv->amplitude)) {
853 g_value_init (&self->priv->amplitude, type);
854 g_value_set_float (&self->priv->amplitude, 0.0);
857 if (!G_IS_VALUE (&self->priv->offset)) {
858 g_value_init (&self->priv->offset, type);
859 g_value_set_float (&self->priv->offset, tpspec->default_value);
864 GParamSpecDouble *tpspec = G_PARAM_SPEC_DOUBLE (pspec);
866 g_value_init (&self->priv->minimum_value, type);
867 g_value_set_double (&self->priv->minimum_value, tpspec->minimum);
868 g_value_init (&self->priv->maximum_value, type);
869 g_value_set_double (&self->priv->maximum_value, tpspec->maximum);
870 if (!G_IS_VALUE (&self->priv->amplitude)) {
871 g_value_init (&self->priv->amplitude, type);
872 g_value_set_double (&self->priv->amplitude, 0.0);
875 if (!G_IS_VALUE (&self->priv->offset)) {
876 g_value_init (&self->priv->offset, type);
877 g_value_set_double (&self->priv->offset, tpspec->default_value);
882 GST_WARNING ("incomplete implementation for paramspec type '%s'",
883 G_PARAM_SPEC_TYPE_NAME (pspec));
893 /* This should never fail unless the user already set amplitude or offset
894 * with an incompatible type before _bind () */
895 if (!g_value_type_transformable (G_VALUE_TYPE (&self->priv->amplitude),
897 || !g_value_type_transformable (G_VALUE_TYPE (&self->priv->offset),
899 GST_WARNING ("incompatible types for amplitude or offset");
900 gst_lfo_control_source_reset (self);
904 /* Generate copies and transform to the correct type */
905 g_value_init (&, base);
906 g_value_transform (&self->priv->amplitude, &);
907 g_value_init (&off, base);
908 g_value_transform (&self->priv->offset, &off);
910 ret = gst_lfo_control_source_set_waveform (self, self->priv->waveform);
912 g_value_unset (&self->priv->amplitude);
913 g_value_init (&self->priv->amplitude, self->priv->base);
914 g_value_transform (&, &self->priv->amplitude);
916 g_value_unset (&self->priv->offset);
917 g_value_init (&self->priv->offset, self->priv->base);
918 g_value_transform (&off, &self->priv->offset);
920 g_value_unset (&);
921 g_value_unset (&off);
925 gst_lfo_control_source_reset (self);
931 gst_lfo_control_source_init (GstLFOControlSource * self)
934 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_LFO_CONTROL_SOURCE,
935 GstLFOControlSourcePrivate);
936 self->priv->waveform = GST_LFO_WAVEFORM_SINE;
937 self->priv->frequency = 1.0;
938 self->priv->period = GST_SECOND / self->priv->frequency;
939 self->priv->timeshift = 0;
941 self->lock = g_mutex_new ();
945 gst_lfo_control_source_finalize (GObject * obj)
947 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (obj);
949 gst_lfo_control_source_reset (self);
952 g_mutex_free (self->lock);
956 G_OBJECT_CLASS (parent_class)->finalize (obj);
960 gst_lfo_control_source_dispose (GObject * obj)
962 G_OBJECT_CLASS (parent_class)->dispose (obj);
966 gst_lfo_control_source_set_property (GObject * object, guint prop_id,
967 const GValue * value, GParamSpec * pspec)
969 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
973 g_mutex_lock (self->lock);
974 gst_lfo_control_source_set_waveform (self,
975 (GstLFOWaveform) g_value_get_enum (value));
976 g_mutex_unlock (self->lock);
978 case PROP_FREQUENCY:{
979 gdouble frequency = g_value_get_double (value);
981 g_return_if_fail (frequency > 0
982 || ((GstClockTime) (GST_SECOND / frequency)) != 0);
984 g_mutex_lock (self->lock);
985 self->priv->frequency = frequency;
986 self->priv->period = GST_SECOND / frequency;
987 g_mutex_unlock (self->lock);
991 g_mutex_lock (self->lock);
992 self->priv->timeshift = g_value_get_uint64 (value);
993 g_mutex_unlock (self->lock);
995 case PROP_AMPLITUDE:{
996 GValue *val = g_value_get_boxed (value);
998 if (self->priv->type != G_TYPE_INVALID) {
999 g_return_if_fail (g_value_type_transformable (self->priv->type,
1000 G_VALUE_TYPE (val)));
1002 g_mutex_lock (self->lock);
1003 if (G_IS_VALUE (&self->priv->amplitude))
1004 g_value_unset (&self->priv->amplitude);
1006 g_value_init (&self->priv->amplitude, self->priv->type);
1007 g_value_transform (val, &self->priv->amplitude);
1008 g_mutex_unlock (self->lock);
1010 g_mutex_lock (self->lock);
1011 if (G_IS_VALUE (&self->priv->amplitude))
1012 g_value_unset (&self->priv->amplitude);
1014 g_value_init (&self->priv->amplitude, G_VALUE_TYPE (val));
1015 g_value_copy (val, &self->priv->amplitude);
1016 g_mutex_unlock (self->lock);
1022 GValue *val = g_value_get_boxed (value);
1024 if (self->priv->type != G_TYPE_INVALID) {
1025 g_return_if_fail (g_value_type_transformable (self->priv->type,
1026 G_VALUE_TYPE (val)));
1028 g_mutex_lock (self->lock);
1029 if (G_IS_VALUE (&self->priv->offset))
1030 g_value_unset (&self->priv->offset);
1032 g_value_init (&self->priv->offset, self->priv->type);
1033 g_value_transform (val, &self->priv->offset);
1034 g_mutex_unlock (self->lock);
1036 g_mutex_lock (self->lock);
1037 if (G_IS_VALUE (&self->priv->offset))
1038 g_value_unset (&self->priv->offset);
1040 g_value_init (&self->priv->offset, G_VALUE_TYPE (val));
1041 g_value_copy (val, &self->priv->offset);
1042 g_mutex_unlock (self->lock);
1048 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1054 gst_lfo_control_source_get_property (GObject * object, guint prop_id,
1055 GValue * value, GParamSpec * pspec)
1057 GstLFOControlSource *self = GST_LFO_CONTROL_SOURCE (object);
1061 g_value_set_enum (value, self->priv->waveform);
1063 case PROP_FREQUENCY:
1064 g_value_set_double (value, self->priv->frequency);
1066 case PROP_TIMESHIFT:
1067 g_value_set_uint64 (value, self->priv->timeshift);
1069 case PROP_AMPLITUDE:
1070 g_value_set_boxed (value, &self->priv->amplitude);
1073 g_value_set_boxed (value, &self->priv->offset);
1076 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1082 gst_lfo_control_source_class_init (GstLFOControlSourceClass * klass)
1084 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1085 GstControlSourceClass *csource_class = GST_CONTROL_SOURCE_CLASS (klass);
1087 parent_class = g_type_class_peek_parent (klass);
1088 g_type_class_add_private (klass, sizeof (GstLFOControlSourcePrivate));
1090 gobject_class->finalize = gst_lfo_control_source_finalize;
1091 gobject_class->dispose = gst_lfo_control_source_dispose;
1092 gobject_class->set_property = gst_lfo_control_source_set_property;
1093 gobject_class->get_property = gst_lfo_control_source_get_property;
1095 csource_class->bind = gst_lfo_control_source_bind;
1098 * GstLFOControlSource:waveform
1100 * Specifies the waveform that should be used for this #GstLFOControlSource.
1103 g_object_class_install_property (gobject_class, PROP_WAVEFORM,
1104 g_param_spec_enum ("waveform", "Waveform", "Waveform",
1105 GST_TYPE_LFO_WAVEFORM, GST_LFO_WAVEFORM_SINE,
1106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1109 * GstLFOControlSource:frequency
1111 * Specifies the frequency that should be used for the waveform
1112 * of this #GstLFOControlSource. It should be large enough
1113 * so that the period is longer than one nanosecond.
1116 g_object_class_install_property (gobject_class, PROP_FREQUENCY,
1117 g_param_spec_double ("frequency", "Frequency",
1118 "Frequency of the waveform", 0.0, G_MAXDOUBLE, 1.0,
1119 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1122 * GstLFOControlSource:timeshift
1124 * Specifies the timeshift to the right that should be used for the waveform
1125 * of this #GstLFOControlSource in nanoseconds.
1127 * To get a n nanosecond shift to the left use
1128 * "(GST_SECOND / frequency) - n".
1131 g_object_class_install_property (gobject_class, PROP_TIMESHIFT,
1132 g_param_spec_uint64 ("timeshift", "Timeshift",
1133 "Timeshift of the waveform to the right", 0, G_MAXUINT64, 0,
1134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1137 * GstLFOControlSource:amplitude
1139 * Specifies the amplitude for the waveform of this #GstLFOControlSource.
1141 * It should be given as a #GValue with a type that can be transformed
1142 * to the type of the bound property.
1144 g_object_class_install_property (gobject_class, PROP_AMPLITUDE,
1145 g_param_spec_boxed ("amplitude", "Amplitude", "Amplitude of the waveform",
1146 G_TYPE_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1149 * GstLFOControlSource:offset
1151 * Specifies the offset for the waveform of this #GstLFOControlSource.
1153 * It should be given as a #GValue with a type that can be transformed
1154 * to the type of the bound property.
1156 g_object_class_install_property (gobject_class, PROP_OFFSET,
1157 g_param_spec_boxed ("offset", "Offset", "Offset of the waveform",
1158 G_TYPE_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));