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 (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;
218 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
219 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
223 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
224 /* system info file is newer, use it */
225 return g_fopen (sys_info, "rb");
228 return g_fopen (own_info, "rb");
236 vstfx_infofile_create (char* dllpath, int personal)
238 if (strstr (dllpath, ".so" ) == 0) {
242 string const path = vstfx_infofile_path (dllpath, personal);
243 return fopen (path.c_str(), "w");
247 vstfx_infofile_for_write (char* dllpath)
251 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
252 f = vstfx_infofile_create (dllpath, 1);
259 int vstfx_can_midi (VSTState* vstfx)
261 AEffect* plugin = vstfx->plugin;
263 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
265 if (vst_version >= 2) {
266 /* should we send it VST events (i.e. MIDI) */
268 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
277 vstfx_info_from_plugin (VSTState* vstfx)
281 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
286 /*We need to init the creator because some plugins
287 fail to implement getVendorString, and so won't stuff the
288 string with any name*/
290 char creator[65] = "Unknown\0";
292 AEffect* plugin = vstfx->plugin;
294 info->name = strdup (vstfx->handle->name);
296 /*If the plugin doesn't bother to implement GetVendorString we will
297 have pre-stuffed the string with 'Unkown' */
299 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
301 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
302 so if its just a zero length string we replace it with 'Unknown' */
304 if (strlen(creator) == 0) {
305 info->creator = strdup ("Unknown");
307 info->creator = strdup (creator);
310 info->UniqueID = plugin->uniqueID;
312 info->Category = strdup("None"); /* XXX */
313 info->numInputs = plugin->numInputs;
314 info->numOutputs = plugin->numOutputs;
315 info->numParams = plugin->numParams;
316 info->wantMidi = vstfx_can_midi(vstfx);
317 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
318 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
319 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
320 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
322 for (int i = 0; i < info->numParams; ++i) {
326 /* Not all plugins give parameters labels as well as names */
328 strcpy (name, "No Name");
329 strcpy (label, "No Label");
331 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
332 info->ParamNames[i] = strdup(name);
334 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
335 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
336 info->ParamLabels[i] = strdup(label);
341 /* A simple 'dummy' audiomaster callback which should be ok,
342 we will only be instantiating the plugin in order to get its info
346 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
348 if (opcode == audioMasterVersion) {
355 /** Try to get plugin info - first by looking for a .fsi cache of the
356 data, and if that doesn't exist, load the plugin, get its data and
357 then cache it for future ref
361 vstfx_get_info (char* dllpath)
367 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
369 info = load_vstfx_info_file (infofile);
372 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
377 if (!(h = vstfx_load(dllpath))) {
378 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
382 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
384 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
388 infofile = vstfx_infofile_for_write (dllpath);
393 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
397 VSTInfo* info = vstfx_info_from_plugin (vstfx);
399 save_vstfx_info_file (info, infofile);
409 vstfx_free_info (VSTInfo *info)
411 for (int i = 0; i < info->numParams; i++) {
412 free (info->ParamNames[i]);
413 free (info->ParamLabels[i]);
417 free (info->creator);
418 free (info->Category);