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.

578 lines
16KB

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