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.

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