]> git.sesse.net Git - mlt/blob - src/modules/jackrack/producer_ladspa.c
Add ladspa producer
[mlt] / src / modules / jackrack / producer_ladspa.c
1 /*
2  * producer_ladspa.c -- LADSPA plugin producer
3  * Copyright (C) 2013 Ushodaya Enterprises Limited
4  * Author: Brian Matherly <pez4brian@yahoo.com>
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_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <string.h>
31
32 #include "jack_rack.h"
33
34 #define BUFFER_LEN 10000
35
36 /** One-time initialization of jack rack.
37 */
38
39 static jack_rack_t* initialise_jack_rack( mlt_properties properties, int channels )
40 {
41         jack_rack_t *jackrack = NULL;
42         unsigned long plugin_id = mlt_properties_get_int64( properties, "_pluginid" );
43
44         // Start JackRack
45         if ( plugin_id )
46         {
47                 // Create JackRack without Jack client name so that it only uses LADSPA
48                 jackrack = jack_rack_new( NULL, channels );
49                 mlt_properties_set_data( properties, "_jackrack", jackrack, 0,
50                         (mlt_destructor) jack_rack_destroy, NULL );
51
52                 // Load one LADSPA plugin by its UniqueID
53                 plugin_desc_t *desc = plugin_mgr_get_any_desc( jackrack->plugin_mgr, plugin_id );
54                 plugin_t *plugin;
55
56                 if ( desc && ( plugin = jack_rack_instantiate_plugin( jackrack, desc ) ) )
57                 {
58                         LADSPA_Data value;
59                         int index, c;
60
61                         plugin->enabled = TRUE;
62                         plugin->wet_dry_enabled = FALSE;
63                         for ( index = 0; index < desc->control_port_count; index++ )
64                         {
65                                 // Apply the control port values
66                                 char key[20];
67                                 value = plugin_desc_get_default_control_value( desc, index, sample_rate );
68                                 snprintf( key, sizeof(key), "%d", index );
69                                 if ( mlt_properties_get( properties, key ) )
70                                         value = mlt_properties_get_double( properties, key );
71                                 for ( c = 0; c < plugin->copies; c++ )
72                                         plugin->holders[c].control_memory[index] = value;
73                         }
74                         process_add_plugin( jackrack->procinfo, plugin );
75                 }
76                 else
77                 {
78                         mlt_log_error( properties, "failed to load plugin %lu\n", plugin_id );
79                 }
80         }
81
82         return jackrack;
83 }
84
85 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
86 {
87         // Get the producer service
88         mlt_producer producer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer_ladspa", NULL );
89         mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
90         int size = 0;
91         LADSPA_Data** output_buffers = NULL;
92         int i = 0;
93
94         // Initialize LADSPA if needed
95         jack_rack_t *jackrack = mlt_properties_get_data( producer_properties, "_jackrack", NULL );
96         if ( !jackrack )
97         {
98                 sample_rate = *frequency; // global inside jack_rack
99                 jackrack = initialise_jack_rack( producer_properties, *channels );
100         }
101
102         if( jackrack )
103         {
104                 // Correct the returns if necessary
105                 *samples = *samples <= 0 ? 1920 : *samples;
106                 *channels = *channels <= 0 ? 2 : *channels;
107                 *frequency = *frequency <= 0 ? 48000 : *frequency;
108                 *format = mlt_audio_float;
109
110                 // Calculate the size of the buffer
111                 size = *samples * *channels * sizeof( float );
112
113                 // Allocate the buffer
114                 *buffer = mlt_pool_alloc( size );
115
116                 // Initialize the LADSPA output buffer.
117                 output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data* ) * *channels );
118                 for ( i = 0; i < *channels; i++ )
119                 {
120                         output_buffers[i] = (LADSPA_Data*) *buffer + i * *samples;
121                 }
122
123                 // Do LADSPA processing
124                 process_ladspa( jackrack->procinfo, *samples, NULL, output_buffers );
125                 mlt_pool_release( output_buffers );
126
127                 // Set the buffer for destruction
128                 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
129         }
130
131         return 0;
132 }
133
134 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
135 {
136         // Generate a frame
137         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
138
139         // Check that we created a frame and initialize it
140         if ( *frame != NULL )
141         {
142                 // Obtain properties of frame
143                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
144
145                 // Update timecode on the frame we're creating
146                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
147
148                 // Save the producer to be used in get_audio
149                 mlt_properties_set_data( frame_properties, "_producer_ladspa", producer, 0, NULL, NULL );
150
151                 // Push the get_audio method
152                 mlt_frame_push_audio( *frame, producer_get_audio );
153         }
154
155         // Calculate the next time code
156         mlt_producer_prepare_next( producer );
157
158         return 0;
159 }
160
161 /** Destructor for the producer.
162 */
163
164 static void producer_close( mlt_producer producer )
165 {
166         producer->close = NULL;
167         mlt_producer_close( producer );
168         free( producer );
169 }
170
171 /** Constructor for the producer.
172 */
173
174 mlt_producer producer_ladspa_init( mlt_profile profile, mlt_service_type type, const char* id, char* arg )
175 {
176         // Create a new producer object
177         mlt_producer producer = mlt_producer_new( profile );
178
179         if ( producer != NULL )
180         {
181                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
182
183                 producer->get_frame = producer_get_frame;
184                 producer->close = ( mlt_destructor )producer_close;
185
186                 // Save the plugin ID.
187                 if ( !strncmp( id, "ladspa.", 7 ) )
188                 {
189                         mlt_properties_set( properties, "_pluginid", id + 7 );
190                 }
191
192                 // Make sure the plugin ID is valid.
193                 unsigned long plugin_id = mlt_properties_get_int64( properties, "_pluginid" );
194                 if( plugin_id < 1000 || plugin_id > 0x00FFFFFF )
195                 {
196                         producer_close( producer );
197                         producer = NULL;
198                 }
199         }
200         return producer;
201 }