diff options
Diffstat (limited to 'plugins/eg-sampler.lv2')
-rw-r--r-- | plugins/eg-sampler.lv2/manifest.ttl | 7 | ||||
-rw-r--r-- | plugins/eg-sampler.lv2/monosample.wav | bin | 0 -> 72798 bytes | |||
-rw-r--r-- | plugins/eg-sampler.lv2/sampler.c | 354 | ||||
-rw-r--r-- | plugins/eg-sampler.lv2/sampler.ttl | 43 | ||||
l--------- | plugins/eg-sampler.lv2/waf | 1 | ||||
-rw-r--r-- | plugins/eg-sampler.lv2/wscript | 67 |
6 files changed, 472 insertions, 0 deletions
diff --git a/plugins/eg-sampler.lv2/manifest.ttl b/plugins/eg-sampler.lv2/manifest.ttl new file mode 100644 index 0000000..f6ab02d --- /dev/null +++ b/plugins/eg-sampler.lv2/manifest.ttl @@ -0,0 +1,7 @@ +@prefix lv2: <http://lv2plug.in/ns/lv2core#> . +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . + +<http://lv2plug.in/plugins/eg-sampler> + a lv2:Plugin ; + lv2:binary <sampler.so> ; + rdfs:seeAlso <sampler.ttl> . diff --git a/plugins/eg-sampler.lv2/monosample.wav b/plugins/eg-sampler.lv2/monosample.wav Binary files differnew file mode 100644 index 0000000..df3b478 --- /dev/null +++ b/plugins/eg-sampler.lv2/monosample.wav diff --git a/plugins/eg-sampler.lv2/sampler.c b/plugins/eg-sampler.lv2/sampler.c new file mode 100644 index 0000000..173f844 --- /dev/null +++ b/plugins/eg-sampler.lv2/sampler.c @@ -0,0 +1,354 @@ +/* + LV2 Sampler Example Plugin + Copyright 2011 Gabriel M. Beddingfield <gabriel@teuton.org>, + James Morris <jwm.art.net@gmail.com>, + David Robillard <d@drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file sampler.c Sampler Plugin + + A simple example of an LV2 sampler that dynamically loads samples (based on + incoming events) and also triggers their playback (based on incoming MIDI + note events). The sample must be monophonic. + + So that the runSampler() method stays real-time safe, the plugin creates a + worker thread (worker_thread_main) that listens for file loading events. It + loads everything in plugin->pending_samp and then signals the runSampler() + that it's time to install it. runSampler() just has to swap pointers... so + the change happens very fast and atomically. +*/ + +#include <assert.h> +#include <math.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include <pthread.h> + +#include <sndfile.h> + +#include "lv2/lv2plug.in/ns/lv2core/lv2.h" +#include "lv2/lv2plug.in/ns/ext/event/event-helpers.h" +#include "lv2/lv2plug.in/ns/ext/uri-map/uri-map.h" + +#define SAMPLER_URI "http://lv2plug.in/plugins/eg-sampler" +#define MIDI_EVENT_URI "http://lv2plug.in/ns/ext/midi#MidiEvent" +#define STRING_BUF 8192 + +enum { + SAMPLER_CONTROL = 0, + SAMPLER_OUT = 1 +}; + +static const char* default_sample_file = "monosample.wav"; + +typedef struct { + char filepath[STRING_BUF]; + SF_INFO info; + float* data; +} SampleFile; + +typedef struct { + /* Sample */ + SampleFile* samp; + SampleFile* pending_samp; + pthread_mutex_t pending_samp_mutex; /**< Protects pending_samp */ + pthread_cond_t pending_samp_cond; /**< Signaling mechanism */ + int pending_sample_ready; + + /* Ports */ + float* outputPort; + LV2_Event_Buffer* eventPort; + LV2_Event_Feature* event_ref; + int midi_event_id; + + /* Playback state */ + bool play; + sf_count_t frame; + + /* File loading */ + pthread_t worker_thread; +} Sampler; + +static void +handle_load_sample(Sampler* plugin) +{ + plugin->pending_sample_ready = 0; + + SF_INFO* const info = &plugin->pending_samp->info; + SNDFILE* const sample = sf_open(plugin->pending_samp->filepath, + SFM_READ, + info); + + if (!sample + || !info->frames + || (info->channels != 1)) { + fprintf(stderr, "failed to open sample '%s'.\n", + plugin->pending_samp->filepath); + return; + } + + /* Read data */ + float* const data = malloc(sizeof(float) * info->frames); + plugin->pending_samp->data = data; + + if (!data) { + fprintf(stderr, "failed to allocate memory for sample.\n"); + return; + } + + sf_seek(sample, 0ul, SEEK_SET); + sf_read_float(sample, + data, + info->frames); + sf_close(sample); + + /* Queue the sample for installation on next run() */ + plugin->pending_sample_ready = 1; +} + +void* +worker_thread_main(void* arg) +{ + Sampler* plugin; + + plugin = (Sampler*)arg; + + pthread_mutex_lock(&plugin->pending_samp_mutex); + while (true) { + /* Wait for run() to signal that we need to load a sample */ + pthread_cond_wait(&plugin->pending_samp_cond, + &plugin->pending_samp_mutex); + + /* Then load it */ + handle_load_sample(plugin); + } + pthread_mutex_unlock(&plugin->pending_samp_mutex); + + return 0; +} + +static void +cleanup(LV2_Handle instance) +{ + Sampler* plugin = (Sampler*)instance; + pthread_cancel(plugin->worker_thread); + pthread_join(plugin->worker_thread, 0); + + free(plugin->samp->data); + free(plugin->pending_samp->data); + free(plugin->samp); + free(plugin->pending_samp); + free(instance); +} + +static void +connect_port(LV2_Handle instance, + uint32_t port, + void* data) +{ + Sampler* plugin = (Sampler*)instance; + + switch (port) { + case SAMPLER_CONTROL: + plugin->eventPort = (LV2_Event_Buffer*)data; + break; + case SAMPLER_OUT: + plugin->outputPort = (float*)data; + break; + default: + break; + } +} + +static LV2_Handle +instantiate(const LV2_Descriptor* descriptor, + double rate, + const char* path, + const LV2_Feature* const* features) +{ + Sampler* plugin = (Sampler*)malloc(sizeof(Sampler)); + assert(plugin); + memset(plugin, 0, sizeof(Sampler)); + + plugin->samp = (SampleFile*)malloc(sizeof(SampleFile)); + assert(plugin->samp); + memset(plugin->samp, 0, sizeof(SampleFile)); + plugin->pending_samp = (SampleFile*)malloc(sizeof(SampleFile)); + assert(plugin->pending_samp); + memset(plugin->pending_samp, 0, sizeof(SampleFile)); + + plugin->midi_event_id = -1; + plugin->event_ref = 0; + + /* Initialise mutexes and conditions for the worker thread */ + if (pthread_mutex_init(&plugin->pending_samp_mutex, 0)) { + fprintf(stderr, "Could not initialize next_sample_mutex.\n"); + goto fail; + } + if (pthread_cond_init(&plugin->pending_samp_cond, 0)) { + fprintf(stderr, "Could not initialize next_sample_waitcond.\n"); + goto fail; + } + if (pthread_create(&plugin->worker_thread, 0, worker_thread_main, plugin)) { + fprintf(stderr, "Could not initialize worker thread.\n"); + goto fail; + } + + /* Scan host features for event and uri-map */ + for (int i = 0; features[i]; ++i) { + if (strcmp(features[i]->URI, LV2_URI_MAP_URI) == 0) { + LV2_URI_Map_Feature* + map_feature = (LV2_URI_Map_Feature*)features[i]->data; + + plugin->midi_event_id + = map_feature->uri_to_id(map_feature->callback_data, + LV2_EVENT_URI, MIDI_EVENT_URI); + } else if (strcmp(features[i]->URI, LV2_EVENT_URI) == 0) { + plugin->event_ref = (LV2_Event_Feature*)features[i]->data; + } + } + + if (plugin->midi_event_id == -1) { + /* Host does not support uri-map extension */ + fprintf(stderr, "Host does not support uri-map extension.\n"); + goto fail; + } + + /* Open the default sample file */ + strncpy(plugin->pending_samp->filepath, path, STRING_BUF); + strncat(plugin->pending_samp->filepath, + default_sample_file, + STRING_BUF - strlen(plugin->pending_samp->filepath) ); + handle_load_sample(plugin); + + return (LV2_Handle)plugin; + +fail: + free(plugin); + return 0; +} + +static void +run(LV2_Handle instance, + uint32_t sample_count) +{ + Sampler* plugin = (Sampler*)instance; + LV2_Event* ev = NULL; + + sf_count_t start_frame = 0; + sf_count_t pos = 0; + float* output = plugin->outputPort; + + /* Read incoming events */ + LV2_Event_Iterator iterator; + for (lv2_event_begin(&iterator, plugin->eventPort); + lv2_event_is_valid(&iterator); + lv2_event_increment(&iterator)) { + + ev = lv2_event_get(&iterator, NULL); + + if (ev->type == 0) { + if (plugin->event_ref) { + plugin->event_ref->lv2_event_unref( + plugin->event_ref->callback_data, ev); + } + } else if (ev->type == plugin->midi_event_id) { + uint8_t* const data = (uint8_t* const)(ev + 1); + + if ((data[0] & 0xF0) == 0x90) { + start_frame = ev->frames; + plugin->frame = 0; + plugin->play = true; + } + } + /*************************************************** + * XXX TODO: * + * ADD CODE HERE TO DETECT AN INCOMING MESSAGE TO * + * DYNAMICALLY LOAD A SAMPLE * + *************************************************** + */ + else if (0) { + /* message to load a sample comes in */ + /* write filename to plugin->pending_samp->filepath */ + /* strncpy(plugin->pending_samp->filepath, some_src_string, STRING_BUF); */ + pthread_cond_signal(&plugin->pending_samp_cond); + } + } + + /* Render the sample (possibly already in progress) */ + if (plugin->play) { + uint32_t f = plugin->frame; + const uint32_t lf = plugin->samp->info.frames; + + for (pos = 0; pos < start_frame; ++pos) { + output[pos] = 0; + } + + for (; pos < sample_count && f < lf; ++pos, ++f) { + output[pos] = plugin->samp->data[f]; + } + + plugin->frame = f; + + if (f == lf) { + plugin->play = false; + } + } + + /* Check if we have a sample pending */ + if (!plugin->play + && plugin->pending_sample_ready + && pthread_mutex_trylock(&plugin->pending_samp_mutex)) { + /* Install the new sample */ + SampleFile* tmp; + tmp = plugin->samp; + plugin->samp = plugin->pending_samp; + plugin->pending_samp = tmp; + plugin->pending_sample_ready = 0; + free(plugin->pending_samp->data); + + pthread_mutex_unlock(&plugin->pending_samp_mutex); + } + + /* Add zeros to end if sample not long enough (or not playing) */ + for (; pos < sample_count; ++pos) { + output[pos] = 0; + } +} + +static const LV2_Descriptor descriptor = { + SAMPLER_URI, + instantiate, + connect_port, + NULL, // activate, + run, + NULL, // deactivate, + cleanup, + NULL, // extension_data +}; + +LV2_SYMBOL_EXPORT +const LV2_Descriptor* lv2_descriptor(uint32_t index) +{ + switch (index) { + case 0: + return &descriptor; + default: + return NULL; + } +} diff --git a/plugins/eg-sampler.lv2/sampler.ttl b/plugins/eg-sampler.lv2/sampler.ttl new file mode 100644 index 0000000..1926652 --- /dev/null +++ b/plugins/eg-sampler.lv2/sampler.ttl @@ -0,0 +1,43 @@ +# LV2 Sampler Example Plugin +# Copyright 2011 Gabriel M. Beddingfield <gabriel@teuton.org>, +# James Morris <jwm.art.net@gmail.com>, +# David Robillard <d@drobilla.net> +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +@prefix doap: <http://usefulinc.com/ns/doap#> . +@prefix foaf: <http://xmlns.com/foaf/0.1/> . +@prefix lv2: <http://lv2plug.in/ns/lv2core#> . +@prefix lv2ev: <http://lv2plug.in/ns/ext/event#> . + +<http://lv2plug.in/plugins/eg-sampler> + a lv2:Plugin ; + doap:name "Simple Sampler" ; + doap:license <http://opensource.org/licenses/isc-license> ; + lv2:requiredFeature <http://lv2plug.in/ns/ext/uri-map> ; + lv2:optionalFeature lv2:hardRtCapable , + <http://lv2plug.in/ns/ext/event> ; + lv2:port [ + a lv2:InputPort , + lv2ev:EventPort ; + lv2ev:supportsEvent <http://lv2plug.in/ns/ext/midi#MidiEvent> ; + lv2:index 0 ; + lv2:symbol "control" ; + lv2:name "Control" + ] , [ + a lv2:AudioPort , + lv2:OutputPort ; + lv2:index 1 ; + lv2:symbol "out" ; + lv2:name "Out" + ] . diff --git a/plugins/eg-sampler.lv2/waf b/plugins/eg-sampler.lv2/waf new file mode 120000 index 0000000..59a1ac9 --- /dev/null +++ b/plugins/eg-sampler.lv2/waf @@ -0,0 +1 @@ +../../waf
\ No newline at end of file diff --git a/plugins/eg-sampler.lv2/wscript b/plugins/eg-sampler.lv2/wscript new file mode 100644 index 0000000..2957ce1 --- /dev/null +++ b/plugins/eg-sampler.lv2/wscript @@ -0,0 +1,67 @@ +#!/usr/bin/env python +import os +import shutil +from waflib import Logs +from waflib.extras import autowaf as autowaf + +# Variables for 'waf dist' +APPNAME = 'eg-sampler.lv2' +VERSION = '1.0.0' + +# Mandatory variables +top = '.' +out = 'build' + +def options(opt): + autowaf.set_options(opt) + opt.load('compiler_c') + +def configure(conf): + autowaf.configure(conf) + + conf.line_just = 51 + autowaf.display_header('Sampler Configuration') + conf.load('compiler_c') + + autowaf.check_header(conf, 'c', 'lv2/lv2plug.in/ns/lv2core/lv2.h') + autowaf.check_pkg(conf, 'sndfile', uselib_store='SNDFILE', + atleast_version='1.0.0', mandatory=True) + + conf.env.append_value('CFLAGS', '-std=c99') + + # Set env['pluginlib_PATTERN'] + pat = conf.env['cshlib_PATTERN'] + if pat.startswith('lib'): + pat = pat[3:] + conf.env['pluginlib_PATTERN'] = pat + conf.env['pluginlib_EXT'] = pat[pat.rfind('.'):] + + autowaf.display_msg(conf, "LV2 bundle directory", + conf.env['LV2DIR']) + print('') + +def build(bld): + bundle = 'eg-sampler.lv2' + + # Copy data files to build bundle (build/eg-sampler.lv2) + for i in [ 'sampler.ttl', 'manifest.ttl', 'monosample.wav' ]: + bld(rule = 'cp ${SRC} ${TGT}', + source = i, + target = bld.path.get_bld().make_node('%s/%s' % (bundle, i)), + install_path = '${LV2DIR}/%s' % bundle) + + # Create a build environment that builds module-style library names + # e.g. eg-sampler.so instead of libeg-sampler.so + # Note for C++ you must set cxxshlib_PATTERN instead + penv = bld.env.derive() + penv['cshlib_PATTERN'] = bld.env['pluginlib_PATTERN'] + + # Build plugin library + obj = bld(features = 'c cshlib', + env = penv, + source = 'sampler.c', + name = 'sampler', + target = '%s/sampler' % bundle, + install_path = '${LV2DIR}/%s' % bundle, + use = 'SNDFILE') + |