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.

243 lines
5.8KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2020 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 <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. // -----------------------------------------------------------------------
  23. typedef enum {
  24. // all versions
  25. PARAM_LIMITER = 0,
  26. PARAM_COUNT
  27. } CvToAudioParams;
  28. typedef struct {
  29. bool limiterOn;
  30. } CvToAudioHandle;
  31. // -----------------------------------------------------------------------
  32. static NativePluginHandle cv2audio_instantiate(const NativeHostDescriptor* host)
  33. {
  34. CvToAudioHandle* const handle = (CvToAudioHandle*)malloc(sizeof(CvToAudioHandle));
  35. if (handle == NULL)
  36. return NULL;
  37. handle->limiterOn = true;
  38. return handle;
  39. // unused
  40. (void)host;
  41. }
  42. #define handlePtr ((CvToAudioHandle*)handle)
  43. static void cv2audio_cleanup(NativePluginHandle handle)
  44. {
  45. free(handlePtr);
  46. }
  47. static uint32_t cv2audio_get_parameter_count(NativePluginHandle handle)
  48. {
  49. return PARAM_COUNT;
  50. // unused
  51. (void)handle;
  52. }
  53. static const NativeParameter* cv2audio_get_parameter_info(NativePluginHandle handle, uint32_t index)
  54. {
  55. if (index > PARAM_COUNT)
  56. return NULL;
  57. static NativeParameter param;
  58. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMATABLE;
  59. param.unit = NULL;
  60. param.scalePointCount = 0;
  61. param.scalePoints = NULL;
  62. switch (index)
  63. {
  64. case PARAM_LIMITER:
  65. param.name = "Briwall Limiter";
  66. param.hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  67. param.ranges.def = 1.0f;
  68. param.ranges.min = 0.0f;
  69. param.ranges.max = 1.0f;
  70. param.ranges.step = 1.0f;
  71. param.ranges.stepSmall = 1.0f;
  72. param.ranges.stepLarge = 1.0f;
  73. break;
  74. }
  75. return &param;
  76. // unused
  77. (void)handle;
  78. }
  79. static float cv2audio_get_parameter_value(NativePluginHandle handle, uint32_t index)
  80. {
  81. switch (index)
  82. {
  83. case PARAM_LIMITER:
  84. return handlePtr->limiterOn ? 1.0f : 0.0f;
  85. default:
  86. return 0.0f;
  87. }
  88. }
  89. static void cv2audio_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  90. {
  91. switch (index)
  92. {
  93. case PARAM_LIMITER:
  94. handlePtr->limiterOn = (value >= 0.5f);
  95. break;
  96. }
  97. }
  98. static const NativePortRange* cv2audio_get_buffer_port_range(NativePluginHandle handle, uint32_t index, bool isOutput)
  99. {
  100. if (isOutput)
  101. return NULL;
  102. static NativePortRange npr;
  103. switch (index)
  104. {
  105. case 0:
  106. npr.minimum = -1.0f;
  107. npr.maximum = 1.0f;
  108. return &npr;
  109. default:
  110. return NULL;
  111. }
  112. // unused
  113. (void)handle;
  114. }
  115. static const char* cv2audio_get_buffer_port_name(NativePluginHandle handle, uint32_t index, bool isOutput)
  116. {
  117. if (index != 0)
  118. return NULL;
  119. return isOutput ? "Audio Output" : "CV Input";
  120. // unused
  121. (void)handle;
  122. }
  123. // FIXME for v3.0, use const for the input buffer
  124. static void cv2audio_process(NativePluginHandle handle,
  125. float** inBuffer, float** outBuffer, uint32_t frames,
  126. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  127. {
  128. const float* const inBuf = inBuffer[0];
  129. /**/ float* const outBuf = outBuffer[0];
  130. if (handlePtr->limiterOn)
  131. {
  132. for (uint32_t i=0; i<frames; ++i)
  133. outBuf[i] = fmaxf(fminf(inBuf[i], 1.0f), -1.0f);
  134. }
  135. else
  136. {
  137. if (outBuf != inBuf)
  138. memcpy(outBuf, inBuf, sizeof(float)*frames);
  139. }
  140. return;
  141. // unused
  142. (void)midiEvents;
  143. (void)midiEventCount;
  144. }
  145. // -----------------------------------------------------------------------
  146. static const NativePluginDescriptor cv2audioDesc = {
  147. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  148. .hints = NATIVE_PLUGIN_IS_RTSAFE|NATIVE_PLUGIN_USES_CONTROL_VOLTAGE,
  149. .supports = NATIVE_PLUGIN_SUPPORTS_NOTHING,
  150. .audioIns = 0,
  151. .audioOuts = 1,
  152. .cvIns = 1,
  153. .cvOuts = 0,
  154. .midiIns = 0,
  155. .midiOuts = 0,
  156. .paramIns = PARAM_COUNT,
  157. .paramOuts = 0,
  158. .name = "CV to Audio",
  159. .label = "cv2audio",
  160. .maker = "falkTX",
  161. .copyright = "GNU GPL v2+",
  162. .instantiate = cv2audio_instantiate,
  163. .cleanup = cv2audio_cleanup,
  164. .get_parameter_count = cv2audio_get_parameter_count,
  165. .get_parameter_info = cv2audio_get_parameter_info,
  166. .get_parameter_value = cv2audio_get_parameter_value,
  167. .get_midi_program_count = NULL,
  168. .get_midi_program_info = NULL,
  169. .set_parameter_value = cv2audio_set_parameter_value,
  170. .set_midi_program = NULL,
  171. .set_custom_data = NULL,
  172. .get_buffer_port_name = cv2audio_get_buffer_port_name,
  173. .get_buffer_port_range = cv2audio_get_buffer_port_range,
  174. .ui_show = NULL,
  175. .ui_idle = NULL,
  176. .ui_set_parameter_value = NULL,
  177. .ui_set_midi_program = NULL,
  178. .ui_set_custom_data = NULL,
  179. .activate = NULL,
  180. .deactivate = NULL,
  181. .process = cv2audio_process,
  182. .get_state = NULL,
  183. .set_state = NULL,
  184. .dispatcher = NULL
  185. };
  186. // -----------------------------------------------------------------------
  187. void carla_register_native_plugin_cv2audio(void);
  188. void carla_register_native_plugin_cv2audio(void)
  189. {
  190. carla_register_native_plugin(&cv2audioDesc);
  191. }
  192. // -----------------------------------------------------------------------