1 /*
2 * producer_ladspa.c -- LADSPA plugin producer
3 * Copyright (C) 2013-2014 Meltytech, LLC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <framework/mlt_producer.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_log.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <string.h>
30
31 #include "jack_rack.h"
32
33 #define BUFFER_LEN 10000
34
35 /** One-time initialization of jack rack.
36 */
37
initialise_jack_rack(mlt_properties properties,int channels)38 static jack_rack_t* initialise_jack_rack( mlt_properties properties, int channels )
39 {
40 jack_rack_t *jackrack = NULL;
41 unsigned long plugin_id = mlt_properties_get_int64( properties, "_pluginid" );
42
43 // Start JackRack
44 if ( plugin_id )
45 {
46 // Create JackRack without Jack client name so that it only uses LADSPA
47 jackrack = jack_rack_new( NULL, channels );
48 mlt_properties_set_data( properties, "_jackrack", jackrack, 0,
49 (mlt_destructor) jack_rack_destroy, NULL );
50
51 // Load one LADSPA plugin by its UniqueID
52 plugin_desc_t *desc = plugin_mgr_get_any_desc( jackrack->plugin_mgr, plugin_id );
53 plugin_t *plugin;
54
55 if ( desc && ( plugin = jack_rack_instantiate_plugin( jackrack, desc ) ) )
56 {
57 plugin->enabled = TRUE;
58 plugin->wet_dry_enabled = FALSE;
59 process_add_plugin( jackrack->procinfo, plugin );
60 mlt_properties_set_int( properties, "instances", plugin->copies );
61 }
62 else
63 {
64 mlt_log_error( properties, "failed to load plugin %lu\n", plugin_id );
65 }
66 }
67
68 return jackrack;
69 }
70
producer_get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)71 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
72 {
73 // Get the producer service
74 mlt_producer producer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer_ladspa", NULL );
75 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
76 int size = 0;
77 LADSPA_Data** output_buffers = NULL;
78 int i = 0;
79
80 // Initialize LADSPA if needed
81 jack_rack_t *jackrack = mlt_properties_get_data( producer_properties, "_jackrack", NULL );
82 if ( !jackrack )
83 {
84 sample_rate = *frequency; // global inside jack_rack
85 jackrack = initialise_jack_rack( producer_properties, *channels );
86 }
87
88 if ( jackrack )
89 {
90 // Correct the returns if necessary
91 *samples = *samples <= 0 ? 1920 : *samples;
92 *channels = *channels <= 0 ? 2 : *channels;
93 *frequency = *frequency <= 0 ? 48000 : *frequency;
94 *format = mlt_audio_float;
95
96 if ( jackrack->procinfo && jackrack->procinfo->chain )
97 {
98 plugin_t *plugin = jackrack->procinfo->chain;
99 LADSPA_Data value;
100 int index, c;
101 mlt_position position = mlt_frame_get_position( frame );
102 mlt_position length = mlt_producer_get_length( producer );
103
104 for ( index = 0; index < plugin->desc->control_port_count; index++ )
105 {
106 // Apply the control port values
107 char key[20];
108 value = plugin_desc_get_default_control_value( plugin->desc, index, sample_rate );
109 snprintf( key, sizeof(key), "%d", index );
110 if ( mlt_properties_get( producer_properties, key ) )
111 value = mlt_properties_anim_get_double( producer_properties, key, position, length );
112 for ( c = 0; c < plugin->copies; c++ )
113 plugin->holders[c].control_memory[index] = value;
114 }
115 }
116
117 // Calculate the size of the buffer
118 size = *samples * *channels * sizeof( float );
119
120 // Allocate the buffer
121 *buffer = mlt_pool_alloc( size );
122
123 // Initialize the LADSPA output buffer.
124 output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data* ) * *channels );
125 for ( i = 0; i < *channels; i++ )
126 {
127 output_buffers[i] = (LADSPA_Data*) *buffer + i * *samples;
128 }
129
130 // Do LADSPA processing
131 process_ladspa( jackrack->procinfo, *samples, NULL, output_buffers );
132 mlt_pool_release( output_buffers );
133
134 // Set the buffer for destruction
135 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
136
137 if ( jackrack && jackrack->procinfo && jackrack->procinfo->chain &&
138 mlt_properties_get_int64( producer_properties, "_pluginid" ) )
139 {
140 plugin_t *plugin = jackrack->procinfo->chain;
141 LADSPA_Data value;
142 int i, c;
143 for ( i = 0; i < plugin->desc->status_port_count; i++ )
144 {
145 // read the status port values
146 char key[20];
147 int p = plugin->desc->status_port_indicies[i];
148 for ( c = 0; c < plugin->copies; c++ )
149 {
150 snprintf( key, sizeof(key), "%d[%d]", p, c );
151 value = plugin->holders[c].status_memory[i];
152 mlt_properties_set_double( producer_properties, key, value );
153 }
154 }
155 }
156 }
157
158 return 0;
159 }
160
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)161 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
162 {
163 // Generate a frame
164 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
165
166 // Check that we created a frame and initialize it
167 if ( *frame != NULL )
168 {
169 // Obtain properties of frame
170 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
171
172 // Update timecode on the frame we're creating
173 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
174
175 // Save the producer to be used in get_audio
176 mlt_properties_set_data( frame_properties, "_producer_ladspa", producer, 0, NULL, NULL );
177
178 // Push the get_audio method
179 mlt_frame_push_audio( *frame, producer_get_audio );
180 }
181
182 // Calculate the next time code
183 mlt_producer_prepare_next( producer );
184
185 return 0;
186 }
187
188 /** Destructor for the producer.
189 */
190
producer_close(mlt_producer producer)191 static void producer_close( mlt_producer producer )
192 {
193 producer->close = NULL;
194 mlt_producer_close( producer );
195 free( producer );
196 }
197
198 /** Constructor for the producer.
199 */
200
producer_ladspa_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)201 mlt_producer producer_ladspa_init( mlt_profile profile, mlt_service_type type, const char* id, char* arg )
202 {
203 // Create a new producer object
204 mlt_producer producer = mlt_producer_new( profile );
205
206 if ( producer != NULL )
207 {
208 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
209
210 producer->get_frame = producer_get_frame;
211 producer->close = ( mlt_destructor )producer_close;
212
213 // Save the plugin ID.
214 if ( !strncmp( id, "ladspa.", 7 ) )
215 {
216 mlt_properties_set( properties, "_pluginid", id + 7 );
217 }
218
219 // Make sure the plugin ID is valid.
220 unsigned long plugin_id = mlt_properties_get_int64( properties, "_pluginid" );
221 if( plugin_id < 1000 || plugin_id > 0x00FFFFFF )
222 {
223 producer_close( producer );
224 producer = NULL;
225 }
226 }
227 return producer;
228 }
229