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.

462 lines
13KB

  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