2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2001> Steve Baker <stevebaker_org@yahoo.co.uk>
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.
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.
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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 //#define DEBUG_ENABLED
25 #include "gstladspa.h"
31 static GstPadTemplate*
32 ladspa_src_factory (void)
43 "format", GST_PROPS_STRING ("float"),
44 "layout", GST_PROPS_STRING ("gfloat"),
45 "intercept", GST_PROPS_FLOAT(0.0),
46 "slope", GST_PROPS_FLOAT(1.0),
47 "channels", GST_PROPS_INT (1),
48 "rate", GST_PROPS_INT_RANGE (0,G_MAXINT),
53 static GstPadTemplate*
54 ladspa_sink_factory (void)
65 "format", GST_PROPS_STRING ("float"),
66 "layout", GST_PROPS_STRING ("gfloat"),
67 "intercept", GST_PROPS_FLOAT(0.0),
68 "slope", GST_PROPS_FLOAT(1.0),
69 "channels", GST_PROPS_INT (1),
70 "rate", GST_PROPS_INT_RANGE (0,G_MAXINT),
83 static GstPadTemplate *srctempl, *sinktempl;
85 static void gst_ladspa_class_init (GstLADSPAClass *klass);
86 static void gst_ladspa_init (GstLADSPA *ladspa);
88 static GstPadNegotiateReturn gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data);
89 static GstPadNegotiateReturn gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data);
90 static GstPadNegotiateReturn gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data);
91 static void gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad);
93 static void gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
94 static void gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
96 static gboolean gst_ladspa_instantiate (GstLADSPA *ladspa);
97 static void gst_ladspa_activate(GstLADSPA *ladspa);
98 static void gst_ladspa_deactivate(GstLADSPA *ladspa);
100 static GstElementStateReturn gst_ladspa_change_state (GstElement *element);
101 static void gst_ladspa_loop (GstElement *element);
102 static void gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf);
103 static GstBuffer * gst_ladspa_get_mono(GstPad *pad);
104 static GstBuffer * gst_ladspa_get(GstPad *pad);
106 static GstElementClass *parent_class = NULL;
107 //static guint gst_ladspa_signals[LAST_SIGNAL] = { 0 };
109 static GstPlugin *ladspa_plugin;
110 static GHashTable *ladspa_descriptors;
113 gst_ladspa_class_init (GstLADSPAClass *klass)
115 GObjectClass *gobject_class;
116 GstElementClass *gstelement_class;
117 LADSPA_Descriptor *desc;
118 gint i,current_portnum,sinkcount,srccount,controlcount;
120 gint argtype,argperms;
121 GParamSpec *paramspec = NULL;
124 gobject_class = (GObjectClass*)klass;
125 gstelement_class = (GstElementClass*)klass;
127 gobject_class->set_property = gst_ladspa_set_property;
128 gobject_class->get_property = gst_ladspa_get_property;
130 gstelement_class->change_state = gst_ladspa_change_state;
132 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOOP_BASED,
133 g_param_spec_boolean("loop-based","loop-based","loop-based",
134 FALSE,G_PARAM_READWRITE));
136 // look up and store the ladspa descriptor
137 klass->descriptor = g_hash_table_lookup(ladspa_descriptors,GINT_TO_POINTER(G_TYPE_FROM_CLASS(klass)));
138 desc = klass->descriptor;
140 klass->numports = desc->PortCount;
142 klass->numsinkpads = 0;
143 klass->numsrcpads = 0;
144 klass->numcontrols = 0;
146 // walk through the ports, count the input, output and control ports
147 for (i=0;i<desc->PortCount;i++) {
148 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
149 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
150 klass->numsinkpads++;
153 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
154 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
158 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
159 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
160 klass->numcontrols++;
164 klass->srcpad_portnums = g_new0(gint,klass->numsrcpads);
165 klass->sinkpad_portnums = g_new0(gint,klass->numsinkpads);
166 klass->control_portnums = g_new0(gint,klass->numcontrols);
171 // walk through the ports, note the portnums for srcpads, sinkpads and control params
172 for (i=0;i<desc->PortCount;i++) {
173 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
174 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
175 //g_print("input port %d\n", i);
176 klass->sinkpad_portnums[sinkcount++] = i;
179 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
180 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])){
181 //g_print("output port %d\n", i);
182 klass->srcpad_portnums[srccount++] = i;
185 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
186 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])){
187 //g_print("control port %d\n", i);
188 klass->control_portnums[controlcount++] = i;
192 // no sink pads - we'll use get mode and add params for samplerate and buffersize
193 if (klass->numsinkpads == 0 && klass->numsrcpads > 0){
194 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_SAMPLERATE,
195 g_param_spec_int("samplerate","samplerate","samplerate",
196 0,G_MAXINT,44100,G_PARAM_READWRITE));
197 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BUFFERSIZE,
198 g_param_spec_int("buffersize","buffersize","buffersize",
199 0,G_MAXINT,64,G_PARAM_READWRITE));
203 // now build the contorl info from the control ports
204 klass->control_info = g_new0(ladspa_control_info,klass->numcontrols);
206 for (i=0;i<klass->numcontrols;i++) {
208 current_portnum = klass->control_portnums[i];
210 // short name for hint descriptor
211 hintdesc = desc->PortRangeHints[current_portnum].HintDescriptor;
213 // get the various bits
214 if (LADSPA_IS_HINT_TOGGLED(hintdesc))
215 klass->control_info[i].toggled = TRUE;
216 if (LADSPA_IS_HINT_LOGARITHMIC(hintdesc))
217 klass->control_info[i].logarithmic = TRUE;
218 if (LADSPA_IS_HINT_INTEGER(hintdesc))
219 klass->control_info[i].integer = TRUE;
221 // figure out the argument details
222 if (klass->control_info[i].toggled) argtype = G_TYPE_BOOLEAN;
223 else if (klass->control_info[i].integer) argtype = G_TYPE_INT;
224 else argtype = G_TYPE_FLOAT;
227 if (LADSPA_IS_HINT_BOUNDED_BELOW(hintdesc)) {
228 klass->control_info[i].lower = TRUE;
229 klass->control_info[i].lowerbound =
230 desc->PortRangeHints[current_portnum].LowerBound;
232 if (argtype==G_TYPE_INT) klass->control_info[i].lowerbound = (gfloat)G_MININT;
233 if (argtype==G_TYPE_FLOAT) klass->control_info[i].lowerbound = G_MINFLOAT;
236 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hintdesc)) {
237 klass->control_info[i].upper = TRUE;
238 klass->control_info[i].upperbound =
239 desc->PortRangeHints[current_portnum].UpperBound;
240 if (LADSPA_IS_HINT_SAMPLE_RATE(hintdesc))
241 klass->control_info[i].samplerate = TRUE;
243 if (argtype==G_TYPE_INT) klass->control_info[i].upperbound = (gfloat)G_MAXINT;
244 if (argtype==G_TYPE_FLOAT) klass->control_info[i].upperbound = G_MAXFLOAT;
247 if (LADSPA_IS_PORT_INPUT(desc->PortDescriptors[current_portnum])) {
248 argperms = G_PARAM_READWRITE;
249 klass->control_info[i].writable = TRUE;
251 argperms = G_PARAM_READABLE;
252 klass->control_info[i].writable = FALSE;
255 klass->control_info[i].name = g_strdup(desc->PortNames[current_portnum]);
256 argname = g_strdup(klass->control_info[i].name);
257 // this is the same thing that param_spec_* will do
258 g_strcanon (argname, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
260 // check for duplicate property names
261 if (g_object_class_find_property(G_OBJECT_CLASS(klass), argname) != NULL){
263 gchar *numargname = g_strdup_printf("%s_%d",argname,numarg++);
264 while (g_object_class_find_property(G_OBJECT_CLASS(klass), numargname) != NULL){
266 numargname = g_strdup_printf("%s_%d",argname,numarg++);
268 argname = numargname;
271 if (argtype==G_TYPE_BOOLEAN){
272 paramspec = g_param_spec_boolean(argname,argname,argname, FALSE, argperms);
273 } else if (argtype==G_TYPE_INT){
274 paramspec = g_param_spec_int(argname,argname,argname,
275 (gint)klass->control_info[i].lowerbound, (gint)klass->control_info[i].upperbound, 0, argperms);
277 paramspec = g_param_spec_float(argname,argname,argname,
278 klass->control_info[i].lowerbound, klass->control_info[i].upperbound,
279 (klass->control_info[i].lowerbound + klass->control_info[i].upperbound) / 2.0f, argperms);
281 g_object_class_install_property(G_OBJECT_CLASS(klass), i+ARG_LAST, paramspec);
283 g_print("added arg %s from %s\n",argname, klass->control_info[i].name);
288 gst_ladspa_init (GstLADSPA *ladspa)
290 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
292 LADSPA_Descriptor *desc;
293 gint i,sinkcount,srccount,controlcount;
295 desc = oclass->descriptor;
296 ladspa->descriptor = oclass->descriptor;
298 // allocate the various arrays
299 ladspa->srcpads = g_new0(GstPad*,oclass->numsrcpads);
300 ladspa->sinkpads = g_new0(GstPad*,oclass->numsinkpads);
301 ladspa->controls = g_new(gfloat,oclass->numcontrols);
303 // walk through the ports and add all the pads
307 for (i=0;i<desc->PortCount;i++) {
308 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
309 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
310 ladspa->sinkpads[sinkcount] = gst_pad_new_from_template
311 (sinktempl, (gchar *)desc->PortNames[i]);
312 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->sinkpads[sinkcount]);
315 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[i]) &&
316 LADSPA_IS_PORT_OUTPUT(desc->PortDescriptors[i])) {
317 ladspa->srcpads[srccount] = gst_pad_new_from_template
318 (srctempl, (gchar *)desc->PortNames[i]);
319 gst_element_add_pad(GST_ELEMENT(ladspa),ladspa->srcpads[srccount]);
322 if (LADSPA_IS_PORT_CONTROL(desc->PortDescriptors[i]) &&
323 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[i])) {
325 // use the lowerbound as the default value if it exists
326 if (oclass->control_info[controlcount].lower){
327 ladspa->controls[controlcount]=oclass->control_info[controlcount].lowerbound;
329 ladspa->controls[controlcount] = 0.0;
335 ladspa->samplerate = 0;
336 ladspa->buffersize = 0;
337 ladspa->newcaps = FALSE;
338 ladspa->activated = FALSE;
341 if (sinkcount==1 && srccount==1){
342 //g_print("inplace mono chain mode\n");
343 gst_pad_set_negotiate_function (ladspa->sinkpads[0], gst_ladspa_negotiate_sink_mono);
344 gst_pad_set_chain_function(ladspa->sinkpads[0],gst_ladspa_chain_inplace_mono);
345 gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_mono);
348 // mono get (no sink pads)
349 if (sinkcount==0 && srccount == 1){
350 //g_print("get mode\n");
351 ladspa->newcaps = TRUE;
352 ladspa->samplerate = 44100;
353 ladspa->buffersize = 64;
354 gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get_mono);
355 gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
356 gst_ladspa_instantiate(ladspa);
360 if (sinkcount==0 && srccount > 1){
361 //g_print("multi get mode\n");
362 ladspa->newcaps = TRUE;
363 ladspa->samplerate = 44100;
364 ladspa->buffersize = 64;
365 gst_pad_set_get_function(ladspa->srcpads[0],gst_ladspa_get);
366 gst_pad_set_negotiate_function (ladspa->srcpads[0], gst_ladspa_negotiate_src_get_mono);
367 gst_ladspa_instantiate(ladspa);
368 ladspa->buffers = g_new0(GstBuffer*,oclass->numsrcpads);
372 static GstPadNegotiateReturn
373 gst_ladspa_negotiate_src_mono (GstPad *pad, GstCaps **caps, gpointer *data)
375 GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
377 // have to instantiate ladspa plugin when samplerate changes (groan)
378 if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
379 ladspa->samplerate = gst_caps_get_int (*caps, "rate");
380 if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
382 return gst_pad_negotiate_proxy (pad, ladspa->sinkpads[0], caps);
385 static GstPadNegotiateReturn
386 gst_ladspa_negotiate_sink_mono (GstPad *pad, GstCaps **caps, gpointer *data)
388 GstLADSPA *ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
390 // have to instantiate ladspa plugin when samplerate changes (groan)
391 if (ladspa->samplerate != gst_caps_get_int (*caps, "rate")){
392 ladspa->samplerate = gst_caps_get_int (*caps, "rate");
393 if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
395 return gst_pad_negotiate_proxy (pad, ladspa->srcpads[0], caps);
398 static GstPadNegotiateReturn
399 gst_ladspa_negotiate_src_get_mono (GstPad *pad, GstCaps **caps, gpointer *data)
402 //g_print("gst_ladspa_negotiate_src_get_mono\n");
404 g_return_val_if_fail (pad != NULL, GST_PAD_NEGOTIATE_FAIL);
405 ladspa = (GstLADSPA*)GST_OBJECT_PARENT (pad);
406 ladspa->samplerate = gst_caps_get_int (*caps, "rate");
407 if (!gst_ladspa_instantiate(ladspa)) return GST_PAD_NEGOTIATE_FAIL;
408 return GST_PAD_NEGOTIATE_AGREE;
410 return GST_PAD_NEGOTIATE_FAIL;
414 gst_ladspa_force_caps(GstLADSPA *ladspa, GstPad *pad) {
416 // g_print("forcing caps\n");
417 gst_pad_set_caps (pad, gst_caps_new (
421 "format", GST_PROPS_STRING ("float"),
422 "layout", GST_PROPS_STRING ("gfloat"),
423 "intercept", GST_PROPS_FLOAT(0.0),
424 "slope", GST_PROPS_FLOAT(1.0),
425 "rate", GST_PROPS_INT (ladspa->samplerate),
426 "channels", GST_PROPS_INT (1),
430 ladspa->newcaps=FALSE;
434 gst_ladspa_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
436 GstLADSPA *ladspa = (GstLADSPA*)object;
437 gint cid = prop_id - ARG_LAST;
438 GstLADSPAClass *oclass;
439 ladspa_control_info *control_info;
442 // these are only registered in get mode
445 ladspa->samplerate = g_value_get_int (value);
446 ladspa->newcaps=TRUE;
449 ladspa->buffersize = g_value_get_int (value);
453 // is it a ladspa plugin arg?
457 if (id == ARG_LOOP_BASED) {
458 // we can only do this in NULL state
459 g_return_if_fail (GST_STATE(object) != GST_STATE_NULL);
460 ladspa->loopbased = g_value_get_boolean (value);
461 if (ladspa->loopbased) {
462 gst_element_set_loop_function (GST_ELEMENT (ladspa), gst_ladspa_loop);
464 gst_element_set_loop_function (GST_ELEMENT (ladspa), NULL);
469 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
471 // verify it exists and is a control (not a port)
472 g_return_if_fail(cid < oclass->numcontrols);
474 control_info = &(oclass->control_info[cid]);
475 g_return_if_fail (control_info->name != NULL);
477 // check to see if it's writable
478 g_return_if_fail (control_info->writable);
480 // g_print("set arg %s to %f\n",control_info->name,ladspa->controls[cid]);
482 // now see what type it is
483 if (control_info->toggled) {
484 if (g_value_get_boolean (value))
485 ladspa->controls[cid] = 1.0;
487 ladspa->controls[cid] = 0.0;
488 } else if (control_info->integer) {
489 val = (gfloat)g_value_get_int (value);
490 ladspa->controls[cid] = val;
492 val = g_value_get_float (value);
493 ladspa->controls[cid] = val;
498 gst_ladspa_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
500 GstLADSPA *ladspa = (GstLADSPA*)object;
501 gint cid = prop_id - ARG_LAST;
502 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (object));
503 ladspa_control_info *control_info;
505 // these are only registered in get mode
508 g_value_set_int (value, ladspa->samplerate);
511 g_value_set_int (value, ladspa->buffersize);
516 // verify it exists and is a control (not a port)
517 if (cid >= oclass->numcontrols) return;
518 control_info = &(oclass->control_info[cid]);
519 if (control_info->name == NULL) return;
521 //g_print("got arg %s as %f\n",control_info->name,ladspa->controls[cid]);
523 // now see what type it is
524 if (control_info->toggled) {
525 if (ladspa->controls[cid] == 1.0)
526 g_value_set_boolean (value, TRUE);
528 g_value_set_boolean (value, FALSE);
529 } else if (control_info->integer) {
530 g_value_set_int (value, (gint)ladspa->controls[cid]);
532 g_value_set_float (value, ladspa->controls[cid]);
537 gst_ladspa_instantiate (GstLADSPA *ladspa)
539 LADSPA_Descriptor *desc;
541 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
542 gboolean was_activated;
544 desc = ladspa->descriptor;
546 // check for old handle
547 was_activated = ladspa->activated;
548 if (ladspa->handle != NULL){
549 gst_ladspa_deactivate(ladspa);
550 desc->cleanup(ladspa->handle);
553 // instantiate the plugin
554 ladspa->handle = desc->instantiate(desc,ladspa->samplerate);
555 g_return_val_if_fail (ladspa->handle != NULL, FALSE);
557 // walk through the ports and add all the arguments
558 for (i=0;i<oclass->numcontrols;i++) {
559 // connect the argument to the plugin
560 //g_print("added control port %d\n", oclass->control_portnums[i]);
561 desc->connect_port(ladspa->handle,
562 oclass->control_portnums[i],
563 &(ladspa->controls[i]));
566 // reactivate if it was activated before the reinstantiation
568 gst_ladspa_activate(ladspa);
573 static GstElementStateReturn
574 gst_ladspa_change_state (GstElement *element)
576 LADSPA_Descriptor *desc;
577 GstLADSPA *ladspa = (GstLADSPA*)element;
578 // GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT(ladspa)->klass);
579 desc = ladspa->descriptor;
581 //g_print("changing state\n");
582 switch (GST_STATE_TRANSITION (element)) {
583 case GST_STATE_NULL_TO_READY:
584 gst_ladspa_activate(ladspa);
586 case GST_STATE_READY_TO_NULL:
587 gst_ladspa_deactivate(ladspa);
593 if (GST_ELEMENT_CLASS (parent_class)->change_state)
594 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
596 return GST_STATE_SUCCESS;
600 gst_ladspa_activate(GstLADSPA *ladspa)
602 LADSPA_Descriptor *desc;
603 desc = ladspa->descriptor;
605 if (ladspa->activated){
606 gst_ladspa_deactivate(ladspa);
609 //g_print("activating\n");
611 // activate the plugin (function might be null)
612 if (desc->activate != NULL){
613 desc->activate(ladspa->handle);
616 ladspa->activated = TRUE;
620 gst_ladspa_deactivate(GstLADSPA *ladspa)
622 LADSPA_Descriptor *desc;
623 desc = ladspa->descriptor;
625 //g_print("deactivating\n");
627 // deactivate the plugin (function might be null)
628 if (ladspa->activated && desc->deactivate != NULL){
629 desc->deactivate(ladspa->handle);
631 ladspa->activated = FALSE;
635 gst_ladspa_loop (GstElement *element)
638 GstLADSPA *ladspa = (GstLADSPA *)element;
639 GstLADSPAClass *oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
640 LADSPA_Descriptor *desc;
642 desc = ladspa->descriptor;
643 printf("looping something\n");
645 // first get all the necessary data from the input ports
646 for (i=0;i<oclass->numsinkpads;i++){
647 ladspa->buffers[i] = gst_pad_pull(ladspa->sinkpads[i]);
648 printf("pulling buffer %d\n", i);
651 for (i=0;i<oclass->numsinkpads;i++) {
652 // desc->connect_port(ladspa->handle,i,&(ladspa->controls[i]));
655 for (i=0;i<oclass->numsrcpads && i<oclass->numsinkpads;i++){
656 printf("pushing buffer %d\n", i);
657 gst_pad_push (ladspa->srcpads[i], ladspa->buffers[i]);
658 ladspa->buffers[i] = NULL;
663 gst_ladspa_chain_inplace_mono (GstPad *pad,GstBuffer *buf)
665 LADSPA_Descriptor *desc;
667 unsigned long num_samples;
670 GstLADSPAClass *oclass;
672 g_return_if_fail(pad != NULL);
673 g_return_if_fail(GST_IS_PAD(pad));
674 g_return_if_fail(buf != NULL);
676 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
677 g_return_if_fail(ladspa != NULL);
679 // this might happen if caps nego hasn't happened
680 g_return_if_fail(ladspa->handle != NULL);
682 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS (ladspa));
683 data=(LADSPA_Data*)GST_BUFFER_DATA(buf);
684 num_samples = GST_BUFFER_SIZE(buf) / sizeof(gfloat);
686 desc = ladspa->descriptor;
688 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],data);
689 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
691 desc->run(ladspa->handle,num_samples);
693 desc->connect_port(ladspa->handle,oclass->sinkpad_portnums[0],NULL);
694 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
696 gst_pad_push (ladspa->srcpads[0], buf);
700 gst_ladspa_get_mono(GstPad *pad)
702 LADSPA_Descriptor *desc;
706 GstLADSPAClass *oclass;
709 g_return_val_if_fail(pad != NULL, NULL);
710 g_return_val_if_fail(GST_IS_PAD(pad), NULL);
712 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
713 g_return_val_if_fail(ladspa != NULL, NULL);
715 // this might happen if caps nego hasn't happened
716 g_return_val_if_fail(ladspa->handle != NULL, NULL);
718 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
720 if (ladspa->newcaps) {
721 gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
724 buf = gst_buffer_new();
725 g_return_val_if_fail (buf, NULL);
726 data = g_new(LADSPA_Data, ladspa->buffersize);
727 GST_BUFFER_DATA(buf) = (gpointer) data;
728 GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
729 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
731 desc = ladspa->descriptor;
732 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
733 desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);
734 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
740 gst_ladspa_get(GstPad *pad)
742 LADSPA_Descriptor *desc;
746 GstLADSPAClass *oclass;
749 g_return_val_if_fail(pad != NULL, NULL);
750 g_return_val_if_fail(GST_IS_PAD(pad), NULL);
752 ladspa = (GstLADSPA *)gst_pad_get_parent (pad);
753 g_return_val_if_fail(ladspa != NULL, NULL);
755 // this might happen if caps nego hasn't happened
756 g_return_val_if_fail(ladspa->handle != NULL, NULL);
758 oclass = (GstLADSPAClass*)(G_OBJECT_GET_CLASS(ladspa));
760 if (ladspa->newcaps) {
761 gst_ladspa_force_caps(ladspa, ladspa->srcpads[0]);
764 buf = gst_buffer_new();
765 g_return_val_if_fail (buf, NULL);
766 data = g_new(LADSPA_Data, ladspa->buffersize);
767 GST_BUFFER_DATA(buf) = (gpointer) data;
768 GST_BUFFER_SIZE(buf) = sizeof(LADSPA_Data) * ladspa->buffersize;
769 GST_BUFFER_TIMESTAMP(buf) = ladspa->timestamp;
770 ladspa->timestamp+= ladspa->buffersize * ladspa->samplerate * 10^9;
772 desc = ladspa->descriptor;
773 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],data);
774 desc->run(ladspa->handle,(unsigned long)ladspa->buffersize);
775 desc->connect_port(ladspa->handle,oclass->srcpad_portnums[0],NULL);
781 ladspa_describe_plugin(const char *pcFullFilename,
782 void *pvPluginHandle,
783 LADSPA_Descriptor_Function pfDescriptorFunction)
785 const LADSPA_Descriptor *desc;
788 GstElementDetails *details;
789 GTypeInfo typeinfo = {
790 sizeof(GstLADSPAClass), NULL,
792 (GClassInitFunc)gst_ladspa_class_init,
797 (GInstanceInitFunc)gst_ladspa_init,
800 GstElementFactory *factory;
802 // walk through all the plugins in this pluginlibrary
804 while ((desc = pfDescriptorFunction(i++))) {
807 // construct the type
808 type_name = g_strdup_printf("ladspa_%s",desc->Label);
809 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-_+", '-');
810 // if it's already registered, drop it
811 if (g_type_from_name(type_name)) {
815 // create the type now
816 type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);
818 // construct the element details struct
819 details = g_new0(GstElementDetails,1);
820 details->longname = g_strdup(desc->Name);
821 details->klass = "Filter/LADSPA";
822 details->description = details->longname;
823 details->version = g_strdup_printf("%ld",desc->UniqueID);
824 details->author = g_strdup(desc->Maker);
825 details->copyright = g_strdup(desc->Copyright);
827 // register the plugin with gstreamer
828 factory = gst_elementfactory_new(type_name,type,details);
829 g_return_if_fail(factory != NULL);
830 gst_plugin_add_feature (ladspa_plugin, GST_PLUGIN_FEATURE (factory));
832 // add this plugin to the hash
833 g_hash_table_insert(ladspa_descriptors,
834 GINT_TO_POINTER(type),
838 // only add sink padtemplate if there are sinkpads
839 for (j=0;j<desc->PortCount;j++) {
840 if (LADSPA_IS_PORT_AUDIO(desc->PortDescriptors[j]) &&
841 LADSPA_IS_PORT_INPUT(desc->PortDescriptors[j])) {
842 sinktempl = ladspa_sink_factory();
843 gst_elementfactory_add_padtemplate (factory, sinktempl);
848 srctempl = ladspa_src_factory();
849 gst_elementfactory_add_padtemplate (factory, srctempl);
855 plugin_init (GModule *module, GstPlugin *plugin)
858 ladspa_descriptors = g_hash_table_new(NULL,NULL);
859 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
861 ladspa_plugin = plugin;
863 LADSPAPluginSearch(ladspa_describe_plugin);
868 GstPluginDesc plugin_desc = {