1 /***********************************************************/
2 /*vstfx infofile - module to manage info files */
3 /*containing cached information about a plugin. e.g. its */
4 /*name, creator etc etc */
5 /***********************************************************/
10 #include <sys/types.h>
22 #include <glib/gstdio.h>
24 #include "pbd/error.h"
26 #include "ardour/linux_vst_support.h"
28 #define MAX_STRING_LEN 256
30 static char* read_string(FILE *fp)
32 char buf[MAX_STRING_LEN];
34 if (!fgets( buf, MAX_STRING_LEN, fp )) {
38 if(strlen(buf) < MAX_STRING_LEN) {
40 buf[strlen(buf)-1] = 0;
48 /** Read an integer value from a line in fp into n,
49 * @return true on success, false on failure.
52 read_int (FILE* fp, int* n)
54 char buf[MAX_STRING_LEN];
56 char* p = fgets (buf, MAX_STRING_LEN, fp);
61 return (sscanf (p, "%d", n) != 1);
65 load_vstfx_info_file (FILE* fp)
69 if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
73 if ((info->name = read_string(fp)) == 0) goto error;
74 if ((info->creator = read_string(fp)) == 0) goto error;
75 if (read_int (fp, &info->UniqueID)) goto error;
76 if ((info->Category = read_string(fp)) == 0) goto error;
77 if (read_int (fp, &info->numInputs)) goto error;
78 if (read_int (fp, &info->numOutputs)) goto error;
79 if (read_int (fp, &info->numParams)) goto error;
80 if (read_int (fp, &info->wantMidi)) goto error;
81 if (read_int (fp, &info->hasEditor)) goto error;
82 if (read_int (fp, &info->canProcessReplacing)) goto error;
84 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
88 for (int i = 0; i < info->numParams; ++i) {
89 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
92 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
96 for (int i = 0; i < info->numParams; ++i) {
97 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
108 save_vstfx_info_file (VSTInfo *info, FILE* fp)
111 vstfx_error("** ERROR ** VSTFXinfofile : info ptr is 0\n");
116 vstfx_error("** ERROR ** VSTFXinfofile : file ptr is 0\n");
120 fprintf( fp, "%s\n", info->name );
121 fprintf( fp, "%s\n", info->creator );
122 fprintf( fp, "%d\n", info->UniqueID );
123 fprintf( fp, "%s\n", info->Category );
124 fprintf( fp, "%d\n", info->numInputs );
125 fprintf( fp, "%d\n", info->numOutputs );
126 fprintf( fp, "%d\n", info->numParams );
127 fprintf( fp, "%d\n", info->wantMidi );
128 fprintf( fp, "%d\n", info->hasEditor );
129 fprintf( fp, "%d\n", info->canProcessReplacing );
131 for (int i = 0; i < info->numParams; i++) {
132 fprintf(fp, "%s\n", info->ParamNames[i]);
135 for (int i = 0; i < info->numParams; i++) {
136 fprintf(fp, "%s\n", info->ParamLabels[i]);
142 static char* vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
150 if (strstr (dllpath, ".so" ) == 0) {
155 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
157 dir_path = g_path_get_dirname (dllpath);
160 base = g_path_get_basename (dllpath);
161 blen = strlen (base) + 2; // null char and '.'
162 basename = (char*) g_malloc (blen);
163 snprintf (basename, blen, ".%s.fsi", base);
166 path = g_build_filename (dir_path, basename, NULL);
172 if (g_file_test (path, GFileTest (G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR))) {
174 /* info file exists in same location as the shared object, so
175 check if its current and up to date
181 if (stat (dllpath, &dllstat) == 0) {
182 if (stat(path, statbuf) == 0) {
183 if (dllstat.st_mtime <= statbuf->st_mtime) {
184 /* plugin is older than info file */
197 static FILE* vstfx_infofile_for_read (char* dllpath)
199 struct stat own_statbuf;
200 struct stat sys_statbuf;
204 own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
205 sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
209 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
210 /* system info file is newer, use it */
211 return fopen (sys_info, "r");
214 return fopen (own_info, "r");
221 static FILE* vstfx_infofile_create (char* dllpath, int personal)
229 if (strstr (dllpath, ".so" ) == 0) {
234 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
236 /* if the directory doesn't yet exist, try to create it */
238 if (!g_file_test (dir_path, G_FILE_TEST_IS_DIR)) {
239 if (g_mkdir (dir_path, 0700)) {
245 dir_path = g_path_get_dirname (dllpath);
248 base = g_path_get_basename (dllpath);
249 blen = strlen (base) + 2; // null char and '.'
250 basename = (char*) g_malloc (blen);
251 snprintf (basename, blen, ".%s.fsi", base);
254 path = g_build_filename (dir_path, basename, NULL);
259 FILE* f = fopen (path, "w");
265 static FILE* vstfx_infofile_for_write (char* dllpath)
269 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
270 f = vstfx_infofile_create (dllpath, 1);
277 int vstfx_can_midi (VSTState* vstfx)
279 AEffect *plugin = vstfx->plugin;
281 int vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
283 if (vst_version >= 2)
285 /* should we send it VST events (i.e. MIDI) */
287 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0))
294 vstfx_info_from_plugin (VSTState* vstfx)
298 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
302 /*We need to init the creator because some plugins
303 fail to implement getVendorString, and so won't stuff the
304 string with any name*/
306 char creator[65] = "Unknown\0";
311 plugin = vstfx->plugin;
313 info->name = strdup(vstfx->handle->name );
315 /*If the plugin doesn't bother to implement GetVendorString we will
316 have pre-stuffed the string with 'Unkown' */
318 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
320 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
321 so if its just a zero length string we replace it with 'Unknown' */
323 if (strlen(creator) == 0) {
324 info->creator = strdup("Unknown");
326 info->creator = strdup (creator);
329 info->UniqueID = plugin->uniqueID;
331 info->Category = strdup("None"); // FIXME:
332 info->numInputs = plugin->numInputs;
333 info->numOutputs = plugin->numOutputs;
334 info->numParams = plugin->numParams;
335 info->wantMidi = vstfx_can_midi(vstfx);
336 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
337 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
338 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
339 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
341 for (int i = 0; i < info->numParams; ++i) {
345 /*Not all plugins give parameters labels as well as names*/
347 strcpy(name, "No Name");
348 strcpy(label, "No Label");
350 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
351 info->ParamNames[i] = strdup(name);
353 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
354 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
355 info->ParamLabels[i] = strdup(label);
360 /* A simple 'dummy' audiomaster callback which should be ok,
361 we will only be instantiating the plugin in order to get its info*/
364 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
366 if (opcode == audioMasterVersion)
372 /*Try to get plugin info - first by looking for a .fsi cache of the
373 data, and if that doesn't exist, load the plugin, get its data and
374 then cache it for future ref*/
377 vstfx_get_info (char* dllpath)
384 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
386 info = load_vstfx_info_file (infofile);
389 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
394 if(!(h = vstfx_load(dllpath))) {
395 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
399 if(!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
401 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
405 infofile = vstfx_infofile_for_write (dllpath);
410 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
414 info = vstfx_info_from_plugin(vstfx);
416 save_vstfx_info_file(info, infofile);
426 vstfx_free_info (VSTInfo *info)
428 for (int i = 0; i < info->numParams; i++) {
429 free (info->ParamNames[i]);
430 free (info->ParamLabels[i]);
434 free (info->creator);
435 free (info->Category);