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.

CarlaNative.hpp 13KB

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