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 /***********************************************************/
21 #include <glib/gstdio.h>
23 #include "ardour/linux_vst_support.h"
25 #define MAX_STRING_LEN 256
27 static char* read_string(FILE *fp)
29 char buf[MAX_STRING_LEN];
31 if (!fgets( buf, MAX_STRING_LEN, fp )) {
35 if(strlen(buf) < MAX_STRING_LEN) {
37 buf[strlen(buf)-1] = 0;
46 load_vstfx_info_file (FILE* fp)
51 if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
55 if((info->name = read_string(fp)) == 0) goto error;
56 if((info->creator = read_string(fp)) == 0) goto error;
57 if(1 != fscanf(fp, "%d\n", &info->UniqueID)) goto error;
58 if((info->Category = read_string(fp)) == 0) goto error;
59 if(1 != fscanf(fp, "%d\n", &info->numInputs)) goto error;
60 if(1 != fscanf(fp, "%d\n", &info->numOutputs)) goto error;
61 if(1 != fscanf(fp, "%d\n", &info->numParams)) goto error;
62 if(1 != fscanf(fp, "%d\n", &info->wantMidi)) goto error;
63 if(1 != fscanf(fp, "%d\n", &info->hasEditor)) goto error;
64 if(1 != fscanf(fp, "%d\n", &info->canProcessReplacing)) goto error;
66 if((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
70 for (i=0; i<info->numParams; i++) {
71 if((info->ParamNames[i] = read_string(fp)) == 0) goto error;
74 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
78 for (i=0; i < info->numParams; i++) {
79 if((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
90 save_vstfx_info_file (VSTInfo *info, FILE* fp)
95 vstfx_error("** ERROR ** VSTFXinfofile : info ptr is 0\n");
100 vstfx_error("** ERROR ** VSTFXinfofile : file ptr is 0\n");
104 fprintf( fp, "%s\n", info->name );
105 fprintf( fp, "%s\n", info->creator );
106 fprintf( fp, "%d\n", info->UniqueID );
107 fprintf( fp, "%s\n", info->Category );
108 fprintf( fp, "%d\n", info->numInputs );
109 fprintf( fp, "%d\n", info->numOutputs );
110 fprintf( fp, "%d\n", info->numParams );
111 fprintf( fp, "%d\n", info->wantMidi );
112 fprintf( fp, "%d\n", info->hasEditor );
113 fprintf( fp, "%d\n", info->canProcessReplacing );
115 for (i=0; i < info->numParams; i++) {
116 fprintf(fp, "%s\n", info->ParamNames[i]);
119 for (i=0; i < info->numParams; i++) {
120 fprintf(fp, "%s\n", info->ParamLabels[i]);
126 static char* vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
134 if (strstr (dllpath, ".so" ) == 0) {
139 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
141 dir_path = g_path_get_dirname (dllpath);
144 base = g_path_get_basename (dllpath);
145 blen = strlen (base) + 2; // null char and '.'
146 basename = (char*) g_malloc (blen);
147 snprintf (basename, blen, ".%s.fsi", base);
150 path = g_build_filename (dir_path, basename, NULL);
156 if (g_file_test (path, GFileTest (G_FILE_TEST_EXISTS|G_FILE_TEST_IS_REGULAR))) {
158 /* info file exists in same location as the shared object, so
159 check if its current and up to date
165 if (stat (dllpath, &dllstat) == 0) {
166 if (stat(path, statbuf) == 0) {
167 if (dllstat.st_mtime <= statbuf->st_mtime) {
168 /* plugin is older than info file */
181 static FILE* vstfx_infofile_for_read (char* dllpath)
183 struct stat own_statbuf;
184 struct stat sys_statbuf;
188 own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
189 sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
193 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
194 /* system info file is newer, use it */
195 return fopen (sys_info, "r");
198 return fopen (own_info, "r");
205 static FILE* vstfx_infofile_create (char* dllpath, int personal)
213 if (strstr (dllpath, ".so" ) == 0) {
218 dir_path = g_build_filename (g_get_home_dir(), ".fst", NULL);
220 /* if the directory doesn't yet exist, try to create it */
222 if (!g_file_test (dir_path, G_FILE_TEST_IS_DIR)) {
223 if (g_mkdir (dir_path, 0700)) {
229 dir_path = g_path_get_dirname (dllpath);
232 base = g_path_get_basename (dllpath);
233 blen = strlen (base) + 2; // null char and '.'
234 basename = (char*) g_malloc (blen);
235 snprintf (basename, blen, ".%s.fsi", base);
238 path = g_build_filename (dir_path, basename, NULL);
243 FILE* f = fopen (path, "w");
249 static FILE* vstfx_infofile_for_write (char* dllpath)
253 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
254 f = vstfx_infofile_create (dllpath, 1);
261 int vstfx_can_midi (VSTState* vstfx)
263 AEffect *plugin = vstfx->plugin;
265 int vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
267 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))
278 vstfx_info_from_plugin (VSTState* vstfx)
280 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
285 /*We need to init the creator because some plugins
286 fail to implement getVendorString, and so won't stuff the
287 string with any name*/
289 char creator[65] = "Unknown\0";
293 vstfx_error( "** ERROR ** VSTFXinfofile : vstfx ptr is 0\n" );
300 plugin = vstfx->plugin;
302 info->name = strdup(vstfx->handle->name );
304 /*If the plugin doesn't bother to implement GetVendorString we will
305 have pre-stuffed the string with 'Unkown' */
307 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
309 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
310 so if its just a zero length string we replace it with 'Unknown' */
312 if (strlen(creator) == 0) {
313 info->creator = strdup("Unknown");
315 info->creator = strdup (creator);
318 info->UniqueID = plugin->uniqueID;
320 info->Category = strdup("None"); // FIXME:
321 info->numInputs = plugin->numInputs;
322 info->numOutputs = plugin->numOutputs;
323 info->numParams = plugin->numParams;
324 info->wantMidi = vstfx_can_midi(vstfx);
325 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
326 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
327 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
328 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
330 for(i=0; i < info->numParams; i++) {
334 /*Not all plugins give parameters labels as well as names*/
336 strcpy(name, "No Name");
337 strcpy(label, "No Label");
339 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
340 info->ParamNames[i] = strdup(name);
342 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
343 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
344 info->ParamLabels[i] = strdup(label);
349 /* A simple 'dummy' audiomaster callback which should be ok,
350 we will only be instantiating the plugin in order to get its info*/
353 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
355 if (opcode == audioMasterVersion)
361 /*Try to get plugin info - first by looking for a .fsi cache of the
362 data, and if that doesn't exist, load the plugin, get its data and
363 then cache it for future ref*/
366 vstfx_get_info (char* dllpath)
373 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
375 info = load_vstfx_info_file (infofile);
380 if(!(h = vstfx_load(dllpath)))
383 if(!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
385 vstfx_error( "** ERROR ** VSTFXinfofile : Instantiate failed\n" );
389 infofile = vstfx_infofile_for_write (dllpath);
394 vstfx_error("cannot create new FST info file for plugin");
398 info = vstfx_info_from_plugin(vstfx);
400 save_vstfx_info_file(info, infofile);
410 vstfx_free_info (VSTInfo *info)
412 for (int i = 0; i < info->numParams; i++) {
413 free (info->ParamNames[i]);
414 free (info->ParamLabels[i]);
418 free (info->creator);
419 free (info->Category);