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
52 // TODO: make static parts of this file re-usable for standalone app
53 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
54 // consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
57 #ifdef WINDOWS_VST_SUPPORT
60 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
64 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
67 static int vstfx_current_loading_id = 0;
70 read_string (FILE *fp)
72 char buf[MAX_STRING_LEN];
74 if (!fgets (buf, MAX_STRING_LEN, fp)) {
78 if (strlen(buf) < MAX_STRING_LEN) {
80 buf[strlen(buf)-1] = 0;
88 /** Read an integer value from a line in fp into n,
89 * @return true on failure, false on success.
92 read_int (FILE* fp, int* n)
94 char buf[MAX_STRING_LEN];
96 char* p = fgets (buf, MAX_STRING_LEN, fp);
101 return (sscanf (p, "%d", n) != 1);
105 vstfx_free_info (VSTInfo *info)
107 for (int i = 0; i < info->numParams; i++) {
108 free (info->ParamNames[i]);
109 free (info->ParamLabels[i]);
113 free (info->creator);
114 free (info->Category);
115 free (info->ParamNames);
116 free (info->ParamLabels);
121 vstfx_clear_info_list (vector<VSTInfo *> *infos)
123 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
130 vstfx_load_info_block(FILE* fp, VSTInfo *info)
132 if ((info->name = read_string(fp)) == 0) return false;
133 if ((info->creator = read_string(fp)) == 0) return false;
134 if (read_int (fp, &info->UniqueID)) return false;
135 if ((info->Category = read_string(fp)) == 0) return false;
136 if (read_int (fp, &info->numInputs)) return false;
137 if (read_int (fp, &info->numOutputs)) return false;
138 if (read_int (fp, &info->numParams)) return false;
139 if (read_int (fp, &info->wantMidi)) return false;
140 if (read_int (fp, &info->hasEditor)) return false;
141 if (read_int (fp, &info->canProcessReplacing)) return false;
143 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
147 for (int i = 0; i < info->numParams; ++i) {
148 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
151 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
155 for (int i = 0; i < info->numParams; ++i) {
156 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
164 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
167 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
170 if (vstfx_load_info_block(fp, info)) {
171 if (strncmp (info->Category, "Shell", 5)) {
172 infos->push_back(info);
175 vstfx_free_info(info);
176 if (read_int (fp, &plugin_cnt)) {
177 for (int i = 0; i < plugin_cnt; i++) {
178 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
179 vstfx_clear_info_list(infos);
182 if (vstfx_load_info_block(fp, info)) {
183 infos->push_back(info);
185 vstfx_free_info(info);
186 vstfx_clear_info_list(infos);
191 return false; /* Bad file */
196 vstfx_free_info(info);
197 vstfx_clear_info_list(infos);
202 vstfx_write_info_block (FILE* fp, VSTInfo *info)
207 fprintf (fp, "%s\n", info->name);
208 fprintf (fp, "%s\n", info->creator);
209 fprintf (fp, "%d\n", info->UniqueID);
210 fprintf (fp, "%s\n", info->Category);
211 fprintf (fp, "%d\n", info->numInputs);
212 fprintf (fp, "%d\n", info->numOutputs);
213 fprintf (fp, "%d\n", info->numParams);
214 fprintf (fp, "%d\n", info->wantMidi);
215 fprintf (fp, "%d\n", info->hasEditor);
216 fprintf (fp, "%d\n", info->canProcessReplacing);
218 for (int i = 0; i < info->numParams; i++) {
219 fprintf (fp, "%s\n", info->ParamNames[i]);
222 for (int i = 0; i < info->numParams; i++) {
223 fprintf (fp, "%s\n", info->ParamLabels[i]);
228 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
233 if (infos->size() > 1) {
234 vector<VSTInfo *>::iterator x = infos->begin();
235 /* write out the shell info first along with count of the number of
236 * plugins contained in this shell
238 vstfx_write_info_block(fp, *x);
239 fprintf( fp, "%d\n", infos->size() - 1 );
241 /* Now write out the info for each plugin */
242 for (; x != infos->end(); ++x) {
243 vstfx_write_info_block(fp, *x);
245 } else if (infos->size() == 1) {
246 vstfx_write_info_block(fp, infos->front());
248 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
253 vstfx_infofile_path (const char* dllpath, int personal)
257 // TODO use XDG_CACHE_HOME
258 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
260 /* If the directory doesn't exist, try to create it */
261 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
262 if (g_mkdir (dir.c_str (), 0700)) {
268 dir = Glib::path_get_dirname (std::string(dllpath));
272 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
273 return Glib::build_filename (dir, s.str ());
277 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
279 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
283 string const path = vstfx_infofile_path (dllpath, personal);
285 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
287 /* info file exists in same location as the shared object, so
288 check if its current and up to date
294 if (stat (dllpath, &dllstat) == 0) {
295 if (stat (path.c_str(), statbuf) == 0) {
296 if (dllstat.st_mtime <= statbuf->st_mtime) {
297 /* plugin is older than info file */
298 return strdup (path.c_str ());
309 vstfx_infofile_for_read (const char* dllpath)
311 struct stat own_statbuf;
312 struct stat sys_statbuf;
315 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
316 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
320 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
321 /* system info file is newer, use it */
322 rv = g_fopen (sys_info, "rb");
325 rv = g_fopen (own_info, "rb");
327 } else if (sys_info) {
328 rv = g_fopen (sys_info, "rb");
337 vstfx_infofile_create (const char* dllpath, int personal)
339 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
343 string const path = vstfx_infofile_path (dllpath, personal);
344 return fopen (path.c_str(), "w");
348 vstfx_infofile_for_write (const char* dllpath)
352 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
353 f = vstfx_infofile_create (dllpath, 1);
360 int vstfx_can_midi (VSTState* vstfx)
362 AEffect* plugin = vstfx->plugin;
364 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
366 if (vst_version >= 2) {
367 /* should we send it VST events (i.e. MIDI) */
369 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
378 vstfx_parse_vst_state (VSTState* vstfx)
382 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
387 /*We need to init the creator because some plugins
388 fail to implement getVendorString, and so won't stuff the
389 string with any name*/
391 char creator[65] = "Unknown\0";
393 AEffect* plugin = vstfx->plugin;
395 info->name = strdup (vstfx->handle->name);
397 /*If the plugin doesn't bother to implement GetVendorString we will
398 have pre-stuffed the string with 'Unkown' */
400 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
402 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
403 so if its just a zero length string we replace it with 'Unknown' */
405 if (strlen(creator) == 0) {
406 info->creator = strdup ("Unknown");
408 info->creator = strdup (creator);
412 switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
414 case kPlugCategEffect: info->Category = strdup ("Effect"); break;
415 case kPlugCategSynth: info->Category = strdup ("Synth"); break;
416 case kPlugCategAnalysis: info->Category = strdup ("Anaylsis"); break;
417 case kPlugCategMastering: info->Category = strdup ("Mastering"); break;
418 case kPlugCategSpacializer: info->Category = strdup ("Spacializer"); break;
419 case kPlugCategRoomFx: info->Category = strdup ("RoomFx"); break;
420 case kPlugSurroundFx: info->Category = strdup ("SurroundFx"); break;
421 case kPlugCategRestoration: info->Category = strdup ("Restoration"); break;
422 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
423 case kPlugCategShell: info->Category = strdup ("Shell"); break;
424 case kPlugCategGenerator: info->Category = strdup ("Generator"); break;
425 default: info->Category = strdup ("Unknown"); break;
428 info->UniqueID = plugin->uniqueID;
430 info->numInputs = plugin->numInputs;
431 info->numOutputs = plugin->numOutputs;
432 info->numParams = plugin->numParams;
433 info->wantMidi = vstfx_can_midi(vstfx);
434 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
435 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
436 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
437 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
439 for (int i = 0; i < info->numParams; ++i) {
443 /* Not all plugins give parameters labels as well as names */
445 strcpy (name, "No Name");
446 strcpy (label, "No Label");
448 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
449 info->ParamNames[i] = strdup(name);
451 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
452 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
453 info->ParamLabels[i] = strdup(label);
459 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
464 if ((info = vstfx_parse_vst_state(vstfx))) {
465 infos->push_back(info);
467 /* If this plugin is a Shell and we are not already inside a shell plugin
468 * read the info for all of the plugins contained in this shell.
470 if (!strncmp (info->Category, "Shell", 5)
471 && vstfx->handle->plugincnt == 1) {
473 vector< pair<int, string> > ids;
474 AEffect *plugin = vstfx->plugin;
475 string path = vstfx->handle->path;
478 char name[65] = "Unknown\0";
479 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
480 ids.push_back(std::make_pair(id, name));
484 #ifdef WINDOWS_VST_SUPPORT
485 case 1: fst_close(vstfx); break;
488 case 2: vstfx_close (vstfx); break;
490 default: assert(0); break;
493 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
495 if (id == 0) continue;
496 /* recurse vstfx_get_info() */
499 switch (type) { // TODO use lib ardour's type
500 #ifdef WINDOWS_VST_SUPPORT
501 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
504 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
509 // One shell (some?, all?) does not report the actual plugin name
510 // even after the shelled plugin has been instantiated.
511 // Replace the name of the shell with the real name.
512 info = infos->back();
515 if ((*x).second.length() == 0) {
516 info->name = strdup("Unknown");
519 info->name = strdup ((*x).second.c_str());
528 /* A simple 'dummy' audiomaster callback which should be ok,
529 we will only be instantiating the plugin in order to get its info
533 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
535 const char* vstfx_can_do_strings[] = {
539 const int vstfx_can_do_string_count = 2;
541 if (opcode == audioMasterVersion) {
544 else if (opcode == audioMasterCanDo) {
545 for (int i = 0; i < vstfx_can_do_string_count; i++) {
546 if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
552 else if (opcode == audioMasterCurrentId) {
553 return vstfx_current_loading_id;
561 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
565 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
566 rv = vstfx_load_info_file(infofile, infos);
569 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
577 vstfx_instantiate_and_get_info_lx (
578 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
582 if (!(h = vstfx_load(dllpath))) {
583 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
587 vstfx_current_loading_id = uniqueID;
589 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
591 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
595 vstfx_current_loading_id = 0;
597 vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
605 #ifdef WINDOWS_VST_SUPPORT
607 vstfx_instantiate_and_get_info_fst (
608 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
612 if(!(h = fst_load(dllpath))) {
613 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
617 vstfx_current_loading_id = uniqueID;
619 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
621 vstfx_current_loading_id = 0;
622 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
625 vstfx_current_loading_id = 0;
627 vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
630 //fst_unload(&h); // XXX -> fst_close()
635 static vector<VSTInfo *> *
636 vstfx_get_info (const char* dllpath, int type)
639 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
641 // TODO check blacklist
642 // TODO pre-check file extension ?
644 if (vstfx_get_info_from_file(dllpath, infos)) {
649 // TODO blacklist (in case instantiat fails)
650 switch (type) { // TODO use lib ardour's type
651 #ifdef WINDOWS_VST_SUPPORT
652 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
655 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
666 infofile = vstfx_infofile_for_write (dllpath);
668 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
671 vstfx_write_info_file (infofile, infos);
677 /* *** public API *** */
680 vstfx_free_info_list (vector<VSTInfo *> *infos)
682 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
690 vstfx_get_info_lx (char* dllpath)
692 return vstfx_get_info(dllpath, 2);
696 #ifdef WINDOWS_VST_SUPPORT
698 vstfx_get_info_fst (char* dllpath)
700 return vstfx_get_info(dllpath, 1);