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.

452 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. // -------------------------------------------------------------------
  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. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter)
  94. {
  95. CARLA_ASSERT(kHost != nullptr);
  96. if (kHost != nullptr)
  97. return kHost->ui_open_file(kHost->handle, isDir, title, filter);
  98. return nullptr;
  99. }
  100. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter)
  101. {
  102. CARLA_ASSERT(kHost != nullptr);
  103. if (kHost != nullptr)
  104. return kHost->ui_save_file(kHost->handle, isDir, title, filter);
  105. return nullptr;
  106. }
  107. protected:
  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 chunk calls
  219. virtual size_t getChunk(void** const data)
  220. {
  221. CARLA_ASSERT(data != nullptr);
  222. return 0;
  223. // unused
  224. (void)data;
  225. }
  226. virtual void setChunk(void* const data, const size_t size)
  227. {
  228. CARLA_ASSERT(data != nullptr);
  229. CARLA_ASSERT(size > 0);
  230. }
  231. // -------------------------------------------------------------------
  232. private:
  233. const HostDescriptor* const kHost;
  234. // -------------------------------------------------------------------
  235. #ifndef DOXYGEN
  236. public:
  237. #define handlePtr ((PluginDescriptorClass*)handle)
  238. static uint32_t _get_parameter_count(PluginHandle handle)
  239. {
  240. return handlePtr->getParameterCount();
  241. }
  242. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  243. {
  244. return handlePtr->getParameterInfo(index);
  245. }
  246. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  247. {
  248. return handlePtr->getParameterValue(index);
  249. }
  250. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  251. {
  252. return handlePtr->getParameterText(index);
  253. }
  254. static uint32_t _get_midi_program_count(PluginHandle handle)
  255. {
  256. return handlePtr->getMidiProgramCount();
  257. }
  258. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  259. {
  260. return handlePtr->getMidiProgramInfo(index);
  261. }
  262. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  263. {
  264. return handlePtr->setParameterValue(index, value);
  265. }
  266. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  267. {
  268. return handlePtr->setMidiProgram(bank, program);
  269. }
  270. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  271. {
  272. return handlePtr->setCustomData(key, value);
  273. }
  274. static void _ui_show(PluginHandle handle, bool show)
  275. {
  276. return handlePtr->uiShow(show);
  277. }
  278. static void _ui_idle(PluginHandle handle)
  279. {
  280. return handlePtr->uiIdle();
  281. }
  282. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  283. {
  284. return handlePtr->uiSetParameterValue(index, value);
  285. }
  286. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  287. {
  288. return handlePtr->uiSetMidiProgram(bank, program);
  289. }
  290. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  291. {
  292. return handlePtr->uiSetCustomData(key, value);
  293. }
  294. static void _activate(PluginHandle handle)
  295. {
  296. handlePtr->activate();
  297. }
  298. static void _deactivate(PluginHandle handle)
  299. {
  300. handlePtr->deactivate();
  301. }
  302. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  303. {
  304. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  305. }
  306. static size_t _get_chunk(PluginHandle handle, void** data)
  307. {
  308. return handlePtr->getChunk(data);
  309. }
  310. static void _set_chunk(PluginHandle handle, void* data, size_t size)
  311. {
  312. handlePtr->setChunk(data, size);
  313. }
  314. #undef handlePtr
  315. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginDescriptorClass)
  316. #endif
  317. };
  318. /**@}*/
  319. // -----------------------------------------------------------------------
  320. #define PluginDescriptorClassEND(className) \
  321. public: \
  322. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  323. { \
  324. return new className(host); \
  325. } \
  326. static void _cleanup(PluginHandle handle) \
  327. { \
  328. delete (className*)handle; \
  329. }
  330. #define PluginDescriptorFILL(className) \
  331. className::_instantiate, \
  332. className::_cleanup, \
  333. className::_get_parameter_count, \
  334. className::_get_parameter_info, \
  335. className::_get_parameter_value, \
  336. className::_get_parameter_text, \
  337. className::_get_midi_program_count, \
  338. className::_get_midi_program_info, \
  339. className::_set_parameter_value, \
  340. className::_set_midi_program, \
  341. className::_set_custom_data, \
  342. className::_ui_show, \
  343. className::_ui_idle, \
  344. className::_ui_set_parameter_value, \
  345. className::_ui_set_midi_program, \
  346. className::_ui_set_custom_data, \
  347. className::_activate, \
  348. className::_deactivate, \
  349. className::_process, \
  350. className::_get_chunk, \
  351. className::_set_chunk
  352. #endif // __CARLA_NATIVE_HPP__