b9b362dd62c8fde5d02e46598930bc44d0a9021b
[melted] / src / modules / jackrack / filter_ladspa.c
1 /*
2 * filter_ladspa.c -- filter audio through LADSPA plugins
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "filter_ladspa.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <pthread.h>
31 #include <string.h>
32
33 #include "jack_rack.h"
34
35 #define BUFFER_LEN 10000
36
37 static void initialise_jack_rack( mlt_properties properties, int channels )
38 {
39 int i;
40 char mlt_name[20];
41
42 // Propogate these for the Jack processing callback
43 mlt_properties_set_int( properties, "channels", channels );
44
45 // Start JackRack
46 if ( mlt_properties_get( properties, "src" ) )
47 {
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" ) );
52 }
53
54 // Allocate buffers
55 LADSPA_Data **input_buffers = mlt_pool_alloc( sizeof( LADSPA_Data ) * channels );
56 LADSPA_Data **output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data ) * channels );
57
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 );
61
62 // Register Jack ports
63 for ( i = 0; i < channels; i++ )
64 {
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 );
68
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 );
72 }
73 }
74
75
76 /** Get the audio.
77 */
78
79 static int ladspa_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
80 {
81 // Get the filter service
82 mlt_filter filter = mlt_frame_pop_audio( frame );
83
84 // Get the filter properties
85 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
86
87 // Get the producer's audio
88 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
89
90 // Initialise LADSPA if needed
91 jack_rack_t *jackrack = mlt_properties_get_data( filter_properties, "jackrack", NULL );
92 if ( jackrack == NULL )
93 {
94 sample_rate = *frequency;
95 initialise_jack_rack( filter_properties, *channels );
96 jackrack = mlt_properties_get_data( filter_properties, "jackrack", NULL );
97 }
98
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 );
102
103 // Process the audio
104 int16_t *q = *buffer;
105 int i, j;
106
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;
111
112 // Do LADSPA processing
113 if ( jackrack && process_ladspa( jackrack->procinfo, *samples, input_buffers, output_buffers) == 0 )
114 {
115 // Read from output buffer and convert from floats
116 q = *buffer;
117 for ( i = 0; i < *samples; i++ )
118 for ( j = 0; j < *channels; j++ )
119 {
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;
124
125 if ( output_buffers[ j ][ i ] > 0 )
126 *q ++ = 32767 * output_buffers[ j ][ i ];
127 else
128 *q ++ = 32768 * output_buffers[ j ][ i ];
129 }
130 }
131
132 return 0;
133 }
134
135
136 /** Filter processing.
137 */
138
139 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
140 {
141 {
142 mlt_frame_push_audio( frame, this );
143 mlt_frame_push_audio( frame, ladspa_get_audio );
144 }
145
146 return frame;
147 }
148
149
150 static void filter_close( mlt_filter this )
151 {
152 int i;
153 char mlt_name[20];
154 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
155
156 if ( mlt_properties_get_data( properties, "jackrack", NULL ) != NULL )
157 {
158 for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
159 {
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 ) );
164 }
165 mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
166 mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
167
168 jack_rack_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
169 jack_rack_destroy( jackrack );
170 }
171 this->parent.close = NULL;
172 mlt_service_close( &this->parent );
173 }
174
175 /** Constructor for the filter.
176 */
177
178 mlt_filter filter_ladspa_init( char *arg )
179 {
180 mlt_filter this = mlt_filter_new( );
181 if ( this != NULL )
182 {
183 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
184
185 this->process = filter_process;
186 this->close = filter_close;
187
188 mlt_properties_set( properties, "src", arg );
189 }
190 return this;
191 }