📄 gstlfocontrolsource.c
字号:
/* GStreamer * * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org> * * gstlfocontrolsource.c: Control source that provides some periodic waveforms * as control values. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//** * SECTION:gstlfocontrolsource * @short_description: LFO control source * * #GstLFOControlSource is a #GstControlSource, that provides several periodic waveforms * as control values. It supports all fundamental, numeric GValue types as property. * * To use #GstLFOControlSource get a new instance by calling gst_lfo_control_source_new(), * bind it to a #GParamSpec and set the relevant properties or use * gst_lfo_control_source_set_waveform. * * All functions are MT-safe. * */#include <glib-object.h>#include <gst/gst.h>#include "gstcontrolsource.h"#include "gstlfocontrolsource.h"#include "gstlfocontrolsourceprivate.h"#include "math.h"#define EMPTY(x) (x)/* FIXME: as % in C is not the modulo operator we need here for * negative numbers implement our own. Are there better ways? */static inline GstClockTime_calculate_pos (GstClockTime timestamp, GstClockTime timeshift, GstClockTime period){ while (timestamp < timeshift) timestamp += period; timestamp -= timeshift; return timestamp % period;}#define DEFINE_SINE(type,round,convert) \\static inline g##type \_sine_get_##type (GstLFOControlSource *self, GstClockTime timestamp) \{ \ gdouble ret; \ g##type max = g_value_get_##type (&self->priv->maximum_value); \ g##type min = g_value_get_##type (&self->priv->minimum_value); \ gdouble amp = convert (g_value_get_##type (&self->priv->amplitude)); \ gdouble off = convert (g_value_get_##type (&self->priv->offset)); \ GstClockTime pos = _calculate_pos (timestamp, self->priv->timeshift, self->priv->period); \ \ ret = sin (2.0 * M_PI * (self->priv->frequency / GST_SECOND) * gst_util_guint64_to_gdouble (pos)); \ ret *= amp; \ ret += off; \ \ if (round) \ ret += 0.5; \ \ return (g##type) CLAMP (ret, convert (min), convert (max)); \} \\static gboolean \waveform_sine_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \ GValue *value) \{ \ g##type ret; \ g_mutex_lock (self->lock); \ ret = _sine_get_##type (self, timestamp); \ g_value_set_##type (value, ret); \ g_mutex_unlock (self->lock); \ return TRUE; \} \\static gboolean \waveform_sine_get_##type##_value_array (GstLFOControlSource *self, \ GstClockTime timestamp, GstValueArray * value_array) \{ \ gint i; \ GstClockTime ts = timestamp; \ g##type *values = (g##type *) value_array->values; \ \ g_mutex_lock (self->lock); \ for(i = 0; i < value_array->nbsamples; i++) { \ *values = _sine_get_##type (self, ts); \ ts += value_array->sample_interval; \ values++; \ } \ g_mutex_unlock (self->lock); \ return TRUE; \}DEFINE_SINE (int, TRUE, EMPTY);DEFINE_SINE (uint, TRUE, EMPTY);DEFINE_SINE (long, TRUE, EMPTY);DEFINE_SINE (ulong, TRUE, EMPTY);DEFINE_SINE (int64, TRUE, EMPTY);DEFINE_SINE (uint64, TRUE, gst_util_guint64_to_gdouble);DEFINE_SINE (float, FALSE, EMPTY);DEFINE_SINE (double, FALSE, EMPTY);static GstWaveformImplementation waveform_sine = { (GstControlSourceGetValue) waveform_sine_get_int, (GstControlSourceGetValueArray) waveform_sine_get_int_value_array, (GstControlSourceGetValue) waveform_sine_get_uint, (GstControlSourceGetValueArray) waveform_sine_get_uint_value_array, (GstControlSourceGetValue) waveform_sine_get_long, (GstControlSourceGetValueArray) waveform_sine_get_long_value_array, (GstControlSourceGetValue) waveform_sine_get_ulong, (GstControlSourceGetValueArray) waveform_sine_get_ulong_value_array, (GstControlSourceGetValue) waveform_sine_get_int64, (GstControlSourceGetValueArray) waveform_sine_get_int64_value_array, (GstControlSourceGetValue) waveform_sine_get_uint64, (GstControlSourceGetValueArray) waveform_sine_get_uint64_value_array, (GstControlSourceGetValue) waveform_sine_get_float, (GstControlSourceGetValueArray) waveform_sine_get_float_value_array, (GstControlSourceGetValue) waveform_sine_get_double, (GstControlSourceGetValueArray) waveform_sine_get_double_value_array};#define DEFINE_SQUARE(type,round, convert) \\static inline g##type \_square_get_##type (GstLFOControlSource *self, GstClockTime timestamp) \{ \ g##type max = g_value_get_##type (&self->priv->maximum_value); \ g##type min = g_value_get_##type (&self->priv->minimum_value); \ gdouble amp = convert (g_value_get_##type (&self->priv->amplitude)); \ gdouble off = convert (g_value_get_##type (&self->priv->offset)); \ GstClockTime period = self->priv->period; \ GstClockTime pos = _calculate_pos (timestamp, self->priv->timeshift, period); \ gdouble ret; \ \ if (pos >= period / 2) \ ret = amp; \ else \ ret = - amp; \ \ ret += off; \ \ if (round) \ ret += 0.5; \ \ return (g##type) CLAMP (ret, convert (min), convert (max)); \} \\static gboolean \waveform_square_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \ GValue *value) \{ \ g##type ret; \ g_mutex_lock (self->lock); \ ret = _square_get_##type (self, timestamp); \ g_value_set_##type (value, ret); \ g_mutex_unlock (self->lock); \ return TRUE; \} \\static gboolean \waveform_square_get_##type##_value_array (GstLFOControlSource *self, \ GstClockTime timestamp, GstValueArray * value_array) \{ \ gint i; \ GstClockTime ts = timestamp; \ g##type *values = (g##type *) value_array->values; \ \ g_mutex_lock (self->lock); \ for(i = 0; i < value_array->nbsamples; i++) { \ *values = _square_get_##type (self, ts); \ ts += value_array->sample_interval; \ values++; \ } \ g_mutex_unlock (self->lock); \ return TRUE; \}DEFINE_SQUARE (int, TRUE, EMPTY);DEFINE_SQUARE (uint, TRUE, EMPTY);DEFINE_SQUARE (long, TRUE, EMPTY);DEFINE_SQUARE (ulong, TRUE, EMPTY);DEFINE_SQUARE (int64, TRUE, EMPTY);DEFINE_SQUARE (uint64, TRUE, gst_util_guint64_to_gdouble);DEFINE_SQUARE (float, FALSE, EMPTY);DEFINE_SQUARE (double, FALSE, EMPTY);static GstWaveformImplementation waveform_square = { (GstControlSourceGetValue) waveform_square_get_int, (GstControlSourceGetValueArray) waveform_square_get_int_value_array, (GstControlSourceGetValue) waveform_square_get_uint, (GstControlSourceGetValueArray) waveform_square_get_uint_value_array, (GstControlSourceGetValue) waveform_square_get_long, (GstControlSourceGetValueArray) waveform_square_get_long_value_array, (GstControlSourceGetValue) waveform_square_get_ulong, (GstControlSourceGetValueArray) waveform_square_get_ulong_value_array, (GstControlSourceGetValue) waveform_square_get_int64, (GstControlSourceGetValueArray) waveform_square_get_int64_value_array, (GstControlSourceGetValue) waveform_square_get_uint64, (GstControlSourceGetValueArray) waveform_square_get_uint64_value_array, (GstControlSourceGetValue) waveform_square_get_float, (GstControlSourceGetValueArray) waveform_square_get_float_value_array, (GstControlSourceGetValue) waveform_square_get_double, (GstControlSourceGetValueArray) waveform_square_get_double_value_array};#define DEFINE_SAW(type,round,convert) \\static inline g##type \_saw_get_##type (GstLFOControlSource *self, GstClockTime timestamp) \{ \ g##type max = g_value_get_##type (&self->priv->maximum_value); \ g##type min = g_value_get_##type (&self->priv->minimum_value); \ gdouble amp = convert (g_value_get_##type (&self->priv->amplitude)); \ gdouble off = convert (g_value_get_##type (&self->priv->offset)); \ GstClockTime period = self->priv->period; \ GstClockTime pos = _calculate_pos (timestamp, self->priv->timeshift, period); \ gdouble ret; \ \ ret = - ((gst_util_guint64_to_gdouble (pos) - gst_util_guint64_to_gdouble (period) / 2.0) * ((2.0 * amp) / gst_util_guint64_to_gdouble (period)));\ \ ret += off; \ \ if (round) \ ret += 0.5; \ \ return (g##type) CLAMP (ret, convert (min), convert (max)); \} \\static gboolean \waveform_saw_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \ GValue *value) \{ \ g##type ret; \ g_mutex_lock (self->lock); \ ret = _saw_get_##type (self, timestamp); \ g_value_set_##type (value, ret); \ g_mutex_unlock (self->lock); \ return TRUE; \} \\static gboolean \waveform_saw_get_##type##_value_array (GstLFOControlSource *self, \ GstClockTime timestamp, GstValueArray * value_array) \{ \ gint i; \ GstClockTime ts = timestamp; \ g##type *values = (g##type *) value_array->values; \ \ g_mutex_lock (self->lock); \ for(i = 0; i < value_array->nbsamples; i++) { \ *values = _saw_get_##type (self, ts); \ ts += value_array->sample_interval; \ values++; \ } \ g_mutex_unlock (self->lock); \ return TRUE; \}DEFINE_SAW (int, TRUE, EMPTY);DEFINE_SAW (uint, TRUE, EMPTY);DEFINE_SAW (long, TRUE, EMPTY);DEFINE_SAW (ulong, TRUE, EMPTY);DEFINE_SAW (int64, TRUE, EMPTY);DEFINE_SAW (uint64, TRUE, gst_util_guint64_to_gdouble);DEFINE_SAW (float, FALSE, EMPTY);DEFINE_SAW (double, FALSE, EMPTY);static GstWaveformImplementation waveform_saw = { (GstControlSourceGetValue) waveform_saw_get_int, (GstControlSourceGetValueArray) waveform_saw_get_int_value_array, (GstControlSourceGetValue) waveform_saw_get_uint, (GstControlSourceGetValueArray) waveform_saw_get_uint_value_array, (GstControlSourceGetValue) waveform_saw_get_long, (GstControlSourceGetValueArray) waveform_saw_get_long_value_array, (GstControlSourceGetValue) waveform_saw_get_ulong, (GstControlSourceGetValueArray) waveform_saw_get_ulong_value_array, (GstControlSourceGetValue) waveform_saw_get_int64, (GstControlSourceGetValueArray) waveform_saw_get_int64_value_array, (GstControlSourceGetValue) waveform_saw_get_uint64, (GstControlSourceGetValueArray) waveform_saw_get_uint64_value_array, (GstControlSourceGetValue) waveform_saw_get_float, (GstControlSourceGetValueArray) waveform_saw_get_float_value_array, (GstControlSourceGetValue) waveform_saw_get_double, (GstControlSourceGetValueArray) waveform_saw_get_double_value_array};#define DEFINE_RSAW(type,round,convert) \\static inline g##type \_rsaw_get_##type (GstLFOControlSource *self, GstClockTime timestamp) \{ \ g##type max = g_value_get_##type (&self->priv->maximum_value); \ g##type min = g_value_get_##type (&self->priv->minimum_value); \ gdouble amp = convert (g_value_get_##type (&self->priv->amplitude)); \ gdouble off = convert (g_value_get_##type (&self->priv->offset)); \ GstClockTime period = self->priv->period; \ GstClockTime pos = _calculate_pos (timestamp, self->priv->timeshift, period); \ gdouble ret; \ \ ret = ((gst_util_guint64_to_gdouble (pos) - gst_util_guint64_to_gdouble (period) / 2.0) * ((2.0 * amp) / gst_util_guint64_to_gdouble (period)));\ \ ret += off; \ \ if (round) \ ret += 0.5; \ \ return (g##type) CLAMP (ret, convert (min), convert (max)); \} \\static gboolean \waveform_rsaw_get_##type (GstLFOControlSource *self, GstClockTime timestamp, \ GValue *value) \{ \ g##type ret; \ g_mutex_lock (self->lock); \ ret = _rsaw_get_##type (self, timestamp); \ g_value_set_##type (value, ret); \ g_mutex_unlock (self->lock); \ return TRUE; \} \\static gboolean \waveform_rsaw_get_##type##_value_array (GstLFOControlSource *self, \ GstClockTime timestamp, GstValueArray * value_array) \{ \ gint i; \ GstClockTime ts = timestamp; \ g##type *values = (g##type *) value_array->values; \ \ g_mutex_lock (self->lock); \ for(i = 0; i < value_array->nbsamples; i++) { \ *values = _rsaw_get_##type (self, ts); \ ts += value_array->sample_interval; \ values++; \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -