summaryrefslogtreecommitdiffstats
path: root/hosts
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-02-05 22:34:56 +0000
committerDavid Robillard <d@drobilla.net>2007-02-05 22:34:56 +0000
commit1988a63b41a0e81f348d5df3394d41d3248d442b (patch)
tree2ec6758a2554f6456ae0f377cc47b4f1260a8b2d /hosts
parent1f0922ab4982da825ddfbb792ec9f871b2b88804 (diff)
downloadlilv-1988a63b41a0e81f348d5df3394d41d3248d442b.tar.gz
lilv-1988a63b41a0e81f348d5df3394d41d3248d442b.tar.bz2
lilv-1988a63b41a0e81f348d5df3394d41d3248d442b.zip
Reorganized tree in preparation for beta release.
Split simple (example) jack host and more useful one (with midi). Working Jack/LV2 midi in lv2_jack_host. Added lv2_list. git-svn-id: http://svn.drobilla.net/lad/slv2@279 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'hosts')
-rw-r--r--hosts/Makefile.am18
-rw-r--r--hosts/lv2-midifunctions.h161
-rw-r--r--hosts/lv2-miditype.h170
-rw-r--r--hosts/lv2_jack_host.c338
-rw-r--r--hosts/lv2_simple_jack_host.c220
5 files changed, 907 insertions, 0 deletions
diff --git a/hosts/Makefile.am b/hosts/Makefile.am
new file mode 100644
index 0000000..aa794c5
--- /dev/null
+++ b/hosts/Makefile.am
@@ -0,0 +1,18 @@
+AM_CFLAGS = -std=c99 -I$(top_srcdir) @RASQAL_CFLAGS@
+
+if WITH_JACK
+
+bin_PROGRAMS = lv2_jack_host lv2_simple_jack_host
+
+lv2_jack_host_CFLAGS = @JACK_CFLAGS@ $(AM_CFLAGS)
+
+lv2_jack_host_DEPENDENCIES = ../src/libslv2.la
+lv2_jack_host_LDADD = ../src/libslv2.la @JACK_LIBS@ @RASQAL_LIBS@
+
+lv2_simple_jack_host_LDADD = ../src/libslv2.la @JACK_LIBS@ @RASQAL_LIBS@
+
+lv2_jack_host_SOURCES = \
+ lv2_jack_host.c
+
+endif
+
diff --git a/hosts/lv2-midifunctions.h b/hosts/lv2-midifunctions.h
new file mode 100644
index 0000000..3a17395
--- /dev/null
+++ b/hosts/lv2-midifunctions.h
@@ -0,0 +1,161 @@
+/****************************************************************************
+
+ lv2-midifunctions.h - support file for using MIDI in LV2 plugins
+
+ Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
+
+****************************************************************************/
+
+#ifndef LV2_MIDIFUNCTIONS
+#define LV2_MIDIFUNCTIONS
+
+#include <string.h>
+
+#include "lv2-miditype.h"
+
+
+/** This structure contains information about a MIDI port buffer, the
+ current period size, and the position in the MIDI data buffer that
+ we are currently reading from or writing to. It needs to be recreated
+ or at least reinitialised every process() call. */
+typedef struct {
+
+ /** The MIDI port structure that we want to read or write. */
+ LV2_MIDI* midi;
+
+ /** The number of frames in this process cycle. */
+ uint32_t frame_count;
+
+ /** The current position in the data buffer. Should be initialised to 0. */
+ uint32_t position;
+
+} LV2_MIDIState;
+
+
+static LV2_MIDI* lv2midi_new(uint32_t capacity)
+{
+ LV2_MIDI* midi = malloc(sizeof(LV2_MIDI));
+
+ midi->event_count = 0;
+ midi->capacity = capacity;
+ midi->size = 0;
+ midi->data = malloc(sizeof(char) * capacity);
+
+ return midi;
+}
+
+
+static void lv2midi_free(LV2_MIDI* midi)
+{
+ free(midi->data);
+ free(midi);
+}
+
+
+static void lv2midi_reset_buffer(LV2_MIDI* midi)
+{
+ midi->event_count = 0;
+ midi->size = 0;
+}
+
+static void lv2midi_reset_state(LV2_MIDIState* state, LV2_MIDI* midi, uint32_t frame_count)
+{
+ state->midi = midi;
+ state->frame_count = frame_count;
+ state->position = 0;
+}
+
+
+/** This function advances the read/write position in @c state to the next
+ event and returns its timestamp, or the @c frame_count member of @c state
+ is there are no more events. */
+static double lv2midi_increment(LV2_MIDIState* state) {
+
+ if (state->position + sizeof(double) + sizeof(size_t) >= state->midi->size) {
+ state->position = state->midi->size;
+ return state->frame_count;
+ }
+
+ state->position += sizeof(double);
+ size_t size = *(size_t*)(state->midi->data + state->position);
+ state->position += sizeof(size_t);
+ state->position += size;
+
+ if (state->position >= state->midi->size)
+ return state->frame_count;
+
+ return *(double*)(state->midi->data + state->position);
+}
+
+
+/** This function reads one event from the port associated with the @c state
+ parameter and writes its timestamp, size and a pointer to its data bytes
+ into the parameters @c timestamp, @c size and @c data, respectively.
+ It does not advance the read position in the MIDI data buffer, two
+ subsequent calls to lv2midi_get_event() will read the same event.
+
+ The function returns the timestamp for the read event, or the @c frame_count
+ member of @c state if there are no more events in the buffer. */
+static double lv2midi_get_event(LV2_MIDIState* state,
+ double* timestamp,
+ uint32_t* size,
+ unsigned char** data) {
+
+ if (state->position >= state->midi->size) {
+ state->position = state->midi->size;
+ *timestamp = state->frame_count;
+ *size = 0;
+ *data = NULL;
+ return *timestamp;
+ }
+
+ *timestamp = *(double*)(state->midi->data + state->position);
+ *size = *(size_t*)(state->midi->data + state->position + sizeof(double));
+ *data = state->midi->data + state->position +
+ sizeof(double) + sizeof(size_t);
+ return *timestamp;
+}
+
+
+/** This function writes one MIDI event to the port buffer associated with
+ @c state. It returns 0 when the event was written successfully to the
+ buffer, and -1 when there was not enough room. The read/write position
+ is advanced automatically. */
+static int lv2midi_put_event(LV2_MIDIState* state,
+ double timestamp,
+ uint32_t size,
+ const unsigned char* data) {
+
+ if (state->midi->capacity - state->midi->size <
+ sizeof(double) + sizeof(size_t) + size)
+ return -1;
+
+ *(double*)(state->midi->data + state->midi->size) = timestamp;
+ state->midi->size += sizeof(double);
+ *(size_t*)(state->midi->data + state->midi->size) = size;
+ state->midi->size += sizeof(size_t);
+ memcpy(state->midi->data + state->midi->size, data, (size_t)size);
+ state->midi->size += size;
+
+ ++state->midi->event_count;
+
+ return 0;
+}
+
+
+#endif
+
diff --git a/hosts/lv2-miditype.h b/hosts/lv2-miditype.h
new file mode 100644
index 0000000..465d5d5
--- /dev/null
+++ b/hosts/lv2-miditype.h
@@ -0,0 +1,170 @@
+/****************************************************************************
+
+ lv2-miditype.h - header file for using MIDI in LV2 plugins
+
+ Copyright (C) 2006 Lars Luthman <lars.luthman@gmail.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
+
+****************************************************************************/
+
+#ifndef LV2_MIDITYPE_H
+#define LV2_MIDITYPE_H
+
+
+/** This data structure is used to contain the MIDI events for one run()
+ cycle. The port buffer for a LV2 port that has the datatype
+ <http://ll-plugins.nongnu.org/lv2/ext/miditype> should be a pointer
+ to an instance of this struct.
+
+ To store two Note On events on MIDI channel 0 in a buffer, with timestamps
+ 12 and 35.5, you could use something like this code (assuming that
+ midi_data is a variable of type LV2_MIDI):
+ @code
+
+ size_t buffer_offset = 0;
+ *(double*)(midi_data->data + buffer_offset) = 12;
+ buffer_offset += sizeof(double);
+ *(size_t*)(midi_data->data + buffer_offset) = 3;
+ buffer_offset += sizeof(size_t);
+ midi_data->data[buffer_offset++] = 0x90;
+ midi_data->data[buffer_offset++] = 0x48;
+ midi_data->data[buffer_offset++] = 0x64;
+ ++midi_data->event_count;
+
+ *(double*)(midi_data->data + buffer_offset) = 35.5;
+ buffer_offset += sizeof(double);
+ *(size_t*)(midi_data->data + buffer_offset) = 3;
+ buffer_offset += sizeof(size_t);
+ midi_data->data[buffer_offset++] = 0x90;
+ midi_data->data[buffer_offset++] = 0x55;
+ midi_data->data[buffer_offset++] = 0x64;
+ ++midi_data->event_count;
+
+ midi_data->size = buffer_offset;
+
+ @endcode
+
+ This would be done by the host in the case of an input port, and by the
+ plugin in the case of an output port. Whoever is writing events to the
+ buffer must also take care not to exceed the capacity of the data buffer.
+
+ To read events from a buffer, you could do something like this:
+ @code
+
+ size_t buffer_offset = 0;
+ uint32_t i;
+ for (i = 0; i < midi_data->event_count; ++i) {
+ double timestamp = *(double*)(midi_data->data + buffer_offset);
+ buffer_offset += sizeof(double);
+ size_t size = *(size_t*)(midi_data->data + buffer_offset);
+ buffer_offset += sizeof(size_t);
+ do_something_with_event(timestamp, size,
+ midi_data->data + buffer_offset);
+ buffer_offset += size;
+ }
+
+ @endcode
+*/
+typedef struct {
+
+ /** The number of MIDI events in the data buffer.
+ INPUT PORTS: It's the host's responsibility to set this field to the
+ number of MIDI events contained in the data buffer before calling the
+ plugin's run() function. The plugin may not change this field.
+ OUTPUT PORTS: It's the plugin's responsibility to set this field to the
+ number of MIDI events it has stored in the data buffer before returning
+ from the run() function. Any initial value should be ignored by the
+ plugin.
+ */
+ uint32_t event_count;
+
+ /** The size of the data buffer in bytes. It is set by the host and may not
+ be changed by the plugin. The host is allowed to change this between
+ run() calls.
+ */
+ uint32_t capacity;
+
+ /** The size of the initial part of the data buffer that actually contains
+ data.
+ INPUT PORTS: It's the host's responsibility to set this field to the
+ number of bytes used by all MIDI events it has written to the buffer
+ (including timestamps and size fields) before calling the plugin's
+ run() function. The plugin may not change this field.
+ OUTPUT PORTS: It's the plugin's responsibility to set this field to
+ the number of bytes used by all MIDI events it has written to the
+ buffer (including timestamps and size fields) before returning from
+ the run() function. Any initial value should be ignored by the plugin.
+ */
+ uint32_t size;
+
+ /** The data buffer that is used to store MIDI events. The events are packed
+ after each other, and the format of each event is as follows:
+
+ First there is a timestamp, which should have the type "double",
+ i.e. have the same bit size as a double and the same bit layout as a
+ double (whatever that is on the current platform). This timestamp gives
+ the offset from the beginning of the current cycle, in frames, that
+ the MIDI event occurs on. It must be strictly smaller than the 'nframes'
+ parameter to the current run() call. The MIDI events in the buffer must
+ be ordered by their timestamp, e.g. an event with a timestamp of 123.23
+ must be stored after an event with a timestamp of 65.0.
+
+ The second part of the event is a size field, which should have the type
+ "size_t" (as defined in the standard C header stddef.h). It should
+ contain the size of the MIDI data for this event, i.e. the number of
+ bytes used to store the actual MIDI event. The bytes used by the
+ timestamp and the size field should not be counted.
+
+ The third part of the event is the actual MIDI data. There are some
+ requirements that must be followed:
+
+ * Running status is not allowed. Every event must have its own status
+ byte.
+ * Note On events with velocity 0 are not allowed. These events are
+ equivalent to Note Off in standard MIDI streams, but in order to make
+ plugins and hosts easier to write, as well as more efficient, only
+ proper Note Off events are allowed as Note Off.
+ * "Realtime events" (status bytes 0xF8 to 0xFF) are allowed, but may not
+ occur inside other events like they are allowed to in hardware MIDI
+ streams.
+ * All events must be fully contained in a single data buffer, i.e. events
+ may not "wrap around" by storing the first few bytes in one buffer and
+ then wait for the next run() call to store the rest of the event. If
+ there isn't enough space in the current data buffer to store an event,
+ the event will either have to wait until next run() call, be ignored,
+ or compensated for in some more clever way.
+ * All events must be valid MIDI events. This means for example that
+ only the first byte in each event (the status byte) may have the eighth
+ bit set, that Note On and Note Off events are always 3 bytes long etc.
+ The MIDI writer (host or plugin) is responsible for writing valid MIDI
+ events to the buffer, and the MIDI reader (plugin or host) can assume
+ that all events are valid.
+
+ On a platform where double is 8 bytes and size_t is 4 bytes, the data
+ buffer layout for a 3-byte event followed by a 4-byte event may look
+ something like this:
+ _______________________________________________________________
+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ...
+ |TIMESTAMP 1 |SIZE 1 |DATA |TIMESTAMP 2 |SIZE 2 |DATA | ...
+
+ */
+ unsigned char* data;
+
+} LV2_MIDI;
+
+
+
+#endif
diff --git a/hosts/lv2_jack_host.c b/hosts/lv2_jack_host.c
new file mode 100644
index 0000000..e5e51a3
--- /dev/null
+++ b/hosts/lv2_jack_host.c
@@ -0,0 +1,338 @@
+/* jack_host - SLV2 Jack Host
+ * Copyright (C) 2007 Dave Robillard <drobilla.net>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <slv2/slv2.h>
+#include <jack/jack.h>
+
+#define WITH_MIDI 1
+#define MIDI_BUFFER_SIZE 1024
+
+#ifdef WITH_MIDI
+#include <jack/midiport.h>
+#include "lv2-miditype.h"
+#include "lv2-midifunctions.h"
+#endif // WITH_MIDI
+
+struct Port {
+ enum Direction { INPUT, OUTPUT} direction;
+ enum Type { UNKNOWN, FLOAT, MIDI } type;
+
+ jack_port_t* jack_port; /**< For audio and MIDI ports, otherwise NULL */
+ float control; /**< For control ports, otherwise 0.0f */
+ LV2_MIDI* midi_buffer; /**< For midi ports, otherwise NULL */
+};
+
+
+/** 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: */
+ struct Port* ports; /** Port array of size num_ports */
+};
+
+
+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(SLV2List list);
+
+
+int
+main(int argc, char** argv)
+{
+ struct JackHost host;
+ host.jack_client = NULL;
+ host.num_ports = 0;
+ host.ports = NULL;
+
+ /* Find all installed plugins */
+ SLV2List plugins = slv2_list_new();
+ slv2_list_load_all(plugins);
+ //slv2_list_load_bundle(plugins, "http://www.scs.carleton.ca/~drobilla/files/Amp-swh.lv2");
+
+ /* 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);
+ return EXIT_FAILURE;
+ }
+
+ printf("URI:\t%s\n", plugin_uri);
+ host.plugin = slv2_list_get_plugin_by_uri(plugins, plugin_uri);
+
+ if (!host.plugin) {
+ fprintf(stderr, "Failed to find plugin %s.\n", plugin_uri);
+ slv2_list_free(plugins);
+ return EXIT_FAILURE;
+ }
+
+ /* 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");
+
+ 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.ports = calloc((size_t)host.num_ports, sizeof(struct Port));
+
+ 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);
+ slv2_list_free(plugins);
+
+ printf("Shutting down JACK.\n");
+ for (unsigned long i=0; i < host.num_ports; ++i) {
+ if (host.ports[i].jack_port != NULL) {
+ jack_port_unregister(host.jack_client, host.ports[i].jack_port);
+ host.ports[i].jack_port = NULL;
+ }
+ if (host.ports[i].midi_buffer != NULL) {
+ lv2midi_free(host.ports[i].midi_buffer);
+ }
+ }
+ jack_client_close(host.jack_client);
+
+ return 0;
+}
+
+
+/** 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 port_index)
+{
+ //struct Port* port = (Port*)malloc(sizeof(Port));
+ struct Port* const port = &host->ports[port_index];
+
+ port->type = UNKNOWN;
+ port->jack_port = NULL;
+ port->control = 0.0f;
+ port->midi_buffer = NULL;
+
+ slv2_instance_connect_port(host->instance, port_index, NULL);
+
+ char* type_str = slv2_port_get_data_type(host->plugin, port_index);
+ if (!strcmp(type_str, SLV2_DATA_TYPE_FLOAT))
+ port->type = FLOAT;
+ else if (!strcmp(type_str, SLV2_DATA_TYPE_MIDI))
+ port->type = MIDI;
+
+ /* Get the port symbol (label) for console printing */
+ char* symbol = slv2_port_get_symbol(host->plugin, port_index);
+
+ /* Get the 'class' (not data type) of the port (control input, audio output, etc) */
+ enum SLV2PortClass class = slv2_port_get_class(host->plugin, port_index);
+
+ if (port->type == FLOAT) {
+
+ /* Connect the port based on it's 'class' */
+ switch (class) {
+ case SLV2_CONTROL_RATE_INPUT:
+ port->direction = INPUT;
+ port->control = slv2_port_get_default_value(host->plugin, port_index);
+ slv2_instance_connect_port(host->instance, port_index, &port->control);
+ printf("Set %s to %f\n", symbol, host->ports[port_index].control);
+ break;
+ case SLV2_CONTROL_RATE_OUTPUT:
+ port->direction = OUTPUT;
+ slv2_instance_connect_port(host->instance, port_index, &port->control);
+ break;
+ case SLV2_AUDIO_RATE_INPUT:
+ port->direction = INPUT;
+ port->jack_port = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
+ break;
+ case SLV2_AUDIO_RATE_OUTPUT:
+ port->direction = OUTPUT;
+ port->jack_port = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
+ break;
+ default:
+ fprintf(stderr, "ERROR: Unknown port class\n");
+ }
+
+ } else if (port->type == MIDI) {
+
+ if (class == SLV2_CONTROL_RATE_INPUT) {
+ port->direction = INPUT;
+ port->jack_port = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
+ port->midi_buffer = lv2midi_new(MIDI_BUFFER_SIZE);
+ slv2_instance_connect_port(host->instance, port_index, port->midi_buffer);
+ } else if (class == SLV2_CONTROL_RATE_OUTPUT) {
+ port->direction = OUTPUT;
+ port->jack_port = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
+ port->midi_buffer = lv2midi_new(MIDI_BUFFER_SIZE);
+ slv2_instance_connect_port(host->instance, port_index, port->midi_buffer);
+ } else {
+ fprintf(stderr, "ERROR: Audio rate MIDI port?? Ignoring.\n");
+ }
+
+ } else {
+
+ fprintf(stderr, "Unrecognized data type %s for port %s, ignored.\n",
+ type_str, symbol);
+ fprintf(stderr, " %s\n",
+ SLV2_DATA_TYPE_MIDI);
+
+ }
+
+ free(symbol);
+ free(type_str);
+}
+
+
+/** Jack process callback. */
+int
+jack_process_cb(jack_nframes_t nframes, void* data)
+{
+ struct JackHost* const host = (struct JackHost*)data;
+
+ /* Connect inputs */
+ for (uint32_t p=0; p < host->num_ports; ++p) {
+ if (!host->ports[p].jack_port)
+ continue;
+
+ if (host->ports[p].type == FLOAT) {
+ slv2_instance_connect_port(host->instance, p,
+ jack_port_get_buffer(host->ports[p].jack_port, nframes));
+ } else if (host->ports[p].type == MIDI) {
+
+ void* jack_buffer = jack_port_get_buffer(host->ports[p].jack_port, nframes);
+
+ LV2_MIDIState state;
+ lv2midi_reset_state(&state, host->ports[p].midi_buffer, nframes);
+ lv2midi_reset_buffer(state.midi);
+
+ if (host->ports[p].direction == INPUT) {
+ jack_midi_event_t ev;
+
+ const jack_nframes_t event_count
+ = jack_midi_get_event_count(jack_buffer, nframes);
+
+ for (jack_nframes_t e=0; e < event_count; ++e) {
+
+ jack_midi_event_get(&ev, jack_buffer, e, nframes);
+
+ state.midi = host->ports[p].midi_buffer;
+ lv2midi_put_event(&state, (double)ev.time, ev.size, ev.buffer);
+ }
+ }
+ }
+ }
+
+
+ /* Run plugin for this cycle */
+ slv2_instance_run(host->instance, nframes);
+
+
+ /* Deliver output */
+ for (uint32_t p=0; p < host->num_ports; ++p) {
+ if (host->ports[p].jack_port
+ && host->ports[p].type == MIDI
+ && host->ports[p].direction == OUTPUT) {
+
+ void* jack_buffer = jack_port_get_buffer(host->ports[p].jack_port, nframes);
+
+ jack_midi_clear_buffer(jack_buffer, nframes);
+
+ LV2_MIDIState state;
+ lv2midi_reset_state(&state, host->ports[p].midi_buffer, nframes);
+
+ double timestamp = 0.0f;
+ uint32_t size = 0;
+ unsigned char* data = NULL;
+
+ const uint32_t event_count = state.midi->event_count;
+
+ for (uint32_t i=0; i < event_count; ++i) {
+ lv2midi_get_event(&state, &timestamp, &size, &data);
+
+ jack_midi_event_write(jack_buffer,
+ (jack_nframes_t)timestamp, data, size, nframes);
+
+ lv2midi_increment(&state);
+ }
+
+ }
+ }
+
+ return 0;
+}
+
+
+void
+list_plugins(SLV2List list)
+{
+ for (size_t i=0; i < slv2_list_get_length(list); ++i) {
+ const SLV2Plugin* const p = slv2_list_get_plugin_by_index(list, i);
+ printf("%s\n", slv2_plugin_get_uri(p));
+ }
+}
diff --git a/hosts/lv2_simple_jack_host.c b/hosts/lv2_simple_jack_host.c
new file mode 100644
index 0000000..f0a266d
--- /dev/null
+++ b/hosts/lv2_simple_jack_host.c
@@ -0,0 +1,220 @@
+/* SLV2 Simple Jack Host Example
+ * Copyright (C) 2007 Dave Robillard <drobilla.net>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#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(SLV2List list);
+
+
+int
+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 */
+ SLV2List plugins = slv2_list_new();
+ slv2_list_load_all(plugins);
+ //slv2_list_load_bundle(plugins, "http://www.scs.carleton.ca/~drobilla/files/Amp-swh.lv2");
+
+ /* 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);
+ return EXIT_FAILURE;
+ }
+
+ printf("URI:\t%s\n", plugin_uri);
+ host.plugin = slv2_list_get_plugin_by_uri(plugins, plugin_uri);
+
+ if (!host.plugin) {
+ fprintf(stderr, "Failed to find plugin %s.\n", plugin_uri);
+ slv2_list_free(plugins);
+ return EXIT_FAILURE;
+ }
+
+ /* 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");
+
+ 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);
+ slv2_list_free(plugins);
+
+ 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);
+
+ return 0;
+}
+
+
+/** 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 port_index)
+{
+ /* Make sure this is a float port */
+ char* type = slv2_port_get_data_type(host->plugin, port_index);
+ if (strcmp(type, SLV2_DATA_TYPE_FLOAT))
+ die("Unrecognized data type, aborting.");
+ free(type);
+
+ /* Get the port symbol (label) for console printing */
+ char* symbol = slv2_port_get_symbol(host->plugin, port_index);
+
+ /* Initialize the port array elements */
+ host->jack_ports[port_index] = NULL;
+ host->controls[port_index] = 0.0f;
+
+ /* Get the 'class' of the port (control input, audio output, etc) */
+ enum SLV2PortClass class = slv2_port_get_class(host->plugin, port_index);
+
+ /* Connect the port based on it's 'class' */
+ switch (class) {
+ case SLV2_CONTROL_RATE_INPUT:
+ host->controls[port_index] = slv2_port_get_default_value(host->plugin, port_index);
+ slv2_instance_connect_port(host->instance, port_index, &host->controls[port_index]);
+ printf("Set %s to %f\n", symbol, host->controls[port_index]);
+ break;
+ case SLV2_CONTROL_RATE_OUTPUT:
+ slv2_instance_connect_port(host->instance, port_index, &host->controls[port_index]);
+ break;
+ case SLV2_AUDIO_RATE_INPUT:
+ host->jack_ports[port_index] = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
+ break;
+ case SLV2_AUDIO_RATE_OUTPUT:
+ host->jack_ports[port_index] = jack_port_register(host->jack_client,
+ symbol, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
+ break;
+ default:
+ die("ERROR: Unknown port type, aborting messily!");
+ }
+
+ free(symbol);
+}
+
+
+/** 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(SLV2List list)
+{
+ for (size_t i=0; i < slv2_list_get_length(list); ++i) {
+ const SLV2Plugin* const p = slv2_list_get_plugin_by_index(list, i);
+ printf("%s\n", slv2_plugin_get_uri(p));
+ }
+}