command-line-formatter: Stop uselessly looping over options
[platform/upstream/gstreamer.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 (GESTrackElement * self);
61
62 static void
63 ges_audio_test_source_class_init (GESAudioTestSourceClass * klass)
64 {
65   GObjectClass *object_class = G_OBJECT_CLASS (klass);
66   GESAudioSourceClass *source_class = GES_AUDIO_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 (GESTrackElement * trksrc)
104 {
105   GESAudioTestSource *self;
106   GstElement *ret;
107   const gchar *props[] = { "volume", "freq", NULL };
108
109   self = (GESAudioTestSource *) trksrc;
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 (trksrc, ret, NULL, NULL, props);
115
116   return ret;
117 }
118
119 /**
120  * ges_audio_test_source_set_freq:
121  * @self: a #GESAudioTestSource
122  * @freq: The frequency you want to apply on @self
123  *
124  * Lets you set the frequency applied on the track element
125  */
126 void
127 ges_audio_test_source_set_freq (GESAudioTestSource * self, gdouble freq)
128 {
129   GstElement *element =
130       ges_track_element_get_element (GES_TRACK_ELEMENT (self));
131
132   self->priv->freq = freq;
133   if (element) {
134     GValue val = { 0 };
135
136     g_value_init (&val, G_TYPE_DOUBLE);
137     g_value_set_double (&val, freq);
138     ges_track_element_set_child_property (GES_TRACK_ELEMENT (self), "freq",
139         &val);
140   }
141 }
142
143 /**
144  * ges_audio_test_source_set_volume:
145  * @self: a #GESAudioTestSource
146  * @volume: The volume you want to apply on @self
147  *
148  * Sets the volume of the test audio signal.
149  */
150 void
151 ges_audio_test_source_set_volume (GESAudioTestSource * self, gdouble volume)
152 {
153   GstElement *element =
154       ges_track_element_get_element (GES_TRACK_ELEMENT (self));
155
156   self->priv->volume = volume;
157   if (element) {
158     GValue val = { 0 };
159
160     g_value_init (&val, G_TYPE_DOUBLE);
161     g_value_set_double (&val, volume);
162     ges_track_element_set_child_property (GES_TRACK_ELEMENT (self), "volume",
163         &val);
164   }
165 }
166
167 /**
168  * ges_audio_test_source_get_freq:
169  * @self: a #GESAudioTestSource
170  *
171  * Get the current frequency of @self.
172  *
173  * Returns: The current frequency of @self.
174  */
175 double
176 ges_audio_test_source_get_freq (GESAudioTestSource * self)
177 {
178   GValue val = { 0 };
179
180   ges_track_element_get_child_property (GES_TRACK_ELEMENT (self), "freq", &val);
181   return g_value_get_double (&val);
182 }
183
184 /**
185  * ges_audio_test_source_get_volume:
186  * @self: a #GESAudioTestSource
187  *
188  * Get the current volume of @self.
189  *
190  * Returns: The current volume of @self
191  */
192 double
193 ges_audio_test_source_get_volume (GESAudioTestSource * self)
194 {
195   GValue val = { 0 };
196
197   ges_track_element_get_child_property (GES_TRACK_ELEMENT (self), "volume",
198       &val);
199   return g_value_get_double (&val);
200 }
201
202 /* Creates a new #GESAudioTestSource.
203  *
204  * Returns: (transfer floating) (nullable): The newly created #GESAudioTestSource.
205  */
206 GESAudioTestSource *
207 ges_audio_test_source_new (void)
208 {
209   GESAudioTestSource *res;
210   GESAsset *asset = ges_asset_request (GES_TYPE_AUDIO_TEST_SOURCE, NULL, NULL);
211
212   res = GES_AUDIO_TEST_SOURCE (ges_asset_extract (asset, NULL));
213   gst_object_unref (asset);
214
215   return res;
216 }