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.

361 lines
8.7KB

  1. /*
  2. * DISTHRO Plugin Toolkit (DPT)
  3. * Copyright (C) 2012 Filipe Coelho <falktx@gmail.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 license see the GPL.txt file
  16. */
  17. #include "carla_native.hpp"
  18. #include "DistrhoPluginMain.cpp"
  19. #include "DistrhoUIMain.cpp"
  20. #ifdef QTCREATOR_TEST
  21. # define DISTRHO_PLUGIN_HAS_UI 1
  22. //# define DISTRHO_PLUGIN_IS_SYNTH 1
  23. # define DISTRHO_PLUGIN_WANT_PROGRAMS 1
  24. //# define DISTRHO_PLUGIN_WANT_STATE 1
  25. #endif
  26. // -------------------------------------------------
  27. START_NAMESPACE_DISTRHO
  28. #if DISTRHO_PLUGIN_HAS_UI
  29. class UICarla
  30. {
  31. public:
  32. UICarla(const HostDescriptor* const host, PluginInternal* plugin, intptr_t winId)
  33. : m_host(host),
  34. m_plugin(plugin),
  35. ui(this, winId, setParameterCallback, setStateCallback, uiEditParameterCallback, uiSendNoteCallback, uiResizeCallback)
  36. {
  37. }
  38. ~UICarla()
  39. {
  40. }
  41. protected:
  42. void setParameterValue(uint32_t index, float value)
  43. {
  44. m_host->ui_parameter_changed(m_host->handle, index, value);
  45. }
  46. #if DISTRHO_PLUGIN_WANT_STATE
  47. void setState(const char* key, const char* value)
  48. {
  49. m_host->ui_custom_data_changed(m_host->handle, key, value);
  50. }
  51. #endif
  52. void uiEditParameter(uint32_t, bool)
  53. {
  54. // TODO
  55. }
  56. #if DISTRHO_PLUGIN_IS_SYNTH
  57. void uiSendNote(bool, uint8_t, uint8_t, uint8_t)
  58. {
  59. // TODO
  60. }
  61. #endif
  62. void uiResize(unsigned int width, unsigned int height)
  63. {
  64. //hostCallback(audioMasterSizeWindow, width, height, nullptr, 0.0f);
  65. Q_UNUSED(width);
  66. Q_UNUSED(height);
  67. }
  68. private:
  69. // Carla stuff
  70. const HostDescriptor* const m_host;
  71. PluginInternal* const m_plugin;
  72. // Plugin UI
  73. UIInternal ui;
  74. // ---------------------------------------------
  75. // Callbacks
  76. static void setParameterCallback(void* ptr, uint32_t rindex, float value)
  77. {
  78. UICarla* _this_ = (UICarla*)ptr;
  79. CARLA_ASSERT(_this_);
  80. _this_->setParameterValue(rindex, value);
  81. }
  82. static void setStateCallback(void* ptr, const char* key, const char* value)
  83. {
  84. #if DISTRHO_PLUGIN_WANT_STATE
  85. UICarla* _this_ = (UICarla*)ptr;
  86. CARLA_ASSERT(_this_);
  87. _this_->setState(key, value);
  88. #else
  89. Q_UNUSED(ptr);
  90. Q_UNUSED(key);
  91. Q_UNUSED(value);
  92. #endif
  93. }
  94. static void uiEditParameterCallback(void* ptr, uint32_t index, bool started)
  95. {
  96. UICarla* _this_ = (UICarla*)ptr;
  97. CARLA_ASSERT(_this_);
  98. _this_->uiEditParameter(index, started);
  99. }
  100. static void uiSendNoteCallback(void* ptr, bool onOff, uint8_t channel, uint8_t note, uint8_t velocity)
  101. {
  102. #if DISTRHO_PLUGIN_IS_SYNTH
  103. UICarla* _this_ = (UICarla*)ptr;
  104. CARLA_ASSERT(_this_);
  105. _this_->uiSendNote(onOff, channel, note, velocity);
  106. #else
  107. Q_UNUSED(ptr);
  108. Q_UNUSED(onOff);
  109. Q_UNUSED(channel);
  110. Q_UNUSED(note);
  111. Q_UNUSED(velocity);
  112. #endif
  113. }
  114. static void uiResizeCallback(void* ptr, unsigned int width, unsigned int height)
  115. {
  116. UICarla* _this_ = (UICarla*)ptr;
  117. CARLA_ASSERT(_this_);
  118. _this_->uiResize(width, height);
  119. }
  120. friend class PluginCarla;
  121. };
  122. #endif
  123. class PluginCarla : public PluginDescriptorClass
  124. {
  125. public:
  126. PluginCarla(const HostDescriptor* host)
  127. : PluginDescriptorClass(host),
  128. m_host(host)
  129. {
  130. uiPtr = nullptr;
  131. }
  132. ~PluginCarla()
  133. {
  134. if (uiPtr)
  135. delete uiPtr;
  136. }
  137. protected:
  138. // -------------------------------------------------------------------
  139. // Plugin parameter calls
  140. uint32_t getParameterCount()
  141. {
  142. return plugin.parameterCount();
  143. }
  144. const ::Parameter* getParameterInfo(uint32_t index)
  145. {
  146. static ::Parameter param;
  147. {
  148. uint32_t paramHints = plugin.parameterHints(index);
  149. if (paramHints & PARAMETER_IS_AUTOMABLE)
  150. param.hints |= ::PARAMETER_IS_AUTOMABLE;
  151. if (paramHints & PARAMETER_IS_BOOLEAN)
  152. param.hints |= ::PARAMETER_IS_BOOLEAN;
  153. if (paramHints & PARAMETER_IS_INTEGER)
  154. param.hints |= ::PARAMETER_IS_INTEGER;
  155. if (paramHints & PARAMETER_IS_LOGARITHMIC)
  156. param.hints |= ::PARAMETER_IS_LOGARITHMIC;
  157. if (paramHints & PARAMETER_IS_OUTPUT)
  158. param.hints |= ::PARAMETER_IS_OUTPUT;
  159. }
  160. param.name = plugin.parameterName(index);
  161. param.unit = plugin.parameterUnit(index);
  162. {
  163. const ParameterRanges* ranges(plugin.parameterRanges(index));
  164. param.ranges.def = ranges->def;
  165. param.ranges.min = ranges->min;
  166. param.ranges.max = ranges->max;
  167. param.ranges.step = ranges->step;
  168. param.ranges.stepSmall = ranges->stepSmall;
  169. param.ranges.stepLarge = ranges->stepLarge;
  170. }
  171. param.scalePointCount = 0;
  172. param.scalePoints = nullptr;
  173. return &param;
  174. }
  175. float getParameterValue(uint32_t index)
  176. {
  177. return plugin.parameterValue(index);
  178. }
  179. // -------------------------------------------------------------------
  180. // Plugin midi-program calls
  181. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  182. virtual uint32_t getMidiProgramCount()
  183. {
  184. return plugin.programCount();
  185. }
  186. virtual const ::MidiProgram* getMidiProgramInfo(uint32_t index)
  187. {
  188. static ::MidiProgram midiProgram;
  189. midiProgram.bank = index / 128;
  190. midiProgram.program = index % 128;
  191. midiProgram.name = plugin.programName(index);
  192. return &midiProgram;
  193. }
  194. #endif
  195. // -------------------------------------------------------------------
  196. // Plugin state calls
  197. void setParameterValue(uint32_t index, float value)
  198. {
  199. plugin.setParameterValue(index, value);
  200. }
  201. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  202. void setMidiProgram(uint32_t bank, uint32_t program)
  203. {
  204. uint32_t realProgram = bank * 128 + program;
  205. plugin.setProgram(realProgram);
  206. }
  207. #endif
  208. #if DISTRHO_PLUGIN_WANT_STATE
  209. void setCustomData(const char* key, const char* value)
  210. {
  211. plugin.setState(key, value);
  212. }
  213. #endif
  214. // -------------------------------------------------------------------
  215. // Plugin UI calls
  216. #if DISTRHO_PLUGIN_HAS_UI
  217. void uiShow(bool show)
  218. {
  219. if (show)
  220. createUiIfNeeded();
  221. //if (uiPtr)
  222. // uiPtr->setVisible(show);
  223. }
  224. void uiIdle()
  225. {
  226. if (uiPtr)
  227. uiPtr->ui.idle();
  228. }
  229. void uiSetParameterValue(uint32_t index, float value)
  230. {
  231. if (uiPtr)
  232. uiPtr->ui.parameterChanged(index, value);
  233. }
  234. # if DISTRHO_PLUGIN_WANT_PROGRAMS
  235. void uiSetMidiProgram(uint32_t bank, uint32_t program)
  236. {
  237. uint32_t realProgram = bank * 128 + program;
  238. if (uiPtr)
  239. uiPtr->ui.programChanged(realProgram);
  240. }
  241. # endif
  242. # if DISTRHO_PLUGIN_WANT_STATE
  243. void uiSetCustomData(const char* key, const char* value)
  244. {
  245. if (uiPtr)
  246. uiPtr->ui.stateChanged(key, value);
  247. }
  248. # endif
  249. #endif
  250. // -------------------------------------------------------------------
  251. // Plugin process calls
  252. void activate()
  253. {
  254. plugin.activate();
  255. }
  256. void deactivate()
  257. {
  258. plugin.deactivate();
  259. }
  260. void process(float**, float**, uint32_t, uint32_t, ::MidiEvent*)
  261. {
  262. //plugin->d_run();
  263. }
  264. // -------------------------------------------------------------------
  265. private:
  266. PluginInternal plugin;
  267. const HostDescriptor* const m_host;
  268. #if DISTRHO_PLUGIN_HAS_UI
  269. // UI
  270. UICarla* uiPtr;
  271. #endif
  272. void createUiIfNeeded()
  273. {
  274. if (! uiPtr)
  275. {
  276. setLastUiSampleRate(getSampleRate());
  277. uiPtr = new UICarla(m_host, &plugin, 0);
  278. }
  279. }
  280. // -------------------------------------------------------------------
  281. public:
  282. static PluginHandle _instantiate(struct _PluginDescriptor*, HostDescriptor* host)
  283. {
  284. d_lastBufferSize = host->get_buffer_size(host->handle);
  285. d_lastSampleRate = host->get_sample_rate(host->handle);
  286. return new PluginCarla(host);
  287. }
  288. static void _cleanup(PluginHandle handle)
  289. {
  290. delete (PluginCarla*)handle;
  291. }
  292. };
  293. END_NAMESPACE_DISTRHO
  294. // -----------------------------------------------------------------------