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.

471 lines
12KB

  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. protected:
  37. // -------------------------------------------------------------------
  38. // Host calls
  39. const HostDescriptor* getHostHandle() const
  40. {
  41. return kHost;
  42. }
  43. uint32_t getBufferSize() const
  44. {
  45. CARLA_ASSERT(kHost != nullptr);
  46. if (kHost != nullptr)
  47. return kHost->get_buffer_size(kHost->handle);
  48. return 0;
  49. }
  50. double getSampleRate() const
  51. {
  52. CARLA_ASSERT(kHost != nullptr);
  53. if (kHost != nullptr)
  54. return kHost->get_sample_rate(kHost->handle);
  55. return 0.0;
  56. }
  57. const TimeInfo* getTimeInfo() const
  58. {
  59. CARLA_ASSERT(kHost != nullptr);
  60. if (kHost != nullptr)
  61. return kHost->get_time_info(kHost->handle);
  62. return nullptr;
  63. }
  64. void writeMidiEvent(const MidiEvent* const event)
  65. {
  66. CARLA_ASSERT(kHost != nullptr);
  67. if (kHost != nullptr)
  68. kHost->write_midi_event(kHost->handle, event);
  69. }
  70. void uiParameterChanged(const uint32_t index, const float value)
  71. {
  72. CARLA_ASSERT(kHost != nullptr);
  73. if (kHost != nullptr)
  74. kHost->ui_parameter_changed(kHost->handle, index, value);
  75. }
  76. void uiMidiProgramChanged(const uint32_t bank, const uint32_t program)
  77. {
  78. CARLA_ASSERT(kHost != nullptr);
  79. if (kHost != nullptr)
  80. kHost->ui_midi_program_changed(kHost->handle, bank, program);
  81. }
  82. void uiCustomDataChanged(const char* const key, const char* const value)
  83. {
  84. CARLA_ASSERT(kHost != nullptr);
  85. if (kHost != nullptr)
  86. kHost->ui_custom_data_changed(kHost->handle, key, value);
  87. }
  88. void uiClosed()
  89. {
  90. CARLA_ASSERT(kHost != nullptr);
  91. if (kHost != nullptr)
  92. kHost->ui_closed(kHost->handle);
  93. }
  94. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter)
  95. {
  96. CARLA_ASSERT(kHost != nullptr);
  97. if (kHost != nullptr)
  98. return kHost->ui_open_file(kHost->handle, isDir, title, filter);
  99. return nullptr;
  100. }
  101. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter)
  102. {
  103. CARLA_ASSERT(kHost != nullptr);
  104. if (kHost != nullptr)
  105. return kHost->ui_save_file(kHost->handle, isDir, title, filter);
  106. return nullptr;
  107. }
  108. // -------------------------------------------------------------------
  109. // Plugin parameter calls
  110. virtual uint32_t getParameterCount()
  111. {
  112. return 0;
  113. }
  114. virtual const Parameter* getParameterInfo(const uint32_t index)
  115. {
  116. CARLA_ASSERT(index < getParameterCount());
  117. return nullptr;
  118. // unused
  119. (void)index;
  120. }
  121. virtual float getParameterValue(const uint32_t index)
  122. {
  123. CARLA_ASSERT(index < getParameterCount());
  124. return 0.0f;
  125. // unused
  126. (void)index;
  127. }
  128. virtual const char* getParameterText(const uint32_t index)
  129. {
  130. CARLA_ASSERT(index < getParameterCount());
  131. return nullptr;
  132. // unused
  133. (void)index;
  134. }
  135. // -------------------------------------------------------------------
  136. // Plugin midi-program calls
  137. virtual uint32_t getMidiProgramCount()
  138. {
  139. return 0;
  140. }
  141. virtual const MidiProgram* getMidiProgramInfo(const uint32_t index)
  142. {
  143. CARLA_ASSERT(index < getMidiProgramCount());
  144. return nullptr;
  145. // unused
  146. (void)index;
  147. }
  148. // -------------------------------------------------------------------
  149. // Plugin state calls
  150. virtual void setParameterValue(const uint32_t index, const float value)
  151. {
  152. CARLA_ASSERT(index < getParameterCount());
  153. return;
  154. // unused
  155. (void)index;
  156. (void)value;
  157. }
  158. virtual void setMidiProgram(const uint32_t bank, const uint32_t program)
  159. {
  160. return;
  161. // unused
  162. (void)bank;
  163. (void)program;
  164. }
  165. virtual void setCustomData(const char* const key, const char* const value)
  166. {
  167. CARLA_ASSERT(key != nullptr);
  168. CARLA_ASSERT(value != nullptr);
  169. return;
  170. // unused
  171. (void)key;
  172. (void)value;
  173. }
  174. // -------------------------------------------------------------------
  175. // Plugin process calls
  176. virtual void activate()
  177. {
  178. }
  179. virtual void deactivate()
  180. {
  181. }
  182. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const MidiEvent* const midiEvents) = 0;
  183. // -------------------------------------------------------------------
  184. // Plugin UI calls
  185. virtual void uiShow(const bool show)
  186. {
  187. return;
  188. // unused
  189. (void)show;
  190. }
  191. virtual void uiIdle()
  192. {
  193. }
  194. virtual void uiSetParameterValue(const uint32_t index, const float value)
  195. {
  196. CARLA_ASSERT(index < getParameterCount());
  197. return;
  198. // unused
  199. (void)value;
  200. }
  201. virtual void uiSetMidiProgram(const uint32_t bank, const uint32_t program)
  202. {
  203. return;
  204. // unused
  205. (void)bank;
  206. (void)program;
  207. }
  208. virtual void uiSetCustomData(const char* const key, const char* const value)
  209. {
  210. CARLA_ASSERT(key != nullptr);
  211. CARLA_ASSERT(value != nullptr);
  212. return;
  213. // unused
  214. (void)key;
  215. (void)value;
  216. }
  217. // -------------------------------------------------------------------
  218. // Plugin state calls
  219. virtual char* getState()
  220. {
  221. return nullptr;
  222. }
  223. virtual void setState(const char* const data)
  224. {
  225. CARLA_ASSERT(data != nullptr);
  226. return;
  227. // unused
  228. (void)data;
  229. }
  230. // -------------------------------------------------------------------
  231. // Dispatcher
  232. virtual intptr_t dispatcher(int32_t opcode, int32_t index, intptr_t value, void* ptr)
  233. {
  234. return 0;
  235. // unused
  236. (void)opcode;
  237. (void)index;
  238. (void)value;
  239. (void)ptr;
  240. }
  241. // -------------------------------------------------------------------
  242. private:
  243. const HostDescriptor* const kHost;
  244. // -------------------------------------------------------------------
  245. #ifndef DOXYGEN
  246. public:
  247. #define handlePtr ((PluginDescriptorClass*)handle)
  248. static uint32_t _get_parameter_count(PluginHandle handle)
  249. {
  250. return handlePtr->getParameterCount();
  251. }
  252. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  253. {
  254. return handlePtr->getParameterInfo(index);
  255. }
  256. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  257. {
  258. return handlePtr->getParameterValue(index);
  259. }
  260. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  261. {
  262. return handlePtr->getParameterText(index);
  263. }
  264. static uint32_t _get_midi_program_count(PluginHandle handle)
  265. {
  266. return handlePtr->getMidiProgramCount();
  267. }
  268. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  269. {
  270. return handlePtr->getMidiProgramInfo(index);
  271. }
  272. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  273. {
  274. return handlePtr->setParameterValue(index, value);
  275. }
  276. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  277. {
  278. return handlePtr->setMidiProgram(bank, program);
  279. }
  280. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  281. {
  282. return handlePtr->setCustomData(key, value);
  283. }
  284. static void _ui_show(PluginHandle handle, bool show)
  285. {
  286. return handlePtr->uiShow(show);
  287. }
  288. static void _ui_idle(PluginHandle handle)
  289. {
  290. return handlePtr->uiIdle();
  291. }
  292. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  293. {
  294. return handlePtr->uiSetParameterValue(index, value);
  295. }
  296. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  297. {
  298. return handlePtr->uiSetMidiProgram(bank, program);
  299. }
  300. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  301. {
  302. return handlePtr->uiSetCustomData(key, value);
  303. }
  304. static void _activate(PluginHandle handle)
  305. {
  306. handlePtr->activate();
  307. }
  308. static void _deactivate(PluginHandle handle)
  309. {
  310. handlePtr->deactivate();
  311. }
  312. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  313. {
  314. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  315. }
  316. static char* _get_state(PluginHandle handle)
  317. {
  318. return handlePtr->getState();
  319. }
  320. static void _set_state(PluginHandle handle, const char* data)
  321. {
  322. handlePtr->setState(data);
  323. }
  324. static intptr_t _dispatcher(PluginHandle handle, int32_t opcode, int32_t index, intptr_t value, void* ptr)
  325. {
  326. return handlePtr->dispatcher(opcode, index, value, ptr);
  327. }
  328. #undef handlePtr
  329. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDescriptorClass)
  330. #endif
  331. };
  332. /**@}*/
  333. // -----------------------------------------------------------------------
  334. #define PluginDescriptorClassEND(className) \
  335. public: \
  336. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  337. { \
  338. return new className(host); \
  339. } \
  340. static void _cleanup(PluginHandle handle) \
  341. { \
  342. delete (className*)handle; \
  343. }
  344. #define PluginDescriptorFILL(className) \
  345. className::_instantiate, \
  346. className::_cleanup, \
  347. className::_get_parameter_count, \
  348. className::_get_parameter_info, \
  349. className::_get_parameter_value, \
  350. className::_get_parameter_text, \
  351. className::_get_midi_program_count, \
  352. className::_get_midi_program_info, \
  353. className::_set_parameter_value, \
  354. className::_set_midi_program, \
  355. className::_set_custom_data, \
  356. className::_ui_show, \
  357. className::_ui_idle, \
  358. className::_ui_set_parameter_value, \
  359. className::_ui_set_midi_program, \
  360. className::_ui_set_custom_data, \
  361. className::_activate, \
  362. className::_deactivate, \
  363. className::_process, \
  364. className::_get_state, \
  365. className::_set_state, \
  366. className::_dispatcher
  367. #endif // __CARLA_NATIVE_HPP__