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.

451 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. 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. private:
  232. const HostDescriptor* const kHost;
  233. // -------------------------------------------------------------------
  234. #ifndef DOXYGEN
  235. public:
  236. #define handlePtr ((PluginDescriptorClass*)handle)
  237. static uint32_t _get_parameter_count(PluginHandle handle)
  238. {
  239. return handlePtr->getParameterCount();
  240. }
  241. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  242. {
  243. return handlePtr->getParameterInfo(index);
  244. }
  245. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  246. {
  247. return handlePtr->getParameterValue(index);
  248. }
  249. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  250. {
  251. return handlePtr->getParameterText(index);
  252. }
  253. static uint32_t _get_midi_program_count(PluginHandle handle)
  254. {
  255. return handlePtr->getMidiProgramCount();
  256. }
  257. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  258. {
  259. return handlePtr->getMidiProgramInfo(index);
  260. }
  261. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  262. {
  263. return handlePtr->setParameterValue(index, value);
  264. }
  265. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  266. {
  267. return handlePtr->setMidiProgram(bank, program);
  268. }
  269. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  270. {
  271. return handlePtr->setCustomData(key, value);
  272. }
  273. static void _ui_show(PluginHandle handle, bool show)
  274. {
  275. return handlePtr->uiShow(show);
  276. }
  277. static void _ui_idle(PluginHandle handle)
  278. {
  279. return handlePtr->uiIdle();
  280. }
  281. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  282. {
  283. return handlePtr->uiSetParameterValue(index, value);
  284. }
  285. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  286. {
  287. return handlePtr->uiSetMidiProgram(bank, program);
  288. }
  289. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  290. {
  291. return handlePtr->uiSetCustomData(key, value);
  292. }
  293. static void _activate(PluginHandle handle)
  294. {
  295. handlePtr->activate();
  296. }
  297. static void _deactivate(PluginHandle handle)
  298. {
  299. handlePtr->deactivate();
  300. }
  301. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  302. {
  303. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  304. }
  305. static char* _get_state(PluginHandle handle)
  306. {
  307. return handlePtr->getState();
  308. }
  309. static void _set_state(PluginHandle handle, const char* data)
  310. {
  311. handlePtr->setState(data);
  312. }
  313. #undef handlePtr
  314. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDescriptorClass)
  315. #endif
  316. };
  317. /**@}*/
  318. // -----------------------------------------------------------------------
  319. #define PluginDescriptorClassEND(className) \
  320. public: \
  321. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  322. { \
  323. return new className(host); \
  324. } \
  325. static void _cleanup(PluginHandle handle) \
  326. { \
  327. delete (className*)handle; \
  328. }
  329. #define PluginDescriptorFILL(className) \
  330. className::_instantiate, \
  331. className::_cleanup, \
  332. className::_get_parameter_count, \
  333. className::_get_parameter_info, \
  334. className::_get_parameter_value, \
  335. className::_get_parameter_text, \
  336. className::_get_midi_program_count, \
  337. className::_get_midi_program_info, \
  338. className::_set_parameter_value, \
  339. className::_set_midi_program, \
  340. className::_set_custom_data, \
  341. className::_ui_show, \
  342. className::_ui_idle, \
  343. className::_ui_set_parameter_value, \
  344. className::_ui_set_midi_program, \
  345. className::_ui_set_custom_data, \
  346. className::_activate, \
  347. className::_deactivate, \
  348. className::_process, \
  349. className::_get_state, \
  350. className::_set_state
  351. #endif // __CARLA_NATIVE_HPP__