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.

432 lines
11KB

  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 != nullptr);
  154. CARLA_ASSERT(value != nullptr);
  155. return;
  156. // unused
  157. (void)key;
  158. (void)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 != nullptr);
  197. CARLA_ASSERT(value != nullptr);
  198. return;
  199. // unused
  200. (void)key;
  201. (void)value;
  202. }
  203. // -------------------------------------------------------------------
  204. // Plugin chunk calls
  205. virtual size_t getChunk(void** const data)
  206. {
  207. CARLA_ASSERT(data != nullptr);
  208. return 0;
  209. // unused
  210. (void)data;
  211. }
  212. virtual void setChunk(void* const data, const size_t size)
  213. {
  214. CARLA_ASSERT(data != nullptr);
  215. CARLA_ASSERT(size > 0);
  216. }
  217. // -------------------------------------------------------------------
  218. private:
  219. const HostDescriptor* const kHost;
  220. // -------------------------------------------------------------------
  221. #ifndef DOXYGEN
  222. public:
  223. #define handlePtr ((PluginDescriptorClass*)handle)
  224. static uint32_t _get_parameter_count(PluginHandle handle)
  225. {
  226. return handlePtr->getParameterCount();
  227. }
  228. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  229. {
  230. return handlePtr->getParameterInfo(index);
  231. }
  232. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  233. {
  234. return handlePtr->getParameterValue(index);
  235. }
  236. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  237. {
  238. return handlePtr->getParameterText(index);
  239. }
  240. static uint32_t _get_midi_program_count(PluginHandle handle)
  241. {
  242. return handlePtr->getMidiProgramCount();
  243. }
  244. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  245. {
  246. return handlePtr->getMidiProgramInfo(index);
  247. }
  248. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  249. {
  250. return handlePtr->setParameterValue(index, value);
  251. }
  252. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  253. {
  254. return handlePtr->setMidiProgram(bank, program);
  255. }
  256. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  257. {
  258. return handlePtr->setCustomData(key, value);
  259. }
  260. static void _ui_show(PluginHandle handle, bool show)
  261. {
  262. return handlePtr->uiShow(show);
  263. }
  264. static void _ui_idle(PluginHandle handle)
  265. {
  266. return handlePtr->uiIdle();
  267. }
  268. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  269. {
  270. return handlePtr->uiSetParameterValue(index, value);
  271. }
  272. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  273. {
  274. return handlePtr->uiSetMidiProgram(bank, program);
  275. }
  276. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  277. {
  278. return handlePtr->uiSetCustomData(key, value);
  279. }
  280. static void _activate(PluginHandle handle)
  281. {
  282. handlePtr->activate();
  283. }
  284. static void _deactivate(PluginHandle handle)
  285. {
  286. handlePtr->deactivate();
  287. }
  288. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  289. {
  290. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  291. }
  292. static size_t _get_chunk(PluginHandle handle, void** data)
  293. {
  294. return handlePtr->getChunk(data);
  295. }
  296. static void _set_chunk(PluginHandle handle, void* data, size_t size)
  297. {
  298. handlePtr->setChunk(data, size);
  299. }
  300. #undef handlePtr
  301. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDescriptorClass)
  302. #endif
  303. };
  304. /**@}*/
  305. // -----------------------------------------------------------------------
  306. #define PluginDescriptorClassEND(className) \
  307. public: \
  308. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  309. { \
  310. return new className(host); \
  311. } \
  312. static void _cleanup(PluginHandle handle) \
  313. { \
  314. delete (className*)handle; \
  315. }
  316. #define PluginDescriptorFILL(className) \
  317. className::_instantiate, \
  318. className::_cleanup, \
  319. className::_get_parameter_count, \
  320. className::_get_parameter_info, \
  321. className::_get_parameter_value, \
  322. className::_get_parameter_text, \
  323. className::_get_midi_program_count, \
  324. className::_get_midi_program_info, \
  325. className::_set_parameter_value, \
  326. className::_set_midi_program, \
  327. className::_set_custom_data, \
  328. className::_ui_show, \
  329. className::_ui_idle, \
  330. className::_ui_set_parameter_value, \
  331. className::_ui_set_midi_program, \
  332. className::_ui_set_custom_data, \
  333. className::_activate, \
  334. className::_deactivate, \
  335. className::_process, \
  336. className::_get_chunk, \
  337. className::_set_chunk
  338. #endif // __CARLA_NATIVE_HPP__