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.

316 lines
8.5KB

  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 "CarlaMIDI.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. // -----------------------------------------------------------------------
  22. typedef enum {
  23. // all versions
  24. PARAM_GAIN = 0,
  25. PARAM_COUNT_MONO,
  26. // stereo version
  27. PARAM_APPLY_LEFT = PARAM_COUNT_MONO,
  28. PARAM_APPLY_RIGHT,
  29. PARAM_COUNT_STEREO
  30. } AudioGainParams;
  31. typedef struct {
  32. float gain;
  33. bool isMono;
  34. bool applyLeft;
  35. bool applyRight;
  36. } AudioGainHandle;
  37. // -----------------------------------------------------------------------
  38. static NativePluginHandle audiogain_instantiate(const NativeHostDescriptor* host, const bool isMono)
  39. {
  40. AudioGainHandle* const handle = (AudioGainHandle*)malloc(sizeof(AudioGainHandle));
  41. if (handle == NULL)
  42. return NULL;
  43. handle->gain = 1.0f;
  44. handle->isMono = isMono;
  45. handle->applyLeft = true;
  46. handle->applyRight = true;
  47. return handle;
  48. // unused
  49. (void)host;
  50. }
  51. static NativePluginHandle audiogain_instantiate_mono(const NativeHostDescriptor* host)
  52. {
  53. return audiogain_instantiate(host, true);
  54. }
  55. static NativePluginHandle audiogain_instantiate_stereo(const NativeHostDescriptor* host)
  56. {
  57. return audiogain_instantiate(host, false);
  58. }
  59. #define handlePtr ((AudioGainHandle*)handle)
  60. static void audiogain_cleanup(NativePluginHandle handle)
  61. {
  62. free(handlePtr);
  63. }
  64. static uint32_t audiogain_get_parameter_count(NativePluginHandle handle)
  65. {
  66. return handlePtr->isMono ? PARAM_COUNT_MONO : PARAM_COUNT_STEREO;
  67. }
  68. static const NativeParameter* audiogain_get_parameter_info(NativePluginHandle handle, uint32_t index)
  69. {
  70. if (index > (handlePtr->isMono ? PARAM_COUNT_MONO : PARAM_COUNT_STEREO))
  71. return NULL;
  72. static NativeParameter param;
  73. param.hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  74. param.unit = NULL;
  75. param.scalePointCount = 0;
  76. param.scalePoints = NULL;
  77. switch (index)
  78. {
  79. case PARAM_GAIN:
  80. param.name = "Gain";
  81. param.ranges.def = 1.0f;
  82. param.ranges.min = 0.0f;
  83. param.ranges.max = 4.0f;
  84. param.ranges.step = PARAMETER_RANGES_DEFAULT_STEP;
  85. param.ranges.stepSmall = PARAMETER_RANGES_DEFAULT_STEP_SMALL;
  86. param.ranges.stepLarge = PARAMETER_RANGES_DEFAULT_STEP_LARGE;
  87. break;
  88. case PARAM_APPLY_LEFT:
  89. param.name = "Apply Left";
  90. param.hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  91. param.ranges.def = 1.0f;
  92. param.ranges.min = 0.0f;
  93. param.ranges.max = 1.0f;
  94. param.ranges.step = 1.0f;
  95. param.ranges.stepSmall = 1.0f;
  96. param.ranges.stepLarge = 1.0f;
  97. break;
  98. case PARAM_APPLY_RIGHT:
  99. param.name = "Apply Right";
  100. param.hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  101. param.ranges.def = 1.0f;
  102. param.ranges.min = 0.0f;
  103. param.ranges.max = 1.0f;
  104. param.ranges.step = 1.0f;
  105. param.ranges.stepSmall = 1.0f;
  106. param.ranges.stepLarge = 1.0f;
  107. break;
  108. }
  109. return &param;
  110. }
  111. static float audiogain_get_parameter_value(NativePluginHandle handle, uint32_t index)
  112. {
  113. switch (index)
  114. {
  115. case PARAM_GAIN:
  116. return handlePtr->gain;
  117. case PARAM_APPLY_LEFT:
  118. return handlePtr->applyLeft ? 1.0f : 0.0f;
  119. case PARAM_APPLY_RIGHT:
  120. return handlePtr->applyRight ? 1.0f : 0.0f;
  121. default:
  122. return 0.0f;
  123. }
  124. }
  125. static void audiogain_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  126. {
  127. switch (index)
  128. {
  129. case PARAM_GAIN:
  130. handlePtr->gain = value;
  131. break;
  132. case PARAM_APPLY_LEFT:
  133. handlePtr->applyLeft = (value >= 0.5f);
  134. break;
  135. case PARAM_APPLY_RIGHT:
  136. handlePtr->applyRight = (value >= 0.5f);
  137. break;
  138. }
  139. }
  140. static void handle_audio_buffers(const float* inBuffer, float* outBuffer, const float gain, const uint32_t frames)
  141. {
  142. /*
  143. if (gain == 0.0f)
  144. {
  145. memset(outBuffer, 0, sizeof(float)*frames);
  146. }
  147. else if (gain == 1.0f)
  148. {
  149. if (outBuffer != inBuffer)
  150. memcpy(outBuffer, inBuffer, sizeof(float)*frames);
  151. }
  152. else
  153. */
  154. {
  155. for (uint32_t i=0; i < frames; ++i)
  156. *outBuffer++ = *inBuffer++ * gain;
  157. }
  158. }
  159. static void audiogain_process(NativePluginHandle handle,
  160. const float** inBuffer, float** outBuffer, uint32_t frames,
  161. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  162. {
  163. const float gain = handlePtr->gain;
  164. const bool applyLeft = handlePtr->applyLeft;
  165. const bool applyRight = handlePtr->applyRight;
  166. const bool isMono = handlePtr->isMono;
  167. handle_audio_buffers(inBuffer[0], outBuffer[0], (isMono || applyLeft) ? gain : 1.0f, frames);
  168. if (! isMono)
  169. handle_audio_buffers(inBuffer[1], outBuffer[1], applyRight ? gain : 1.0f, frames);
  170. return;
  171. // unused
  172. (void)midiEvents;
  173. (void)midiEventCount;
  174. }
  175. // -----------------------------------------------------------------------
  176. static const NativePluginDescriptor audiogainMonoDesc = {
  177. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  178. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  179. .supports = NATIVE_PLUGIN_SUPPORTS_NOTHING,
  180. .audioIns = 1,
  181. .audioOuts = 1,
  182. .midiIns = 0,
  183. .midiOuts = 0,
  184. .paramIns = PARAM_COUNT_MONO,
  185. .paramOuts = 0,
  186. .name = "Audio Gain (Mono)",
  187. .label = "audiogain",
  188. .maker = "falkTX",
  189. .copyright = "GNU GPL v2+",
  190. .instantiate = audiogain_instantiate_mono,
  191. .cleanup = audiogain_cleanup,
  192. .get_parameter_count = audiogain_get_parameter_count,
  193. .get_parameter_info = audiogain_get_parameter_info,
  194. .get_parameter_value = audiogain_get_parameter_value,
  195. .get_midi_program_count = NULL,
  196. .get_midi_program_info = NULL,
  197. .set_parameter_value = audiogain_set_parameter_value,
  198. .set_midi_program = NULL,
  199. .set_custom_data = NULL,
  200. .ui_show = NULL,
  201. .ui_idle = NULL,
  202. .ui_set_parameter_value = NULL,
  203. .ui_set_midi_program = NULL,
  204. .ui_set_custom_data = NULL,
  205. .activate = NULL,
  206. .deactivate = NULL,
  207. .process = audiogain_process,
  208. .get_state = NULL,
  209. .set_state = NULL,
  210. .dispatcher = NULL,
  211. .render_inline_display = NULL
  212. };
  213. static const NativePluginDescriptor audiogainStereoDesc = {
  214. .category = NATIVE_PLUGIN_CATEGORY_UTILITY,
  215. .hints = NATIVE_PLUGIN_IS_RTSAFE,
  216. .supports = NATIVE_PLUGIN_SUPPORTS_NOTHING,
  217. .audioIns = 2,
  218. .audioOuts = 2,
  219. .cvIns = 0,
  220. .cvOuts = 0,
  221. .midiIns = 0,
  222. .midiOuts = 0,
  223. .paramIns = PARAM_COUNT_STEREO,
  224. .paramOuts = 0,
  225. .name = "Audio Gain (Stereo)",
  226. .label = "audiogain_s",
  227. .maker = "falkTX",
  228. .copyright = "GNU GPL v2+",
  229. .instantiate = audiogain_instantiate_stereo,
  230. .cleanup = audiogain_cleanup,
  231. .get_parameter_count = audiogain_get_parameter_count,
  232. .get_parameter_info = audiogain_get_parameter_info,
  233. .get_parameter_value = audiogain_get_parameter_value,
  234. .get_midi_program_count = NULL,
  235. .get_midi_program_info = NULL,
  236. .set_parameter_value = audiogain_set_parameter_value,
  237. .set_midi_program = NULL,
  238. .set_custom_data = NULL,
  239. .ui_show = NULL,
  240. .ui_idle = NULL,
  241. .ui_set_parameter_value = NULL,
  242. .ui_set_midi_program = NULL,
  243. .ui_set_custom_data = NULL,
  244. .activate = NULL,
  245. .deactivate = NULL,
  246. .process = audiogain_process,
  247. .get_state = NULL,
  248. .set_state = NULL,
  249. .dispatcher = NULL,
  250. .render_inline_display = NULL
  251. };
  252. // -----------------------------------------------------------------------
  253. void carla_register_native_plugin_audiogain(void);
  254. void carla_register_native_plugin_audiogain(void)
  255. {
  256. carla_register_native_plugin(&audiogainMonoDesc);
  257. carla_register_native_plugin(&audiogainStereoDesc);
  258. }
  259. // -----------------------------------------------------------------------