changeset 393:632c662e95e7 vh

Small test program & some changes to support it
author Chris Cannam
date Wed, 20 May 2015 17:46:17 +0100
parents f4e07ae2725a
children 96cdf661d538
files host/test-c.c src/vamp-hostsdk/host-c.cpp vamp-hostsdk/host-c.h
diffstat 3 files changed, 101 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/test-c.c	Wed May 20 17:46:17 2015 +0100
@@ -0,0 +1,40 @@
+
+#include <vamp-hostsdk/host-c.h>
+
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int i;
+    int libcount = vhGetLibraryCount();
+
+    printf("Vamp plugin libraries found:\n");
+    for (i = 0; i < libcount; ++i) {
+	printf("%d: %s\n", i, vhGetLibraryName(i));
+    }
+
+    printf("Going to try loading qm-vamp-plugins...\n");
+    int libindex = vhGetLibraryIndex("qm-vamp-plugins");
+    vhLibrary lib = vhLoadLibrary(libindex);
+    if (!lib) {
+	printf("Failure!\n");
+	return 1;
+    }
+
+    int plugincount = vhGetPluginCount(lib);
+    printf("Success: it contains %d plugins; they are:\n", plugincount);
+
+    for (i = 0; i < plugincount; ++i) {
+	const VampPluginDescriptor *descriptor = vhGetPluginDescriptor(lib, i);
+	if (!descriptor) {
+	    printf("<unknown! failed to load>\n");
+	} else {
+	    printf("%s\n", descriptor->identifier);
+	}
+    }
+
+    vhUnloadLibrary(lib);
+    
+    return 0;
+}
+
--- a/src/vamp-hostsdk/host-c.cpp	Wed May 20 16:55:46 2015 +0100
+++ b/src/vamp-hostsdk/host-c.cpp	Wed May 20 17:46:17 2015 +0100
@@ -74,45 +74,52 @@
     return int(files.size());
 }
 
