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.

603 lines
17KB

  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2019 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. // Native Plugin Class
  28. class NativePluginClass
  29. {
  30. public:
  31. NativePluginClass(const NativeHostDescriptor* const host)
  32. : pHost(host)
  33. {
  34. CARLA_SAFE_ASSERT(host != nullptr);
  35. }
  36. virtual ~NativePluginClass() {}
  37. protected:
  38. // -------------------------------------------------------------------
  39. // Host calls
  40. const NativeHostDescriptor* getHostHandle() const noexcept
  41. {
  42. return pHost;
  43. }
  44. const char* getResourceDir() const noexcept
  45. {
  46. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  47. return pHost->resourceDir;
  48. }
  49. const char* getUiName() const noexcept
  50. {
  51. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  52. return pHost->uiName;
  53. }
  54. uintptr_t getUiParentId() const noexcept
  55. {
  56. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  57. return pHost->uiParentId;
  58. }
  59. uint32_t getBufferSize() const
  60. {
  61. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  62. return pHost->get_buffer_size(pHost->handle);
  63. }
  64. double getSampleRate() const
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0.0);
  67. return pHost->get_sample_rate(pHost->handle);
  68. }
  69. bool isOffline() const
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  72. return pHost->is_offline(pHost->handle);
  73. }
  74. const NativeTimeInfo* getTimeInfo() const
  75. {
  76. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  77. return pHost->get_time_info(pHost->handle);
  78. }
  79. void writeMidiEvent(const NativeMidiEvent* const event) const
  80. {
  81. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  82. pHost->write_midi_event(pHost->handle, event);
  83. }
  84. void uiParameterChanged(const uint32_t index, const float value) const
  85. {
  86. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  87. pHost->ui_parameter_changed(pHost->handle, index, value);
  88. }
  89. void uiParameterTouch(const uint32_t index, const bool touch) const
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  92. pHost->dispatcher(pHost->handle,
  93. NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER,
  94. static_cast<int32_t>(index),
  95. touch ? 1 : 0,
  96. nullptr, 0.0f);
  97. }
  98. void uiMidiProgramChanged(const uint8_t channel, const uint32_t bank, const uint32_t program) const
  99. {
  100. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  101. pHost->ui_midi_program_changed(pHost->handle, channel, bank, program);
  102. }
  103. void uiCustomDataChanged(const char* const key, const char* const value) const
  104. {
  105. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  106. pHost->ui_custom_data_changed(pHost->handle, key, value);
  107. }
  108. void uiClosed() const
  109. {
  110. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  111. pHost->ui_closed(pHost->handle);
  112. }
  113. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter) const
  114. {
  115. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  116. return pHost->ui_open_file(pHost->handle, isDir, title, filter);
  117. }
  118. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter) const
  119. {
  120. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  121. return pHost->ui_save_file(pHost->handle, isDir, title, filter);
  122. }
  123. // -------------------------------------------------------------------
  124. // Host dispatcher calls
  125. void hostUpdateParameter(const int32_t index) const
  126. {
  127. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  128. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, index, 0, nullptr, 0.0f);
  129. }
  130. void hostUpdateAllParameters() const
  131. {
  132. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  133. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, -1, 0, nullptr, 0.0f);
  134. }
  135. void hostUpdateMidiProgram(const int32_t index, const intptr_t channel = 0) const
  136. {
  137. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  138. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, index, channel, nullptr, 0.0f);
  139. }
  140. void hostUpdateAllMidiPrograms(const intptr_t channel = 0) const
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  143. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, -1, channel, nullptr, 0.0f);
  144. }
  145. void hostReloadParameters() const
  146. {
  147. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  148. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_PARAMETERS, 0, 0, nullptr, 0.0f);
  149. }
  150. void hostReloadMidiPrograms() const
  151. {
  152. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  153. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS, 0, 0, nullptr, 0.0f);
  154. }
  155. void hostReloadAll() const
  156. {
  157. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  158. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  159. }
  160. void hostUiUnavailable() const
  161. {
  162. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  163. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  164. }
  165. void hostGiveIdle() const
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  168. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_HOST_IDLE, 0, 0, nullptr, 0.0f);
  169. }
  170. void hostQueueDrawInlineDisplay()
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  173. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY, 0, 0, nullptr, 0.0f);
  174. }
  175. // -------------------------------------------------------------------
  176. // Plugin parameter calls
  177. virtual uint32_t getParameterCount() const
  178. {
  179. return 0;
  180. }
  181. virtual const NativeParameter* getParameterInfo(const uint32_t index) const
  182. {
  183. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  184. return nullptr;
  185. }
  186. virtual float getParameterValue(const uint32_t index) const
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), 0.0f);
  189. return 0.0f;
  190. }
  191. // -------------------------------------------------------------------
  192. // Plugin midi-program calls
  193. virtual uint32_t getMidiProgramCount() const
  194. {
  195. return 0;
  196. }
  197. virtual const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  198. {
  199. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(), nullptr);
  200. return nullptr;
  201. }
  202. // -------------------------------------------------------------------
  203. // Plugin state calls
  204. virtual void setParameterValue(const uint32_t index, const float value)
  205. {
  206. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  207. return;
  208. // unused
  209. (void)value;
  210. }
  211. virtual void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  214. return;
  215. // unused
  216. (void)bank; (void)program;
  217. }
  218. virtual void setCustomData(const char* const key, const char* const value)
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  221. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  222. }
  223. // -------------------------------------------------------------------
  224. // Plugin process calls
  225. virtual void activate() {}
  226. virtual void deactivate() {}
  227. virtual void process(const float** const inBuffer, float** const outBuffer, const uint32_t frames,
  228. const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) = 0;
  229. // -------------------------------------------------------------------
  230. // Plugin UI calls
  231. virtual void uiShow(const bool show)
  232. {
  233. return;
  234. // unused
  235. (void)show;
  236. }
  237. virtual void uiIdle()
  238. {
  239. }
  240. virtual void uiSetParameterValue(const uint32_t index, const float value)
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  243. return;
  244. // unused
  245. (void)value;
  246. }
  247. virtual void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  248. {
  249. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  250. return;
  251. // unused
  252. (void)bank; (void)program;
  253. }
  254. virtual void uiSetCustomData(const char* const key, const char* const value)
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  257. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  258. }
  259. // -------------------------------------------------------------------
  260. // Plugin state calls
  261. virtual char* getState() const
  262. {
  263. return nullptr;
  264. }
  265. virtual void setState(const char* const data)
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  268. }
  269. // -------------------------------------------------------------------
  270. // Plugin dispatcher calls
  271. virtual void bufferSizeChanged(const uint32_t bufferSize)
  272. {
  273. return;
  274. // unused
  275. (void)bufferSize;
  276. }
  277. virtual void sampleRateChanged(const double sampleRate)
  278. {
  279. return;
  280. // unused
  281. (void)sampleRate;
  282. }
  283. virtual void offlineChanged(const bool offline)
  284. {
  285. return;
  286. // unused
  287. (void)offline;
  288. }
  289. virtual void uiNameChanged(const char* const uiName)
  290. {
  291. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  292. }
  293. virtual const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(width > 0 && height > 0, nullptr);
  296. return nullptr;
  297. }
  298. // -------------------------------------------------------------------
  299. private:
  300. const NativeHostDescriptor* const pHost;
  301. // -------------------------------------------------------------------
  302. #ifndef DOXYGEN
  303. public:
  304. #define handlePtr ((NativePluginClass*)handle)
  305. static uint32_t _get_parameter_count(NativePluginHandle handle)
  306. {
  307. return handlePtr->getParameterCount();
  308. }
  309. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  310. {
  311. return handlePtr->getParameterInfo(index);
  312. }
  313. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  314. {
  315. return handlePtr->getParameterValue(index);
  316. }
  317. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  318. {
  319. return handlePtr->getMidiProgramCount();
  320. }
  321. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  322. {
  323. return handlePtr->getMidiProgramInfo(index);
  324. }
  325. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  326. {
  327. handlePtr->setParameterValue(index, value);
  328. }
  329. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  330. {
  331. handlePtr->setMidiProgram(channel, bank, program);
  332. }
  333. static void _set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  334. {
  335. handlePtr->setCustomData(key, value);
  336. }
  337. static void _ui_show(NativePluginHandle handle, bool show)
  338. {
  339. handlePtr->uiShow(show);
  340. }
  341. static void _ui_idle(NativePluginHandle handle)
  342. {
  343. handlePtr->uiIdle();
  344. }
  345. static void _ui_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  346. {
  347. handlePtr->uiSetParameterValue(index, value);
  348. }
  349. static void _ui_set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  350. {
  351. handlePtr->uiSetMidiProgram(channel, bank, program);
  352. }
  353. static void _ui_set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  354. {
  355. handlePtr->uiSetCustomData(key, value);
  356. }
  357. static void _activate(NativePluginHandle handle)
  358. {
  359. handlePtr->activate();
  360. }
  361. static void _deactivate(NativePluginHandle handle)
  362. {
  363. handlePtr->deactivate();
  364. }
  365. static void _process(NativePluginHandle handle,
  366. const float** inBuffer, float** outBuffer, const uint32_t frames,
  367. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  368. {
  369. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  370. }
  371. static char* _get_state(NativePluginHandle handle)
  372. {
  373. return handlePtr->getState();
  374. }
  375. static void _set_state(NativePluginHandle handle, const char* data)
  376. {
  377. handlePtr->setState(data);
  378. }
  379. static intptr_t _dispatcher(NativePluginHandle handle,
  380. NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  381. {
  382. switch(opcode)
  383. {
  384. case NATIVE_PLUGIN_OPCODE_NULL:
  385. return 0;
  386. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  387. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  388. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  389. return 0;
  390. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  391. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  392. handlePtr->sampleRateChanged(static_cast<double>(opt));
  393. return 0;
  394. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  395. handlePtr->offlineChanged(value != 0);
  396. return 0;
  397. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  398. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  399. handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  400. return 0;
  401. case NATIVE_PLUGIN_OPCODE_GET_INTERNAL_HANDLE:
  402. return 0;
  403. }
  404. return 0;
  405. // unused
  406. (void)index;
  407. }
  408. static const NativeInlineDisplayImageSurface* _render_inline_display(NativePluginHandle handle, uint32_t width, uint32_t height)
  409. {
  410. return handlePtr->renderInlineDisplay(width, height);
  411. }
  412. #undef handlePtr
  413. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginClass)
  414. #endif
  415. };
  416. /**@}*/
  417. // ---------------------------------------------------------------------------------------------------------------------
  418. // -Weffc++ compat ext widget
  419. extern "C" {
  420. typedef struct _NativeInlineDisplayImageSurfaceCompat {
  421. unsigned char* data;
  422. int width, height, stride;
  423. size_t dataSize;
  424. _NativeInlineDisplayImageSurfaceCompat() noexcept
  425. : data(nullptr), width(0), height(0), stride(0), dataSize(0) {}
  426. } NativeInlineDisplayImageSurfaceCompat;
  427. }
  428. // -----------------------------------------------------------------------
  429. #define PluginClassEND(ClassName) \
  430. public: \
  431. static NativePluginHandle _instantiate(const NativeHostDescriptor* host) \
  432. { \
  433. return (host != nullptr) ? new ClassName(host) : nullptr; \
  434. } \
  435. static void _cleanup(NativePluginHandle handle) \
  436. { \
  437. delete (ClassName*)handle; \
  438. }
  439. #define PluginDescriptorFILL(ClassName) \
  440. ClassName::_instantiate, \
  441. ClassName::_cleanup, \
  442. ClassName::_get_parameter_count, \
  443. ClassName::_get_parameter_info, \
  444. ClassName::_get_parameter_value, \
  445. ClassName::_get_midi_program_count, \
  446. ClassName::_get_midi_program_info, \
  447. ClassName::_set_parameter_value, \
  448. ClassName::_set_midi_program, \
  449. ClassName::_set_custom_data, \
  450. ClassName::_ui_show, \
  451. ClassName::_ui_idle, \
  452. ClassName::_ui_set_parameter_value, \
  453. ClassName::_ui_set_midi_program, \
  454. ClassName::_ui_set_custom_data, \
  455. ClassName::_activate, \
  456. ClassName::_deactivate, \
  457. ClassName::_process, \
  458. ClassName::_get_state, \
  459. ClassName::_set_state, \
  460. ClassName::_dispatcher, \
  461. ClassName::_render_inline_display, \
  462. 0, 0
  463. // -----------------------------------------------------------------------
  464. #endif // CARLA_NATIVE_HPP_INCLUDED