Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
8.6KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaNative.h"
  18. #include "CarlaDefines.h"
  19. #include "CarlaMIDI.h"
  20. #include <math.h>
  21. #include <stdlib.h>
  22. // -----------------------------------------------------------------------
  23. typedef enum {
  24. PARAM_MODE = 0,
  25. PARAM_SPEED,
  26. PARAM_MULTIPLIER,
  27. PARAM_BASE_START,
  28. PARAM_LFO_OUT,
  29. PARAM_COUNT
  30. } LfoParams;
  31. typedef struct {
  32. const NativeHostDescriptor* host;
  33. int mode;
  34. double speed;
  35. float multiplier;
  36. float baseStart;
  37. float value;
  38. } LfoHandle;
  39. // -----------------------------------------------------------------------
  40. static NativePluginHandle lfo_instantiate(const NativeHostDescriptor* host)
  41. {
  42. LfoHandle* const handle = (LfoHandle*)malloc(sizeof(LfoHandle));
  43. if (handle == NULL)
  44. return NULL;
  45. handle->host = host;
  46. handle->mode = 1;
  47. handle->speed = 1.0f;
  48. handle->multiplier = 1.0f;
  49. handle->baseStart = 0.0f;
  50. handle->value = 0.0f;
  51. return handle;
  52. }
  53. #define handlePtr ((LfoHandle*)handle)
  54. static void lfo_cleanup(NativePluginHandle handle)
  55. {
  56. free(handlePtr);
  57. }
  58. static uint32_t lfo_get_parameter_count(NativePluginHandle handle)
  59. {
  60. return PARAM_COUNT;
  61. // unused
  62. (void)handle;
  63. }
  64. static const NativeParameter* lfo_get_parameter_info(NativePluginHandle handle, uint32_t index)
  65. {
  66. if (index > PARAM_COUNT)
  67. return NULL;
  68. static NativeParameter param;
  69. static NativeParameterScalePoint paramModes[5];
  70. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  71. param.scalePointCount = 0;
  72. param.scalePoints = NULL;
  73. paramModes[0].label = "Triangle";
  74. paramModes[1].label = "Sawtooth";
  75. paramModes[2].label = "Sawtooth (inverted)";
  76. paramModes[3].label = "Sine (TODO)";
  77. paramModes[4].label = "Square";
  78. paramModes[0].value = 1.0f;
  79. paramModes[1].value = 2.0f;
  80. paramModes[2].value = 3.0f;
  81. paramModes[3].value = 4.0f;
  82. paramModes[4].value = 5.0f;
  83. switch (index)
  84. {
  85. case PARAM_MODE:
  86. param.name = "Mode";
  87. param.unit = NULL;
  88. param.hints |= NATIVE_PARAMETER_IS_INTEGER|NATIVE_PARAMETER_USES_SCALEPOINTS;
  89. param.ranges.def = 1.0f;
  90. param.ranges.min = 1.0f;
  91. param.ranges.max = 5.0f;
  92. param.ranges.step = 1.0f;
  93. param.ranges.stepSmall = 1.0f;
  94. param.ranges.stepLarge = 1.0f;
  95. param.scalePointCount = 5;
  96. param.scalePoints = paramModes;
  97. break;
  98. case PARAM_SPEED:
  99. param.name = "Speed";
  100. param.unit = "(coef)";
  101. param.ranges.def = 1.0f;
  102. param.ranges.min = 0.01f;
  103. param.ranges.max = 2048.0f;
  104. param.ranges.step = 0.25f;
  105. param.ranges.stepSmall = 0.1f;
  106. param.ranges.stepLarge = 0.5f;
  107. break;
  108. case PARAM_MULTIPLIER:
  109. param.name = "Multiplier";
  110. param.unit = "(coef)";
  111. param.ranges.def = 1.0f;
  112. param.ranges.min = 0.01f;
  113. param.ranges.max = 2.0f;
  114. param.ranges.step = 0.01f;
  115. param.ranges.stepSmall = 0.0001f;
  116. param.ranges.stepLarge = 0.1f;
  117. break;
  118. case PARAM_BASE_START:
  119. param.name = "Start value";
  120. param.unit = NULL;
  121. param.ranges.def = 0.0f;
  122. param.ranges.min = -1.0f;
  123. param.ranges.max = 1.0f;
  124. param.ranges.step = 0.01f;
  125. param.ranges.stepSmall = 0.0001f;
  126. param.ranges.stepLarge = 0.1f;
  127. break;
  128. case PARAM_LFO_OUT:
  129. param.name = "LFO Out";
  130. param.unit = NULL;
  131. param.hints |= NATIVE_PARAMETER_IS_OUTPUT;
  132. param.ranges.def = 0.0f;
  133. param.ranges.min = 0.0f;
  134. param.ranges.max = 1.0f;
  135. param.ranges.step = 0.01f;
  136. param.ranges.stepSmall = 0.0001f;
  137. param.ranges.stepLarge = 0.1f;
  138. break;
  139. }
  140. return &param;
  141. // unused
  142. (void)handle;
  143. }
  144. static float lfo_get_parameter_value(NativePluginHandle handle, uint32_t index)
  145. {
  146. switch (index)
  147. {
  148. case PARAM_MODE:
  149. return (float)handlePtr->mode;
  150. case PARAM_SPEED:
  151. return (float)handlePtr->speed;
  152. case PARAM_MULTIPLIER:
  153. return handlePtr->multiplier;
  154. case PARAM_BASE_START:
  155. return handlePtr->baseStart;
  156. case PARAM_LFO_OUT:
  157. return handlePtr->value;
  158. default:
  159. return 0.0f;
  160. }
  161. }
  162. static void lfo_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  163. {
  164. switch (index)
  165. {
  166. case PARAM_MODE:
  167. handlePtr->mode = (int)value;
  168. break;
  169. case PARAM_SPEED:
  170. handlePtr->speed = value;
  171. break;
  172. case PARAM_MULTIPLIER:
  173. handlePtr->multiplier = value;
  174. break;
  175. case PARAM_BASE_START:
  176. handlePtr->baseStart = value;
  177. break;
  178. case PARAM_LFO_OUT:
  179. handlePtr->value = value;
  180. break;
  181. }
  182. }
  183. // FIXME for v3.0, use const for the input buffer
  184. static void lfo_process(NativePluginHandle handle,
  185. float** inBuffer, float** outBuffer, uint32_t frames,
  186. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  187. {
  188. const NativeHostDescriptor* const host = handlePtr->host;
  189. const NativeTimeInfo* const timeInfo = host->get_time_info(host->handle);
  190. if (! timeInfo->playing)
  191. return;
  192. const double bpm = timeInfo->bbt.valid ? timeInfo->bbt.beatsPerMinute : 120.0;
  193. const double sampleRate = host->get_sample_rate(host->handle);
  194. const double speedRate = handlePtr->speed/(bpm/60.0/sampleRate);
  195. const uint speedRatei = (uint)speedRate;
  196. double value = 0.0;
  197. switch (handlePtr->mode)
  198. {
  199. case 1: // Triangle
  200. value = fabs(1.0-(double)(timeInfo->frame % speedRatei)/(speedRate/2.0));
  201. break;
  202. case 2: // Sawtooth
  203. value = (double)(timeInfo->frame % speedRatei)/speedRate;
  204. break;
  205. case 3: // Sawtooth (inverted)
  206. value = 1.0 - (double)(timeInfo->frame % speedRatei)/speedRate;
  207. break;
  208. case 4: // Sine -- TODO!
  209. value = 0.0;
  210. break;
  211. case 5: // Square
  212. value = (timeInfo->frame % speedRatei <= speedRatei/2) ? 1.0 : 0.0;
  213. break;
  214. }
  215. value *= (double)handlePtr->multiplier;
  216. value += (double)handlePtr->baseStart;
  217. if (value <= 0.0)
  218. handlePtr->value = 0.0f;
  219. else if (value >= 1.0)
  220. handlePtr->value = 1.0f;
  221. else
  222. handlePtr->value = (float)value;
  223. return;
  224. // unused
  225. (void)inBuffer;
  226. (void)outBuffer;
  227. (void)frames;
  228. (void)midiEvents;
  229. (void)midiEventCount;
  230. }
  231. #undef handlePtr
  232. // -----------------------------------------------------------------------
  233. static const NativePluginDescriptor lfoDesc = {
  234. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  235. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  236. .supports = NATIVE_PLUGIN_SUPPORTS_NOTHING,
  237. .audioIns = 0,
  238. .audioOuts = 0,
  239. .cvIns = 0,
  240. .cvOuts = 0,
  241. .midiIns = 0,
  242. .midiOuts = 0,
  243. .paramIns = PARAM_COUNT-1,
  244. .paramOuts = 1,
  245. .name = "LFO",
  246. .label = "lfo",
  247. .maker = "falkTX",
  248. .copyright = "GNU GPL v2+",
  249. .instantiate = lfo_instantiate,
  250. .cleanup = lfo_cleanup,
  251. .get_parameter_count = lfo_get_parameter_count,
  252. .get_parameter_info = lfo_get_parameter_info,
  253. .get_parameter_value = lfo_get_parameter_value,
  254. .get_midi_program_count = NULL,
  255. .get_midi_program_info = NULL,
  256. .set_parameter_value = lfo_set_parameter_value,
  257. .set_midi_program = NULL,
  258. .set_custom_data = NULL,
  259. .ui_show = NULL,
  260. .ui_idle = NULL,
  261. .ui_set_parameter_value = NULL,
  262. .ui_set_midi_program = NULL,
  263. .ui_set_custom_data = NULL,
  264. .activate = NULL,
  265. .deactivate = NULL,
  266. .process = lfo_process,
  267. .get_state = NULL,
  268. .set_state = NULL,
  269. .dispatcher = NULL
  270. };
  271. // -----------------------------------------------------------------------
  272. void carla_register_native_plugin_lfo(void);
  273. void carla_register_native_plugin_lfo(void)
  274. {
  275. carla_register_native_plugin(&lfoDesc);
  276. }
  277. // -----------------------------------------------------------------------