1 /*
2  * factory.c -- the factory method interfaces
3  * Copyright (C) 2003-2019 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.h>
21 
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <float.h>
27 
28 
29 extern mlt_consumer consumer_jack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
30 
31 #ifdef GPL
32 #include <ladspa.h>
33 #include "plugin_mgr.h"
34 
35 extern mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
36 extern mlt_filter filter_ladspa_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
37 extern mlt_producer producer_ladspa_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
38 
39 plugin_mgr_t *g_jackrack_plugin_mgr = NULL;
40 
add_port_to_metadata(mlt_properties p,plugin_desc_t * desc,int j)41 static void add_port_to_metadata( mlt_properties p, plugin_desc_t* desc, int j )
42 {
43 	LADSPA_Data sample_rate = 48000;
44 	LADSPA_PortRangeHintDescriptor hint_descriptor = desc->port_range_hints[j].HintDescriptor;
45 
46 	mlt_properties_set( p, "title", desc->port_names[ j ] );
47 	if ( LADSPA_IS_HINT_INTEGER( hint_descriptor ) )
48 	{
49 		mlt_properties_set( p, "type", "integer" );
50 		mlt_properties_set_int( p, "default", plugin_desc_get_default_control_value( desc, j, sample_rate ) );
51 	}
52 	else if ( LADSPA_IS_HINT_TOGGLED( hint_descriptor ) )
53 	{
54 		mlt_properties_set( p, "type", "boolean" );
55 		mlt_properties_set_int( p, "default", plugin_desc_get_default_control_value( desc, j, sample_rate ) );
56 	}
57 	else
58 	{
59 		mlt_properties_set( p, "type", "float" );
60 		mlt_properties_set_double( p, "default", plugin_desc_get_default_control_value( desc, j, sample_rate ) );
61 	}
62 	/* set upper and lower, possibly adjusted to the sample rate */
63 	if ( LADSPA_IS_HINT_BOUNDED_BELOW( hint_descriptor ) )
64 	{
65 		LADSPA_Data lower = desc->port_range_hints[j].LowerBound;
66 		if ( LADSPA_IS_HINT_SAMPLE_RATE( hint_descriptor ) )
67 			lower *= sample_rate;
68 		if ( LADSPA_IS_HINT_LOGARITHMIC( hint_descriptor ) )
69 		{
70 			if (lower < FLT_EPSILON)
71 				lower = FLT_EPSILON;
72 		}
73 		mlt_properties_set_double( p, "minimum", lower );
74 	}
75 	if ( LADSPA_IS_HINT_BOUNDED_ABOVE( hint_descriptor ) )
76 	{
77 		LADSPA_Data upper = desc->port_range_hints[j].UpperBound;
78 		if ( LADSPA_IS_HINT_SAMPLE_RATE( hint_descriptor ) )
79 			upper *= sample_rate;
80 		mlt_properties_set_double( p, "maximum", upper );
81 	}
82 	if ( LADSPA_IS_HINT_LOGARITHMIC( hint_descriptor ) )
83 		mlt_properties_set( p, "scale", "log" );
84 }
85 
86 #endif
87 
metadata(mlt_service_type type,const char * id,char * data)88 static mlt_properties metadata( mlt_service_type type, const char *id, char *data )
89 {
90 	char file[ PATH_MAX ];
91 	if( type == filter_type )
92 	{
93 		snprintf( file, PATH_MAX, "%s/jackrack/%s",
94 			  mlt_environment( "MLT_DATA" ), strncmp( id, "ladspa.", 7 ) ? data : "filter_ladspa.yml" );
95 	}
96 	else
97 	{
98 		snprintf( file, PATH_MAX, "%s/jackrack/%s",
99 			  mlt_environment( "MLT_DATA" ), strncmp( id, "ladspa.", 7 ) ? data : "producer_ladspa.yml" );
100 	}
101 	mlt_properties result = mlt_properties_parse_yaml( file );
102 
103 #ifdef GPL
104 	if ( !strncmp( id, "ladspa.", 7 ) )
105 	{
106 		// Annotate the yaml properties with ladspa control port info.
107 		plugin_desc_t *desc = plugin_mgr_get_any_desc( g_jackrack_plugin_mgr, strtol( id + 7, NULL, 10 ) );
108 
109 		if ( desc )
110 		{
111 			mlt_properties params = mlt_properties_new();
112 			mlt_properties p;
113 			char key[20];
114 			int i;
115 
116 			mlt_properties_set( result, "identifier", id );
117 			mlt_properties_set( result, "title", desc->name );
118 			mlt_properties_set( result, "creator", desc->maker ? desc->maker : "unknown" );
119 			mlt_properties_set( result, "description", "LADSPA plugin" );
120 			mlt_properties_set_data( result, "parameters", params, 0, (mlt_destructor) mlt_properties_close, NULL );
121 			for ( i = 0; i < desc->control_port_count; i++ )
122 			{
123 				int j = desc->control_port_indicies[i];
124 				p = mlt_properties_new();
125 				snprintf( key, sizeof(key), "%d", mlt_properties_count( params ) );
126 				mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
127 				snprintf( key, sizeof(key), "%d", j );
128 				mlt_properties_set( p, "identifier", key );
129 				add_port_to_metadata( p, desc, j );
130 				mlt_properties_set( p, "mutable", "yes" );
131 			}
132 			for ( i = 0; i < desc->status_port_count; i++ )
133 			{
134 				int j = desc->status_port_indicies[i];
135 				p = mlt_properties_new();
136 				snprintf( key, sizeof(key), "%d", mlt_properties_count( params ) );
137 				mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
138 				snprintf( key, sizeof(key), "%d[*]", j );
139 				mlt_properties_set( p, "identifier", key );
140 				add_port_to_metadata( p, desc, j );
141 				mlt_properties_set( p, "readonly", "yes" );
142 			}
143 
144 			p = mlt_properties_new();
145 			snprintf( key, sizeof(key), "%d", mlt_properties_count( params ) );
146 			mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
147 			mlt_properties_set( p, "identifier", "instances" );
148 			mlt_properties_set( p, "title", "Instances" );
149 			mlt_properties_set( p, "description",
150 								"The number of instances of the plugin that are in use.\n"
151 								"MLT will create the number of plugins that are required "
152 								"to support the number of audio channels.\n"
153 								"Status parameters (readonly) are provided for each instance "
154 								"and are accessed by specifying the instance number after the "
155 								"identifier (starting at zero).\n"
156 								"e.g. 9[0] provides the value of status 9 for the first instance."
157 								);
158 			mlt_properties_set( p, "type", "integer" );
159 			mlt_properties_set( p, "readonly", "yes" );
160 
161 			if( type == filter_type )
162 			{
163 				p = mlt_properties_new();
164 				snprintf( key, sizeof(key), "%d", mlt_properties_count( params ) );
165 				mlt_properties_set_data( params, key, p, 0, (mlt_destructor) mlt_properties_close, NULL );
166 				mlt_properties_set( p, "identifier", "wetness" );
167 				mlt_properties_set( p, "title", "Wet/Dry" );
168 				mlt_properties_set( p, "type", "float" );
169 				mlt_properties_set_double( p, "default", 1 );
170 				mlt_properties_set_double( p, "minimum", 0 );
171 				mlt_properties_set_double( p, "maximum", 1 );
172 				mlt_properties_set( p, "mutable", "yes" );
173 			}
174 		}
175 	}
176 #endif
177 
178 	return result;
179 }
180 
181 MLT_REPOSITORY
182 {
183 #ifdef GPL
184 	GSList *list;
185 	g_jackrack_plugin_mgr = plugin_mgr_new();
186 
187 	for ( list = g_jackrack_plugin_mgr->all_plugins; list; list = g_slist_next( list ) )
188 	{
189 		plugin_desc_t *desc = (plugin_desc_t *) list->data;
190 		char *s = malloc( strlen( "ladpsa." ) + 21 );
191 
192 		sprintf( s, "ladspa.%lu", desc->id );
193 
194 		if( desc->has_input )
195 		{
196 			MLT_REGISTER( filter_type, s, filter_ladspa_init );
197 			MLT_REGISTER_METADATA( filter_type, s, metadata, NULL );
198 		}
199 		else
200 		{
201 			MLT_REGISTER( producer_type, s, producer_ladspa_init );
202 			MLT_REGISTER_METADATA( producer_type, s, metadata, NULL );
203 		}
204 
205 		free( s );
206 	}
207 	mlt_factory_register_for_clean_up( g_jackrack_plugin_mgr, (mlt_destructor) plugin_mgr_destroy );
208 
209 	MLT_REGISTER( filter_type, "jack", filter_jackrack_init );
210 	MLT_REGISTER( filter_type, "jackrack", filter_jackrack_init );
211 	MLT_REGISTER_METADATA( filter_type, "jackrack", metadata, "filter_jackrack.yml" );
212 	MLT_REGISTER( filter_type, "ladspa", filter_ladspa_init );
213 	MLT_REGISTER_METADATA( filter_type, "ladspa", metadata, "filter_ladspa.yml" );
214 #endif
215 	MLT_REGISTER( consumer_type, "jack", consumer_jack_init );
216 	MLT_REGISTER_METADATA( consumer_type, "jack", metadata, "consumer_jack.yml" );
217 }
218