2 * producer_ladspa.c -- LADSPA plugin producer
3 * Copyright (C) 2013 Ushodaya Enterprises Limited
4 * Author: Brian Matherly <pez4brian@yahoo.com>
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_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
27 #include <sys/types.h>
32 #include "jack_rack.h"
34 #define BUFFER_LEN 10000
36 /** One-time initialization of jack rack.
39 static jack_rack_t* initialise_jack_rack( mlt_properties properties, int channels )
41 jack_rack_t *jackrack = NULL;
42 unsigned long plugin_id = mlt_properties_get_int64( properties, "_pluginid" );
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 );
52 // Load one LADSPA plugin by its UniqueID
53 plugin_desc_t *desc = plugin_mgr_get_any_desc( jackrack->plugin_mgr, plugin_id );
56 if ( desc && ( plugin = jack_rack_instantiate_plugin( jackrack, desc ) ) )
61 plugin->enabled = TRUE;
62 plugin->wet_dry_enabled = FALSE;
63 for ( index = 0; index < desc->control_port_count; index++ )
65 // Apply the control port values
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;
74 process_add_plugin( jackrack->procinfo, plugin );
78 mlt_log_error( properties, "failed to load plugin %lu\n", plugin_id );
85 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
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 );
91 LADSPA_Data** output_buffers = NULL;
94 // Initialize LADSPA if needed
95 jack_rack_t *jackrack = mlt_properties_get_data( producer_properties, "_jackrack", NULL );
98 sample_rate = *frequency; // global inside jack_rack
99 jackrack = initialise_jack_rack( producer_properties, *channels );
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;
110 // Calculate the size of the buffer
111 size = *samples * *channels * sizeof( float );
113 // Allocate the buffer
114 *buffer = mlt_pool_alloc( size );
116 // Initialize the LADSPA output buffer.
117 output_buffers = mlt_pool_alloc( sizeof( LADSPA_Data* ) * *channels );
118 for ( i = 0; i < *channels; i++ )
120 output_buffers[i] = (LADSPA_Data*) *buffer + i * *samples;
123 // Do LADSPA processing
124 process_ladspa( jackrack->procinfo, *samples, NULL, output_buffers );
125 mlt_pool_release( output_buffers );
127 // Set the buffer for destruction
128 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
134 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
137 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
139 // Check that we created a frame and initialize it
140 if ( *frame != NULL )
142 // Obtain properties of frame
143 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
145 // Update timecode on the frame we're creating
146 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
148 // Save the producer to be used in get_audio
149 mlt_properties_set_data( frame_properties, "_producer_ladspa", producer, 0, NULL, NULL );
151 // Push the get_audio method
152 mlt_frame_push_audio( *frame, producer_get_audio );
155 // Calculate the next time code
156 mlt_producer_prepare_next( producer );
161 /** Destructor for the producer.
164 static void producer_close( mlt_producer producer )
166 producer->close = NULL;
167 mlt_producer_close( producer );
171 /** Constructor for the producer.
174 mlt_producer producer_ladspa_init( mlt_profile profile, mlt_service_type type, const char* id, char* arg )
176 // Create a new producer object
177 mlt_producer producer = mlt_producer_new( profile );
179 if ( producer != NULL )
181 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
183 producer->get_frame = producer_get_frame;
184 producer->close = ( mlt_destructor )producer_close;
186 // Save the plugin ID.
187 if ( !strncmp( id, "ladspa.", 7 ) )
189 mlt_properties_set( properties, "_pluginid", id + 7 );
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 )
196 producer_close( producer );