diff options
Diffstat (limited to 'src/sync_square.c')
-rw-r--r-- | src/sync_square.c | 108 |
1 files changed, 52 insertions, 56 deletions
diff --git a/src/sync_square.c b/src/sync_square.c index 51ceac5..e954a46 100644 --- a/src/sync_square.c +++ b/src/sync_square.c @@ -19,19 +19,23 @@ */ #include <stdlib.h> +#include "lv2/lv2plug.in/ns/ext/morph/morph.h" #include "lv2/lv2plug.in/ns/lv2core/lv2.h" +#include "uris.h" #define SYNCSQUARE_FREQUENCY 0 #define SYNCSQUARE_GATE 1 #define SYNCSQUARE_OUTPUT 2 typedef struct { - float* frequency; - float* gate; - float* output; - float srate; - float nyquist; - float phase; + float* frequency; + float* gate; + float* output; + float srate; + float nyquist; + float phase; + uint32_t frequency_is_cv; + URIs uris; } SyncSquare; static void @@ -60,6 +64,31 @@ connect_port(LV2_Handle instance, } } +static LV2_Morph_Status +morph_port(LV2_Handle instance, + uint32_t port, + LV2_URID type, + const LV2_Morph_Property*const* properties) +{ + SyncSquare* plugin = (SyncSquare*)instance; + + switch (port) { + case SYNCSQUARE_FREQUENCY: + if (type == plugin->uris.lv2_ControlPort) { + plugin->frequency_is_cv = 0; + } else if (type == plugin->uris.lv2_CVPort) { + plugin->frequency_is_cv = 1; + } else { + return LV2_MORPH_ERR_BAD_TYPE; + } + return LV2_MORPH_SUCCESS; + default: + return LV2_MORPH_ERR_BAD_PORT; + } + + return LV2_MORPH_SUCCESS; +} + static LV2_Handle instantiate(const LV2_Descriptor* descriptor, double sample_rate, @@ -68,8 +97,10 @@ instantiate(const LV2_Descriptor* descriptor, { SyncSquare* plugin = (SyncSquare*)malloc(sizeof(SyncSquare)); - plugin->srate = (float)sample_rate; - plugin->nyquist = (float)(sample_rate / 2); + plugin->srate = (float)sample_rate; + plugin->nyquist = (float)(sample_rate / 2.0f); + plugin->frequency_is_cv = 0; + map_uris(&plugin->uris, features); return (LV2_Handle)plugin; } @@ -83,12 +114,12 @@ activate(LV2_Handle instance) } static void -runSyncSquare_faga_oa(LV2_Handle instance, - uint32_t sample_count) +run(LV2_Handle instance, + uint32_t sample_count) { SyncSquare* plugin = (SyncSquare*)instance; - /* Frequency (array of float of length sample_count) */ + /* Frequency (array of float of length 1 or sample_count) */ const float* frequency = plugin->frequency; /* Gate (array of float of length sample_count) */ @@ -102,11 +133,9 @@ runSyncSquare_faga_oa(LV2_Handle instance, float srate = plugin->srate; float nyquist = plugin->nyquist; - float freq; - for (uint32_t s = 0; s < sample_count; ++s) { if (gate[s] > 0.0f) { - freq = frequency[s]; + const float freq = frequency[s * plugin->frequency_is_cv]; if (phase < nyquist) { output[s] = 1.0f; @@ -129,58 +158,25 @@ runSyncSquare_faga_oa(LV2_Handle instance, plugin->phase = phase; } -static void -runSyncSquare_fcga_oa(LV2_Handle instance, - uint32_t sample_count) +static const void* +extension_data(const char* uri) { - SyncSquare* plugin = (SyncSquare*)instance; - - /* Frequency (float value) */ - const float frequency = *(plugin->frequency); - - /* Gate (array of float of length sample_count) */ - const float* gate = plugin->gate; - - /* Output (pointer to float value) */ - float* output = plugin->output; - - /* Instance Data */ - float phase = plugin->phase; - float srate = plugin->srate; - float nyquist = plugin->nyquist; - - for (uint32_t s = 0; s < sample_count; ++s) { - if (gate[s] > 0.0f) { - if (phase < nyquist) { - output[s] = 1.0f; - } else { - output[s] = -1.0f; - } - - phase += frequency; - if (phase < 0.0f) { - phase += srate; - } else if (phase > srate) { - phase -= srate; - } - } else { - output[s] = 0.0f; - phase = 0.0f; - } + static const LV2_Morph_Interface morph = { morph_port, NULL }; + if (!strcmp(uri, LV2_MORPH__interface)) { + return &morph; } - - plugin->phase = phase; + return NULL; } static const LV2_Descriptor descriptor = { - "http://drobilla.net/plugins/blip/sync_square", + "http://drobilla.net/plugins/blop/sync_square", instantiate, connect_port, activate, - runSyncSquare_fcga_oa, + run, NULL, cleanup, - NULL, + extension_data, }; LV2_SYMBOL_EXPORT const LV2_Descriptor* |