2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
26 #include <sys/soundcard.h>
31 #include <gstsinesrc.h>
34 GstElementDetails gst_sinesrc_details = {
37 "Create a sine wave of a given frequency and volume",
39 "Erik Walthinsen <omega@cse.ogi.edu>",
44 /* SineSrc signals and args */
59 static void gst_sinesrc_class_init(GstSineSrcClass *klass);
60 static void gst_sinesrc_init(GstSineSrc *sinesrc);
61 static void gst_sinesrc_set_arg(GtkObject *object,GtkArg *arg,guint id);
62 static void gst_sinesrc_get_arg(GtkObject *object,GtkArg *arg,guint id);
63 //static gboolean gst_sinesrc_change_state(GstElement *element,
64 // GstElementState state);
65 //static void gst_sinesrc_close_audio(GstSineSrc *src);
66 //static gboolean gst_sinesrc_open_audio(GstSineSrc *src);
67 void gst_sinesrc_sync_parms(GstSineSrc *sinesrc);
69 static GstBuffer * gst_sinesrc_get(GstPad *pad);
71 static GstElementClass *parent_class = NULL;
72 //static guint gst_sinesrc_signals[LAST_SIGNAL] = { 0 };
75 gst_sinesrc_get_type(void) {
76 static GtkType sinesrc_type = 0;
79 static const GtkTypeInfo sinesrc_info = {
82 sizeof(GstSineSrcClass),
83 (GtkClassInitFunc)gst_sinesrc_class_init,
84 (GtkObjectInitFunc)gst_sinesrc_init,
85 (GtkArgSetFunc)gst_sinesrc_set_arg,
86 (GtkArgGetFunc)gst_sinesrc_get_arg,
87 (GtkClassInitFunc)NULL,
89 sinesrc_type = gtk_type_unique(GST_TYPE_ELEMENT,&sinesrc_info);
95 gst_sinesrc_class_init(GstSineSrcClass *klass) {
96 GtkObjectClass *gtkobject_class;
97 GstElementClass *gstelement_class;
99 gtkobject_class = (GtkObjectClass*)klass;
100 gstelement_class = (GstElementClass*)klass;
102 parent_class = gtk_type_class(GST_TYPE_ELEMENT);
104 gtk_object_add_arg_type("GstSineSrc::volume", GTK_TYPE_DOUBLE,
105 GTK_ARG_READWRITE, ARG_VOLUME);
106 gtk_object_add_arg_type("GstSineSrc::format", GTK_TYPE_INT,
107 GTK_ARG_READWRITE, ARG_FORMAT);
108 gtk_object_add_arg_type("GstSineSrc::channels", GTK_TYPE_INT,
109 GTK_ARG_READWRITE, ARG_CHANNELS);
110 gtk_object_add_arg_type("GstSineSrc::frequency", GTK_TYPE_INT,
111 GTK_ARG_READWRITE, ARG_FREQUENCY);
113 gtkobject_class->set_arg = gst_sinesrc_set_arg;
114 gtkobject_class->get_arg = gst_sinesrc_get_arg;
116 // gstelement_class->change_state = gst_sinesrc_change_state;
119 static void gst_sinesrc_init(GstSineSrc *sinesrc) {
120 sinesrc->srcpad = gst_pad_new("src",GST_PAD_SRC);
121 gst_pad_set_get_function(sinesrc->srcpad,gst_sinesrc_get);
122 gst_element_add_pad(GST_ELEMENT(sinesrc),sinesrc->srcpad);
124 sinesrc->volume = 1.0;
126 sinesrc->format = AFMT_S16_LE;
127 sinesrc->channels = 2;
128 sinesrc->frequency = 44100;
132 sinesrc->sentmeta = FALSE;
136 gst_sinesrc_get(GstPad *pad)
145 g_return_val_if_fail (pad != NULL, NULL);
146 src = GST_SINESRC(gst_pad_get_parent (pad));
148 buf = gst_buffer_new();
149 g_return_val_if_fail (buf, NULL);
150 GST_BUFFER_DATA(buf) = (gpointer)malloc(4096);
151 samples = (gint16*)GST_BUFFER_DATA(buf);
152 GST_BUFFER_SIZE(buf) = 4096;
154 volume = 65535 * src->volume;
155 for (i=0;i<1024;i++) {
156 val = sin((gdouble)i/src->frequency);
157 samples[i] = val * volume;
158 samples[i+1] = samples[i];
161 if (!src->sentmeta) {
162 MetaAudioRaw *newmeta = g_new(MetaAudioRaw,1);
163 memcpy(newmeta,&src->meta,sizeof(MetaAudioRaw));
164 gst_buffer_add_meta(buf,GST_META(newmeta));
165 src->sentmeta = TRUE;
172 static void gst_sinesrc_set_arg(GtkObject *object,GtkArg *arg,guint id) {
175 /* it's not null if we got it, but it might not be ours */
176 g_return_if_fail(GST_IS_SINESRC(object));
177 src = GST_SINESRC(object);
181 src->volume = GTK_VALUE_DOUBLE(*arg);
184 src->format = GTK_VALUE_INT(*arg);
187 src->channels = GTK_VALUE_INT(*arg);
190 src->frequency = GTK_VALUE_INT(*arg);
197 static void gst_sinesrc_get_arg(GtkObject *object,GtkArg *arg,guint id) {
200 /* it's not null if we got it, but it might not be ours */
201 g_return_if_fail(GST_IS_SINESRC(object));
202 src = GST_SINESRC(object);
206 GTK_VALUE_DOUBLE(*arg) = src->volume;
209 GTK_VALUE_INT(*arg) = src->format;
212 GTK_VALUE_INT(*arg) = src->channels;
215 GTK_VALUE_INT(*arg) = src->frequency;
218 arg->type = GTK_TYPE_INVALID;
224 static gboolean gst_sinesrc_change_state(GstElement *element,
225 GstElementState state) {
226 g_return_if_fail(GST_IS_SINESRC(element));
229 case GST_STATE_RUNNING:
230 if (!gst_sinesrc_open_audio(GST_SINESRC(element)))
233 case ~GST_STATE_RUNNING:
234 gst_sinesrc_close_audio(GST_SINESRC(element));
240 if (GST_ELEMENT_CLASS(parent_class)->change_state)
241 return GST_ELEMENT_CLASS(parent_class)->change_state(element,state);
246 void gst_sinesrc_sync_parms(GstSineSrc *sinesrc) {
247 sinesrc->meta.format = sinesrc->format;
248 sinesrc->meta.channels = sinesrc->channels;
249 sinesrc->meta.frequency = sinesrc->frequency;
250 sinesrc->sentmeta = FALSE;