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.

410 lines
10KB

  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2013 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 GPL.txt file
  16. */
  17. #ifndef __CARLA_NATIVE_HPP__
  18. #define __CARLA_NATIVE_HPP__
  19. #include "CarlaNative.h"
  20. #include "CarlaJuceUtils.hpp"
  21. /*!
  22. * @defgroup CarlaNativeAPI Carla Native API
  23. * @{
  24. */
  25. class PluginDescriptorClass
  26. {
  27. public:
  28. PluginDescriptorClass(const HostDescriptor* const host)
  29. : kHost(host)
  30. {
  31. CARLA_ASSERT(host != nullptr);
  32. }
  33. virtual ~PluginDescriptorClass()
  34. {
  35. }
  36. // -------------------------------------------------------------------
  37. // Host calls
  38. const HostDescriptor* getHostHandle() const
  39. {
  40. return kHost;
  41. }
  42. uint32_t getBufferSize() const
  43. {
  44. CARLA_ASSERT(kHost != nullptr);
  45. if (kHost != nullptr)
  46. return kHost->get_buffer_size(kHost->handle);
  47. return 0;
  48. }
  49. double getSampleRate() const
  50. {
  51. CARLA_ASSERT(kHost != nullptr);
  52. if (kHost != nullptr)
  53. return kHost->get_sample_rate(kHost->handle);
  54. return 0.0;
  55. }
  56. const TimeInfo* getTimeInfo() const
  57. {
  58. CARLA_ASSERT(kHost != nullptr);
  59. if (kHost != nullptr)
  60. return kHost->get_time_info(kHost->handle);
  61. return nullptr;
  62. }
  63. void writeMidiEvent(const MidiEvent* const event)
  64. {
  65. CARLA_ASSERT(kHost != nullptr);
  66. if (kHost != nullptr)
  67. kHost->write_midi_event(kHost->handle, event);
  68. }
  69. void uiParameterChanged(const uint32_t index, const float value)
  70. {
  71. CARLA_ASSERT(kHost != nullptr);
  72. if (kHost != nullptr)
  73. kHost->ui_parameter_changed(kHost->handle, index, value);
  74. }
  75. void uiMidiProgramChanged(const uint32_t bank, const uint32_t program)
  76. {
  77. CARLA_ASSERT(kHost != nullptr);
  78. if (kHost != nullptr)
  79. kHost->ui_midi_program_changed(kHost->handle, bank, program);
  80. }
  81. void uiCustomDataChanged(const char* const key, const char* const value)
  82. {
  83. CARLA_ASSERT(kHost != nullptr);
  84. if (kHost != nullptr)
  85. kHost->ui_custom_data_changed(kHost->handle, key, value);
  86. }
  87. void uiClosed()
  88. {
  89. CARLA_ASSERT(kHost != nullptr);
  90. if (kHost != nullptr)
  91. kHost->ui_closed(kHost->handle);
  92. }
  93. protected:
  94. // -------------------------------------------------------------------
  95. // Plugin parameter calls
  96. virtual uint32_t getParameterCount()
  97. {
  98. return 0;
  99. }
  100. virtual const Parameter* getParameterInfo(const uint32_t index)
  101. {
  102. CARLA_ASSERT(index < getParameterCount());
  103. return nullptr;
  104. // unused
  105. (void)index;
  106. }
  107. virtual float getParameterValue(const uint32_t index)
  108. {
  109. CARLA_ASSERT(index < getParameterCount());
  110. return 0.0f;
  111. // unused
  112. (void)index;
  113. }
  114. virtual const char* getParameterText(const uint32_t index)
  115. {
  116. CARLA_ASSERT(index < getParameterCount());
  117. return nullptr;
  118. // unused
  119. (void)index;
  120. }
  121. // -------------------------------------------------------------------
  122. // Plugin midi-program calls
  123. virtual uint32_t getMidiProgramCount()
  124. {
  125. return 0;
  126. }
  127. virtual const MidiProgram* getMidiProgramInfo(const uint32_t index)
  128. {
  129. CARLA_ASSERT(index < getMidiProgramCount());
  130. return nullptr;
  131. // unused
  132. (void)index;
  133. }
  134. // -------------------------------------------------------------------
  135. // Plugin state calls
  136. virtual void setParameterValue(const uint32_t index, const float value)
  137. {
  138. CARLA_ASSERT(index < getParameterCount());
  139. return;
  140. // unused
  141. (void)index;
  142. (void)value;
  143. }
  144. virtual void setMidiProgram(const uint32_t bank, const uint32_t program)
  145. {
  146. return;
  147. // unused
  148. (void)bank;
  149. (void)program;
  150. }
  151. virtual void setCustomData(const char* const key, const char* const value)
  152. {
  153. CARLA_ASSERT(key);
  154. CARLA_ASSERT(value);
  155. return;
  156. // unused
  157. CARLA_ASSERT(key);
  158. CARLA_ASSERT(value);
  159. }
  160. // -------------------------------------------------------------------
  161. // Plugin process calls
  162. virtual void activate()
  163. {
  164. }
  165. virtual void deactivate()
  166. {
  167. }
  168. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const MidiEvent* const midiEvents) = 0;
  169. // -------------------------------------------------------------------
  170. // Plugin UI calls
  171. virtual void uiShow(const bool show)
  172. {
  173. return;
  174. // unused
  175. (void)show;
  176. }
  177. virtual void uiIdle()
  178. {
  179. }
  180. virtual void uiSetParameterValue(const uint32_t index, const float value)
  181. {
  182. CARLA_ASSERT(index < getParameterCount());
  183. return;
  184. // unused
  185. (void)value;
  186. }
  187. virtual void uiSetMidiProgram(const uint32_t bank, const uint32_t program)
  188. {
  189. return;
  190. // unused
  191. (void)bank;
  192. (void)program;
  193. }
  194. virtual void uiSetCustomData(const char* const key, const char* const value)
  195. {
  196. CARLA_ASSERT(key);
  197. CARLA_ASSERT(value);
  198. return;
  199. // unused
  200. (void)key;
  201. (void)value;
  202. }
  203. // -------------------------------------------------------------------
  204. private:
  205. const HostDescriptor* const kHost;
  206. // -------------------------------------------------------------------
  207. #ifndef DOXYGEN
  208. public:
  209. #define handlePtr ((PluginDescriptorClass*)handle)
  210. static uint32_t _get_parameter_count(PluginHandle handle)
  211. {
  212. return handlePtr->getParameterCount();
  213. }
  214. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  215. {
  216. return handlePtr->getParameterInfo(index);
  217. }
  218. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  219. {
  220. return handlePtr->getParameterValue(index);
  221. }
  222. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  223. {
  224. return handlePtr->getParameterText(index);
  225. }
  226. static uint32_t _get_midi_program_count(PluginHandle handle)
  227. {
  228. return handlePtr->getMidiProgramCount();
  229. }
  230. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  231. {
  232. return handlePtr->getMidiProgramInfo(index);
  233. }
  234. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  235. {
  236. return handlePtr->setParameterValue(index, value);
  237. }
  238. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  239. {
  240. return handlePtr->setMidiProgram(bank, program);
  241. }
  242. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  243. {
  244. return handlePtr->setCustomData(key, value);
  245. }
  246. static void _ui_show(PluginHandle handle, bool show)
  247. {
  248. return handlePtr->uiShow(show);
  249. }
  250. static void _ui_idle(PluginHandle handle)
  251. {
  252. return handlePtr->uiIdle();
  253. }
  254. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  255. {
  256. return handlePtr->uiSetParameterValue(index, value);
  257. }
  258. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  259. {
  260. return handlePtr->uiSetMidiProgram(bank, program);
  261. }
  262. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  263. {
  264. return handlePtr->uiSetCustomData(key, value);
  265. }
  266. static void _activate(PluginHandle handle)
  267. {
  268. handlePtr->activate();
  269. }
  270. static void _deactivate(PluginHandle handle)
  271. {
  272. handlePtr->deactivate();
  273. }
  274. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  275. {
  276. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  277. }
  278. #undef handlePtr
  279. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDescriptorClass)
  280. #endif
  281. };
  282. /**@}*/
  283. // -----------------------------------------------------------------------
  284. #define PluginDescriptorClassEND(className) \
  285. public: \
  286. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  287. { \
  288. return new className(host); \
  289. } \
  290. static void _cleanup(PluginHandle handle) \
  291. { \
  292. delete (className*)handle; \
  293. }
  294. #define PluginDescriptorFILL(className) \
  295. className::_instantiate, \
  296. className::_cleanup, \
  297. className::_get_parameter_count, \
  298. className::_get_parameter_info, \
  299. className::_get_parameter_value, \
  300. className::_get_parameter_text, \
  301. className::_get_midi_program_count, \
  302. className::_get_midi_program_info, \
  303. className::_set_parameter_value, \
  304. className::_set_midi_program, \
  305. className::_set_custom_data, \
  306. className::_ui_show, \
  307. className::_ui_idle, \
  308. className::_ui_set_parameter_value, \
  309. className::_ui_set_midi_program, \
  310. className::_ui_set_custom_data, \
  311. className::_activate, \
  312. className::_deactivate, \
  313. className::_process
  314. #endif // __CARLA_NATIVE_HPP__