2 Copyright (C) 2012 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"
47 #define MAX_STRING_LEN 256
52 read_string (FILE *fp)
54 char buf[MAX_STRING_LEN];
56 if (!fgets (buf, MAX_STRING_LEN, fp)) {
60 if (strlen(buf) < MAX_STRING_LEN) {
62 buf[strlen(buf)-1] = 0;
70 /** Read an integer value from a line in fp into n,
71 * @return true on failure, false on success.
74 read_int (FILE* fp, int* n)
76 char buf[MAX_STRING_LEN];
78 char* p = fgets (buf, MAX_STRING_LEN, fp);
83 return (sscanf (p, "%d", n) != 1);
87 load_vstfx_info_file (FILE* fp)
91 if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
95 if ((info->name = read_string(fp)) == 0) goto error;
96 if ((info->creator = read_string(fp)) == 0) goto error;
97 if (read_int (fp, &info->UniqueID)) goto error;
98 if ((info->Category = read_string(fp)) == 0) goto error;
99 if (read_int (fp, &info->numInputs)) goto error;
100 if (read_int (fp, &info->numOutputs)) goto error;
101 if (read_int (fp, &info->numParams)) goto error;
102 if (read_int (fp, &info->wantMidi)) goto error;
103 if (read_int (fp, &info->hasEditor)) goto error;
104 if (read_int (fp, &info->canProcessReplacing)) goto error;
106 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
110 for (int i = 0; i < info->numParams; ++i) {
111 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
114 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
118 for (int i = 0; i < info->numParams; ++i) {
119 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
130 save_vstfx_info_file (VSTInfo *info, FILE* fp)
135 fprintf (fp, "%s\n", info->name);
136 fprintf (fp, "%s\n", info->creator);
137 fprintf (fp, "%d\n", info->UniqueID);
138 fprintf (fp, "%s\n", info->Category);
139 fprintf (fp, "%d\n", info->numInputs);
140 fprintf (fp, "%d\n", info->numOutputs);
141 fprintf (fp, "%d\n", info->numParams);
142 fprintf (fp, "%d\n", info->wantMidi);
143 fprintf (fp, "%d\n", info->hasEditor);
144 fprintf (fp, "%d\n", info->canProcessReplacing);
146 for (int i = 0; i < info->numParams; i++) {
147 fprintf (fp, "%s\n", info->ParamNames[i]);
150 for (int i = 0; i < info->numParams; i++) {
151 fprintf (fp, "%s\n", info->ParamLabels[i]);
158 vstfx_infofile_path (char* dllpath, int personal)
162 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
164 /* If the directory doesn't exist, try to create it */
165 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
166 if (g_mkdir (dir.c_str (), 0700)) {
172 dir = Glib::path_get_dirname (std::string(dllpath));
176 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
177 return Glib::build_filename (dir, s.str ());
181 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
183 if (strstr (dllpath, ".so" ) == 0) {
187 string const path = vstfx_infofile_path (dllpath, personal);
189 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
191 /* info file exists in same location as the shared object, so
192 check if its current and up to date
198 if (stat (dllpath, &dllstat) == 0) {
199 if (stat (path.c_str(), statbuf) == 0) {
200 if (dllstat.st_mtime <= statbuf->st_mtime) {
201 /* plugin is older than info file */
202 return strdup (path.c_str ());
213 vstfx_infofile_for_read (char* dllpath)
215 struct stat own_statbuf;
216 struct stat sys_statbuf;
219 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
220 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
224 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
225 /* system info file is newer, use it */
226 rv = g_fopen (sys_info, "rb");
229 rv = g_fopen (own_info, "rb");
239 vstfx_infofile_create (char* dllpath, int personal)
241 if (strstr (dllpath, ".so" ) == 0) {
245 string const path = vstfx_infofile_path (dllpath, personal);
246 return fopen (path.c_str(), "w");
250 vstfx_infofile_for_write (char* dllpath)
254 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
255 f = vstfx_infofile_create (dllpath, 1);
262 int vstfx_can_midi (VSTState* vstfx)
264 AEffect* plugin = vstfx->plugin;
266 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
268 if (vst_version >= 2) {
269 /* should we send it VST events (i.e. MIDI) */
271 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
280 vstfx_info_from_plugin (VSTState* vstfx)
284 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
289 /*We need to init the creator because some plugins
290 fail to implement getVendorString, and so won't stuff the
291 string with any name*/
293 char creator[65] = "Unknown\0";
295 AEffect* plugin = vstfx->plugin;
297 info->name = strdup (vstfx->handle->name);
299 /*If the plugin doesn't bother to implement GetVendorString we will
300 have pre-stuffed the string with 'Unkown' */
302 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
304 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
305 so if its just a zero length string we replace it with 'Unknown' */
307 if (strlen(creator) == 0) {
308 info->creator = strdup ("Unknown");
310 info->creator = strdup (creator);
313 info->UniqueID = plugin->uniqueID;
315 info->Category = strdup("None"); /* XXX */
316 info->numInputs = plugin->numInputs;
317 info->numOutputs = plugin->numOutputs;
318 info->numParams = plugin->numParams;
319 info->wantMidi = vstfx_can_midi(vstfx);
320 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
321 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
322 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
323 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
325 for (int i = 0; i < info->numParams; ++i) {
329 /* Not all plugins give parameters labels as well as names */
331 strcpy (name, "No Name");
332 strcpy (label, "No Label");
334 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
335 info->ParamNames[i] = strdup(name);
337 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
338 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
339 info->ParamLabels[i] = strdup(label);
344 /* A simple 'dummy' audiomaster callback which should be ok,
345 we will only be instantiating the plugin in order to get its info
349 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
351 if (opcode == audioMasterVersion) {
358 /** Try to get plugin info - first by looking for a .fsi cache of the
359 data, and if that doesn't exist, load the plugin, get its data and
360 then cache it for future ref
364 vstfx_get_info (char* dllpath)
370 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
372 info = load_vstfx_info_file (infofile);
375 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
380 if (!(h = vstfx_load(dllpath))) {
381 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
385 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
387 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
391 infofile = vstfx_infofile_for_write (dllpath);
396 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
400 VSTInfo* info = vstfx_info_from_plugin (vstfx);
402 save_vstfx_info_file (info, infofile);
412 vstfx_free_info (VSTInfo *info)
414 for (int i = 0; i < info->numParams; i++) {
415 free (info->ParamNames[i]);
416 free (info->ParamLabels[i]);
420 free (info->creator);
421 free (info->Category);
422 free (info->ParamNames);
423 free (info->ParamLabels);