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 <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
26 #include <sys/types.h>
32 #include "jack_rack.h"
34 #define BUFFER_LEN 10000
36 static void initialise_jack_rack( mlt_properties properties, int channels )
41 // Propogate these for the Jack processing callback
42 mlt_properties_set_int( properties, "channels", channels );
45 if ( mlt_properties_get( properties, "src" ) )
47 // Create JackRack without Jack client name so that it only uses LADSPA
48 jack_rack_t *jackrack = jack_rack_new( NULL, channels );
49 mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
50 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
54 LADSPA_Data **input_buffers = mlt_pool_alloc( sizeof( LADSPA_Data ) * channels );
55 LADSPA_Data **output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data ) * channels );
57 // Set properties - released inside filter_close
58 mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( LADSPA_Data *) * channels, NULL, NULL );
59 mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( LADSPA_Data *) * channels, NULL, NULL );
61 // Register Jack ports
62 for ( i = 0; i < channels; i++ )
64 input_buffers[i] = mlt_pool_alloc( BUFFER_LEN * sizeof( LADSPA_Data ) );
65 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
66 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof( LADSPA_Data ), NULL, NULL );
68 output_buffers[i] = mlt_pool_alloc( BUFFER_LEN * sizeof( LADSPA_Data ) );
69 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
70 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof( LADSPA_Data ), NULL, NULL );
78 static int ladspa_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
80 // Get the filter service
81 mlt_filter filter = mlt_frame_pop_audio( frame );
83 // Get the filter properties
84 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
86 // Get the producer's audio
87 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
89 // Initialise LADSPA if needed
90 jack_rack_t *jackrack = mlt_properties_get_data( filter_properties, "jackrack", NULL );
91 if ( jackrack == NULL )
93 sample_rate = *frequency;
94 initialise_jack_rack( filter_properties, *channels );
95 jackrack = mlt_properties_get_data( filter_properties, "jackrack", NULL );
98 // Get the filter-specific properties
99 LADSPA_Data **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
100 LADSPA_Data **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
103 int16_t *q = *buffer;
106 // Convert to floats and write into output ringbuffer
107 for ( i = 0; i < *samples; i++ )
108 for ( j = 0; j < *channels; j++ )
109 input_buffers[ j ][ i ] = ( float )( *q ++ ) / 32768.0;
111 // Do LADSPA processing
112 if ( jackrack && process_ladspa( jackrack->procinfo, *samples, input_buffers, output_buffers) == 0 )
114 // Read from output buffer and convert from floats
116 for ( i = 0; i < *samples; i++ )
117 for ( j = 0; j < *channels; j++ )
119 if ( output_buffers[ j ][ i ] > 1.0 )
120 output_buffers[ j ][ i ] = 1.0;
121 else if ( output_buffers[ j ][ i ] < -1.0 )
122 output_buffers[ j ][ i ] = -1.0;
124 if ( output_buffers[ j ][ i ] > 0 )
125 *q ++ = 32767 * output_buffers[ j ][ i ];
127 *q ++ = 32768 * output_buffers[ j ][ i ];
135 /** Filter processing.
138 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
141 mlt_frame_push_audio( frame, this );
142 mlt_frame_push_audio( frame, ladspa_get_audio );
149 static void filter_close( mlt_filter this )
153 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
155 if ( mlt_properties_get_data( properties, "jackrack", NULL ) != NULL )
157 for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
159 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
160 mlt_pool_release( mlt_properties_get_data( properties, mlt_name, NULL ) );
161 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
162 mlt_pool_release( mlt_properties_get_data( properties, mlt_name, NULL ) );
164 mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
165 mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
167 jack_rack_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
168 jack_rack_destroy( jackrack );
170 this->parent.close = NULL;
171 mlt_service_close( &this->parent );
174 /** Constructor for the filter.
177 mlt_filter filter_ladspa_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
179 mlt_filter this = mlt_filter_new( );
182 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
184 this->process = filter_process;
185 this->close = filter_close;
187 mlt_properties_set( properties, "src", arg );