aboutsummaryrefslogtreecommitdiffstats
path: root/src/ratio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ratio.c')
-rw-r--r--src/ratio.c150
1 files changed, 79 insertions, 71 deletions
diff --git a/src/ratio.c b/src/ratio.c
index 4659f4f..0bb3f1f 100644
--- a/src/ratio.c
+++ b/src/ratio.c
@@ -18,18 +18,24 @@
*/
#include <stdlib.h>
+#include "lv2/lv2plug.in/ns/ext/morph/morph.h"
#include "lv2/lv2plug.in/ns/lv2core/lv2.h"
#include "math_func.h"
#include "common.h"
+#include "uris.h"
#define RATIO_NUMERATOR 0
#define RATIO_DENOMINATOR 1
#define RATIO_OUTPUT 2
typedef struct {
- float* numerator;
- float* denominator;
- float* output;
+ float* numerator;
+ float* denominator;
+ float* output;
+ uint32_t numerator_is_cv;
+ uint32_t denominator_is_cv;
+ uint32_t output_is_cv;
+ URIs uris;
} Ratio;
static void
@@ -58,6 +64,51 @@ 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)
+{
+ Ratio* plugin = (Ratio*)instance;
+
+ if (type != plugin->uris.lv2_ControlPort &&
+ type != plugin->uris.lv2_CVPort) {
+ return LV2_MORPH_ERR_BAD_TYPE;
+ }
+
+ switch (port) {
+ case RATIO_NUMERATOR:
+ plugin->numerator_is_cv = (type == plugin->uris.lv2_CVPort);
+ break;
+ case RATIO_DENOMINATOR:
+ plugin->denominator_is_cv = (type == plugin->uris.lv2_CVPort);
+ break;
+ default:
+ return LV2_MORPH_ERR_BAD_PORT;
+ }
+
+ plugin->output_is_cv = plugin->numerator_is_cv || plugin->denominator_is_cv;
+ return LV2_MORPH_SUCCESS;
+}
+
+static LV2_URID
+port_type(LV2_Handle instance,
+ uint32_t port,
+ LV2_Morph_Property*const* properties)
+{
+ Ratio* plugin = (Ratio*)instance;
+
+ switch (port) {
+ case RATIO_OUTPUT:
+ return (plugin->output_is_cv
+ ? plugin->uris.lv2_CVPort
+ : plugin->uris.lv2_ControlPort);
+ default:
+ return 0;
+ }
+}
+
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double sample_rate,
@@ -66,12 +117,20 @@ instantiate(const LV2_Descriptor* descriptor,
{
Ratio* plugin = (Ratio*)malloc(sizeof(Ratio));
+ if (plugin) {
+ plugin->numerator_is_cv = 0;
+ plugin->denominator_is_cv = 0;
+ plugin->output_is_cv = 0;
+
+ map_uris(&plugin->uris, features);
+ }
+
return (LV2_Handle)plugin;
}
static void
-runRatio_nada_oa(LV2_Handle instance,
- uint32_t sample_count)
+run(LV2_Handle instance,
+ uint32_t sample_count)
{
Ratio* plugin = (Ratio*)instance;
@@ -84,90 +143,39 @@ runRatio_nada_oa(LV2_Handle instance,
/* Output (array of floats of length sample_count) */
float* output = plugin->output;
- for (uint32_t s = 0; s < sample_count; ++s) {
- const float n = numerator[s];
- float d = denominator[s];
-
- d = COPYSIGNF(f_max(FABSF(d), 1e-16f), d);
-
- output[s] = n / d;
+ if (!plugin->output_is_cv) { /* TODO: Avoid this branch */
+ sample_count = 1;
}
-}
-
-static void
-runRatio_nadc_oa(LV2_Handle instance,
- uint32_t sample_count)
-{
- Ratio* plugin = (Ratio*)instance;
-
- /* Numerator (array of floats of length sample_count) */
- const float* numerator = plugin->numerator;
-
- /* Denominator (float value) */
- float denominator = *(plugin->denominator);
-
- /* Output (array of floats of length sample_count) */
- float* output = plugin->output;
-
- denominator = COPYSIGNF(f_max(FABSF(denominator), 1e-16f), denominator);
for (uint32_t s = 0; s < sample_count; ++s) {
- output[s] = numerator[s] / denominator;
- }
-}
-
-static void
-runRatio_ncda_oa(LV2_Handle instance,
- uint32_t sample_count)
-{
- Ratio* plugin = (Ratio*)instance;
+ const float n = numerator[s * plugin->numerator_is_cv];
+ float d = denominator[s * plugin->denominator_is_cv];
- /* Numerator (float value) */
- const float numerator = *(plugin->numerator);
-
- /* Denominator (array of floats of length sample_count) */
- const float* denominator = plugin->denominator;
-
- /* Output (array of floats of length sample_count) */
- float* output = plugin->output;
-
- for (uint32_t s = 0; s < sample_count; ++s) {
- float d = denominator[s];
d = COPYSIGNF(f_max(FABSF(d), 1e-16f), d);
- output[s] = numerator / d;
+ output[s] = n / d;
}
}
-static void
-runRatio_ncdc_oc(LV2_Handle instance,
- uint32_t sample_count)
+static const void*
+extension_data(const char* uri)
{
- Ratio* plugin = (Ratio*)instance;
-
- /* Numerator (float value) */
- const float numerator = *(plugin->numerator);
-
- /* Denominator (float value) */
- float denominator = *(plugin->denominator);
-
- /* Output Frequency (pointer to float value) */
- float* output = plugin->output;
-
- denominator = COPYSIGNF(f_max(FABSF(denominator), 1e-16f), denominator);
-
- output[0] = numerator / denominator;
+ static const LV2_Morph_Interface morph = { morph_port, port_type };
+ if (!strcmp(uri, LV2_MORPH__interface)) {
+ return &morph;
+ }
+ return NULL;
}
static const LV2_Descriptor descriptor = {
- "http://drobilla.net/plugins/blip/ratio",
+ "http://drobilla.net/plugins/blop/ratio",
instantiate,
connect_port,
NULL,
- runRatio_nada_oa,
+ run,
NULL,
cleanup,
- NULL,
+ extension_data,
};
LV2_SYMBOL_EXPORT const LV2_Descriptor*