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.

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