-const char *vhGetLibraryName(int library)
+const char *vhGetLibraryName(int index)
 {
     initFilenames();
-    if (library < int(files.size())) {
-        return cnames[files[library]];
+    if (index >= 0 && index < int(files.size())) {
+        return cnames[files[index]];
     }
     else return 0;
 }
 
-vhLibrary vhLoadLibrary(const char *libraryName)
+int vhGetLibraryIndex(const char *name)
 {
     for (size_t i = 0; i < files.size(); ++i) {
-
-        if (Files::lcBasename(libraryName) == Files::lcBasename(files[i])) {
-
-            string fullPath = files[i];
-            void *lib = Files::loadLibrary(fullPath);
-
-            if (!lib) return 0;
-            
-            VampGetPluginDescriptorFunction func =
-                (VampGetPluginDescriptorFunction)Files::lookupInLibrary
-                (lib, "vampGetPluginDescriptor");
-            if (!func) {
-                cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \""
-                     << fullPath << "\"" << endl;
-                Files::unloadLibrary(lib);
-                return 0;
-            }
-
-            vhLibrary_t *vhl = new vhLibrary_t(lib, func);
-            while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) {
-                ++vhl->nplugins;
-            }
-            return vhl;
+        if (Files::lcBasename(name) == Files::lcBasename(files[i])) {
+            return i;
         }
     }
+    return -1;
+}
 
-    return 0;
+vhLibrary vhLoadLibrary(int index)
+{
+    initFilenames();
+    if (index < 0 || index >= int(files.size())) {
+        return 0;
+    }
+
+    string fullPath = files[index];
+    void *lib = Files::loadLibrary(fullPath);
+
+    if (!lib) return 0;
+            
+    VampGetPluginDescriptorFunction func =
+        (VampGetPluginDescriptorFunction)Files::lookupInLibrary
+        (lib, "vampGetPluginDescriptor");
+    if (!func) {
+        cerr << "vhLoadLibrary: No vampGetPluginDescriptor function found in library \""
+             << fullPath << "\"" << endl;
+        Files::unloadLibrary(lib);
+        return 0;
+    }
+
+    vhLibrary_t *vhl = new vhLibrary_t(lib, func);
+    while (vhl->func(VAMP_API_VERSION, vhl->nplugins)) {
+        ++vhl->nplugins;
+    }
+    return vhl;
 }
 
 int vhGetPluginCount(vhLibrary library)
--- a/vamp-hostsdk/host-c.h	Wed May 20 16:55:46 2015 +0100
+++ b/vamp-hostsdk/host-c.h	Wed May 20 17:46:17 2015 +0100
@@ -37,8 +37,10 @@
 /*
     This file defines a low-level API for enumerating and loading
     plugin libraries using C calling conventions. It could be used in
-    C programs or in languages with C-compatible foreign-function
-    interfaces.
+    C programs, or in languages with C-compatible foreign-function
+    interfaces. Note that this works by calling to the C++ Vamp host
+    SDK, so any program using this interface must still link against
+    the rest of the Vamp plugin library and the C++ standard library.
 
     This is not the simplest or easiest interface for hosting Vamp
     plugins -- if you have the capability to use the C++ API, please
@@ -55,7 +57,7 @@
     you go straight from the key to a live instance of the plugin. The
     PluginLoader also provides various facilities to adapt the plugin
     based on your requirements (e.g. to do time- to frequency-domain
-    conversion for its input).
+    conversion for you if the plugin requires it).
 
     This low-level C interface, on the other hand, deals only in
     plugin libraries and static descriptors, not in plugin
@@ -63,13 +65,16 @@
     the base .soname of each library. Then you can retrieve each of
     the raw C plugin descriptors from a library, and use the
     descriptor (whose interface is defined in vamp/vamp.h) to
-    instantiate the plugin. So this header corresponds to the first
-    part of the PluginLoader class interface (finding and loading
-    plugin libraries and retrieving plugin descriptors from them) but
-    does not do any of the rest (instantiating and adapting the
-    plugins themselves), which may mean the resulting plugin is
-    relatively hard to use compared to that provided by the
-    PluginLoader API.
+    instantiate the plugin.
+
+    So this header corresponds to the first part of the PluginLoader
+    class interface: finding and loading plugin libraries and
+    retrieving plugin descriptors from them. But it does not do any of
+    the rest, i.e. instantiating and adapting the plugins themselves.
+    Although this makes the API appear very simple, it means the
+    resulting plugins are relatively hard to use compared to those
+    obtained by the PluginLoader API. There is no way to get to the
+    full C++ abstraction using this API.
 
     This API is not thread-safe; use it from a single application
     thread, or guard access to it with a mutex.
@@ -103,12 +108,17 @@
 extern const char *vhGetLibraryName(int library);
 
 /**
- * Load a plugin library given its library name, that is, the base
- * soname as returned by vhGetLibraryName. If the library cannot be
+ * Return the library index for the given library name, or -1 if the
+ * name is not known.
+ */
+extern int vhGetLibraryIndex(const char *name);
+    
+/**
+ * Load the library with the given index. If the library cannot be
  * loaded for any reason, the return value is 0; otherwise it is an
  * opaque pointer suitable for passing to other functions in this API.
  */
-extern vhLibrary vhLoadLibrary(const char *libraryName);
+extern vhLibrary vhLoadLibrary(int library);
 
 /**
  * Return the number of Vamp plugins in the given library.
@@ -117,8 +127,9 @@
 
 /**
  * Return a Vamp plugin descriptor for a plugin in a given
- * library. (This simply calls the vampGetPluginDescriptor function in
- * that library with the given plugin index and returns the result.)
+ * library. This simply calls the vampGetPluginDescriptor function in
+ * that library with the given plugin index and returns the
+ * result. See vamp/vamp.h for details about the plugin descriptor.
  */ 
 extern const VampPluginDescriptor *vhGetPluginDescriptor(vhLibrary library,
 							 int plugin);