2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * sinesrc.c: An elemnt emitting a sine src in lots of different formats
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <string.h> /* memcpy */
25 #define SAMPLES_PER_WAVE 200
27 static GstStaticPadTemplate sinesrc_src_factory =
28 GST_STATIC_PAD_TEMPLATE ("src",
31 GST_STATIC_CAPS ("audio/x-raw-int, "
32 "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
33 "signed = (boolean) { FALSE, TRUE }, "
34 "width = (int) [8, 32], "
35 "depth = (int) [8, 32], "
36 "rate = (int) [8000, 192000], "
37 "channels = (int) [1, 16];"
39 "endianness = (int) BYTE_ORDER, "
40 "width = (int) {32, 64}, "
41 "rate = (int) [8000, 192000], " "channels = (int) [1, 16]")
44 static GstElementClass *parent_class = NULL;
46 static void sinesrc_init (SineSrc * src);
47 static void sinesrc_class_init (SineSrcClass * klass);
49 static GstData *sinesrc_get (GstPad * pad);
50 static GstStateChangeReturn sinesrc_change_state (GstElement * element,
51 GstStateChange transition);
55 sinesrc_get_type (void)
57 static GType sinesrc_type = 0;
60 static const GTypeInfo sinesrc_info = {
61 sizeof (SineSrcClass), NULL, NULL,
62 (GClassInitFunc) sinesrc_class_init, NULL, NULL,
64 (GInstanceInitFunc) sinesrc_init,
67 sinesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "SineSrc",
74 sinesrc_class_init (SineSrcClass * klass)
76 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
78 element_class->change_state = sinesrc_change_state;
80 parent_class = g_type_class_peek_parent (klass);
84 sinesrc_init (SineSrc * src)
87 gst_pad_new_from_template (gst_static_pad_template_get
88 (&sinesrc_src_factory), "src");
89 gst_element_add_pad (GST_ELEMENT (src), src->src);
90 gst_pad_set_get_function (src->src, sinesrc_get);
95 src->endianness = G_BYTE_ORDER;
98 src->type = SINE_SRC_INT;
101 src->pre_get_func = NULL;
103 GST_OBJECT (src)->name = "sinesrc";
107 sinesrc_force_caps (SineSrc * src)
114 src->newcaps = FALSE;
118 caps = gst_caps_new_simple ("audio/x-raw-int",
119 "signed", G_TYPE_BOOLEAN, src->sign,
120 "depth", G_TYPE_INT, src->depth, NULL);
122 gst_caps_set_simple (caps,
123 "endianness", G_TYPE_INT, src->endianness, NULL);
126 g_assert (src->width == 32 || src->width == 64);
127 caps = gst_caps_new_simple ("audio/x-raw-float",
128 "endianness", G_TYPE_INT, src->endianness, NULL);
132 g_assert_not_reached ();
134 gst_caps_set_simple (caps,
135 "width", G_TYPE_INT, src->width,
136 "rate", G_TYPE_INT, src->rate,
137 "channels", G_TYPE_INT, src->channels, NULL);
139 if (gst_pad_try_set_caps (src->src, caps) != GST_PAD_LINK_OK)
140 g_assert_not_reached ();
143 /* always return 1 wave
144 * there are 200 waves in 1 second, so the frequency is samplerate/200
158 #define POPULATE(format, be_func, le_func) G_STMT_START {\
159 format val = (format) int_value;\
161 switch (src->endianness) {\
162 case G_LITTLE_ENDIAN:\
163 val = le_func (val);\
166 val = be_func (val);\
169 g_assert_not_reached ();\
171 for (j = 0; j < src->channels; j++) {\
179 sinesrc_get (GstPad * pad)
188 g_return_val_if_fail (pad != NULL, NULL);
189 src = SINESRC (gst_pad_get_parent (pad));
191 if (src->pre_get_func)
192 src->pre_get_func (src);
194 buf = gst_buffer_new_and_alloc ((src->width / 8) * src->channels *
197 data = GST_BUFFER_DATA (buf);
200 for (i = 0; i < SAMPLES_PER_WAVE; i++) {
201 value = sin (i * 2 * M_PI / SAMPLES_PER_WAVE);
205 (value + (src->sign ? 0 : 1)) * (((guint64) 1) << (src->depth - 1));
207 (1 + (src->sign ? 0 : 1)) * (((guint64) 1) << (src->depth - 1)))
209 switch (src->width) {
212 POPULATE (gint8, IDENTITY, IDENTITY);
214 POPULATE (guint8, UIDENTITY, UIDENTITY);
218 POPULATE (gint16, GINT16_TO_BE, GINT16_TO_LE);
220 POPULATE (guint16, GUINT16_TO_BE, GUINT16_TO_LE);
225 gint32 val = (gint32) int_value;
227 switch (src->endianness) {
228 case G_LITTLE_ENDIAN:
229 val = GINT32_TO_LE (val);
232 val = GINT32_TO_BE (val);
235 g_assert_not_reached ();
238 if (src->endianness == G_BIG_ENDIAN)
240 for (j = 0; j < src->channels; j++) {
246 guint32 val = (guint32) int_value;
248 switch (src->endianness) {
249 case G_LITTLE_ENDIAN:
250 val = GUINT32_TO_LE (val);
253 val = GUINT32_TO_BE (val);
256 g_assert_not_reached ();
259 if (src->endianness == G_BIG_ENDIAN)
261 for (j = 0; j < src->channels; j++) {
269 POPULATE (gint32, GINT32_TO_BE, GINT32_TO_LE);
271 POPULATE (guint32, GUINT32_TO_BE, GUINT32_TO_LE);
274 g_assert_not_reached ();
279 if (src->width == 32) {
280 gfloat *p = (gfloat *) data;
281 gfloat fval = (gfloat) value;
283 for (j = 0; j < src->channels; j++) {
290 if (src->width == 64) {
291 gdouble *p = (gdouble *) data;
293 for (j = 0; j < src->channels; j++) {
300 g_assert_not_reached ();
302 g_assert_not_reached ();
307 sinesrc_force_caps (src);
309 return GST_DATA (buf);
315 return GST_ELEMENT (g_object_new (TYPE_SINESRC, NULL));
319 sinesrc_set_pre_get_func (SineSrc * src, PreGetFunc func)
321 src->pre_get_func = func;
324 static GstStateChangeReturn
325 sinesrc_change_state (GstElement * element, GstStateChange transition)
329 g_return_val_if_fail (element != NULL, FALSE);
330 sinesrc = SINESRC (element);
332 switch (transition) {
333 case GST_STATE_CHANGE_NULL_TO_READY:
334 case GST_STATE_CHANGE_READY_TO_PAUSED:
335 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
336 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
338 case GST_STATE_CHANGE_PAUSED_TO_READY:
339 sinesrc->newcaps = TRUE;
341 case GST_STATE_CHANGE_READY_TO_NULL:
344 g_assert_not_reached ();
347 if (GST_ELEMENT_CLASS (parent_class)->change_state)
348 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
350 return GST_STATE_CHANGE_SUCCESS;