summaryrefslogtreecommitdiffstats
path: root/hosts
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-04-20 03:57:41 +0000
committerDavid Robillard <d@drobilla.net>2007-04-20 03:57:41 +0000
commitbe95afee123c169b083049c2f441d3860a12fea0 (patch)
tree78c907f1828bbe8a8521a83313e793c01a989903 /hosts
parentc625507d8a3101742c083dd030cbd1e1e295a9a0 (diff)
downloadlilv-be95afee123c169b083049c2f441d3860a12fea0.tar.gz
lilv-be95afee123c169b083049c2f441d3860a12fea0.tar.bz2
lilv-be95afee123c169b083049c2f441d3860a12fea0.zip
Clean up API, hide more methods that should not be exposed to user.
Document performance of most methods. Clean up and clarify documentation. git-svn-id: http://svn.drobilla.net/lad/slv2@460 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'hosts')
-rw-r--r--hosts/lv2_jack_host.c7
-rw-r--r--hosts/lv2_simple_jack_host.c203
2 files changed, 181 insertions, 29 deletions
diff --git a/hosts/lv2_jack_host.c b/hosts/lv2_jack_host.c
index a0bca74..4a9995f 100644
--- a/hosts/lv2_jack_host.c
+++ b/hosts/lv2_jack_host.c
@@ -74,6 +74,7 @@ main(int argc, char** argv)
fprintf(stderr, "\nYou must specify a plugin URI to load.\n");
fprintf(stderr, "\nKnown plugins:\n\n");
list_plugins(plugins);
+ slv2_world_free(world);
return EXIT_FAILURE;
}
@@ -82,7 +83,7 @@ main(int argc, char** argv)
if (!host.plugin) {
fprintf(stderr, "Failed to find plugin %s.\n", plugin_uri);
- slv2_plugins_free(plugins);
+ slv2_world_free(world);
return EXIT_FAILURE;
}
@@ -126,7 +127,7 @@ main(int argc, char** argv)
/* Deactivate plugin and JACK */
slv2_instance_free(host.instance);
- slv2_plugins_free(plugins);
+ slv2_plugins_free(world, plugins);
printf("Shutting down JACK.\n");
for (unsigned long i=0; i < host.num_ports; ++i) {
@@ -140,6 +141,7 @@ main(int argc, char** argv)
}
jack_client_close(host.jack_client);
+ slv2_plugins_free(world, plugins);
slv2_world_free(world);
return 0;
@@ -166,7 +168,6 @@ void
create_port(struct JackHost* host,
uint32_t port_index)
{
- //struct Port* port = (Port*)malloc(sizeof(Port));
struct Port* const port = &host->ports[port_index];
port->class = SLV2_UNKNOWN_PORT_CLASS;
diff --git a/hosts/lv2_simple_jack_host.c b/hosts/lv2_simple_jack_host.c
index 0b938e3..d0fabc1 100644
--- a/hosts/lv2_simple_jack_host.c
+++ b/hosts/lv2_simple_jack_host.c
@@ -20,49 +20,200 @@
#include <stdlib.h>
#include <string.h>
#include <slv2/slv2.h>
+#include <jack/jack.h>
+
+
+/** This program's data */
+struct JackHost {
+ jack_client_t* jack_client; /**< Jack client */
+ SLV2Plugin plugin; /**< Plugin "class" (actually just a few strings) */
+ SLV2Instance instance; /**< Plugin "instance" (loaded shared lib) */
+ uint32_t num_ports; /**< Size of the two following arrays: */
+ jack_port_t** jack_ports; /**< For audio ports, otherwise NULL */
+ float* controls; /**< For control ports, otherwise 0.0f */
+};
+
+
+void die(const char* msg);
+void create_port(struct JackHost* host, uint32_t port_index);
+int jack_process_cb(jack_nframes_t nframes, void* data);
+void list_plugins(SLV2Plugins list);
+
int
-main(/*int argc, char** argv*/)
+main(int argc, char** argv)
{
+ struct JackHost host;
+ host.jack_client = NULL;
+ host.num_ports = 0;
+ host.jack_ports = NULL;
+ host.controls = NULL;
+
+ /* Find all installed plugins */
SLV2World world = slv2_world_new();
slv2_world_load_all(world);
+ SLV2Plugins plugins = slv2_world_get_all_plugins(world);
+
+ /* Find the plugin to run */
+ const char* plugin_uri = (argc == 2) ? argv[1] : NULL;
+ if (!plugin_uri) {
+ fprintf(stderr, "\nYou must specify a plugin URI to load.\n");
+ fprintf(stderr, "\nKnown plugins:\n\n");
+ list_plugins(plugins);
+ slv2_world_free(world);
+ return EXIT_FAILURE;
+ }
- /*printf("********** All plugins **********\n");
+ printf("URI:\t%s\n", plugin_uri);
+ host.plugin = slv2_plugins_get_by_uri(plugins, plugin_uri);
+
+ if (!host.plugin) {
+ fprintf(stderr, "Failed to find plugin %s.\n", plugin_uri);
+ slv2_world_free(world);
+ return EXIT_FAILURE;
+ }
- SLV2Plugins plugins = slv2_model_get_all_plugins(model);
+ /* Get the plugin's name */
+ char* name = slv2_plugin_get_name(host.plugin);
+ printf("Name:\t%s\n", name);
+
+ /* Connect to JACK (with plugin name as client name) */
+ host.jack_client = jack_client_open(name, JackNullOption, NULL);
+ free(name);
+ if (!host.jack_client)
+ die("Failed to connect to JACK.");
+ else
+ printf("Connected to JACK.\n");
+
+ /* Instantiate the plugin */
+ host.instance = slv2_plugin_instantiate(
+ host.plugin, jack_get_sample_rate(host.jack_client), NULL);
+ if (!host.instance)
+ die("Failed to instantiate plugin.\n");
+ else
+ printf("Succesfully instantiated plugin.\n");
- for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
- SLV2Plugin p = slv2_plugins_get_at(plugins, i);
- printf("Plugin: %s\n", slv2_plugin_get_uri(p));
+ jack_set_process_callback(host.jack_client, &jack_process_cb, (void*)(&host));
+
+ /* Create ports */
+ host.num_ports = slv2_plugin_get_num_ports(host.plugin);
+ host.jack_ports = calloc((size_t)host.num_ports, sizeof(jack_port_t*));
+ host.controls = calloc((size_t)host.num_ports, sizeof(float*));
+
+ for (uint32_t i=0; i < host.num_ports; ++i)
+ create_port(&host, i);
+
+ /* Activate plugin and JACK */
+ slv2_instance_activate(host.instance);
+ jack_activate(host.jack_client);
+
+ /* Run */
+ printf("Press enter to quit: ");
+ getc(stdin);
+ printf("\n");
+
+ /* Deactivate plugin and JACK */
+ slv2_instance_free(host.instance);
+
+ printf("Shutting down JACK.\n");
+ for (unsigned long i=0; i < host.num_ports; ++i) {
+ if (host.jack_ports[i] != NULL) {
+ jack_port_unregister(host.jack_client, host.jack_ports[i]);
+ host.jack_ports[i] = NULL;
+ }
}
+ jack_client_close(host.jack_client);
+
+ slv2_world_free(world);
+
+ return 0;
+}
+
- slv2_plugins_free(plugins);*/
+/** Abort and exit on error */
+void
+die(const char* msg)
+{
+ fprintf(stderr, "%s\n", msg);
+ exit(EXIT_FAILURE);
+}
+
+
+/** Creates a port and connects the plugin instance to it's data location.
+ *
+ * For audio ports, creates a jack port and connects plugin port to buffer.
+ *
+ * For control ports, sets controls array to default value and connects plugin
+ * port to that element.
+ */
+void
+create_port(struct JackHost* host,
+ uint32_t index)
+{
+ SLV2Port port = slv2_plugin_get_port_by_index(host->plugin, index);
+
+ /* Get the port symbol (label) for console printing */
+ char* symbol = slv2_port_get_symbol(host->plugin, port);
+ /* Initialize the port array elements */
+ host->jack_ports[index] = NULL;
+ host->controls[index] = 0.0f;
- printf("********** Plugins with MIDI input **********\n");
+ /* Get the 'class' of the port (control input, audio output, etc) */
+ SLV2PortClass class = slv2_port_get_class(host->plugin, port);
- /*const char* query =
- "PREFIX : <http://lv2plug.in/ontology#>\n"
- //"PREFIX llext: <http://ll-plugins.nongnu.org/lv2/ext/>\n"
- "SELECT DISTINCT ?plugin WHERE {\n"
- " ?plugin a :Plugin ;\n"
- " :port ?port .\n"
- " ?port :symbol \"in\". \n"
- //" :port [ a llext:MidiPort; a :InputPort ] .\n"
- "}\n";
+ /* Connect the port based on it's 'class' */
+ switch (class) {
+ case SLV2_CONTROL_INPUT:
+ host->controls[index] = slv2_port_get_default_value(host->plugin, port);
+ slv2_instance_connect_port(host->instance, index, &host->controls[index]);
+ printf("Set %s to %f\n", symbol, host->controls[index]);
+ break;
+ case SLV2_CONTROL_OUTPUT:
+ slv2_instance_connect_port(host->instance, index, &host->controls[index]);
+ break;
+ case SLV2_AUDIO_INPUT:
+ host->jack_ports[index] = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
+ break;
+ case SLV2_AUDIO_OUTPUT:
+ host->jack_ports[index] = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
+ break;
+ default:
+ // Simple examples don't have to be robust :)
+ die("ERROR: Unknown port type, aborting messily!");
+ }
- SLV2Plugins plugins = slv2_model_get_plugins_by_query(model, query);
+ free(symbol);
+}
- for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
- SLV2Plugin p = slv2_plugins_get_at(plugins, i);
- printf("Plugin: %s\n", slv2_plugin_get_uri(p));
- }
-
- slv2_plugins_free(plugins);
- */
- slv2_world_free(world);
+/** Jack process callback. */
+int
+jack_process_cb(jack_nframes_t nframes, void* data)
+{
+ struct JackHost* host = (struct JackHost*)data;
+
+ /* Connect plugin ports directly to JACK buffers */
+ for (uint32_t i=0; i < host->num_ports; ++i)
+ if (host->jack_ports[i] != NULL)
+ slv2_instance_connect_port(host->instance, i,
+ jack_port_get_buffer(host->jack_ports[i], nframes));
+ /* Run plugin for this cycle */
+ slv2_instance_run(host->instance, nframes);
+
return 0;
}
+
+
+void
+list_plugins(SLV2Plugins list)
+{
+ for (unsigned i=0; i < slv2_plugins_size(list); ++i) {
+ SLV2Plugin p = slv2_plugins_get_at(list, i);
+ printf("%s\n", slv2_plugin_get_uri(p));
+ }
+}