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.

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