Collection of tools useful for audio production
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.

363 lines
9.4KB

  1. /*
  2. * Carla Native Plugin API
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifndef CARLA_NATIVE_HPP
  18. #define CARLA_NATIVE_HPP
  19. #include "carla_native.h"
  20. #include "carla_utils.hpp"
  21. /*!
  22. * @defgroup CarlaNativeAPI Carla Native API
  23. * @{
  24. */
  25. class PluginDescriptorClass
  26. {
  27. public:
  28. PluginDescriptorClass(const HostDescriptor* const host)
  29. : m_host(host)
  30. {
  31. }
  32. virtual ~PluginDescriptorClass()
  33. {
  34. }
  35. // -------------------------------------------------------------------
  36. // Host calls
  37. const HostDescriptor* getHostHandle() const
  38. {
  39. return m_host;
  40. }
  41. uint32_t getBufferSize() const
  42. {
  43. CARLA_ASSERT(m_host);
  44. if (m_host)
  45. return m_host->get_buffer_size(m_host->handle);
  46. return 0;
  47. }
  48. double getSampleRate() const
  49. {
  50. CARLA_ASSERT(m_host);
  51. if (m_host)
  52. return m_host->get_sample_rate(m_host->handle);
  53. return 0.0;
  54. }
  55. const TimeInfo* getTimeInfo() const
  56. {
  57. CARLA_ASSERT(m_host);
  58. if (m_host)
  59. return m_host->get_time_info(m_host->handle);
  60. return nullptr;
  61. }
  62. void writeMidiEvent(MidiEvent* const event)
  63. {
  64. CARLA_ASSERT(m_host);
  65. if (m_host)
  66. m_host->write_midi_event(m_host->handle, event);
  67. }
  68. void uiParameterChanged(const uint32_t index, const float value)
  69. {
  70. CARLA_ASSERT(m_host);
  71. if (m_host)
  72. m_host->ui_parameter_changed(m_host->handle, index, value);
  73. }
  74. void uiMidiProgramChanged(const uint32_t bank, const uint32_t program)
  75. {
  76. CARLA_ASSERT(m_host);
  77. if (m_host)
  78. m_host->ui_midi_program_changed(m_host->handle, bank, program);
  79. }
  80. void uiCustomDataChanged(const char* const key, const char* const value)
  81. {
  82. CARLA_ASSERT(m_host);
  83. if (m_host)
  84. m_host->ui_custom_data_changed(m_host->handle, key, value);
  85. }
  86. void uiClosed()
  87. {
  88. CARLA_ASSERT(m_host);
  89. if (m_host)
  90. m_host->ui_closed(m_host->handle);
  91. }
  92. protected:
  93. // -------------------------------------------------------------------
  94. // Plugin parameter calls
  95. virtual uint32_t getParameterCount()
  96. {
  97. return 0;
  98. }
  99. virtual const Parameter* getParameterInfo(const uint32_t index)
  100. {
  101. CARLA_ASSERT(index < getParameterCount());
  102. return nullptr;
  103. }
  104. virtual float getParameterValue(const uint32_t index)
  105. {
  106. CARLA_ASSERT(index < getParameterCount());
  107. return 0.0f;
  108. }
  109. virtual const char* getParameterText(const uint32_t index)
  110. {
  111. CARLA_ASSERT(index < getParameterCount());
  112. return nullptr;
  113. }
  114. // -------------------------------------------------------------------
  115. // Plugin midi-program calls
  116. virtual uint32_t getMidiProgramCount()
  117. {
  118. return 0;
  119. }
  120. virtual const MidiProgram* getMidiProgramInfo(const uint32_t index)
  121. {
  122. CARLA_ASSERT(index < getMidiProgramCount());
  123. return nullptr;
  124. }
  125. // -------------------------------------------------------------------
  126. // Plugin state calls
  127. virtual void setParameterValue(const uint32_t index, const float value)
  128. {
  129. CARLA_ASSERT(index < getParameterCount());
  130. Q_UNUSED(value);
  131. }
  132. virtual void setMidiProgram(const uint32_t bank, const uint32_t program)
  133. {
  134. Q_UNUSED(bank);
  135. Q_UNUSED(program);
  136. }
  137. virtual void setCustomData(const char* const key, const char* const value)
  138. {
  139. CARLA_ASSERT(key);
  140. CARLA_ASSERT(value);
  141. }
  142. // -------------------------------------------------------------------
  143. // Plugin UI calls
  144. virtual void uiShow(const bool show)
  145. {
  146. Q_UNUSED(show);
  147. }
  148. virtual void uiIdle()
  149. {
  150. }
  151. virtual void uiSetParameterValue(const uint32_t index, const float value)
  152. {
  153. CARLA_ASSERT(index < getParameterCount());
  154. Q_UNUSED(value);
  155. }
  156. virtual void uiSetMidiProgram(const uint32_t bank, const uint32_t program)
  157. {
  158. Q_UNUSED(bank);
  159. Q_UNUSED(program);
  160. }
  161. virtual void uiSetCustomData(const char* const key, const char* const value)
  162. {
  163. CARLA_ASSERT(key);
  164. CARLA_ASSERT(value);
  165. }
  166. // -------------------------------------------------------------------
  167. // Plugin process calls
  168. virtual void activate()
  169. {
  170. }
  171. virtual void deactivate()
  172. {
  173. }
  174. virtual void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const MidiEvent* const midiEvents) = 0;
  175. // -------------------------------------------------------------------
  176. private:
  177. const HostDescriptor* const m_host;
  178. // -------------------------------------------------------------------
  179. #ifndef DOXYGEN
  180. public:
  181. #define handlePtr ((PluginDescriptorClass*)handle)
  182. static uint32_t _get_parameter_count(PluginHandle handle)
  183. {
  184. return handlePtr->getParameterCount();
  185. }
  186. static const Parameter* _get_parameter_info(PluginHandle handle, uint32_t index)
  187. {
  188. return handlePtr->getParameterInfo(index);
  189. }
  190. static float _get_parameter_value(PluginHandle handle, uint32_t index)
  191. {
  192. return handlePtr->getParameterValue(index);
  193. }
  194. static const char* _get_parameter_text(PluginHandle handle, uint32_t index)
  195. {
  196. return handlePtr->getParameterText(index);
  197. }
  198. static uint32_t _get_midi_program_count(PluginHandle handle)
  199. {
  200. return handlePtr->getMidiProgramCount();
  201. }
  202. static const MidiProgram* _get_midi_program_info(PluginHandle handle, uint32_t index)
  203. {
  204. return handlePtr->getMidiProgramInfo(index);
  205. }
  206. static void _set_parameter_value(PluginHandle handle, uint32_t index, float value)
  207. {
  208. return handlePtr->setParameterValue(index, value);
  209. }
  210. static void _set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  211. {
  212. return handlePtr->setMidiProgram(bank, program);
  213. }
  214. static void _set_custom_data(PluginHandle handle, const char* key, const char* value)
  215. {
  216. return handlePtr->setCustomData(key, value);
  217. }
  218. static void _ui_show(PluginHandle handle, bool show)
  219. {
  220. return handlePtr->uiShow(show);
  221. }
  222. static void _ui_idle(PluginHandle handle)
  223. {
  224. return handlePtr->uiIdle();
  225. }
  226. static void _ui_set_parameter_value(PluginHandle handle, uint32_t index, float value)
  227. {
  228. return handlePtr->uiSetParameterValue(index, value);
  229. }
  230. static void _ui_set_midi_program(PluginHandle handle, uint32_t bank, uint32_t program)
  231. {
  232. return handlePtr->uiSetMidiProgram(bank, program);
  233. }
  234. static void _ui_set_custom_data(PluginHandle handle, const char* key, const char* value)
  235. {
  236. return handlePtr->uiSetCustomData(key, value);
  237. }
  238. static void _activate(PluginHandle handle)
  239. {
  240. handlePtr->activate();
  241. }
  242. static void _deactivate(PluginHandle handle)
  243. {
  244. handlePtr->deactivate();
  245. }
  246. static void _process(PluginHandle handle, float** inBuffer, float** outBuffer, const uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  247. {
  248. return handlePtr->process(inBuffer, outBuffer, frames, midiEventCount, midiEvents);
  249. }
  250. #endif
  251. };
  252. /**@}*/
  253. // -----------------------------------------------------------------------
  254. #define PluginDescriptorClassEND(CLASS) \
  255. public: \
  256. static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host) \
  257. { \
  258. return new CLASS(host); \
  259. } \
  260. static void _cleanup(PluginHandle handle) \
  261. { \
  262. delete (CLASS*)handle; \
  263. }
  264. #define PluginDescriptorFILL(CLASS) \
  265. CLASS::_instantiate, \
  266. CLASS::_get_parameter_count, \
  267. CLASS::_get_parameter_info, \
  268. CLASS::_get_parameter_value, \
  269. CLASS::_get_parameter_text, \
  270. CLASS::_get_midi_program_count, \
  271. CLASS::_get_midi_program_info, \
  272. CLASS::_set_parameter_value, \
  273. CLASS::_set_midi_program, \
  274. CLASS::_set_custom_data, \
  275. CLASS::_ui_show, \
  276. CLASS::_ui_idle, \
  277. CLASS::_ui_set_parameter_value, \
  278. CLASS::_ui_set_midi_program, \
  279. CLASS::_ui_set_custom_data, \
  280. CLASS::_activate, \
  281. CLASS::_deactivate, \
  282. CLASS::_cleanup, \
  283. CLASS::_process
  284. #endif // CARLA_NATIVE_HPP