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-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. leakDetector_NativePluginClass()
  34. {
  35. CARLA_SAFE_ASSERT(host != nullptr);
  36. }
  37. virtual ~NativePluginClass() {}
  38. protected:
  39. // -------------------------------------------------------------------
  40. // Host calls
  41. const NativeHostDescriptor* getHostHandle() const noexcept
  42. {
  43. return pHost;
  44. }
  45. const char* getResourceDir() const noexcept
  46. {
  47. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  48. return pHost->resourceDir;
  49. }
  50. const char* getUiName() const noexcept
  51. {
  52. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  53. return pHost->uiName;
  54. }
  55. uintptr_t getUiParentId() const noexcept
  56. {
  57. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  58. return pHost->uiParentId;
  59. }
  60. uint32_t getBufferSize() const
  61. {
  62. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  63. return pHost->get_buffer_size(pHost->handle);
  64. }
  65. double getSampleRate() const
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0.0);
  68. return pHost->get_sample_rate(pHost->handle);
  69. }
  70. bool isOffline() const
  71. {
  72. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  73. return pHost->is_offline(pHost->handle);
  74. }
  75. const NativeTimeInfo* getTimeInfo() const
  76. {
  77. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  78. return pHost->get_time_info(pHost->handle);
  79. }
  80. void writeMidiEvent(const NativeMidiEvent* const event) const
  81. {
  82. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  83. pHost->write_midi_event(pHost->handle, event);
  84. }
  85. void uiParameterChanged(const uint32_t index, const float value) const
  86. {
  87. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  88. pHost->ui_parameter_changed(pHost->handle, index, value);
  89. }
  90. void uiMidiProgramChanged(const uint8_t channel, const uint32_t bank, const uint32_t program) const
  91. {
  92. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  93. pHost->ui_midi_program_changed(pHost->handle, channel, bank, program);
  94. }
  95. void uiCustomDataChanged(const char* const key, const char* const value) const
  96. {
  97. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  98. pHost->ui_custom_data_changed(pHost->handle, key, value);
  99. }
  100. void uiClosed() const
  101. {
  102. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  103. pHost->ui_closed(pHost->handle);
  104. }
  105. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter) const
  106. {
  107. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  108. return pHost->ui_open_file(pHost->handle, isDir, title, filter);
  109. }
  110. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter) const
  111. {
  112. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  113. return pHost->ui_save_file(pHost->handle, isDir, title, filter);
  114. }
  115. // -------------------------------------------------------------------
  116. // Host dispatcher calls
  117. void hostUpdateParameter(const int32_t index) const
  118. {
  119. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  120. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, index, 0, nullptr, 0.0f);
  121. }
  122. void hostUpdateAllParameters() const
  123. {
  124. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  125. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, -1, 0, nullptr, 0.0f);
  126. }
  127. void hostUpdateMidiProgram(const int32_t index, const intptr_t channel = 0) const
  128. {
  129. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  130. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, index, channel, nullptr, 0.0f);
  131. }
  132. void hostUpdateAllMidiPrograms(const intptr_t channel = 0) const
  133. {
  134. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  135. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, -1, channel, nullptr, 0.0f);
  136. }
  137. void hostReloadParameters() const
  138. {
  139. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  140. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_PARAMETERS, 0, 0, nullptr, 0.0f);
  141. }
  142. void hostReloadMidiPrograms() const
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  145. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS, 0, 0, nullptr, 0.0f);
  146. }
  147. void hostReloadAll() const
  148. {
  149. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  150. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  151. }
  152. void hostUiUnavailable() const
  153. {
  154. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  155. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  156. }
  157. void hostGiveIdle() const
  158. {
  159. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  160. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_HOST_IDLE, 0, 0, nullptr, 0.0f);
  161. }
  162. // -------------------------------------------------------------------
  163. // Plugin parameter calls
  164. virtual uint32_t getParameterCount() const
  165. {
  166. return 0;
  167. }
  168. virtual const NativeParameter* getParameterInfo(const uint32_t index) const
  169. {
  170. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  171. return nullptr;
  172. }
  173. virtual float getParameterValue(const uint32_t index) const
  174. {
  175. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), 0.0f);
  176. return 0.0f;
  177. }
  178. virtual const char* getParameterText(const uint32_t index /*, const float value*/) const
  179. {
  180. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  181. return nullptr;
  182. }
  183. // -------------------------------------------------------------------
  184. // Plugin midi-program calls
  185. virtual uint32_t getMidiProgramCount() const
  186. {
  187. return 0;
  188. }
  189. virtual const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(), nullptr);
  192. return nullptr;
  193. }
  194. // -------------------------------------------------------------------
  195. // Plugin state calls
  196. virtual void setParameterValue(const uint32_t index, const float value)
  197. {
  198. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  199. return;
  200. // unused
  201. (void)value;
  202. }
  203. virtual void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  204. {
  205. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  206. return;
  207. // unused
  208. (void)bank; (void)program;
  209. }
  210. virtual void setCustomData(const char* const key, const char* const value)
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  213. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  214. }
  215. // -------------------------------------------------------------------
  216. // Plugin process calls
  217. virtual void activate() {}
  218. virtual void deactivate() {}
  219. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, 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 const char* _get_parameter_text(NativePluginHandle handle, uint32_t index /*, float value*/)
  304. {
  305. return handlePtr->getParameterText(index /*, value*/);
  306. }
  307. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  308. {
  309. return handlePtr->getMidiProgramCount();
  310. }
  311. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  312. {
  313. return handlePtr->getMidiProgramInfo(index);
  314. }
  315. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  316. {
  317. handlePtr->setParameterValue(index, value);
  318. }
  319. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  320. {
  321. handlePtr->setMidiProgram(channel, bank, program);
  322. }
  323. static void _set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  324. {
  325. handlePtr->setCustomData(key, value);
  326. }
  327. static void _ui_show(NativePluginHandle handle, bool show)
  328. {
  329. handlePtr->uiShow(show);
  330. }
  331. static void _ui_idle(NativePluginHandle handle)
  332. {
  333. handlePtr->uiIdle();
  334. }
  335. static void _ui_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  336. {
  337. handlePtr->uiSetParameterValue(index, value);
  338. }
  339. static void _ui_set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  340. {
  341. handlePtr->uiSetMidiProgram(channel, bank, program);
  342. }
  343. static void _ui_set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  344. {
  345. handlePtr->uiSetCustomData(key, value);
  346. }
  347. static void _activate(NativePluginHandle handle)
  348. {
  349. handlePtr->activate();
  350. }
  351. static void _deactivate(NativePluginHandle handle)
  352. {
  353. handlePtr->deactivate();
  354. }
  355. static void _process(NativePluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  356. {
  357. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  358. }
  359. static char* _get_state(NativePluginHandle handle)
  360. {
  361. return handlePtr->getState();
  362. }
  363. static void _set_state(NativePluginHandle handle, const char* data)
  364. {
  365. handlePtr->setState(data);
  366. }
  367. static intptr_t _dispatcher(NativePluginHandle handle, NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  368. {
  369. switch(opcode)
  370. {
  371. case NATIVE_PLUGIN_OPCODE_NULL:
  372. return 0;
  373. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  374. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  375. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  376. return 0;
  377. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  378. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  379. handlePtr->sampleRateChanged(static_cast<double>(opt));
  380. return 0;
  381. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  382. handlePtr->offlineChanged(value != 0);
  383. return 0;
  384. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  385. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  386. handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  387. return 0;
  388. }
  389. return 0;
  390. // unused
  391. (void)index;
  392. }
  393. #undef handlePtr
  394. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginClass)
  395. #endif
  396. };
  397. /**@}*/
  398. // -----------------------------------------------------------------------
  399. #define PluginClassEND(ClassName) \
  400. public: \
  401. static NativePluginHandle _instantiate(const NativeHostDescriptor* host) \
  402. { \
  403. return (host != nullptr) ? new ClassName(host) : nullptr; \
  404. } \
  405. static void _cleanup(NativePluginHandle handle) \
  406. { \
  407. delete (ClassName*)handle; \
  408. }
  409. #define PluginDescriptorFILL(ClassName) \
  410. ClassName::_instantiate, \
  411. ClassName::_cleanup, \
  412. ClassName::_get_parameter_count, \
  413. ClassName::_get_parameter_info, \
  414. ClassName::_get_parameter_value, \
  415. ClassName::_get_parameter_text, \
  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