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.

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