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.

561 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->ui_parameter_touch(pHost->handle, index, touch);
  93. }
  94. void uiMidiProgramChanged(const uint8_t channel, const uint32_t bank, const uint32_t program) const
  95. {
  96. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  97. pHost->ui_midi_program_changed(pHost->handle, channel, bank, program);
  98. }
  99. void uiCustomDataChanged(const char* const key, const char* const value) const
  100. {
  101. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  102. pHost->ui_custom_data_changed(pHost->handle, key, value);
  103. }
  104. void uiClosed() const
  105. {
  106. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  107. pHost->ui_closed(pHost->handle);
  108. }
  109. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter) const
  110. {
  111. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  112. return pHost->ui_open_file(pHost->handle, isDir, title, filter);
  113. }
  114. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter) const
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  117. return pHost->ui_save_file(pHost->handle, isDir, title, filter);
  118. }
  119. // -------------------------------------------------------------------
  120. // Host dispatcher calls
  121. void hostUpdateParameter(const int32_t index) const
  122. {
  123. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  124. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, index, 0, nullptr, 0.0f);
  125. }
  126. void hostUpdateAllParameters() const
  127. {
  128. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  129. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, -1, 0, nullptr, 0.0f);
  130. }
  131. void hostUpdateMidiProgram(const int32_t index, const intptr_t channel = 0) const
  132. {
  133. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  134. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, index, channel, nullptr, 0.0f);
  135. }
  136. void hostUpdateAllMidiPrograms(const intptr_t channel = 0) const
  137. {
  138. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  139. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, -1, channel, nullptr, 0.0f);
  140. }
  141. void hostReloadParameters() const
  142. {
  143. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  144. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_PARAMETERS, 0, 0, nullptr, 0.0f);
  145. }
  146. void hostReloadMidiPrograms() const
  147. {
  148. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  149. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS, 0, 0, nullptr, 0.0f);
  150. }
  151. void hostReloadAll() const
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  154. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  155. }
  156. void hostUiUnavailable() const
  157. {
  158. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  159. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  160. }
  161. void hostGiveIdle() const
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  164. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_HOST_IDLE, 0, 0, nullptr, 0.0f);
  165. }
  166. // -------------------------------------------------------------------
  167. // Plugin parameter calls
  168. virtual uint32_t getParameterCount() const
  169. {
  170. return 0;
  171. }
  172. virtual const NativeParameter* getParameterInfo(const uint32_t index) const
  173. {
  174. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  175. return nullptr;
  176. }
  177. virtual float getParameterValue(const uint32_t index) const
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), 0.0f);
  180. return 0.0f;
  181. }
  182. // -------------------------------------------------------------------
  183. // Plugin midi-program calls
  184. virtual uint32_t getMidiProgramCount() const
  185. {
  186. return 0;
  187. }
  188. virtual const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  189. {
  190. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(), nullptr);
  191. return nullptr;
  192. }
  193. // -------------------------------------------------------------------
  194. // Plugin state calls
  195. virtual void setParameterValue(const uint32_t index, const float value)
  196. {
  197. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  198. return;
  199. // unused
  200. (void)value;
  201. }
  202. virtual void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  205. return;
  206. // unused
  207. (void)bank; (void)program;
  208. }
  209. virtual void setCustomData(const char* const key, const char* const value)
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  212. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  213. }
  214. // -------------------------------------------------------------------
  215. // Plugin process calls
  216. virtual void activate() {}
  217. virtual void deactivate() {}
  218. virtual void process(const float** const inBuffer, float** const outBuffer, const uint32_t frames,
  219. const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) = 0;
  220. // -------------------------------------------------------------------
  221. // Plugin UI calls
  222. virtual void uiShow(const bool show)
  223. {
  224. return;
  225. // unused
  226. (void)show;
  227. }
  228. virtual void uiIdle()
  229. {
  230. }
  231. virtual void uiSetParameterValue(const uint32_t index, const float value)
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  234. return;
  235. // unused
  236. (void)value;
  237. }
  238. virtual void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  241. return;
  242. // unused
  243. (void)bank; (void)program;
  244. }
  245. virtual void uiSetCustomData(const char* const key, const char* const value)
  246. {
  247. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  248. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  249. }
  250. // -------------------------------------------------------------------
  251. // Plugin state calls
  252. virtual char* getState() const
  253. {
  254. return nullptr;
  255. }
  256. virtual void setState(const char* const data)
  257. {
  258. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  259. }
  260. // -------------------------------------------------------------------
  261. // Plugin dispatcher calls
  262. virtual void bufferSizeChanged(const uint32_t bufferSize)
  263. {
  264. return;
  265. // unused
  266. (void)bufferSize;
  267. }
  268. virtual void sampleRateChanged(const double sampleRate)
  269. {
  270. return;
  271. // unused
  272. (void)sampleRate;
  273. }
  274. virtual void offlineChanged(const bool offline)
  275. {
  276. return;
  277. // unused
  278. (void)offline;
  279. }
  280. virtual void uiNameChanged(const char* const uiName)
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  283. }
  284. // -------------------------------------------------------------------
  285. private:
  286. const NativeHostDescriptor* const pHost;
  287. // -------------------------------------------------------------------
  288. #ifndef DOXYGEN
  289. public:
  290. #define handlePtr ((NativePluginClass*)handle)
  291. static uint32_t _get_parameter_count(NativePluginHandle handle)
  292. {
  293. return handlePtr->getParameterCount();
  294. }
  295. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  296. {
  297. return handlePtr->getParameterInfo(index);
  298. }
  299. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  300. {
  301. return handlePtr->getParameterValue(index);
  302. }
  303. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  304. {
  305. return handlePtr->getMidiProgramCount();
  306. }
  307. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  308. {
  309. return handlePtr->getMidiProgramInfo(index);
  310. }
  311. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  312. {
  313. handlePtr->setParameterValue(index, value);
  314. }
  315. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  316. {
  317. handlePtr->setMidiProgram(channel, bank, program);
  318. }
  319. static void _set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  320. {
  321. handlePtr->setCustomData(key, value);
  322. }
  323. static void _ui_show(NativePluginHandle handle, bool show)
  324. {
  325. handlePtr->uiShow(show);
  326. }
  327. static void _ui_idle(NativePluginHandle handle)
  328. {
  329. handlePtr->uiIdle();
  330. }
  331. static void _ui_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  332. {
  333. handlePtr->uiSetParameterValue(index, value);
  334. }
  335. static void _ui_set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  336. {
  337. handlePtr->uiSetMidiProgram(channel, bank, program);
  338. }
  339. static void _ui_set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  340. {
  341. handlePtr->uiSetCustomData(key, value);
  342. }
  343. static void _activate(NativePluginHandle handle)
  344. {
  345. handlePtr->activate();
  346. }
  347. static void _deactivate(NativePluginHandle handle)
  348. {
  349. handlePtr->deactivate();
  350. }
  351. static void _process(NativePluginHandle handle,
  352. const float** inBuffer, float** outBuffer, const uint32_t frames,
  353. const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  354. {
  355. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  356. }
  357. static char* _get_state(NativePluginHandle handle)
  358. {
  359. return handlePtr->getState();
  360. }
  361. static void _set_state(NativePluginHandle handle, const char* data)
  362. {
  363. handlePtr->setState(data);
  364. }
  365. static intptr_t _dispatcher(NativePluginHandle handle,
  366. NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  367. {
  368. switch(opcode)
  369. {
  370. case NATIVE_PLUGIN_OPCODE_NULL:
  371. return 0;
  372. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  373. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  374. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  375. return 0;
  376. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  377. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  378. handlePtr->sampleRateChanged(static_cast<double>(opt));
  379. return 0;
  380. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  381. handlePtr->offlineChanged(value != 0);
  382. return 0;
  383. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  384. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  385. handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  386. return 0;
  387. case NATIVE_PLUGIN_OPCODE_GET_INTERNAL_HANDLE:
  388. return 0;
  389. }
  390. return 0;
  391. // unused
  392. (void)index;
  393. }
  394. #undef handlePtr
  395. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginClass)
  396. #endif
  397. };
  398. /**@}*/
  399. // -----------------------------------------------------------------------
  400. #define PluginClassEND(ClassName) \
  401. public: \
  402. static NativePluginHandle _instantiate(const NativeHostDescriptor* host) \
  403. { \
  404. return (host != nullptr) ? new ClassName(host) : nullptr; \
  405. } \
  406. static void _cleanup(NativePluginHandle handle) \
  407. { \
  408. delete (ClassName*)handle; \
  409. }
  410. #define PluginDescriptorFILL(ClassName) \
  411. ClassName::_instantiate, \
  412. ClassName::_cleanup, \
  413. ClassName::_get_parameter_count, \
  414. ClassName::_get_parameter_info, \
  415. ClassName::_get_parameter_value, \
  416. ClassName::_get_midi_program_count, \
  417. ClassName::_get_midi_program_info, \
  418. ClassName::_set_parameter_value, \
  419. ClassName::_set_midi_program, \
  420. ClassName::_set_custom_data, \
  421. ClassName::_ui_show, \
  422. ClassName::_ui_idle, \
  423. ClassName::_ui_set_parameter_value, \
  424. ClassName::_ui_set_midi_program, \
  425. ClassName::_ui_set_custom_data, \
  426. ClassName::_activate, \
  427. ClassName::_deactivate, \
  428. ClassName::_process, \
  429. ClassName::_get_state, \
  430. ClassName::_set_state, \
  431. ClassName::_dispatcher
  432. // -----------------------------------------------------------------------
  433. #endif // CARLA_NATIVE_HPP_INCLUDED