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