2 Copyright (C) 2012-2014 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /** @file libs/ardour/vst_info_file.cc
21 * @brief Code to manage info files containing cached information about a plugin.
22 * e.g. its name, creator etc.
28 #include <sys/types.h>
40 #include <glib/gstdio.h>
43 #include "pbd/error.h"
45 #include "ardour/linux_vst_support.h"
46 #include "ardour/vst_info_file.h"
48 #define MAX_STRING_LEN 256
53 read_string (FILE *fp)
55 char buf[MAX_STRING_LEN];
57 if (!fgets (buf, MAX_STRING_LEN, fp)) {
61 if (strlen(buf) < MAX_STRING_LEN) {
63 buf[strlen(buf)-1] = 0;
71 /** Read an integer value from a line in fp into n,
72 * @return true on failure, false on success.
75 read_int (FILE* fp, int* n)
77 char buf[MAX_STRING_LEN];
79 char* p = fgets (buf, MAX_STRING_LEN, fp);
84 return (sscanf (p, "%d", n) != 1);
88 vstfx_clear_info_list (vector<VSTInfo *> *infos)
90 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
97 vstfx_load_info_block(FILE* fp, VSTInfo *info)
99 if ((info->name = read_string(fp)) == 0) return false;
100 if ((info->creator = read_string(fp)) == 0) return false;
101 if (read_int (fp, &info->UniqueID)) return false;
102 if ((info->Category = read_string(fp)) == 0) return false;
103 if (read_int (fp, &info->numInputs)) return false;
104 if (read_int (fp, &info->numOutputs)) return false;
105 if (read_int (fp, &info->numParams)) return false;
106 if (read_int (fp, &info->wantMidi)) return false;
107 if (read_int (fp, &info->hasEditor)) return false;
108 if (read_int (fp, &info->canProcessReplacing)) return false;
110 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
114 for (int i = 0; i < info->numParams; ++i) {
115 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
118 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
122 for (int i = 0; i < info->numParams; ++i) {
123 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
131 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
134 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
137 if (vstfx_load_info_block(fp, info)) {
138 if (strncmp (info->Category, "Shell", 5)) {
139 infos->push_back(info);
142 vstfx_free_info(info);
143 if (read_int (fp, &plugin_cnt)) {
144 for (int i = 0; i < plugin_cnt; i++) {
145 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
146 vstfx_clear_info_list(infos);
149 if (vstfx_load_info_block(fp, info)) {
150 infos->push_back(info);
152 vstfx_free_info(info);
153 vstfx_clear_info_list(infos);
158 return false; /* Bad file */
163 vstfx_free_info(info);
164 vstfx_clear_info_list(infos);
169 vstfx_write_info_block (FILE* fp, VSTInfo *info)
174 fprintf (fp, "%s\n", info->name);
175 fprintf (fp, "%s\n", info->creator);
176 fprintf (fp, "%d\n", info->UniqueID);
177 fprintf (fp, "%s\n", info->Category);
178 fprintf (fp, "%d\n", info->numInputs);
179 fprintf (fp, "%d\n", info->numOutputs);
180 fprintf (fp, "%d\n", info->numParams);
181 fprintf (fp, "%d\n", info->wantMidi);
182 fprintf (fp, "%d\n", info->hasEditor);
183 fprintf (fp, "%d\n", info->canProcessReplacing);
185 for (int i = 0; i < info->numParams; i++) {
186 fprintf (fp, "%s\n", info->ParamNames[i]);
189 for (int i = 0; i < info->numParams; i++) {
190 fprintf (fp, "%s\n", info->ParamLabels[i]);
195 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
200 if (infos->size() > 1) {
201 vector<VSTInfo *>::iterator x = infos->begin();
202 /* write out the shell info first along with count of the number of
203 * plugins contained in this shell
205 vstfx_write_info_block(fp, *x);
206 fprintf( fp, "%d\n", infos->size() - 1 );
208 /* Now write out the info for each plugin */
209 for (; x != infos->end(); ++x) {
210 vstfx_write_info_block(fp, *x);
212 } else if (infos->size() == 1) {
213 vstfx_write_info_block(fp, infos->front());
215 PBD::warning << "Zero plugins in VST." << endmsg; // XXX here?
220 vstfx_infofile_path (char* dllpath, int personal)
224 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
226 /* If the directory doesn't exist, try to create it */
227 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
228 if (g_mkdir (dir.c_str (), 0700)) {
234 dir = Glib::path_get_dirname (std::string(dllpath));
238 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
239 return Glib::build_filename (dir, s.str ());
243 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
245 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
249 string const path = vstfx_infofile_path (dllpath, personal);
251 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
253 /* info file exists in same location as the shared object, so
254 check if its current and up to date
260 if (stat (dllpath, &dllstat) == 0) {
261 if (stat (path.c_str(), statbuf) == 0) {
262 if (dllstat.st_mtime <= statbuf->st_mtime) {
263 /* plugin is older than info file */
264 return strdup (path.c_str ());
275 vstfx_infofile_for_read (char* dllpath)
277 struct stat own_statbuf;
278 struct stat sys_statbuf;
281 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
282 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
286 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
287 /* system info file is newer, use it */
288 rv = g_fopen (sys_info, "rb");
291 rv = g_fopen (own_info, "rb");
293 } else if (sys_info) {
294 rv = g_fopen (sys_info, "rb");
303 vstfx_infofile_create (char* dllpath, int personal)
305 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
309 string const path = vstfx_infofile_path (dllpath, personal);
310 return fopen (path.c_str(), "w");
314 vstfx_infofile_for_write (char* dllpath)
318 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
319 f = vstfx_infofile_create (dllpath, 1);
326 int vstfx_can_midi (VSTState* vstfx)
328 AEffect* plugin = vstfx->plugin;
330 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
332 if (vst_version >= 2) {
333 /* should we send it VST events (i.e. MIDI) */
335 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
344 vstfx_info_from_plugin (VSTState* vstfx)
348 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
353 /*We need to init the creator because some plugins
354 fail to implement getVendorString, and so won't stuff the
355 string with any name*/
357 char creator[65] = "Unknown\0";
359 AEffect* plugin = vstfx->plugin;
361 info->name = strdup (vstfx->handle->name);
363 /*If the plugin doesn't bother to implement GetVendorString we will
364 have pre-stuffed the string with 'Unkown' */
366 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
368 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
369 so if its just a zero length string we replace it with 'Unknown' */
371 if (strlen(creator) == 0) {
372 info->creator = strdup ("Unknown");
374 info->creator = strdup (creator);
377 info->UniqueID = plugin->uniqueID;
379 info->Category = strdup("None"); /* XXX */
380 info->numInputs = plugin->numInputs;
381 info->numOutputs = plugin->numOutputs;
382 info->numParams = plugin->numParams;
383 info->wantMidi = vstfx_can_midi(vstfx);
384 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
385 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
386 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
387 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
389 for (int i = 0; i < info->numParams; ++i) {
393 /* Not all plugins give parameters labels as well as names */
395 strcpy (name, "No Name");
396 strcpy (label, "No Label");
398 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
399 info->ParamNames[i] = strdup(name);
401 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
402 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
403 info->ParamLabels[i] = strdup(label);
408 /* A simple 'dummy' audiomaster callback which should be ok,
409 we will only be instantiating the plugin in order to get its info
413 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
416 static const char* can_do_strings[] = {
420 static const int can_do_string_count = sizeof (can_do_strings) / sizeof (char*);
421 static int current_loading_id = 0;
424 if (opcode == audioMasterVersion) {
428 else if (opcode == audioMasterCanDo) {
429 for (int i = 0; i < can_do_string_count; i++) {
430 if (! strcmp(can_do_strings[i], (const char*)ptr)) {
436 else if (opcode == audioMasterCurrentId) {
437 return current_loading_id;
446 vstfx_get_info_from_file(char* dllpath, vector<VSTInfo*> *infos)
450 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
451 rv = vstfx_load_info_file(infofile, infos);
454 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
461 vstfx_free_info (VSTInfo *info)
463 for (int i = 0; i < info->numParams; i++) {
464 free (info->ParamNames[i]);
465 free (info->ParamLabels[i]);
469 free (info->creator);
470 free (info->Category);
471 free (info->ParamNames);
472 free (info->ParamLabels);
477 vstfx_free_info_list (vector<VSTInfo *> *infos)
479 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
486 /** Try to get plugin info - first by looking for a .fsi cache of the
487 data, and if that doesn't exist, load the plugin, get its data and
488 then cache it for future ref
492 vstfx_get_info_lx (char* dllpath)
497 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
499 // TODO check blacklist
500 // TODO pre-check file extension ?
502 if (vstfx_get_info_from_file(dllpath, infos)) {
503 PBD::info << "using cache for LinuxVST plugin '" << dllpath << "'" << endmsg;
507 if (!(h = vstfx_load(dllpath))) {
508 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
512 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
514 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
518 infofile = vstfx_infofile_for_write (dllpath);
523 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
527 VSTInfo* info = vstfx_info_from_plugin (vstfx);
528 infos->push_back(info); //XXX
530 vstfx_write_info_file (infofile, infos);
539 #ifdef WINDOWS_VST_SUPPORT
543 vstfx_get_info_fst (char* dllpath)
548 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
550 // TODO check blacklist
551 // TODO pre-check file extension ?
553 if (vstfx_get_info_from_file(dllpath, infos)) {
554 PBD::info << "using cache for VST plugin '" << dllpath << "'" << endmsg;
558 if(!(h = fst_load(dllpath))) {
559 PBD::warning << "Cannot get VST information from " << dllpath << ": load failed." << endmsg;
563 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
565 PBD::warning << "Cannot get VST information from " << dllpath << ": instantiation failed." << endmsg;
569 infofile = vstfx_infofile_for_write (dllpath);
573 //fst_unload(&h); // XXX -> fst_close()
574 PBD::warning << "Cannot get VST information from " << dllpath << ": cannot create new FST info file." << endmsg;
578 VSTInfo* info = vstfx_info_from_plugin(vstfx);
579 infos->push_back(info); //XXX
581 vstfx_write_info_file (infofile, infos);
585 //fst_unload(&h); // XXX -> fst_close()