1 /* GStreamer Editing Services
2 * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
3 * 2010 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:ges-track-audio-transition
23 * @short_description: implements audio crossfade transition
26 #include "ges-internal.h"
27 #include "ges-track-object.h"
28 #include "ges-track-audio-transition.h"
30 G_DEFINE_TYPE (GESTrackAudioTransition, ges_track_audio_transition,
31 GES_TYPE_TRACK_TRANSITION);
33 struct _GESTrackAudioTransitionPrivate
35 /* these enable volume interpolation. Unlike video, both inputs are adjusted
37 GstController *a_controller;
38 GstInterpolationControlSource *a_control_source;
40 GstController *b_controller;
41 GstInterpolationControlSource *b_control_source;
51 #define fast_element_link(a,b) gst_element_link_pads_full((a),"src",(b),"sink",GST_PAD_LINK_CHECK_NOTHING)
54 ges_track_audio_transition_duration_changed (GESTrackObject * self, guint64);
56 static GstElement *ges_track_audio_transition_create_element (GESTrackObject
59 static void ges_track_audio_transition_dispose (GObject * object);
61 static void ges_track_audio_transition_finalize (GObject * object);
63 static void ges_track_audio_transition_get_property (GObject * object, guint
64 property_id, GValue * value, GParamSpec * pspec);
66 static void ges_track_audio_transition_set_property (GObject * object, guint
67 property_id, const GValue * value, GParamSpec * pspec);
70 ges_track_audio_transition_class_init (GESTrackAudioTransitionClass * klass)
72 GObjectClass *object_class;
73 GESTrackObjectClass *toclass;
75 g_type_class_add_private (klass, sizeof (GESTrackAudioTransitionPrivate));
77 object_class = G_OBJECT_CLASS (klass);
78 toclass = GES_TRACK_OBJECT_CLASS (klass);
80 object_class->get_property = ges_track_audio_transition_get_property;
81 object_class->set_property = ges_track_audio_transition_set_property;
82 object_class->dispose = ges_track_audio_transition_dispose;
83 object_class->finalize = ges_track_audio_transition_finalize;
85 toclass->duration_changed = ges_track_audio_transition_duration_changed;
87 toclass->create_element = ges_track_audio_transition_create_element;
92 ges_track_audio_transition_init (GESTrackAudioTransition * self)
95 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
96 GES_TYPE_TRACK_AUDIO_TRANSITION, GESTrackAudioTransitionPrivate);
98 self->priv->a_controller = NULL;
99 self->priv->a_control_source = NULL;
101 self->priv->b_controller = NULL;
102 self->priv->b_control_source = NULL;
106 ges_track_audio_transition_dispose (GObject * object)
108 GESTrackAudioTransition *self;
110 self = GES_TRACK_AUDIO_TRANSITION (object);
112 if (self->priv->a_controller) {
113 g_object_unref (self->priv->a_controller);
114 self->priv->a_controller = NULL;
115 if (self->priv->a_control_source)
116 gst_object_unref (self->priv->a_control_source);
117 self->priv->a_control_source = NULL;
120 if (self->priv->b_controller) {
121 g_object_unref (self->priv->b_controller);
122 self->priv->b_controller = NULL;
123 if (self->priv->b_control_source)
124 gst_object_unref (self->priv->b_control_source);
125 self->priv->b_control_source = NULL;
128 G_OBJECT_CLASS (ges_track_audio_transition_parent_class)->dispose (object);
132 ges_track_audio_transition_finalize (GObject * object)
134 G_OBJECT_CLASS (ges_track_audio_transition_parent_class)->finalize (object);
138 ges_track_audio_transition_get_property (GObject * object,
139 guint property_id, GValue * value, GParamSpec * pspec)
141 switch (property_id) {
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
148 ges_track_audio_transition_set_property (GObject * object,
149 guint property_id, const GValue * value, GParamSpec * pspec)
151 switch (property_id) {
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 link_element_to_mixer_with_volume (GstBin * bin, GstElement * element,
161 GstElement *volume = gst_element_factory_make ("volume", NULL);
162 gst_bin_add (bin, volume);
164 if (!fast_element_link (element, volume) ||
165 !gst_element_link_pads_full (volume, "src", mixer, "sink%d",
166 GST_PAD_LINK_CHECK_NOTHING))
167 GST_ERROR_OBJECT (bin, "Error linking volume to mixer");
169 return G_OBJECT (volume);
173 ges_track_audio_transition_create_element (GESTrackObject * object)
175 GESTrackAudioTransition *self;
176 GstElement *topbin, *iconva, *iconvb, *oconv;
177 GObject *atarget, *btarget = NULL;
178 const gchar *propname = "volume";
179 GstElement *mixer = NULL;
180 GstPad *sinka_target, *sinkb_target, *src_target, *sinka, *sinkb, *src;
181 GstController *acontroller, *bcontroller;
182 GstInterpolationControlSource *acontrol_source, *bcontrol_source;
184 self = GES_TRACK_AUDIO_TRANSITION (object);
187 GST_LOG ("creating an audio bin");
189 topbin = gst_bin_new ("transition-bin");
190 iconva = gst_element_factory_make ("audioconvert", "tr-aconv-a");
191 iconvb = gst_element_factory_make ("audioconvert", "tr-aconv-b");
192 oconv = gst_element_factory_make ("audioconvert", "tr-aconv-output");
194 gst_bin_add_many (GST_BIN (topbin), iconva, iconvb, oconv, NULL);
196 mixer = gst_element_factory_make ("adder", NULL);
197 gst_bin_add (GST_BIN (topbin), mixer);
199 atarget = link_element_to_mixer_with_volume (GST_BIN (topbin), iconva, mixer);
200 btarget = link_element_to_mixer_with_volume (GST_BIN (topbin), iconvb, mixer);
202 g_assert (atarget && btarget);
204 fast_element_link (mixer, oconv);
206 sinka_target = gst_element_get_static_pad (iconva, "sink");
207 sinkb_target = gst_element_get_static_pad (iconvb, "sink");
208 src_target = gst_element_get_static_pad (oconv, "src");
210 sinka = gst_ghost_pad_new ("sinka", sinka_target);
211 sinkb = gst_ghost_pad_new ("sinkb", sinkb_target);
212 src = gst_ghost_pad_new ("src", src_target);
214 gst_element_add_pad (topbin, src);
215 gst_element_add_pad (topbin, sinka);
216 gst_element_add_pad (topbin, sinkb);
218 /* set up interpolation */
220 gst_object_unref (sinka_target);
221 gst_object_unref (sinkb_target);
222 gst_object_unref (src_target);
225 //g_object_set(atarget, propname, (gdouble) 0, NULL);
226 //g_object_set(btarget, propname, (gdouble) 0, NULL);
228 acontroller = gst_object_control_properties (atarget, propname, NULL);
229 bcontroller = gst_object_control_properties (btarget, propname, NULL);
231 g_assert (acontroller && bcontroller);
233 acontrol_source = gst_interpolation_control_source_new ();
234 gst_controller_set_control_source (acontroller,
235 propname, GST_CONTROL_SOURCE (acontrol_source));
236 gst_interpolation_control_source_set_interpolation_mode (acontrol_source,
237 GST_INTERPOLATE_LINEAR);
239 bcontrol_source = gst_interpolation_control_source_new ();
240 gst_controller_set_control_source (bcontroller,
241 propname, GST_CONTROL_SOURCE (bcontrol_source));
242 gst_interpolation_control_source_set_interpolation_mode (bcontrol_source,
243 GST_INTERPOLATE_LINEAR);
245 self->priv->a_controller = acontroller;
246 self->priv->b_controller = bcontroller;
247 self->priv->a_control_source = acontrol_source;
248 self->priv->b_control_source = bcontrol_source;
254 ges_track_audio_transition_duration_changed (GESTrackObject * object,
257 GESTrackAudioTransition *self;
258 GstElement *gnlobj = ges_track_object_get_gnlobject (object);
260 GValue zero = { 0, };
263 self = GES_TRACK_AUDIO_TRANSITION (object);
265 GST_LOG ("updating controller: gnlobj (%p) acontroller(%p) bcontroller(%p)",
266 gnlobj, self->priv->a_controller, self->priv->b_controller);
268 if (G_UNLIKELY ((!gnlobj || !self->priv->a_control_source ||
269 !self->priv->b_control_source)))
272 GST_INFO ("duration: %" G_GUINT64_FORMAT, duration);
273 g_value_init (&zero, G_TYPE_DOUBLE);
274 g_value_init (&one, G_TYPE_DOUBLE);
275 g_value_set_double (&zero, 0.0);
276 g_value_set_double (&one, 1.0);
278 GST_LOG ("setting values on controller");
280 gst_interpolation_control_source_unset_all (self->priv->a_control_source);
281 gst_interpolation_control_source_set (self->priv->a_control_source, 0, &one);
282 gst_interpolation_control_source_set (self->priv->a_control_source,
285 gst_interpolation_control_source_unset_all (self->priv->b_control_source);
286 gst_interpolation_control_source_set (self->priv->b_control_source, 0, &zero);
287 gst_interpolation_control_source_set (self->priv->b_control_source, duration,
290 GST_LOG ("done updating controller");
294 * ges_track_audio_transition_new:
296 * Creates a new #GESTrackAudioTransition.
298 * Returns: The newly created #GESTrackAudioTransition.
300 GESTrackAudioTransition *
301 ges_track_audio_transition_new (void)
303 return g_object_new (GES_TYPE_TRACK_AUDIO_TRANSITION, NULL);