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.

641 lines
19KB

  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. bool hostRequestIdle() const
  171. {
  172. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  173. return pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_REQUEST_IDLE, 0, 0, nullptr, 0.0f) == 1;
  174. }
  175. bool hostQueueDrawInlineDisplay()
  176. {
  177. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  178. return pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY, 0, 0, nullptr, 0.0f) == 1;
  179. }
  180. const char* hostGetFilePath(const char* const filetype) const
  181. {
  182. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  183. return (const char*)(uintptr_t)pHost->dispatcher(pHost->handle,
  184. NATIVE_HOST_OPCODE_GET_FILE_PATH,
  185. 0, 0,
  186. (void*)const_cast<char*>(filetype),
  187. 0.0f);
  188. }
  189. // -------------------------------------------------------------------
  190. // Plugin parameter calls
  191. virtual uint32_t getParameterCount() const
  192. {
  193. return 0;
  194. }
  195. virtual const NativeParameter* getParameterInfo(const uint32_t index) const
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  198. return nullptr;
  199. }
  200. virtual float getParameterValue(const uint32_t index) const
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), 0.0f);
  203. return 0.0f;
  204. }
  205. // -------------------------------------------------------------------
  206. // Plugin midi-program calls
  207. virtual uint32_t getMidiProgramCount() const
  208. {
  209. return 0;
  210. }
  211. virtual const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(), nullptr);
  214. return nullptr;
  215. }
  216. // -------------------------------------------------------------------
  217. // Plugin state calls
  218. virtual void setParameterValue(const uint32_t index, const float value)
  219. {
  220. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  221. return;
  222. // unused
  223. (void)value;
  224. }
  225. virtual void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  228. return;
  229. // unused
  230. (void)bank; (void)program;
  231. }
  232. virtual void setCustomData(const char* const key, const char* const value)
  233. {
  234. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  235. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  236. }
  237. // -------------------------------------------------------------------
  238. // Plugin process calls
  239. virtual void activate() {}
  240. virtual void deactivate() {}
  241. virtual void process(const float* const* inBuffer, float** outBuffer, uint32_t frames,
  242. const NativeMidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  243. // -------------------------------------------------------------------
  244. // Plugin UI calls
  245. virtual void uiShow(const bool show)
  246. {
  247. return;
  248. // unused
  249. (void)show;
  250. }
  251. virtual void uiIdle()
  252. {
  253. }
  254. virtual void uiSetParameterValue(const uint32_t index, const float value)
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  257. return;
  258. // unused
  259. (void)value;
  260. }
  261. virtual void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  264. return;
  265. // unused
  266. (void)bank; (void)program;
  267. }
  268. virtual void uiSetCustomData(const char* const key, const char* const value)
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  271. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  272. }
  273. // -------------------------------------------------------------------
  274. // Plugin state calls
  275. virtual char* getState() const
  276. {
  277. return nullptr;
  278. }
  279. virtual void setState(const char* const data)
  280. {
  281. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  282. }
  283. // -------------------------------------------------------------------
  284. // Plugin dispatcher calls
  285. virtual void bufferSizeChanged(const uint32_t bufferSize)
  286. {
  287. return;
  288. // unused
  289. (void)bufferSize;
  290. }
  291. virtual void sampleRateChanged(const double sampleRate)
  292. {
  293. return;
  294. // unused
  295. (void)sampleRate;
  296. }
  297. virtual void offlineChanged(const bool offline)
  298. {
  299. return;
  300. // unused
  301. (void)offline;
  302. }
  303. virtual void uiNameChanged(const char* const uiName)
  304. {
  305. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  306. }
  307. virtual bool uiMIDIEvent(uint8_t size, const uint8_t data[])
  308. {
  309. return false;
  310. // unused
  311. (void)size;
  312. (void)data;
  313. }
  314. virtual const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  315. {
  316. CARLA_SAFE_ASSERT_RETURN(width > 0 && height > 0, nullptr);
  317. return nullptr;
  318. }
  319. virtual void idle() {}
  320. // -------------------------------------------------------------------
  321. private:
  322. const NativeHostDescriptor* const pHost;
  323. // -------------------------------------------------------------------
  324. #ifndef DOXYGEN
  325. public:
  326. #define handlePtr ((NativePluginClass*)handle)
  327. static uint32_t _get_parameter_count(NativePluginHandle handle)
  328. {
  329. return handlePtr->getParameterCount();
  330. }
  331. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  332. {
  333. return handlePtr->getParameterInfo(index);
  334. }
  335. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  336. {
  337. return handlePtr->getParameterValue(index);
  338. }
  339. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  340. {
  341. return handlePtr->getMidiProgramCount();
  342. }
  343. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  344. {
  345. return handlePtr->getMidiProgramInfo(index);
  346. }
  347. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  348. {
  349. handlePtr->setParameterValue(index, value);
  350. }
  351. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  352. {
  353. handlePtr->setMidiProgram(channel, bank, program);
  354. }
  355. static void _set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  356. {
  357. handlePtr->setCustomData(key, value);
  358. }
  359. static void _ui_show(NativePluginHandle handle, bool show)
  360. {
  361. handlePtr->uiShow(show);
  362. }
  363. static void _ui_idle(NativePluginHandle handle)
  364. {
  365. handlePtr->uiIdle();
  366. }
  367. static void _ui_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  368. {
  369. handlePtr->uiSetParameterValue(index, value);
  370. }
  371. static void _ui_set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  372. {
  373. handlePtr->uiSetMidiProgram(channel, bank, program);
  374. }
  375. static void _ui_set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  376. {
  377. handlePtr->uiSetCustomData(key, value);
  378. }
  379. static void _activate(NativePluginHandle handle)
  380. {
  381. handlePtr->activate();
  382. }
  383. static void _deactivate(NativePluginHandle handle)
  384. {
  385. handlePtr->deactivate();
  386. }
  387. // FIXME for v3.0, use const for the input buffer
  388. static void _process(NativePluginHandle handle,
  389. float** inBuffer, float** outBuffer, const uint32_t frames,
  390. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  391. {
  392. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  393. }
  394. static char* _get_state(NativePluginHandle handle)
  395. {
  396. return handlePtr->getState();
  397. }
  398. static void _set_state(NativePluginHandle handle, const char* data)
  399. {
  400. handlePtr->setState(data);
  401. }
  402. static intptr_t _dispatcher(NativePluginHandle handle,
  403. NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  404. {
  405. switch(opcode)
  406. {
  407. case NATIVE_PLUGIN_OPCODE_NULL:
  408. return 0;
  409. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  410. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  411. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  412. return 0;
  413. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  414. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  415. handlePtr->sampleRateChanged(static_cast<double>(opt));
  416. return 0;
  417. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  418. handlePtr->offlineChanged(value != 0);
  419. return 0;
  420. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  421. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  422. handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  423. return 0;
  424. case NATIVE_PLUGIN_OPCODE_GET_INTERNAL_HANDLE:
  425. return 0;
  426. case NATIVE_PLUGIN_OPCODE_IDLE:
  427. handlePtr->idle();
  428. return 0;
  429. case NATIVE_PLUGIN_OPCODE_UI_MIDI_EVENT:
  430. CARLA_SAFE_ASSERT_RETURN(index >= 0 && index < UINT8_MAX, 0);
  431. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  432. return handlePtr->uiMIDIEvent(static_cast<uint8_t>(index),
  433. static_cast<uint8_t*>(ptr));
  434. }
  435. return 0;
  436. // unused
  437. (void)index;
  438. }
  439. static const NativeInlineDisplayImageSurface* _render_inline_display(NativePluginHandle handle, uint32_t width, uint32_t height)
  440. {
  441. return handlePtr->renderInlineDisplay(width, height);
  442. }
  443. #undef handlePtr
  444. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginClass)
  445. #endif
  446. };
  447. /**@}*/
  448. // --------------------------------------------------------------------------------------------------------------------
  449. // -Weffc++ compat ext widget
  450. extern "C" {
  451. typedef struct _NativeInlineDisplayImageSurfaceCompat {
  452. unsigned char* data;
  453. int width, height, stride;
  454. size_t dataSize;
  455. _NativeInlineDisplayImageSurfaceCompat() noexcept
  456. : data(nullptr), width(0), height(0), stride(0), dataSize(0) {}
  457. } NativeInlineDisplayImageSurfaceCompat;
  458. }
  459. // --------------------------------------------------------------------------------------------------------------------
  460. #define PluginClassEND(ClassName) \
  461. public: \
  462. static NativePluginHandle _instantiate(const NativeHostDescriptor* host) \
  463. { \
  464. return (host != nullptr) ? new ClassName(host) : nullptr; \
  465. } \
  466. static void _cleanup(NativePluginHandle handle) \
  467. { \
  468. delete (ClassName*)handle; \
  469. }
  470. #define PluginDescriptorFILL(ClassName) \
  471. ClassName::_instantiate, \
  472. ClassName::_cleanup, \
  473. ClassName::_get_parameter_count, \
  474. ClassName::_get_parameter_info, \
  475. ClassName::_get_parameter_value, \
  476. ClassName::_get_midi_program_count, \
  477. ClassName::_get_midi_program_info, \
  478. ClassName::_set_parameter_value, \
  479. ClassName::_set_midi_program, \
  480. ClassName::_set_custom_data, \
  481. ClassName::_ui_show, \
  482. ClassName::_ui_idle, \
  483. ClassName::_ui_set_parameter_value, \
  484. ClassName::_ui_set_midi_program, \
  485. ClassName::_ui_set_custom_data, \
  486. ClassName::_activate, \
  487. ClassName::_deactivate, \
  488. ClassName::_process, \
  489. ClassName::_get_state, \
  490. ClassName::_set_state, \
  491. ClassName::_dispatcher, \
  492. ClassName::_render_inline_display, \
  493. 0, 0, nullptr, nullptr, 0, 0
  494. // --------------------------------------------------------------------------------------------------------------------
  495. #endif // CARLA_NATIVE_HPP_INCLUDED