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.

554 lines
16KB

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