From 7fd4168fe8581e46f4ee35cc182db6220b6eed04 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 6 Jun 2006 20:20:33 +0000 Subject: Moved libslv2 into it's own subdirectory git-svn-id: http://svn.drobilla.net/lad/libslv2@4 a436a847-0d15-0410-975c-d299462d15a1 --- examples/Makefile.am | 2 + examples/hosts/.jack_host.c.swp | Bin 0 -> 28672 bytes examples/hosts/Makefile.am | 24 ++++ examples/hosts/jack_host.c | 218 ++++++++++++++++++++++++++++ examples/hosts/test_host.c | 231 ++++++++++++++++++++++++++++++ examples/plugins/Amp-swh.lv2/Makefile | 10 ++ examples/plugins/Amp-swh.lv2/amp.c | 96 +++++++++++++ examples/plugins/Amp-swh.lv2/amp.so | Bin 0 -> 2629 bytes examples/plugins/Amp-swh.lv2/amp.ttl | 68 +++++++++ examples/plugins/Amp-swh.lv2/manifest.ttl | 9 ++ examples/plugins/Makefile.am | 24 ++++ 11 files changed, 682 insertions(+) create mode 100644 examples/Makefile.am create mode 100644 examples/hosts/.jack_host.c.swp create mode 100644 examples/hosts/Makefile.am create mode 100644 examples/hosts/jack_host.c create mode 100644 examples/hosts/test_host.c create mode 100644 examples/plugins/Amp-swh.lv2/Makefile create mode 100644 examples/plugins/Amp-swh.lv2/amp.c create mode 100755 examples/plugins/Amp-swh.lv2/amp.so create mode 100644 examples/plugins/Amp-swh.lv2/amp.ttl create mode 100644 examples/plugins/Amp-swh.lv2/manifest.ttl create mode 100644 examples/plugins/Makefile.am (limited to 'examples') diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..94939c3 --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,2 @@ +SUBDIRS = plugins hosts + diff --git a/examples/hosts/.jack_host.c.swp b/examples/hosts/.jack_host.c.swp new file mode 100644 index 0000000..eb9bb86 Binary files /dev/null and b/examples/hosts/.jack_host.c.swp differ diff --git a/examples/hosts/Makefile.am b/examples/hosts/Makefile.am new file mode 100644 index 0000000..340c962 --- /dev/null +++ b/examples/hosts/Makefile.am @@ -0,0 +1,24 @@ +AM_CFLAGS = -std=c99 -I. -I$(top_srcdir)/include -I$(top_srcdir) `pkg-config --cflags rasqal` +AM_LDFLAGS = `pkg-config --libs rasqal` + +bin_PROGRAMS = test_host jack_host + +test_host_DEPENDENCIES = ../../src/libslv2.la +test_host_LDADD = ../../src/libslv2.la + +test_host_SOURCES = \ + test_host.c + + +if WITH_JACK + +jack_host_CFLAGS = @JACK_CFLAGS@ $(AM_CFLAGS) +#jack_host_LIBS = @JACK_LIBS@ + +jack_host_DEPENDENCIES = ../../src/libslv2.la +jack_host_LDADD = ../../src/libslv2.la @JACK_LIBS@ + +jack_host_SOURCES = \ + jack_host.c + +endif # WITH_JACK diff --git a/examples/hosts/jack_host.c b/examples/hosts/jack_host.c new file mode 100644 index 0000000..2247e03 --- /dev/null +++ b/examples/hosts/jack_host.c @@ -0,0 +1,218 @@ +/* LibSLV2 Jack Example Host + * Copyright (C) 2006 Dave Robillard + * + * 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 +#include +#include +#include + + +/** 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) */ + unsigned long 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, unsigned long 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(host.num_ports, sizeof(jack_port_t*)); + host.controls = calloc(host.num_ports, sizeof(float*)); + + for (unsigned long 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, + unsigned long port_index) +{ + /* Make sure this is a float port */ + enum SLV2DataType type = slv2_port_get_data_type(host->plugin, port_index); + if (type != SLV2_DATA_TYPE_FLOAT) + die("Unrecognized data type, aborting."); + + /* 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 (unsigned long 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 (int 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/examples/hosts/test_host.c b/examples/hosts/test_host.c new file mode 100644 index 0000000..3ec8d86 --- /dev/null +++ b/examples/hosts/test_host.c @@ -0,0 +1,231 @@ +/* LibSLV2 Test Host + * Copyright (C) 2006 Dave Robillard + * + * 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. + */ + +#define _XOPEN_SOURCE 500 + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + + +void +create_control_input() +{ + printf("Control Input\n"); +} + + +void +create_control_output() +{ + printf("Control Output\n"); +} + + +void +create_audio_input() +{ + printf("Audio Input\n"); +} + + +void +create_audio_output() +{ + printf("Audio Output\n"); +} + + +void +create_port(SLV2Plugin* plugin, + SLV2Instance* instance, + unsigned long port_index) +{ + enum SLV2PortClass class = slv2_port_get_class(plugin, port_index); + + switch (class) { + case SLV2_CONTROL_RATE_INPUT: + create_control_input(port_index); + break; + case SLV2_CONTROL_RATE_OUTPUT: + create_control_output(port_index); + break; + case SLV2_AUDIO_RATE_INPUT: + + create_audio_input(port_index); + break; + case SLV2_AUDIO_RATE_OUTPUT: + create_audio_output(port_index); + break; + default: + printf("Unknown port type, ignored.\n"); + } + //printf("Port %ld class: %d\n", i, slv2_port_get_class(p, i)); +} + + +int +main() +{ + //const char* path = "foo"; + + const char* path = "/home/dave/code/libslv2/examples/plugins"; + + SLV2List plugins = slv2_list_new(); + slv2_list_load_path(plugins, path); + + const char* plugin_uri = "http://plugin.org.uk/swh-plugins/amp"; + printf("URI:\t%s\n", plugin_uri); + + const SLV2Plugin* p = slv2_list_get_plugin_by_uri(plugins, plugin_uri); + if (p) { + /* Get the plugin's name */ + unsigned char* name = slv2_plugin_get_name(p); + printf("Name:\t%s\n", name); + free(name); + + unsigned long num_ports = slv2_plugin_get_num_ports(p); + //printf("Number of ports: %ld\n", num_ports); + + for (unsigned long i=0; i < num_ports; ++i) { + enum SLV2PortClass class = slv2_port_get_class(p, i); + + switch (class) { + case SLV2_CONTROL_RATE_INPUT: + create_control_input(i); + break; + case SLV2_CONTROL_RATE_OUTPUT: + create_control_output(i); + break; + case SLV2_AUDIO_RATE_INPUT: + create_audio_input(i); + break; + case SLV2_AUDIO_RATE_OUTPUT: + create_audio_output(i); + break; + default: + printf("Unknown port type, ignored.\n"); + } + //printf("Port %ld class: %d\n", i, slv2_port_get_class(p, i)); + + + } + + SLV2Property prop; + for (unsigned long i=0; i < num_ports; ++i) { + const char* property = "a"; + prop = slv2_port_get_property(p, i, property); + if (prop) + printf("Port %ld %s = %s\n", i, property, prop->values[0]); + else + printf("No port %ld %s.\n", i, property); + free(prop); + } + printf("\n"); + + SLV2Instance* i = slv2_plugin_instantiate(p, 48000, NULL); + if (i) { + printf("Succesfully instantiated plugin.\n"); + + float gain = 2.0f; + float input = 0.25f; + float output = 0.0f; + slv2_instance_connect_port(i, 0, &gain); + slv2_instance_connect_port(i, 1, &input); + slv2_instance_connect_port(i, 2, &output); + + slv2_instance_activate(i); + slv2_instance_run(i, 1); + slv2_instance_deactivate(i); + + printf("Gain: %f, Input: %f => Output: %f\n", gain, input, output); + slv2_instance_free(i); + } + } + + slv2_list_free(plugins); + +#if 0 + /* Display all plugins found in path */ + if (plugins) + printf("Plugins found: %ld\n", slv2_list_get_size(plugins)); + else + printf("No plugins found in %s\n", path); + + for (unsigned long i=0; 1; ++i) { + const SLV2Plugin* p = + slv2_list_get_plugin_by_index(plugins, i); + + if (!p) + break; + else + printf("\t%s\n", slv2_plugin_get_uri(p)); + } +#endif + +#if 0 + const uchar* bundle_url = (const uchar*)"file:/home/dave/code/ladspa2/ladspa2_sdk/examples/plugins/Amp-swh.ladspa2/"; + LV2Bundle* b = slv2_bundle_load(bundle_url); + + if (b != NULL) { + printf("Loaded bundle %s\n", slv2_bundle_get_url(b)); + + for (unsigned long i=0; i < slv2_bundle_get_num_plugins(b); ++i) { + const SLV2Plugin* p = slv2_bundle_get_plugin_by_index(b, i); + //printf("Plugin: %s\n", p->plugin_uri); + //printf("Lib: %s\n", p->lib_url); + //printf("Data: %s\n", p->data_url); + + printf("\n"); + const uchar* property = (uchar*)"doap:name"; + printf("%s\t%s\n", slv2_plugin_get_uri(p), property); + struct SLV2Property* result = slv2_plugin_get_property(p, property); + + if (result) { + for (int i=0; i < result->num_values; ++i) + printf("\t%s\n", result->values[i]); + } else { + printf("No results.\n"); + } + printf("\n"); + + /* Instantiate plugin */ + SLV2PluginInstance* instance = slv2_plugin_instantiate( + p, 48000, NULL); + if (instance != NULL) { + printf("Successfully instantiated %s\n", slv2_plugin_get_uri(p)); + slv2_plugin_instance_free(instance); + } + + } + + } else { + printf("Failed to load bundle %s\n", bundle_url); + } +#endif + + return 0; +} + diff --git a/examples/plugins/Amp-swh.lv2/Makefile b/examples/plugins/Amp-swh.lv2/Makefile new file mode 100644 index 0000000..4fb858a --- /dev/null +++ b/examples/plugins/Amp-swh.lv2/Makefile @@ -0,0 +1,10 @@ +CFLAGS = -Wall -I../../../include + +all: amp.so + +amp.so: amp.o + $(LD) amp.o -o amp.so -shared + rm amp.o + +clean: + rm *.o amp.so diff --git a/examples/plugins/Amp-swh.lv2/amp.c b/examples/plugins/Amp-swh.lv2/amp.c new file mode 100644 index 0000000..a30c4bd --- /dev/null +++ b/examples/plugins/Amp-swh.lv2/amp.c @@ -0,0 +1,96 @@ +#include +#include + +#include + +#include "lv2.h" + +#ifdef WIN32 +#define SYMBOL_EXPORT __declspec(dllexport) +#else +#define SYMBOL_EXPORT +#endif + +#define AMP_URI "http://plugin.org.uk/swh-plugins/amp"; +#define AMP_GAIN 0 +#define AMP_INPUT 1 +#define AMP_OUTPUT 2 + +static LV2_Descriptor *ampDescriptor = NULL; + +typedef struct { + float *gain; + float *input; + float *output; +} Amp; + +static void cleanupAmp(LV2_Handle instance) { + free(instance); +} + +static void connectPortAmp(LV2_Handle instance, unsigned long port, + void *data) { + Amp *plugin = (Amp *)instance; + + switch (port) { + case AMP_GAIN: + plugin->gain = data; + break; + case AMP_INPUT: + plugin->input = data; + break; + case AMP_OUTPUT: + plugin->output = data; + break; + } +} + +static LV2_Handle instantiateAmp(const LV2_Descriptor *descriptor, + unsigned long s_rate, const char *path , const LV2_Host_Feature **features) { + Amp *plugin_data = (Amp *)malloc(sizeof(Amp)); + + return (LV2_Handle)plugin_data; +} + +#define DB_CO(g) ((g) > -90.0f ? powf(10.0f, (g) * 0.05f) : 0.0f) + +static void runAmp(LV2_Handle instance, unsigned long sample_count) { + Amp *plugin_data = (Amp *)instance; + + const float gain = *(plugin_data->gain); + const float * const input = plugin_data->input; + float * const output = plugin_data->output; + + unsigned long pos; + float coef = DB_CO(gain); + + for (pos = 0; pos < sample_count; pos++) { + output[pos] = input[pos] * coef; + } +} + +static void init() { + ampDescriptor = + (LV2_Descriptor *)malloc(sizeof(LV2_Descriptor)); + + ampDescriptor->URI = AMP_URI; + ampDescriptor->activate = NULL; + ampDescriptor->cleanup = cleanupAmp; + ampDescriptor->connect_port = connectPortAmp; + ampDescriptor->deactivate = NULL; + ampDescriptor->instantiate = instantiateAmp; + ampDescriptor->run = runAmp; +} + +SYMBOL_EXPORT +const LV2_Descriptor *lv2_descriptor(unsigned long index) { + if (!ampDescriptor) init(); + + switch (index) { + case 0: + return ampDescriptor; + default: + return NULL; + } +} + diff --git a/examples/plugins/Amp-swh.lv2/amp.so b/examples/plugins/Amp-swh.lv2/amp.so new file mode 100755 index 0000000..281b709 Binary files /dev/null and b/examples/plugins/Amp-swh.lv2/amp.so differ diff --git a/examples/plugins/Amp-swh.lv2/amp.ttl b/examples/plugins/Amp-swh.lv2/amp.ttl new file mode 100644 index 0000000..d44c0b9 --- /dev/null +++ b/examples/plugins/Amp-swh.lv2/amp.ttl @@ -0,0 +1,68 @@ +@prefix lv2: . +@prefix foaf: . +@prefix doap: . + + a lv2:Plugin ; + a lv2:AmplifierPlugin ; + doap:maintainer [ + foaf:name "Steve Harris"; + foaf:homepage ; + foaf:mbox ; + ] ; + doap:name "Simple amplifier" ; + doap:name "简单放大器"@ch ; + doap:name "Einfacher Verstärker"@de ; + doap:name "Simple amp"@en-gb ; + doap:name "Amplificador simple"@es ; + doap:name "Amplificateur de base"@fr ; + doap:name "Amplificatore semplice"@it ; + doap:name "簡単なアンプ"@jp ; + doap:name "Просто усилитель"@ru ; + doap:licence ; + lv2:property lv2:hardRtCapable ; + + lv2:port [ + a lv2:InputControlRatePort ; + lv2:datatype lv2:float ; + lv2:index 0 ; + lv2:symbol "gain" ; + lv2:name "gain" ; + lv2:name "收益"@ch ; + lv2:name "gewinn"@de ; + lv2:name "gain"@en-gb ; + lv2:name "aumento"@es ; + lv2:name "gain"@fr ; + lv2:name "guadagno"@it ; + lv2:name "利益"@jp ; + lv2:name "увеличение"@ru ; + lv2:default 0.0 ; + lv2:minimum -90.0 ; + lv2:maximum 24.0 ; + lv2:scalePoint [ + lv2:label "+5" ; + lv2:value 5.0 ; + ] , [ + lv2:label "0" ; + lv2:value 0.0 ; + ] , [ + lv2:label "-5" ; + lv2:value -5.0 ; + ] , [ + lv2:label "-10" ; + lv2:value -10.0 ; + ] + ] , [ + a lv2:InputAudioRatePort ; + lv2:datatype lv2:float ; + lv2:index 1 ; + lv2:symbol "in" ; + lv2:name "in" ; + ] , [ + a lv2:OutputAudioRatePort ; + lv2:datatype lv2:float ; + lv2:index 2 ; + lv2:symbol "out" ; + lv2:name "out" ; + ] +. + diff --git a/examples/plugins/Amp-swh.lv2/manifest.ttl b/examples/plugins/Amp-swh.lv2/manifest.ttl new file mode 100644 index 0000000..a26f506 --- /dev/null +++ b/examples/plugins/Amp-swh.lv2/manifest.ttl @@ -0,0 +1,9 @@ +# LV2 Plugin Manifest +# Lists where plugins' data files and shared objects reside. + +@prefix lv2: . +@prefix rdfs: . + + lv2:binary ; + rdfs:seeAlso . + diff --git a/examples/plugins/Makefile.am b/examples/plugins/Makefile.am new file mode 100644 index 0000000..8044bc9 --- /dev/null +++ b/examples/plugins/Makefile.am @@ -0,0 +1,24 @@ +#AM_CFLAGS = -I$(top_srcdir)/include +#AM_LDFLAGS = -module -avoidversion -Wc,-nostartfiles +# +#plugindir = @umpdir@ +# +#plugin_LTLIBRARIES = \ +# test_plugin.la +# +## Stolen from swh-plugins, makes stupid libtool versions go away +#install-pluginLTLIBRARIES: $(plugin_LTLIBRARIES) +# mkdir -p @umpdir@ +# list='$(plugin_LTLIBRARIES)'; \ +# for file in $$list; do \ +# sofile=`basename $$file .la`.so; \ +# $(INSTALL_PROGRAM) .libs/$$sofile @umpdir@; \ +# done +# +#uninstall-pluginLTLIBRARIES: $(plugin_LTLIBRARIES) +# list='$(plugin_LTLIBRARIES)'; \ +# for file in $$list; do \ +# sofile=`basename $$file .la`.so; \ +# rm -f @umpdir@/$$sofile; \ +# done +# -- cgit v1.2.1