diff options
Diffstat (limited to 'src/tracker.c')
-rw-r--r-- | src/tracker.c | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/src/tracker.c b/src/tracker.c new file mode 100644 index 0000000..4460a34 --- /dev/null +++ b/src/tracker.c @@ -0,0 +1,229 @@ +/* + An LV2 plugin to shape a signal in various ways. + Copyright 2011 David Robillard + Copyright 2003 Mike Rawes + + This 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 3 of the License, or + (at your option) any later version. + + This software 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 software. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdlib.h> +#include "lv2/lv2plug.in/ns/lv2core/lv2.h" +#include "common.h" + +#define TRACKER_GATE 0 +#define TRACKER_HATTACK 1 +#define TRACKER_HDECAY 2 +#define TRACKER_LATTACK 3 +#define TRACKER_LDECAY 4 +#define TRACKER_INPUT 5 +#define TRACKER_OUTPUT 6 + +typedef struct { + float* gate; + float* hattack; + float* hdecay; + float* lattack; + float* ldecay; + float* input; + float* output; + float coeff; + float last_value; +} Tracker; + +static void +cleanup(LV2_Handle instance) +{ + free(instance); +} + +static void +connect_port(LV2_Handle instance, + uint32_t port, + void* data) +{ + Tracker* plugin = (Tracker*)instance; + + switch (port) { + case TRACKER_GATE: + plugin->gate = data; + break; + case TRACKER_HATTACK: + plugin->hattack = data; + break; + case TRACKER_HDECAY: + plugin->hdecay = data; + break; + case TRACKER_LATTACK: + plugin->lattack = data; + break; + case TRACKER_LDECAY: + plugin->ldecay = data; + break; + case TRACKER_INPUT: + plugin->input = data; + break; + case TRACKER_OUTPUT: + plugin->output = data; + break; + } +} + +static LV2_Handle +instantiate(const LV2_Descriptor* descriptor, + double sample_rate, + const char* bundle_path, + const LV2_Feature* const* features) +{ + Tracker* plugin = (Tracker*)malloc(sizeof(Tracker)); + + plugin->coeff = 2.0f * M_PI / (float)sample_rate; + + return (LV2_Handle)plugin; +} + +static void +activate(LV2_Handle instance) +{ + Tracker* plugin = (Tracker*)instance; + + plugin->last_value = 0.0f; +} + +static void +runTracker_gaaadaia_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Tracker* plugin = (Tracker*)instance; + + /* Gate (array of floats of length sample_count) */ + float* gate = plugin->gate; + + /* Gate High Attack Rate (array of floats of length sample_count) */ + float* hattack = plugin->hattack; + + /* Gate High Decay Rate (array of floats of length sample_count) */ + float* hdecay = plugin->hdecay; + + /* Gate Low Attack Rate (array of floats of length sample_count) */ + float* lattack = plugin->lattack; + + /* Gate Low Decay Rate (array of floats of length sample_count) */ + float* ldecay = plugin->ldecay; + + /* Input (array of floats of length sample_count) */ + float* input = plugin->input; + + /* Output (array of floats of length sample_count) */ + float* output = plugin->output; + + /* Instance Data */ + float coeff = plugin->coeff; + float last_value = plugin->last_value; + + float rate; + float in; + + for (uint32_t s = 0; s < sample_count; ++s) { + in = input[s]; + + if (gate[s] > 0.0f) { + rate = in > last_value ? hattack[s] : hdecay[s]; + } else { + rate = in > last_value ? lattack[s] : ldecay[s]; + } + + rate = f_min(1.0f, rate * coeff); + last_value = last_value * (1.0f - rate) + in * rate; + + output[s] = last_value; + } + + plugin->last_value = last_value; +} + +static void +runTracker_gaacdcia_oa(LV2_Handle instance, + uint32_t sample_count) +{ + Tracker* plugin = (Tracker*)instance; + + /* Gate (array of floats of length sample_count) */ + float* gate = plugin->gate; + + /* Gate High Attack Rate (float value) */ + float hattack = *(plugin->hattack); + + /* Gate High Decay Rate (float value) */ + float hdecay = *(plugin->hdecay); + + /* Gate Low Attack Rate (float value) */ + float lattack = *(plugin->lattack); + + /* Gate Low Decay Rate (float value) */ + float ldecay = *(plugin->ldecay); + + /* Input (array of floats of length sample_count) */ + float* input = plugin->input; + + /* Output (array of floats of length sample_count) */ + float* output = plugin->output; + + /* Instance Data */ + float coeff = plugin->coeff; + float last_value = plugin->last_value; + + float in; + float rate; + + hattack = f_min(1.0f, hattack * coeff); + hdecay = f_min(1.0f, hdecay * coeff); + lattack = f_min(1.0f, lattack * coeff); + ldecay = f_min(1.0f, ldecay * coeff); + + for (uint32_t s = 0; s < sample_count; ++s) { + in = input[s]; + + if (gate[s] > 0.0f) { + rate = in > last_value ? hattack : hdecay; + } else { + rate = in > last_value ? lattack : ldecay; + } + + last_value = last_value * (1.0f - rate) + in * rate; + + output[s] = last_value; + } + + plugin->last_value = last_value; +} + +static const LV2_Descriptor descriptor = { + "http://drobilla.net/plugins/blip/tracker", + instantiate, + connect_port, + activate, + runTracker_gaacdcia_oa, + NULL, + cleanup, + NULL, +}; + +LV2_SYMBOL_EXPORT const LV2_Descriptor* +lv2_descriptor(uint32_t index) +{ + switch (index) { + case 0: return &descriptor; + default: return NULL; + } +} |