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.

340 lines
9.1KB

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