From 7b20413c84b14d2c2bc1037bb08134dcdf152ddb Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 31 May 2012 06:23:57 +0000 Subject: Umm... commit pretty much all the work of the past few days. Again. git-svn-id: http://svn.drobilla.net/lad/trunk/plugins/blop.lv2@4488 a436a847-0d15-0410-975c-d299462d15a1 --- src/random.c | 253 +++++++++++++++-------------------------------------------- 1 file changed, 65 insertions(+), 188 deletions(-) (limited to 'src/random.c') diff --git a/src/random.c b/src/random.c index 4a6f5b8..1671854 100644 --- a/src/random.c +++ b/src/random.c @@ -18,24 +18,29 @@ */ #include +#include "lv2/lv2plug.in/ns/ext/morph/morph.h" #include "lv2/lv2plug.in/ns/lv2core/lv2.h" #include #include "math_func.h" #include "common.h" +#include "uris.h" #define RANDOM_FREQUENCY 0 #define RANDOM_SMOOTH 1 #define RANDOM_OUTPUT 2 typedef struct { - float* frequency; - float* smooth; - float* output; - float nyquist; - float inv_nyquist; - float phase; - float value1; - float value2; + float* frequency; + float* smooth; + float* output; + float nyquist; + float inv_nyquist; + float phase; + float value1; + float value2; + uint32_t frequency_is_cv; + uint32_t smooth_is_cv; + URIs uris; } Random; float inv_rand_max; @@ -66,6 +71,33 @@ 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) +{ + Random* plugin = (Random*)instance; + + if (type != plugin->uris.lv2_ControlPort && + type != plugin->uris.lv2_CVPort) { + return LV2_MORPH_ERR_BAD_TYPE; + } + + switch (port) { + case RANDOM_FREQUENCY: + plugin->frequency_is_cv = (type == plugin->uris.lv2_CVPort); + break; + case RANDOM_SMOOTH: + plugin->smooth_is_cv = (type == plugin->uris.lv2_CVPort); + break; + default: + return LV2_MORPH_ERR_BAD_PORT; + } + + return LV2_MORPH_SUCCESS; +} + static LV2_Handle instantiate(const LV2_Descriptor* descriptor, double sample_rate, @@ -73,6 +105,9 @@ instantiate(const LV2_Descriptor* descriptor, const LV2_Feature* const* features) { Random* plugin = (Random*)malloc(sizeof(Random)); + if (!plugin) { + return NULL; + } srand((int)time((time_t*)0)); @@ -84,6 +119,9 @@ instantiate(const LV2_Descriptor* descriptor, plugin->value1 = rand() * inv_rand_max - 1.0f; plugin->value2 = rand() * inv_rand_max - 1.0f; + plugin->frequency_is_cv = 0; + plugin->smooth_is_cv = 0; + return (LV2_Handle)plugin; } @@ -96,15 +134,15 @@ activate(LV2_Handle instance) } static void -runRandom_fasa_oa(LV2_Handle instance, - uint32_t sample_count) +run(LV2_Handle instance, + uint32_t sample_count) { Random* plugin = (Random*)instance; - /* Frequency (Hz) (array of floats of length sample_count) */ + /* Frequency (Hz) (array of floats of length 1 or sample_count) */ const float* frequency = plugin->frequency; - /* Wave smoothness (array of floats of length sample_count) */ + /* Wave smoothness (array of floats of length 1 or sample_count) */ const float* smooth = plugin->smooth; /* Output (array of floats of length sample_count) */ @@ -117,132 +155,16 @@ runRandom_fasa_oa(LV2_Handle instance, float value1 = plugin->value1; float value2 = plugin->value2; - float freq; - float smth; - float interval; float result; for (uint32_t s = 0; s < sample_count; ++s) { - freq = f_clip(frequency[s], 0.0f, nyquist); - - smth = f_clip(smooth[s], 0.0f, 1.0f); - interval = (1.0f - smth) * 0.5f; - - if (phase < interval) { - result = 1.0f; - } else if (phase > (1.0f - interval)) { - result = -1.0f; - } else if (interval > 0.0f) { - result = COSF((phase - interval) / smth * M_PI); - } else { - result = COSF(phase * M_PI); - } - - result *= (value2 - value1) * 0.5f; - result -= (value2 + value1) * 0.5f; - - output[s] = result; - - phase += freq * inv_nyquist; - if (phase > 1.0f) { - phase -= 1.0f; - value1 = value2; - value2 = (float)rand() * inv_rand_max - 1.0f; - } - } - - plugin->phase = phase; - plugin->value1 = value1; - plugin->value2 = value2; -} - -static void -runRandom_fasc_oa(LV2_Handle instance, - uint32_t sample_count) -{ - Random* plugin = (Random*)instance; - - /* Frequency (Hz) (array of floats of length sample_count) */ - const float* frequency = plugin->frequency; - - /* Wave smoothness (float value) */ - const float smooth = f_clip(*(plugin->smooth), 0.0f, 1.0f); - - /* Output (array of floats of length sample_count) */ - float* output = plugin->output; - - /* Instance data */ - float nyquist = plugin->nyquist; - float inv_nyquist = plugin->inv_nyquist; - float phase = plugin->phase; - float value1 = plugin->value1; - float value2 = plugin->value2; - - float freq; - float interval = (1.0f - smooth) * 0.5f; - float result; - - for (uint32_t s = 0; s < sample_count; ++s) { - freq = f_clip(frequency[s], 0.0f, nyquist); - - if (phase < interval) { - result = 1.0f; - } else if (phase > (1.0f - interval)) { - result = -1.0f; - } else if (interval > 0.0f) { - result = COSF((phase - interval) / smooth * M_PI); - } else { - result = COSF(phase * M_PI); - } - - result *= (value2 - value1) * 0.5f; - result -= (value2 + value1) * 0.5f; - - output[s] = result; - - phase += freq * inv_nyquist; - if (phase > 1.0f) { - phase -= 1.0f; - value1 = value2; - value2 = (float)rand() * inv_rand_max - 1.0f; - } - } - - plugin->phase = phase; - plugin->value1 = value1; - plugin->value2 = value2; -} + const float freq = f_clip(frequency[s * plugin->frequency_is_cv], + 0.0f, nyquist); -static void -runRandom_fcsa_oa(LV2_Handle instance, - uint32_t sample_count) -{ - Random* plugin = (Random*)instance; + const float smth = f_clip(smooth[s * plugin->smooth_is_cv], + 0.0f, 1.0f); - /* Frequency (Hz) (float value) */ - const float frequency = *(plugin->frequency); - - /* Wave smoothness (array of floats of length sample_count) */ - const float* smooth = plugin->smooth; - - /* Output (pointer to float value) */ - float* output = plugin->output; - - /* Instance data */ - float nyquist = plugin->nyquist; - float inv_nyquist = plugin->inv_nyquist; - float phase = plugin->phase; - float value1 = plugin->value1; - float value2 = plugin->value2; - - float phase_scale = f_clip(frequency, 0.0f, nyquist) * inv_nyquist; - float smth; - float interval; - float result; - - for (uint32_t s = 0; s < sample_count; ++s) { - smth = f_clip(smooth[s], 0.0f, 1.0f); - interval = (1.0f - smth) * 0.5f; + const float interval = (1.0f - smth) * 0.5f; if (phase < interval) { result = 1.0f; @@ -259,7 +181,7 @@ runRandom_fcsa_oa(LV2_Handle instance, output[s] = result; - phase += phase_scale; + phase += freq * inv_nyquist; if (phase > 1.0f) { phase -= 1.0f; value1 = value2; @@ -272,70 +194,25 @@ runRandom_fcsa_oa(LV2_Handle instance, plugin->value2 = value2; } -static void -runRandom_fcsc_oa(LV2_Handle instance, - uint32_t sample_count) +static const void* +extension_data(const char* uri) { - Random* plugin = (Random*)instance; - - /* Frequency (Hz) (float value) */ - const float frequency = *(plugin->frequency); - - /* Wave smoothness (float value) */ - const float smooth = f_clip(*(plugin->smooth), 0.0f, 1.0f); - - /* Output (array of floats of length sample_count) */ - float* output = plugin->output; - - /* Instance data */ - float nyquist = plugin->nyquist; - float inv_nyquist = plugin->inv_nyquist; - float phase = plugin->phase; - float value1 = plugin->value1; - float value2 = plugin->value2; - - float phase_scale = f_clip(frequency, 0.0f, nyquist) * inv_nyquist; - float interval = (1.0f - smooth) * 0.5f; - float result; - - for (uint32_t s = 0; s < sample_count; ++s) { - if (phase < interval) { - result = 1.0f; - } else if (phase > (1.0f - interval)) { - result = -1.0f; - } else if (interval > 0.0f) { - result = COSF((phase - interval) / smooth * M_PI); - } else { - result = COSF(phase * M_PI); - } - - result *= (value2 - value1) * 0.5f; - result -= (value2 + value1) * 0.5f; - - output[s] = result; - - phase += phase_scale; - if (phase > 1.0f) { - phase -= 1.0f; - value1 = value2; - value2 = (float)rand() * inv_rand_max - 1.0f; - } + static const LV2_Morph_Interface morph = { morph_port, NULL }; + if (!strcmp(uri, LV2_MORPH__interface)) { + return &morph; } - - plugin->phase = phase; - plugin->value1 = value1; - plugin->value2 = value2; + return NULL; } static const LV2_Descriptor descriptor = { - "http://drobilla.net/plugins/blip/random", + "http://drobilla.net/plugins/blop/random", instantiate, connect_port, activate, - runRandom_fcsc_oa, + run, NULL, cleanup, - NULL, + extension_data, }; LV2_SYMBOL_EXPORT const LV2_Descriptor* -- cgit v1.2.1