2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 /*#define DEBUG_ENABLED */
28 #include <gstaudioscale.h>
29 #include <gst/audio/audio.h>
30 #include <gst/resample/resample.h>
32 /* elementfactory information */
33 static GstElementDetails audioscale_details = {
39 "Wim Taymans <wim.taymans@chello.be>",
43 /* Audioscale signals and args */
57 static GstPadTemplate *
60 static GstPadTemplate *template = NULL;
63 template = gst_pad_template_new ("sink",
68 "audio/raw", GST_AUDIO_INT_PAD_TEMPLATE_PROPS), NULL);
73 static GstPadTemplate *
76 static GstPadTemplate *template = NULL;
79 template = gst_pad_template_new ("src",
84 "audio/raw", GST_AUDIO_INT_PAD_TEMPLATE_PROPS), NULL);
89 #define GST_TYPE_AUDIOSCALE_METHOD (gst_audioscale_method_get_type())
91 gst_audioscale_method_get_type (void)
93 static GType audioscale_method_type = 0;
94 static GEnumValue audioscale_methods[] = {
95 { RESAMPLE_NEAREST, "0", "Nearest" },
96 { RESAMPLE_BILINEAR, "1", "Bilinear" },
97 { RESAMPLE_SINC, "2", "Sinc" },
100 if(!audioscale_method_type){
101 audioscale_method_type = g_enum_register_static("GstAudioscaleMethod",
104 return audioscale_method_type;
107 static void gst_audioscale_class_init (AudioscaleClass *klass);
108 static void gst_audioscale_init (Audioscale *audioscale);
110 static void gst_audioscale_chain (GstPad *pad, GstBuffer *buf);
112 static void gst_audioscale_set_property (GObject * object, guint prop_id,
113 const GValue * value, GParamSpec * pspec);
114 static void gst_audioscale_get_property (GObject * object, guint prop_id,
115 GValue * value, GParamSpec * pspec);
117 static GstElementClass *parent_class = NULL;
119 /*static guint gst_audioscale_signals[LAST_SIGNAL] = { 0 }; */
122 audioscale_get_type (void)
124 static GType audioscale_type = 0;
126 if (!audioscale_type) {
127 static const GTypeInfo audioscale_info = {
128 sizeof(AudioscaleClass), NULL,
130 (GClassInitFunc)gst_audioscale_class_init,
135 (GInstanceInitFunc)gst_audioscale_init,
137 audioscale_type = g_type_register_static(GST_TYPE_ELEMENT, "Audioscale", &audioscale_info, 0);
139 return audioscale_type;
143 gst_audioscale_class_init (AudioscaleClass *klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gobject_class = (GObjectClass*)klass;
149 gstelement_class = (GstElementClass*)klass;
151 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FREQUENCY,
152 g_param_spec_int ("frequency","frequency","frequency",
153 0,G_MAXINT,44100,G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
154 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FILTERLEN,
155 g_param_spec_int ("filter_length", "filter_length", "filter_length",
156 0, G_MAXINT, 16, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_METHOD,
158 g_param_spec_enum ("method", "method", "method", GST_TYPE_AUDIOSCALE_METHOD,
159 RESAMPLE_SINC, G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
161 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
163 gobject_class->set_property = gst_audioscale_set_property;
164 gobject_class->get_property = gst_audioscale_get_property;
168 static GstPadLinkReturn
169 gst_audioscale_sinkconnect (GstPad * pad, GstCaps * caps)
171 Audioscale *audioscale;
176 audioscale = GST_AUDIOSCALE (gst_pad_get_parent (pad));
177 r = audioscale->resample;
179 gst_caps_get_int (caps, "rate", &rate);
180 gst_caps_get_int (caps, "channels", &r->channels);
186 newcaps = gst_caps_copy (caps);
187 gst_caps_set (newcaps, "rate", GST_PROPS_INT_TYPE, audioscale->targetfrequency, NULL);
189 if (GST_CAPS_IS_FIXED (caps))
190 return gst_pad_try_set_caps (audioscale->srcpad, newcaps);
192 return GST_PAD_LINK_DELAYED;
196 gst_audioscale_get_buffer (void *priv, unsigned int size)
198 Audioscale * audioscale = priv;
200 audioscale->outbuf = gst_buffer_new();
201 GST_BUFFER_SIZE(audioscale->outbuf) = size;
202 GST_BUFFER_DATA(audioscale->outbuf) = g_malloc(size);
203 GST_BUFFER_TIMESTAMP(audioscale->outbuf) = audioscale->offset * GST_SECOND / audioscale->targetfrequency;
204 audioscale->offset += size / sizeof(gint16) / audioscale->resample->channels;
206 return GST_BUFFER_DATA(audioscale->outbuf);
210 gst_audioscale_init (Audioscale *audioscale)
214 audioscale->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (sink_template), "sink");
215 gst_element_add_pad(GST_ELEMENT(audioscale),audioscale->sinkpad);
216 gst_pad_set_chain_function(audioscale->sinkpad,gst_audioscale_chain);
217 gst_pad_set_link_function (audioscale->sinkpad, gst_audioscale_sinkconnect);
219 audioscale->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (src_template), "src");
221 gst_element_add_pad(GST_ELEMENT(audioscale),audioscale->srcpad);
223 r = g_new0(resample_t,1);
224 audioscale->resample = r;
226 r->priv = audioscale;
227 r->get_buffer = gst_audioscale_get_buffer;
228 r->method = RESAMPLE_SINC;
230 r->filter_length = 16;
233 r->format = RESAMPLE_S16;
238 /* we will be reinitialized when the G_PARAM_CONSTRUCTs hit */
242 gst_audioscale_chain (GstPad *pad, GstBuffer *buf)
244 Audioscale *audioscale;
248 g_return_if_fail(pad != NULL);
249 g_return_if_fail(GST_IS_PAD(pad));
250 g_return_if_fail(buf != NULL);
252 audioscale = GST_AUDIOSCALE (gst_pad_get_parent (pad));
253 data = GST_BUFFER_DATA(buf);
254 size = GST_BUFFER_SIZE(buf);
257 "gst_audioscale_chain: got buffer of %ld bytes in '%s'\n",
258 size, gst_element_get_name (GST_ELEMENT (audioscale)));
261 resample_scale (audioscale->resample, data, size);
263 gst_pad_push (audioscale->srcpad, audioscale->outbuf);
265 gst_buffer_unref (buf);
269 gst_audioscale_set_property (GObject * object, guint prop_id,
270 const GValue * value, GParamSpec * pspec)
275 /* it's not null if we got it, but it might not be ours */
276 g_return_if_fail(GST_IS_AUDIOSCALE(object));
277 src = GST_AUDIOSCALE(object);
282 src->targetfrequency = g_value_get_int (value);
283 r->o_rate = src->targetfrequency;
286 r->filter_length = g_value_get_int (value);
287 GST_DEBUG_OBJECT (GST_ELEMENT(src), "new filter length %d\n", r->filter_length);
290 r->method = g_value_get_enum (value);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 gst_audioscale_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
306 src = GST_AUDIOSCALE (object);
311 g_value_set_int (value, src->targetfrequency);
314 g_value_set_int (value, r->filter_length);
317 g_value_set_enum (value, r->method);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 plugin_init (GModule *module, GstPlugin *plugin)
329 GstElementFactory *factory;
331 /* create an elementfactory for the audioscale element */
332 factory = gst_element_factory_new ("audioscale", GST_TYPE_AUDIOSCALE, &audioscale_details);
333 g_return_val_if_fail(factory != NULL, FALSE);
334 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
336 /* load support library */
337 if (!gst_library_load ("gstresample"))
343 GstPluginDesc plugin_desc = {