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.

546 lines
15KB

  1. /*
  2. * Carla Native Plugin API (C++)
  3. * Copyright (C) 2012-2013 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. // Plugin Class
  28. class PluginClass
  29. {
  30. public:
  31. struct MappedValues {
  32. // event types
  33. MappedValue midi;
  34. MappedValue parameter;
  35. // plugin opcodes
  36. MappedValue msgReceived;
  37. MappedValue bufferSizeChanged;
  38. MappedValue sampleRateChanged;
  39. MappedValue offlineChanged;
  40. // host opcodes
  41. MappedValue needsIdle;
  42. } fMap;
  43. PluginClass(const PluginHostDescriptor* const host)
  44. : pHost(host)
  45. {
  46. std::memset(&fMap, 0, sizeof(MappedValues));
  47. CARLA_SAFE_ASSERT_RETURN(host != nullptr,);
  48. fMap.midi = pHost->map_value(pHost->handle, EVENT_TYPE_MIDI);
  49. fMap.parameter = pHost->map_value(pHost->handle, EVENT_TYPE_PARAMETER);
  50. fMap.msgReceived = pHost->map_value(pHost->handle, PLUGIN_OPCODE_MSG_RECEIVED);
  51. fMap.bufferSizeChanged = pHost->map_value(pHost->handle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED);
  52. fMap.sampleRateChanged = pHost->map_value(pHost->handle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED);
  53. fMap.offlineChanged = pHost->map_value(pHost->handle, PLUGIN_OPCODE_OFFLINE_CHANGED);
  54. fMap.needsIdle = pHost->map_value(pHost->handle, HOST_OPCODE_NEEDS_IDLE);
  55. }
  56. virtual ~PluginClass()
  57. {
  58. }
  59. protected:
  60. // -------------------------------------------------------------------
  61. // Host calls
  62. const PluginHostDescriptor* getHostHandle() const noexcept
  63. {
  64. return pHost;
  65. }
  66. int getPluginVersion() const noexcept
  67. {
  68. return pHost->pluginVersion;
  69. }
  70. MappedValue mapValue(const char* const valueStr) const
  71. {
  72. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  73. CARLA_SAFE_ASSERT_RETURN(valueStr != nullptr, 0);
  74. return pHost->map_value(pHost->handle, valueStr);
  75. }
  76. const char* unmapValue(const MappedValue value) const
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  79. CARLA_SAFE_ASSERT_RETURN(value != 0, nullptr);
  80. return pHost->unmap_value(pHost->handle, value);
  81. }
  82. uint32_t getBufferSize() const
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0);
  85. return pHost->get_buffer_size(pHost->handle);
  86. }
  87. double getSampleRate() const
  88. {
  89. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, 0.0);
  90. return pHost->get_sample_rate(pHost->handle);
  91. }
  92. bool isOffline() const
  93. {
  94. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  95. return pHost->is_offline(pHost->handle);
  96. }
  97. const TimeInfo* getTimeInfo() const
  98. {
  99. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, nullptr);
  100. return pHost->get_time_info(pHost->handle);
  101. }
  102. bool sendUiMessage(const char* const msg)
  103. {
  104. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  105. CARLA_SAFE_ASSERT_RETURN(msg != nullptr, false);
  106. return pHost->send_ui_msg(pHost->handle, msg);
  107. }
  108. bool writeEvent(const Event* const event) const
  109. {
  110. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  111. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  112. return pHost->write_event(pHost->handle, event);
  113. }
  114. bool writeMidiEvent(const MidiEvent* const midiEvent) const
  115. {
  116. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  117. CARLA_SAFE_ASSERT_RETURN(midiEvent != nullptr, false);
  118. return pHost->write_event(pHost->handle, (const Event*)midiEvent);
  119. }
  120. bool writeParameterEvent(const ParameterEvent* const paramEvent) const
  121. {
  122. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, false);
  123. CARLA_SAFE_ASSERT_RETURN(paramEvent != nullptr, false);
  124. return pHost->write_event(pHost->handle, (const Event*)paramEvent);
  125. }
  126. // -------------------------------------------------------------------
  127. // Host dispatcher calls
  128. void hostNeedsIdle() const
  129. {
  130. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  131. pHost->dispatcher(pHost->handle, fMap.needsIdle, 0, 0, nullptr, 0.0f);
  132. }
  133. #if 0
  134. void hostSetVolume(const float value) const
  135. {
  136. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  137. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_VOLUME, 0, 0, nullptr, value);
  138. }
  139. void hostSetDryWet(const float value) const
  140. {
  141. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  142. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_DRYWET, 0, 0, nullptr, value);
  143. }
  144. void hostSetBalanceLeft(const float value) const
  145. {
  146. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  147. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_BALANCE_LEFT, 0, 0, nullptr, value);
  148. }
  149. void hostSetBalanceRight(const float value) const
  150. {
  151. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  152. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_BALANCE_RIGHT, 0, 0, nullptr, value);
  153. }
  154. void hostSetPanning(const float value) const
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  157. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_PANNING, 0, 0, nullptr, value);
  158. }
  159. intptr_t hostGetParameterMidiCC(const int32_t index) const
  160. {
  161. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr, -1);
  162. return pHost->dispatcher(pHost->handle, HOST_OPCODE_GET_PARAMETER_MIDI_CC, index, 0, nullptr, 0.0f);
  163. }
  164. void hostSetParameterMidiCC(const int32_t index, const intptr_t value) const
  165. {
  166. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  167. pHost->dispatcher(pHost->handle, HOST_OPCODE_SET_PARAMETER_MIDI_CC, index, value, nullptr, 0.0f);
  168. }
  169. void hostUpdateParameter(const int32_t index) const
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  172. pHost->dispatcher(pHost->handle, HOST_OPCODE_UPDATE_PARAMETER, index, 0, nullptr, 0.0f);
  173. }
  174. void hostUpdateAllParameters() const
  175. {
  176. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  177. pHost->dispatcher(pHost->handle, HOST_OPCODE_UPDATE_PARAMETER, -1, 0, nullptr, 0.0f);
  178. }
  179. void hostUpdateMidiProgram(const int32_t index, const intptr_t channel = 0) const
  180. {
  181. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  182. pHost->dispatcher(pHost->handle, HOST_OPCODE_UPDATE_MIDI_PROGRAM, index, channel, nullptr, 0.0f);
  183. }
  184. void hostUpdateAllMidiPrograms(const intptr_t channel = 0) const
  185. {
  186. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  187. pHost->dispatcher(pHost->handle, HOST_OPCODE_UPDATE_MIDI_PROGRAM, -1, channel, nullptr, 0.0f);
  188. }
  189. void hostReloadParameters() const
  190. {
  191. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  192. pHost->dispatcher(pHost->handle, HOST_OPCODE_RELOAD_PARAMETERS, 0, 0, nullptr, 0.0f);
  193. }
  194. void hostReloadMidiPrograms() const
  195. {
  196. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  197. pHost->dispatcher(pHost->handle, HOST_OPCODE_RELOAD_MIDI_PROGRAMS, 0, 0, nullptr, 0.0f);
  198. }
  199. void hostReloadAll() const
  200. {
  201. CARLA_SAFE_ASSERT_RETURN(pHost != nullptr,);
  202. pHost->dispatcher(pHost->handle, HOST_OPCODE_RELOAD_ALL, 0, 0, nullptr, 0.0f);
  203. }
  204. #endif
  205. // -------------------------------------------------------------------
  206. // Plugin parameter calls
  207. virtual uint32_t getParameterCount() const
  208. {
  209. return 0;
  210. }
  211. virtual const Parameter* getParameterInfo(const uint32_t index) const
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  214. return nullptr;
  215. }
  216. virtual float getParameterValue(const uint32_t index) const
  217. {
  218. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), 0.0f);
  219. return 0.0f;
  220. }
  221. virtual const char* getParameterText(const uint32_t index, const float value) const
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(), nullptr);
  224. return nullptr;
  225. // unused
  226. (void)value;
  227. }
  228. virtual void setParameterValue(const uint32_t index, const float value)
  229. {
  230. CARLA_SAFE_ASSERT_RETURN(index < getParameterCount(),);
  231. return;
  232. // unused
  233. (void)value;
  234. }
  235. // -------------------------------------------------------------------
  236. // Plugin midi-program calls
  237. virtual uint32_t getMidiProgramCount() const
  238. {
  239. return 0;
  240. }
  241. virtual const MidiProgram* getMidiProgramInfo(const uint32_t index) const
  242. {
  243. CARLA_SAFE_ASSERT_RETURN(index < getMidiProgramCount(), nullptr);
  244. return nullptr;
  245. }
  246. virtual void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program)
  247. {
  248. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  249. return;
  250. // unused
  251. (void)bank;
  252. (void)program;
  253. }
  254. // -------------------------------------------------------------------
  255. // Plugin idle
  256. virtual void idle()
  257. {
  258. }
  259. // -------------------------------------------------------------------
  260. // Plugin state calls
  261. virtual char* getState() const
  262. {
  263. return nullptr;
  264. }
  265. virtual void setState(const char* const data)
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  268. }
  269. // -------------------------------------------------------------------
  270. // Plugin process calls
  271. virtual void activate()
  272. {
  273. }
  274. virtual void deactivate()
  275. {
  276. }
  277. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const Event* const events, const uint32_t eventCount) = 0;
  278. // -------------------------------------------------------------------
  279. // Plugin dispatcher calls
  280. virtual void messageReceived(const char* const msg)
  281. {
  282. CARLA_SAFE_ASSERT_RETURN(msg != nullptr,);
  283. }
  284. virtual void bufferSizeChanged(const uint32_t bufferSize)
  285. {
  286. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  287. }
  288. virtual void sampleRateChanged(const double sampleRate)
  289. {
  290. CARLA_SAFE_ASSERT_RETURN(sampleRate > 0.0,);
  291. }
  292. virtual void offlineModeChanged(const bool isOffline)
  293. {
  294. return;
  295. // unused
  296. (void)isOffline;
  297. }
  298. // -------------------------------------------------------------------
  299. private:
  300. const PluginHostDescriptor* const pHost;
  301. // -------------------------------------------------------------------
  302. #ifndef DOXYGEN
  303. public:
  304. #define handlePtr ((PluginClass*)handle)
  305. static uint32_t _get_parameter_count(PluginHandle handle)
  306. {
  307. return handlePtr->getParameterCount();
  308. }
  309. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  310. {
  311. return handlePtr->getParameterInfo(index);
  312. }
  313. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  314. {
  315. return handlePtr->getParameterValue(index);
  316. }
  317. static const char* _get_parameter_text(PluginHandle handle, uint32_t index, float value)
  318. {
  319. return handlePtr->getParameterText(index, value);
  320. }
  321. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  322. {
  323. handlePtr->setParameterValue(index, value);
  324. }
  325. static uint32_t _get_midi_program_count(PluginHandle handle)
  326. {
  327. return handlePtr->getMidiProgramCount();
  328. }
  329. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  330. {
  331. return handlePtr->getMidiProgramInfo(index);
  332. }
  333. static void _set_midi_program(PluginHandle handle, uint8_t channel, uint32_t bank, uint32_t program)
  334. {
  335. handlePtr->setMidiProgram(channel, bank, program);
  336. }
  337. static void _idle(PluginHandle handle)
  338. {
  339. handlePtr->idle();
  340. }
  341. static char* _get_state(PluginHandle handle)
  342. {
  343. return handlePtr->getState();
  344. }
  345. static void _set_state(PluginHandle handle, const char* data)
  346. {
  347. handlePtr->setState(data);
  348. }
  349. static void _activate(PluginHandle handle)
  350. {
  351. handlePtr->activate();
  352. }
  353. static void _deactivate(PluginHandle handle)
  354. {
  355. handlePtr->deactivate();
  356. }
  357. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, const Event* events, uint32_t eventCount)
  358. {
  359. handlePtr->process(inBuffer, outBuffer, frames, events, eventCount);
  360. }
  361. static intptr_t _dispatcher(PluginHandle handle, MappedValue opcode, int32_t /*index*/, intptr_t value, void* ptr, float opt)
  362. {
  363. const MappedValues& map(handlePtr->fMap);
  364. if (opcode == 0)
  365. {
  366. }
  367. else if (opcode == map.msgReceived)
  368. {
  369. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr, 0);
  370. handlePtr->messageReceived(static_cast<const char*>(ptr));
  371. }
  372. else if (opcode == map.bufferSizeChanged)
  373. {
  374. CARLA_SAFE_ASSERT_RETURN(value > 0, 0);
  375. handlePtr->bufferSizeChanged(static_cast<uint32_t>(value));
  376. }
  377. else if (opcode == map.sampleRateChanged)
  378. {
  379. CARLA_SAFE_ASSERT_RETURN(opt > 0.0f, 0);
  380. handlePtr->sampleRateChanged(static_cast<double>(opt));
  381. }
  382. else if (opcode == map.offlineChanged)
  383. {
  384. handlePtr->offlineModeChanged(value != 0);
  385. }
  386. return 0;
  387. }
  388. #undef handlePtr
  389. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginClass)
  390. #endif
  391. };
  392. /**@}*/
  393. // -----------------------------------------------------------------------
  394. #define PluginClassEND(ClassName) \
  395. public: \
  396. static PluginHandle _instantiate(const PluginHostDescriptor* host) \
  397. { \
  398. return new ClassName(host); \
  399. } \
  400. static void _cleanup(PluginHandle handle) \
  401. { \
  402. delete (ClassName*)handle; \
  403. }
  404. #define PluginDescriptorFILL(ClassName) \
  405. ClassName::_instantiate, \
  406. ClassName::_cleanup, \
  407. ClassName::_get_parameter_count, \
  408. ClassName::_get_parameter_info, \
  409. ClassName::_get_parameter_value, \
  410. ClassName::_get_parameter_text, \
  411. ClassName::_set_parameter_value, \
  412. ClassName::_get_midi_program_count, \
  413. ClassName::_get_midi_program_info, \
  414. ClassName::_set_midi_program, \
  415. ClassName::_idle, \
  416. ClassName::_get_state, \
  417. ClassName::_set_state, \
  418. ClassName::_activate, \
  419. ClassName::_deactivate, \
  420. ClassName::_process, \
  421. ClassName::_dispatcher
  422. // -----------------------------------------------------------------------
  423. #endif // CARLA_NATIVE_HPP_INCLUDED