2 * filter_ladspa.c -- filter audio through LADSPA plugins
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_ladspa.h"
23 #include <framework/mlt_frame.h>
27 #include <sys/types.h>
33 #include "jack_rack.h"
35 #define BUFFER_LEN 10000
37 static void initialise_jack_rack( mlt_properties properties
, int channels
)
42 // Propogate these for the Jack processing callback
43 mlt_properties_set_int( properties
, "channels", channels
);
46 if ( mlt_properties_get( properties
, "src" ) )
48 // Create JackRack without Jack client name so that it only uses LADSPA
49 jack_rack_t
*jackrack
= jack_rack_new( NULL
, channels
);
50 mlt_properties_set_data( properties
, "jackrack", jackrack
, 0, NULL
, NULL
);
51 jack_rack_open_file( jackrack
, mlt_properties_get( properties
, "src" ) );
55 LADSPA_Data
**input_buffers
= mlt_pool_alloc( sizeof( LADSPA_Data
) * channels
);
56 LADSPA_Data
**output_buffers
= mlt_pool_alloc( sizeof( LADSPA_Data
) * channels
);
58 // Set properties - released inside filter_close
59 mlt_properties_set_data( properties
, "input_buffers", input_buffers
, sizeof( LADSPA_Data
*) * channels
, NULL
, NULL
);
60 mlt_properties_set_data( properties
, "output_buffers", output_buffers
, sizeof( LADSPA_Data
*) * channels
, NULL
, NULL
);
62 // Register Jack ports
63 for ( i
= 0; i
< channels
; i
++ )
65 input_buffers
[i
] = mlt_pool_alloc( BUFFER_LEN
* sizeof( LADSPA_Data
) );
66 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
67 mlt_properties_set_data( properties
, mlt_name
, input_buffers
[i
], BUFFER_LEN
* sizeof( LADSPA_Data
), NULL
, NULL
);
69 output_buffers
[i
] = mlt_pool_alloc( BUFFER_LEN
* sizeof( LADSPA_Data
) );
70 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
71 mlt_properties_set_data( properties
, mlt_name
, output_buffers
[i
], BUFFER_LEN
* sizeof( LADSPA_Data
), NULL
, NULL
);
79 static int ladspa_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
81 // Get the filter service
82 mlt_filter filter
= mlt_frame_pop_audio( frame
);
84 // Get the filter properties
85 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
87 // Get the producer's audio
88 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
90 // Initialise LADSPA if needed
91 jack_rack_t
*jackrack
= mlt_properties_get_data( filter_properties
, "jackrack", NULL
);
92 if ( jackrack
== NULL
)
94 sample_rate
= *frequency
;
95 initialise_jack_rack( filter_properties
, *channels
);
96 jackrack
= mlt_properties_get_data( filter_properties
, "jackrack", NULL
);
99 // Get the filter-specific properties
100 LADSPA_Data
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
101 LADSPA_Data
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
104 int16_t *q
= *buffer
;
107 // Convert to floats and write into output ringbuffer
108 for ( i
= 0; i
< *samples
; i
++ )
109 for ( j
= 0; j
< *channels
; j
++ )
110 input_buffers
[ j
][ i
] = ( float )( *q
++ ) / 32768.0;
112 // Do LADSPA processing
113 if ( jackrack
&& process_ladspa( jackrack
->procinfo
, *samples
, input_buffers
, output_buffers
) == 0 )
115 // Read from output buffer and convert from floats
117 for ( i
= 0; i
< *samples
; i
++ )
118 for ( j
= 0; j
< *channels
; j
++ )
120 if ( output_buffers
[ j
][ i
] > 1.0 )
121 output_buffers
[ j
][ i
] = 1.0;
122 else if ( output_buffers
[ j
][ i
] < -1.0 )
123 output_buffers
[ j
][ i
] = -1.0;
125 if ( output_buffers
[ j
][ i
] > 0 )
126 *q
++ = 32767 * output_buffers
[ j
][ i
];
128 *q
++ = 32768 * output_buffers
[ j
][ i
];
136 /** Filter processing.
139 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
142 mlt_frame_push_audio( frame
, this );
143 mlt_frame_push_audio( frame
, ladspa_get_audio
);
150 static void filter_close( mlt_filter
this )
154 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
156 if ( mlt_properties_get_data( properties
, "jackrack", NULL
) != NULL
)
158 for ( i
= 0; i
< mlt_properties_get_int( properties
, "channels" ); i
++ )
160 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
161 mlt_pool_release( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
162 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
163 mlt_pool_release( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
165 mlt_pool_release( mlt_properties_get_data( properties
, "output_buffers", NULL
) );
166 mlt_pool_release( mlt_properties_get_data( properties
, "input_buffers", NULL
) );
168 jack_rack_t
*jackrack
= mlt_properties_get_data( properties
, "jackrack", NULL
);
169 jack_rack_destroy( jackrack
);
171 this->parent
.close
= NULL
;
172 mlt_service_close( &this->parent
);
175 /** Constructor for the filter.
178 mlt_filter
filter_ladspa_init( char *arg
)
180 mlt_filter
this = mlt_filter_new( );
183 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
185 this->process
= filter_process
;
186 this->close
= filter_close
;
188 mlt_properties_set( properties
, "src", arg
);