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.

565 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. {
  39. }
  40. protected:
  41. // -------------------------------------------------------------------
  42. // Host calls
  43. const NativeHostDescriptor* getHostHandle() const noexcept
  44. {
  45. return pHost;
  46. }
  47. const char* getResourceDir() const noexcept
  48. {
  49. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  50. return pHost->resourceDir;
  51. }
  52. const char* getUiName() const noexcept
  53. {
  54. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  55. return pHost->uiName;
  56. }
  57. uintptr_t getUiParentId() const noexcept
  58. {
  59. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  60. return pHost->uiParentId;
  61. }
  62. uint32_t getBufferSize() const
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  65. return pHost->get_buffer_size(pHost->handle);
  66. }
  67. double getSampleRate() const
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0.0);
  70. return pHost->get_sample_rate(pHost->handle);
  71. }
  72. bool isOffline() const
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  75. return pHost->is_offline(pHost->handle);
  76. }
  77. const NativeTimeInfo* getTimeInfo() const
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  80. return pHost->get_time_info(pHost->handle);
  81. }
  82. void writeMidiEvent(const NativeMidiEvent* const event) const
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  85. pHost->write_midi_event(pHost->handle, event);
  86. }
  87. void uiParameterChanged(const uint32_t index, const float value) const
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  90. pHost->ui_parameter_changed(pHost->handle, index, value);
  91. }
  92. void uiMidiProgramChanged(const uint8_t channel, const uint32_t bank, const uint32_t program) const
  93. {
  94. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  95. pHost->ui_midi_program_changed(pHost->handle, channel, bank, program);
  96. }
  97. void uiCustomDataChanged(const char* const key, const char* const value) const
  98. {
  99. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  100. pHost->ui_custom_data_changed(pHost->handle, key, value);
  101. }
  102. void uiClosed() const
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  105. pHost->ui_closed(pHost->handle);
  106. }
  107. const char* uiOpenFile(const bool isDir, const char* const title, const char* const filter) const
  108. {
  109. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  110. return pHost->ui_open_file(pHost->handle, isDir, title, filter);
  111. }
  112. const char* uiSaveFile(const bool isDir, const char* const title, const char* const filter) const
  113. {
  114. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  115. return pHost->ui_save_file(pHost->handle, isDir, title, filter);
  116. }
  117. // -------------------------------------------------------------------
  118. // Host dispatcher calls
  119. void hostUpdateParameter(const int32_t index) const
  120. {
  121. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  122. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, index, 0, nullptr, 0.0f);
  123. }
  124. void hostUpdateAllParameters() const
  125. {
  126. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  127. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_PARAMETER, -1, 0, nullptr, 0.0f);
  128. }
  129. void hostUpdateMidiProgram(const int32_t index, const intptr_t channel = 0) const
  130. {
  131. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  132. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, index, channel, nullptr, 0.0f);
  133. }
  134. void hostUpdateAllMidiPrograms(const intptr_t channel = 0) const
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  137. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM, -1, channel, nullptr, 0.0f);
  138. }
  139. void hostReloadParameters() const
  140. {
  141. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  142. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_PARAMETERS, 0, 0, nullptr, 0.0f);
  143. }
  144. void hostReloadMidiPrograms() const
  145. {
  146. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  147. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS, 0, 0, nullptr, 0.0f);
  148. }
  149. void hostReloadAll() const
  150. {
  151. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  152. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  153. }
  154. void hostUiUnavailable() const
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  157. pHost->dispatcher(pHost->handle, NATIVE_HOST_OPCODE_UI_UNAVAILABLE, 0, 0, nullptr, 0.0f);
  158. }
  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;
  206. (void)program;
  207. }
  208. virtual void setCustomData(const char* const key, const char* const value)
  209. {
  210. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  211. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  212. }
  213. // -------------------------------------------------------------------
  214. // Plugin process calls
  215. virtual void activate()
  216. {
  217. }
  218. virtual void deactivate()
  219. {
  220. }
  221. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) = 0;
  222. // -------------------------------------------------------------------
  223. // Plugin UI calls
  224. virtual void uiShow(const bool show)
  225. {
  226. return;
  227. // unused
  228. (void)show;
  229. }
  230. virtual void uiIdle()
  231. {
  232. }
  233. virtual void uiSetParameterValue(const uint32_t index, const float value)
  234. {
  235. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  236. return;
  237. // unused
  238. (void)value;
  239. }
  240. virtual void uiSetMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  243. return;
  244. // unused
  245. (void)bank;
  246. (void)program;
  247. }
  248. virtual void uiSetCustomData(const char* const key, const char* const value)
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(key != nullptr,);
  251. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  252. }
  253. // -------------------------------------------------------------------
  254. // Plugin state calls
  255. virtual char* getState() const
  256. {
  257. return nullptr;
  258. }
  259. virtual void setState(const char* const data)
  260. {
  261. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  262. }
  263. // -------------------------------------------------------------------
  264. // Plugin dispatcher calls
  265. virtual void bufferSizeChanged(const uint32_t bufferSize)
  266. {
  267. return;
  268. // unused
  269. (void)bufferSize;
  270. }
  271. virtual void sampleRateChanged(const double sampleRate)
  272. {
  273. return;
  274. // unused
  275. (void)sampleRate;
  276. }
  277. virtual void offlineChanged(const bool offline)
  278. {
  279. return;
  280. // unused
  281. (void)offline;
  282. }
  283. virtual void uiNameChanged(const char* const uiName)
  284. {
  285. return;
  286. // unused
  287. (void)uiName;
  288. }
  289. // -------------------------------------------------------------------
  290. private:
  291. const NativeHostDescriptor* const pHost;
  292. // -------------------------------------------------------------------
  293. #ifndef DOXYGEN
  294. public:
  295. #define handlePtr ((NativePluginClass*)handle)
  296. static uint32_t _get_parameter_count(NativePluginHandle handle)
  297. {
  298. return handlePtr->getParameterCount();
  299. }
  300. static const NativeParameter* _get_parameter_info(NativePluginHandle handle, uint32_t index)
  301. {
  302. return handlePtr->getParameterInfo(index);
  303. }
  304. static float _get_parameter_value(NativePluginHandle handle, uint32_t index)
  305. {
  306. return handlePtr->getParameterValue(index);
  307. }
  308. static const char* _get_parameter_text(NativePluginHandle handle, uint32_t index /*, float value*/)
  309. {
  310. return handlePtr->getParameterText(index /*, value*/);
  311. }
  312. static uint32_t _get_midi_program_count(NativePluginHandle handle)
  313. {
  314. return handlePtr->getMidiProgramCount();
  315. }
  316. static const NativeMidiProgram* _get_midi_program_info(NativePluginHandle handle, uint32_t index)
  317. {
  318. return handlePtr->getMidiProgramInfo(index);
  319. }
  320. static void _set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  321. {
  322. handlePtr->setParameterValue(index, value);
  323. }
  324. static void _set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  325. {
  326. handlePtr->setMidiProgram(channel, bank, program);
  327. }
  328. static void _set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  329. {
  330. handlePtr->setCustomData(key, value);
  331. }
  332. static void _ui_show(NativePluginHandle handle, bool show)
  333. {
  334. handlePtr->uiShow(show);
  335. }
  336. static void _ui_idle(NativePluginHandle handle)
  337. {
  338. handlePtr->uiIdle();
  339. }
  340. static void _ui_set_parameter_value(NativePluginHandle handle, uint32_t index, float value)
  341. {
  342. handlePtr->uiSetParameterValue(index, value);
  343. }
  344. static void _ui_set_midi_program(NativePluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  345. {
  346. handlePtr->uiSetMidiProgram(channel, bank, program);
  347. }
  348. static void _ui_set_custom_data(NativePluginHandle handle, const char* key, const char* value)
  349. {
  350. handlePtr->uiSetCustomData(key, value);
  351. }
  352. static void _activate(NativePluginHandle handle)
  353. {
  354. handlePtr->activate();
  355. }
  356. static void _deactivate(NativePluginHandle handle)
  357. {
  358. handlePtr->deactivate();
  359. }
  360. static void _process(NativePluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const NativeMidiEvent* midiEvents, uint32_t midiEventCount)
  361. {
  362. handlePtr->process(inBuffer, outBuffer, frames, midiEvents, midiEventCount);
  363. }
  364. static char* _get_state(NativePluginHandle handle)
  365. {
  366. return handlePtr->getState();
  367. }
  368. static void _set_state(NativePluginHandle handle, const char* data)
  369. {
  370. handlePtr->setState(data);
  371. }
  372. static intptr_t _dispatcher(NativePluginHandle handle, NativePluginDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  373. {
  374. switch(opcode)
  375. {
  376. case NATIVE_PLUGIN_OPCODE_NULL:
  377. return 0;
  378. case NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED:
  379. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  380. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  381. return 0;
  382. case NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED:
  383. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  384. handlePtr->sampleRateChanged(static_cast<double>(opt));
  385. return 0;
  386. case NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED:
  387. handlePtr->offlineChanged(value != 0);
  388. return 0;
  389. case NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED:
  390. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  391. handlePtr->uiNameChanged(static_cast<const char*>(ptr));
  392. return 0;
  393. }
  394. return 0;
  395. // unused
  396. (void)index;
  397. }
  398. #undef handlePtr
  399. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePluginClass)
  400. #endif
  401. };
  402. /**@}*/
  403. // -----------------------------------------------------------------------
  404. #define PluginClassEND(ClassName) \
  405. public: \
  406. static NativePluginHandle _instantiate(const NativeHostDescriptor* host) \
  407. { \
  408. return (host != nullptr) ? new ClassName(host) : nullptr; \
  409. } \
  410. static void _cleanup(NativePluginHandle handle) \
  411. { \
  412. delete (ClassName*)handle; \
  413. }
  414. #define PluginDescriptorFILL(ClassName) \
  415. ClassName::_instantiate, \
  416. ClassName::_cleanup, \
  417. ClassName::_get_parameter_count, \
  418. ClassName::_get_parameter_info, \
  419. ClassName::_get_parameter_value, \
  420. ClassName::_get_parameter_text, \
  421. ClassName::_get_midi_program_count, \
  422. ClassName::_get_midi_program_info, \
  423. ClassName::_set_parameter_value, \
  424. ClassName::_set_midi_program, \
  425. ClassName::_set_custom_data, \
  426. ClassName::_ui_show, \
  427. ClassName::_ui_idle, \
  428. ClassName::_ui_set_parameter_value, \
  429. ClassName::_ui_set_midi_program, \
  430. ClassName::_ui_set_custom_data, \
  431. ClassName::_activate, \
  432. ClassName::_deactivate, \
  433. ClassName::_process, \
  434. ClassName::_get_state, \
  435. ClassName::_set_state, \
  436. ClassName::_dispatcher
  437. // -----------------------------------------------------------------------
  438. #endif // CARLA_NATIVE_HPP_INCLUDED