gstreamer/tests/old/testsuite/alsa/sinesrc.c

350 lines
9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
*
* sinesrc.c: An elemnt emitting a sine src in lots of different formats
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "sinesrc.h"
#include <math.h>
#include <string.h> /* memcpy */
#define SAMPLES_PER_WAVE 200
static GstStaticPadTemplate sinesrc_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw-int, "
"endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, "
"signed = (boolean) { FALSE, TRUE }, "
"width = (int) [8, 32], "
"depth = (int) [8, 32], "
"rate = (int) [8000, 192000], "
"channels = (int) [1, 16];"
"audio/x-raw-float, "
"endianness = (int) BYTE_ORDER, "
"width = (int) {32, 64}, "
"rate = (int) [8000, 192000], " "channels = (int) [1, 16]")
);
static GstElementClass *parent_class = NULL;
static void sinesrc_init (SineSrc * src);
static void sinesrc_class_init (SineSrcClass * klass);
static GstData *sinesrc_get (GstPad * pad);
static GstStateChangeReturn sinesrc_change_state (GstElement * element,
GstStateChange transition);
GType
sinesrc_get_type (void)
{
static GType sinesrc_type = 0;
if (!sinesrc_type) {
static const GTypeInfo sinesrc_info = {
sizeof (SineSrcClass), NULL, NULL,
(GClassInitFunc) sinesrc_class_init, NULL, NULL,
sizeof (SineSrc), 0,
(GInstanceInitFunc) sinesrc_init,
};
sinesrc_type = g_type_register_static (GST_TYPE_ELEMENT, "SineSrc",
&sinesrc_info, 0);
}
return sinesrc_type;
}
static void
sinesrc_class_init (SineSrcClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
element_class->change_state = sinesrc_change_state;
Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent) Original commit message from CVS: * ext/alsa/gstalsamixeroptions.c: (gst_alsa_mixer_options_class_init): * ext/alsa/gstalsamixertrack.c: (gst_alsa_mixer_track_class_init): * ext/ogg/gstoggdemux.c: (gst_ogg_pad_class_init): * ext/ogg/gstoggmux.c: (gst_ogg_mux_class_init): * ext/ogg/gstoggparse.c: (gst_ogg_parse_class_init): * gst-libs/gst/audio/gstaudioclock.c: (gst_audio_clock_class_init): * gst-libs/gst/audio/gstaudiofilter.c: (gst_audio_filter_class_init): * gst-libs/gst/audio/gstaudiosink.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstaudiosrc.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstringbuffer.c: (gst_ring_buffer_class_init): * gst-libs/gst/interfaces/colorbalancechannel.c: (gst_color_balance_channel_class_init): * gst-libs/gst/interfaces/mixeroptions.c: (gst_mixer_options_class_init): * gst-libs/gst/interfaces/mixertrack.c: (gst_mixer_track_class_init): * gst-libs/gst/interfaces/tunerchannel.c: (gst_tuner_channel_class_init): * gst-libs/gst/interfaces/tunernorm.c: (gst_tuner_norm_class_init): * gst-libs/gst/netbuffer/gstnetbuffer.c: (gst_netbuffer_class_init): * gst-libs/gst/rtp/gstbasertppayload.c: (gst_basertppayload_class_init): * gst/playback/gstdecodebin.c: (gst_decode_bin_class_init): * gst/playback/gstplaybasebin.c: (gst_play_base_bin_class_init): * gst/playback/gstplaybin.c: (gst_play_bin_class_init): * gst/playback/gststreaminfo.c: (gst_stream_info_class_init): * gst/playback/gststreamselector.c: (gst_stream_selector_class_init): * gst/subparse/gstsubparse.c: (gst_sub_parse_class_init): * gst/tcp/gsttcpclientsink.c: (gst_tcp_client_sink_class_init): * sys/v4l/gstv4lcolorbalance.c: (gst_v4l_color_balance_channel_class_init): * sys/v4l/gstv4ljpegsrc.c: (gst_v4ljpegsrc_class_init): * sys/v4l/gstv4lmjpegsink.c: (gst_v4lmjpegsink_class_init): * sys/v4l/gstv4lmjpegsrc.c: (gst_v4lmjpegsrc_class_init): * sys/v4l/gstv4ltuner.c: (gst_v4l_tuner_channel_class_init), (gst_v4l_tuner_norm_class_init): * sys/ximage/ximagesink.c: (gst_ximagesink_class_init): * sys/xvimage/xvimagesink.c: (gst_xvimagesink_class_init): * tests/old/testsuite/alsa/sinesrc.c: (sinesrc_class_init): Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent)
2006-04-08 21:02:53 +00:00
parent_class = g_type_class_peek_parent (klass);
}
static void
sinesrc_init (SineSrc * src)
{
src->src =
gst_pad_new_from_template (gst_static_pad_template_get
(&sinesrc_src_factory), "src");
gst_element_add_pad (GST_ELEMENT (src), src->src);
gst_pad_set_get_function (src->src, sinesrc_get);
src->width = 16;
src->depth = 16;
src->sign = TRUE;
src->endianness = G_BYTE_ORDER;
src->rate = 44100;
src->channels = 1;
src->type = SINE_SRC_INT;
src->newcaps = TRUE;
src->pre_get_func = NULL;
GST_OBJECT (src)->name = "sinesrc";
}
static void
sinesrc_force_caps (SineSrc * src)
{
GstCaps *caps;
if (!src->newcaps)
return;
src->newcaps = FALSE;
switch (src->type) {
case SINE_SRC_INT:
caps = gst_caps_new_simple ("audio/x-raw-int",
"signed", G_TYPE_BOOLEAN, src->sign,
"depth", G_TYPE_INT, src->depth, NULL);
if (src->width > 8)
gst_caps_set_simple (caps,
"endianness", G_TYPE_INT, src->endianness, NULL);
break;
case SINE_SRC_FLOAT:
g_assert (src->width == 32 || src->width == 64);
caps = gst_caps_new_simple ("audio/x-raw-float",
"endianness", G_TYPE_INT, src->endianness, NULL);
break;
default:
caps = NULL;
g_assert_not_reached ();
}
gst_caps_set_simple (caps,
"width", G_TYPE_INT, src->width,
"rate", G_TYPE_INT, src->rate,
"channels", G_TYPE_INT, src->channels, NULL);
if (gst_pad_try_set_caps (src->src, caps) != GST_PAD_LINK_OK)
g_assert_not_reached ();
}
/* always return 1 wave
* there are 200 waves in 1 second, so the frequency is samplerate/200
*/
static guint8
UIDENTITY (guint8 x)
{
return x;
};
static gint8
IDENTITY (gint8 x)
{
return x;
};
#define POPULATE(format, be_func, le_func) G_STMT_START {\
format val = (format) int_value;\
format *p = data;\
switch (src->endianness) {\
case G_LITTLE_ENDIAN:\
val = le_func (val);\
break;\
case G_BIG_ENDIAN:\
val = be_func (val);\
break;\
default: \
g_assert_not_reached ();\
};\
for (j = 0; j < src->channels; j++) {\
*p = val;\
p ++;\
}\
data = p;\
} G_STMT_END
static GstData *
sinesrc_get (GstPad * pad)
{
GstBuffer *buf;
SineSrc *src;
void *data;
gint i, j;
gdouble value;
g_return_val_if_fail (pad != NULL, NULL);
src = SINESRC (gst_pad_get_parent (pad));
if (src->pre_get_func)
src->pre_get_func (src);
buf = gst_buffer_new_and_alloc ((src->width / 8) * src->channels *
SAMPLES_PER_WAVE);
g_assert (buf);
data = GST_BUFFER_DATA (buf);
g_assert (data);
for (i = 0; i < SAMPLES_PER_WAVE; i++) {
value = sin (i * 2 * M_PI / SAMPLES_PER_WAVE);
switch (src->type) {
case SINE_SRC_INT:{
gint64 int_value =
(value + (src->sign ? 0 : 1)) * (((guint64) 1) << (src->depth - 1));
if (int_value ==
(1 + (src->sign ? 0 : 1)) * (((guint64) 1) << (src->depth - 1)))
int_value--;
switch (src->width) {
case 8:
if (src->sign)
POPULATE (gint8, IDENTITY, IDENTITY);
else
POPULATE (guint8, UIDENTITY, UIDENTITY);
break;
case 16:
if (src->sign)
POPULATE (gint16, GINT16_TO_BE, GINT16_TO_LE);
else
POPULATE (guint16, GUINT16_TO_BE, GUINT16_TO_LE);
break;
case 24:
if (src->sign) {
gpointer p;
gint32 val = (gint32) int_value;
switch (src->endianness) {
case G_LITTLE_ENDIAN:
val = GINT32_TO_LE (val);
break;
case G_BIG_ENDIAN:
val = GINT32_TO_BE (val);
break;
default:
g_assert_not_reached ();
};
p = &val;
if (src->endianness == G_BIG_ENDIAN)
p++;
for (j = 0; j < src->channels; j++) {
memcpy (data, p, 3);
data += 3;
}
} else {
gpointer p;
guint32 val = (guint32) int_value;
switch (src->endianness) {
case G_LITTLE_ENDIAN:
val = GUINT32_TO_LE (val);
break;
case G_BIG_ENDIAN:
val = GUINT32_TO_BE (val);
break;
default:
g_assert_not_reached ();
};
p = &val;
if (src->endianness == G_BIG_ENDIAN)
p++;
for (j = 0; j < src->channels; j++) {
memcpy (data, p, 3);
data += 3;
}
}
break;
case 32:
if (src->sign)
POPULATE (gint32, GINT32_TO_BE, GINT32_TO_LE);
else
POPULATE (guint32, GUINT32_TO_BE, GUINT32_TO_LE);
break;
default:
g_assert_not_reached ();
}
break;
}
case SINE_SRC_FLOAT:
if (src->width == 32) {
gfloat *p = (gfloat *) data;
gfloat fval = (gfloat) value;
for (j = 0; j < src->channels; j++) {
*p = fval;
p++;
}
data = p;
break;
}
if (src->width == 64) {
gdouble *p = (gdouble *) data;
for (j = 0; j < src->channels; j++) {
*p = value;
p++;
}
data = p;
break;
}
g_assert_not_reached ();
default:
g_assert_not_reached ();
}
}
if (src->newcaps) {
sinesrc_force_caps (src);
}
return GST_DATA (buf);
}
GstElement *
sinesrc_new (void)
{
return GST_ELEMENT (g_object_new (TYPE_SINESRC, NULL));
}
void
sinesrc_set_pre_get_func (SineSrc * src, PreGetFunc func)
{
src->pre_get_func = func;
}
static GstStateChangeReturn
sinesrc_change_state (GstElement * element, GstStateChange transition)
{
SineSrc *sinesrc;
g_return_val_if_fail (element != NULL, FALSE);
sinesrc = SINESRC (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
case GST_STATE_CHANGE_READY_TO_PAUSED:
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
sinesrc->newcaps = TRUE;
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
g_assert_not_reached ();
}
if (GST_ELEMENT_CLASS (parent_class)->change_state)
return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
return GST_STATE_CHANGE_SUCCESS;
}