|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- /*
- * Carla Native Plugins
- * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
- *
- * This program 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 2 of
- * the License, or any later version.
- *
- * This program 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.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaNative.h"
-
- #include "CarlaDefines.h"
- #include "CarlaMIDI.h"
-
- #include <math.h>
- #include <stdlib.h>
-
- // -----------------------------------------------------------------------
-
- typedef enum {
- PARAM_MODE = 0,
- PARAM_SPEED,
- PARAM_MULTIPLIER,
- PARAM_BASE_START,
- PARAM_LFO_OUT,
- PARAM_COUNT
- } LfoParams;
-
- typedef struct {
- const NativeHostDescriptor* host;
- int mode;
- double speed;
- float multiplier;
- float baseStart;
- float value;
- } LfoHandle;
-
- // -----------------------------------------------------------------------
-
- static NativePluginHandle lfo_instantiate(const NativeHostDescriptor* host)
- {
- LfoHandle* const handle = (LfoHandle*)malloc(sizeof(LfoHandle));
-
- if (handle == NULL)
- return NULL;
-
- handle->host = host;
- handle->mode = 1;
- handle->speed = 1.0f;
- handle->multiplier = 1.0f;
- handle->baseStart = 0.0f;
- handle->value = 0.0f;
- return handle;
- }
-
- #define handlePtr ((LfoHandle*)handle)
-
- static void lfo_cleanup(NativePluginHandle handle)
- {
- free(handlePtr);
- }
-
- static uint32_t lfo_get_parameter_count(NativePluginHandle handle)
- {
- return PARAM_COUNT;
-
- // unused
- (void)handle;
- }
-
- static const NativeParameter* lfo_get_parameter_info(NativePluginHandle handle, uint32_t index)
- {
- if (index > PARAM_COUNT)
- return NULL;
-
- static NativeParameter param;
- static NativeParameterScalePoint paramModes[5];
-
- param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
- param.scalePointCount = 0;
- param.scalePoints = NULL;
-
- paramModes[0].label = "Triangle";
- paramModes[1].label = "Sawtooth";
- paramModes[2].label = "Sawtooth (inverted)";
- paramModes[3].label = "Sine (TODO)";
- paramModes[4].label = "Square";
-
- paramModes[0].value = 1.0f;
- paramModes[1].value = 2.0f;
- paramModes[2].value = 3.0f;
- paramModes[3].value = 4.0f;
- paramModes[4].value = 5.0f;
-
- switch (index)
- {
- case PARAM_MODE:
- param.name = "Mode";
- param.unit = NULL;
- param.hints |= NATIVE_PARAMETER_IS_INTEGER|NATIVE_PARAMETER_USES_SCALEPOINTS;
- param.ranges.def = 1.0f;
- param.ranges.min = 1.0f;
- param.ranges.max = 5.0f;
- param.ranges.step = 1.0f;
- param.ranges.stepSmall = 1.0f;
- param.ranges.stepLarge = 1.0f;
- param.scalePointCount = 5;
- param.scalePoints = paramModes;
- break;
- case PARAM_SPEED:
- param.name = "Speed";
- param.unit = "(coef)";
- param.ranges.def = 1.0f;
- param.ranges.min = 0.01f;
- param.ranges.max = 32.0f;
- param.ranges.step = 0.25f;
- param.ranges.stepSmall = 0.1f;
- param.ranges.stepLarge = 0.5f;
- break;
- case PARAM_MULTIPLIER:
- param.name = "Multiplier";
- param.unit = "(coef)";
- param.ranges.def = 1.0f;
- param.ranges.min = 0.01f;
- param.ranges.max = 2.0f;
- param.ranges.step = 0.01f;
- param.ranges.stepSmall = 0.0001f;
- param.ranges.stepLarge = 0.1f;
- break;
- case PARAM_BASE_START:
- param.name = "Start value";
- param.unit = NULL;
- param.ranges.def = 0.0f;
- param.ranges.min = -1.0f;
- param.ranges.max = 1.0f;
- param.ranges.step = 0.01f;
- param.ranges.stepSmall = 0.0001f;
- param.ranges.stepLarge = 0.1f;
- break;
- case PARAM_LFO_OUT:
- param.name = "LFO Out";
- param.unit = NULL;
- param.hints |= NATIVE_PARAMETER_IS_OUTPUT;
- param.ranges.def = 0.0f;
- param.ranges.min = 0.0f;
- param.ranges.max = 1.0f;
- param.ranges.step = 0.01f;
- param.ranges.stepSmall = 0.0001f;
- param.ranges.stepLarge = 0.1f;
- break;
- }
-
- return ¶m;
-
- // unused
- (void)handle;
- }
-
- static float lfo_get_parameter_value(NativePluginHandle handle, uint32_t index)
- {
- switch (index)
- {
- case PARAM_MODE:
- return (float)handlePtr->mode;
- case PARAM_SPEED:
- return (float)handlePtr->speed;
- case PARAM_MULTIPLIER:
- return handlePtr->multiplier;
- case PARAM_BASE_START:
- return handlePtr->baseStart;
- case PARAM_LFO_OUT:
- return handlePtr->value;
- default:
- return 0.0f;
- }
- }
-
- static void lfo_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
- {
- switch (index)
- {
- case PARAM_MODE:
- handlePtr->mode = (int)value;
- break;
- case PARAM_SPEED:
- handlePtr->speed = value;
- break;
- case PARAM_MULTIPLIER:
- handlePtr->multiplier = value;
- break;
- case PARAM_BASE_START:
- handlePtr->baseStart = value;
- break;
- case PARAM_LFO_OUT:
- handlePtr->value = value;
- break;
- }
- }
-
- static void lfo_process(NativePluginHandle handle,
- const float** inBuffer, float** outBuffer, uint32_t frames,
- const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
- {
- const NativeHostDescriptor* const host = handlePtr->host;
- const NativeTimeInfo* const timeInfo = host->get_time_info(host->handle);
-
- if (! timeInfo->playing)
- return;
-
- const double bpm = timeInfo->bbt.valid ? timeInfo->bbt.beatsPerMinute : 120.0;
- const double sampleRate = host->get_sample_rate(host->handle);
-
- const double speedRate = handlePtr->speed/(bpm/60.0/sampleRate);
- const uint speedRatei = (uint)speedRate;
-
- double value = 0.0;
-
- switch (handlePtr->mode)
- {
- case 1: // Triangle
- value = fabs(1.0-(double)(timeInfo->frame % speedRatei)/(speedRate/2.0));
- break;
- case 2: // Sawtooth
- value = (double)(timeInfo->frame % speedRatei)/speedRate;
- break;
- case 3: // Sawtooth (inverted)
- value = 1.0 - (double)(timeInfo->frame % speedRatei)/speedRate;
- break;
- case 4: // Sine -- TODO!
- value = 0.0;
- break;
- case 5: // Square
- value = (timeInfo->frame % speedRatei <= speedRatei/2) ? 1.0 : 0.0;
- break;
- }
-
- value *= (double)handlePtr->multiplier;
- value += (double)handlePtr->baseStart;
-
- if (value <= 0.0)
- handlePtr->value = 0.0f;
- else if (value >= 1.0)
- handlePtr->value = 1.0f;
- else
- handlePtr->value = (float)value;
-
- return;
-
- // unused
- (void)inBuffer;
- (void)outBuffer;
- (void)frames;
- (void)midiEvents;
- (void)midiEventCount;
- }
-
- #undef handlePtr
-
- // -----------------------------------------------------------------------
-
- static const NativePluginDescriptor lfoDesc = {
- .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
- .hints = NATIVE_PLUGIN_IS_RTSAFE,
- .supports = NATIVE_PLUGIN_SUPPORTS_NOTHING,
- .audioIns = 0,
- .audioOuts = 0,
- .midiIns = 0,
- .midiOuts = 0,
- .paramIns = PARAM_COUNT-1,
- .paramOuts = 1,
- .name = "LFO",
- .label = "lfo",
- .maker = "falkTX",
- .copyright = "GNU GPL v2+",
-
- .instantiate = lfo_instantiate,
- .cleanup = lfo_cleanup,
-
- .get_parameter_count = lfo_get_parameter_count,
- .get_parameter_info = lfo_get_parameter_info,
- .get_parameter_value = lfo_get_parameter_value,
-
- .get_midi_program_count = NULL,
- .get_midi_program_info = NULL,
-
- .set_parameter_value = lfo_set_parameter_value,
- .set_midi_program = NULL,
- .set_custom_data = NULL,
-
- .ui_show = NULL,
- .ui_idle = NULL,
-
- .ui_set_parameter_value = NULL,
- .ui_set_midi_program = NULL,
- .ui_set_custom_data = NULL,
-
- .activate = NULL,
- .deactivate = NULL,
- .process = lfo_process,
-
- .get_state = NULL,
- .set_state = NULL,
-
- .dispatcher = NULL,
-
- .render_inline_display = NULL
- };
-
- // -----------------------------------------------------------------------
-
- void carla_register_native_plugin_lfo(void);
-
- void carla_register_native_plugin_lfo(void)
- {
- carla_register_native_plugin(&lfoDesc);
- }
-
- // -----------------------------------------------------------------------
|