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.

548 lines
15KB

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