Merge branch 'tizen' into tizen_gst_1.19.2
[platform/upstream/gst-editing-services.git] / ges / ges-audio-test-source.c
1 /* GStreamer Editing Services
2  * Copyright (C) 2010 Brandon Lewis <brandon.lewis@collabora.co.uk>
3  *               2010 Nokia Corporation
4  *
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.
9  *
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.
14  *
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * SECTION:gesaudiotestsource
23  * @title: GESAudioTestSource
24  * @short_description: produce a simple test waveform or silence
25  *
26  * Outputs a test audio stream using audiotestsrc. The default property values
27  * output silence. Useful for testing pipelines, or to fill gaps in an audio
28  * track.
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "ges-internal.h"
35 #include "ges-track-element.h"
36 #include "ges-audio-test-source.h"
37
38 #define DEFAULT_VOLUME 1.0
39
40 struct _GESAudioTestSourcePrivate
41 {
42   gdouble freq;
43   gdouble volume;
44 };
45
46 enum
47 {
48   PROP_0,
49 };
50
51 G_DEFINE_TYPE_WITH_PRIVATE (GESAudioTestSource, ges_audio_test_source,
52     GES_TYPE_AUDIO_SOURCE);
53
54 static void ges_audio_test_source_get_property (GObject * object, guint
55     property_id, GValue * value, GParamSpec * pspec);
56
57 static void ges_audio_test_source_set_property (GObject * object, guint
58     property_id, const GValue * value, GParamSpec * pspec);
59
60 static GstElement *ges_audio_test_source_create_source (GESSource * source);
61
62 static void
63 ges_audio_test_source_class_init (GESAudioTestSourceClass * klass)
64 {
65   GObjectClass *object_class = G_OBJECT_CLASS (klass);
66   GESSourceClass *source_class = GES_SOURCE_CLASS (klass);
67
68   object_class->get_property = ges_audio_test_source_get_property;
69   object_class->set_property = ges_audio_test_source_set_property;
70
71   source_class->create_source = ges_audio_test_source_create_source;
72 }
73
74 static void
75 ges_audio_test_source_init (GESAudioTestSource * self)
76 {
77   self->priv = ges_audio_test_source_get_instance_private (self);
78   self->priv->freq = 440;
79   self->priv->volume = DEFAULT_VOLUME;
80 }
81
82 static void
83 ges_audio_test_source_get_property (GObject * object,
84     guint property_id, GValue * value, GParamSpec * pspec)
85 {
86   switch (property_id) {
87     default:
88       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89   }
90 }
91
92 static void
93 ges_audio_test_source_set_property (GObject * object,
94     guint property_id, const GValue * value, GParamSpec * pspec)
95 {
96   switch (property_id) {
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99   }
100 }
101
102 static GstElement *
103 ges_audio_test_source_create_source (GESSource * source)
104 {
105   GESAudioTestSource *self;
106   GstElement *ret;
107   const gchar *props[] = { "volume", "freq", NULL };
108
109   self = (GESAudioTestSource *) source;
110   ret = gst_element_factory_make ("audiotestsrc", NULL);
111   g_object_set (ret, "volume", (gdouble) self->priv->volume, "freq", (gdouble)
112       self->priv->freq, NULL);
113
114   ges_track_element_add_children_props (GES_TRACK_ELEMENT (self), ret, NULL,
115       NULL, props);
116
117   return ret;
118 }
119
120 /**
121  * ges_audio_test_source_set_freq:
122  * @self: a #GESAudioTestSource
123  * @freq: The frequency you want to apply on @self
124  *
125  * Lets you set the frequency applied on the track element
126  */
127 void
128 ges_audio_test_source_set_freq (GESAudioTestSource * self, gdouble freq)
129 {
130   GstElement *element =
131       ges_track_element_get_element (GES_TRACK_ELEMENT (self));
132
133   self->priv->freq = freq;
134   if (element) {
135     GValue val = { 0 };
136
137     g_value_init (&val, G_TYPE_DOUBLE);
138     g_value_set_double (&val, freq);
139     ges_track_element_set_child_property (GES_TRACK_ELEMENT (self), "freq",
140         &val);
141   }
142 }
143
144 /**
145  * ges_audio_test_source_set_volume:
146  * @self: a #GESAudioTestSource
147  * @volume: The volume you want to apply on @self
148  *
149  * Sets the volume of the test audio signal.
150  */
151 void
152 ges_audio_test_source_set_volume (GESAudioTestSource * self, gdouble volume)
153 {
154   GstElement *element =
155       ges_track_element_get_element (GES_TRACK_ELEMENT (self));
156
157   self->priv->volume = volume;
158   if (element) {
159     GValue val = { 0 };
160
161     g_value_init (&val, G_TYPE_DOUBLE);
162     g_value_set_double (&val, volume);
163     ges_track_element_set_child_property (GES_TRACK_ELEMENT (self), "volume",
164         &val);
165   }
166 }
167
168 /**
169  * ges_audio_test_source_get_freq:
170  * @self: a #GESAudioTestSource
171  *
172  * Get the current frequency of @self.
173  *
174  * Returns: The current frequency of @self.
175  */
176 double
177 ges_audio_test_source_get_freq (GESAudioTestSource * self)
178 {
179   GValue val = { 0 };
180
181   ges_track_element_get_child_property (GES_TRACK_ELEMENT (self), "freq", &val);
182   return g_value_get_double (&val);
183 }
184
185 /**
186  * ges_audio_test_source_get_volume:
187  * @self: a #GESAudioTestSource
188  *
189  * Get the current volume of @self.
190  *
191  * Returns: The current volume of @self
192  */
193 double
194 ges_audio_test_source_get_volume (GESAudioTestSource * self)
195 {
196   GValue val = { 0 };
197
198   ges_track_element_get_child_property (GES_TRACK_ELEMENT (self), "volume",
199       &val);
200   return g_value_get_double (&val);
201 }
202
203 /* Creates a new #GESAudioTestSource.
204  *
205  * Returns: (transfer floating) (nullable): The newly created #GESAudioTestSource.
206  */
207 GESAudioTestSource *
208 ges_audio_test_source_new (void)
209 {
210   GESAudioTestSource *res;
211   GESAsset *asset = ges_asset_request (GES_TYPE_AUDIO_TEST_SOURCE, NULL, NULL);
212
213   res = GES_AUDIO_TEST_SOURCE (ges_asset_extract (asset, NULL));
214   gst_object_unref (asset);
215
216   return res;
217 }