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.

338 lines
8.0KB

  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. #ifndef __DISTRHO_PLUGIN_INTERNAL_H__
  18. #define __DISTRHO_PLUGIN_INTERNAL_H__
  19. #include "DistrhoPlugin.h"
  20. #include <cassert>
  21. START_NAMESPACE_DISTRHO
  22. // -------------------------------------------------
  23. #define MAX_MIDI_EVENTS 512
  24. static uint32_t d_lastBufferSize = 0;
  25. static double d_lastSampleRate = 0.0;
  26. struct PluginPrivateData {
  27. uint32_t bufferSize;
  28. double sampleRate;
  29. uint32_t parameterCount;
  30. Parameter* parameters;
  31. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  32. uint32_t programCount;
  33. d_string* programNames;
  34. #endif
  35. #if DISTRHO_PLUGIN_WANT_STATE
  36. uint32_t stateCount;
  37. d_string* stateKeys;
  38. #endif
  39. #if DISTRHO_PLUGIN_WANT_LATENCY
  40. uint32_t latency;
  41. #endif
  42. TimePos timePos;
  43. PluginPrivateData()
  44. : bufferSize(d_lastBufferSize),
  45. sampleRate(d_lastSampleRate),
  46. parameterCount(0),
  47. parameters(nullptr),
  48. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  49. programCount(0),
  50. programNames(nullptr),
  51. #endif
  52. #if DISTRHO_PLUGIN_WANT_STATE
  53. stateCount(0),
  54. stateKeys(nullptr),
  55. #endif
  56. #if DISTRHO_PLUGIN_WANT_LATENCY
  57. latency(0),
  58. #endif
  59. timePos()
  60. {
  61. assert(d_lastSampleRate != 0.0);
  62. }
  63. ~PluginPrivateData()
  64. {
  65. if (parameterCount > 0 && parameters)
  66. delete[] parameters;
  67. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  68. if (programCount > 0 && programNames)
  69. delete[] programNames;
  70. #endif
  71. #if DISTRHO_PLUGIN_WANT_STATE
  72. if (stateCount > 0 && stateKeys)
  73. delete[] stateKeys;
  74. #endif
  75. }
  76. };
  77. // -------------------------------------------------
  78. class PluginInternal
  79. {
  80. public:
  81. PluginInternal()
  82. : plugin(createPlugin()),
  83. data(nullptr)
  84. {
  85. assert(plugin);
  86. if (! plugin)
  87. return;
  88. data = plugin->data;
  89. for (uint32_t i=0; i < data->parameterCount; i++)
  90. plugin->d_initParameter(i, data->parameters[i]);
  91. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  92. for (uint32_t i=0; i < data->programCount; i++)
  93. plugin->d_initProgramName(i, data->programNames[i]);
  94. #endif
  95. #if DISTRHO_PLUGIN_WANT_STATE
  96. for (uint32_t i=0; i < data->stateCount; i++)
  97. plugin->d_initStateKey(i, data->stateKeys[i]);
  98. #endif
  99. }
  100. ~PluginInternal()
  101. {
  102. if (plugin)
  103. delete plugin;
  104. }
  105. // ---------------------------------------------
  106. const char* name()
  107. {
  108. assert(plugin);
  109. return plugin ? plugin->d_name() : "";
  110. }
  111. const char* label()
  112. {
  113. assert(plugin);
  114. return plugin ? plugin->d_label() : "";
  115. }
  116. const char* maker()
  117. {
  118. assert(plugin);
  119. return plugin ? plugin->d_maker() : "";
  120. }
  121. const char* license()
  122. {
  123. assert(plugin);
  124. return plugin ? plugin->d_license() : "";
  125. }
  126. uint32_t version()
  127. {
  128. assert(plugin);
  129. return plugin ? plugin->d_version() : 1000;
  130. }
  131. long uniqueId()
  132. {
  133. assert(plugin);
  134. return plugin ? plugin->d_uniqueId() : 0;
  135. }
  136. // ---------------------------------------------
  137. #if DISTRHO_PLUGIN_WANT_LATENCY
  138. uint32_t latency() const
  139. {
  140. assert(data);
  141. return data ? data->latency : 0;
  142. }
  143. #endif
  144. uint32_t parameterCount() const
  145. {
  146. assert(data);
  147. return data ? data->parameterCount : 0;
  148. }
  149. uint32_t parameterHints(uint32_t index) const
  150. {
  151. assert(data && index < data->parameterCount);
  152. return (data && index < data->parameterCount) ? data->parameters[index].hints : 0x0;
  153. }
  154. bool parameterIsOutput(uint32_t index) const
  155. {
  156. uint32_t hints = parameterHints(index);
  157. return bool(hints & PARAMETER_IS_OUTPUT);
  158. }
  159. const d_string& parameterName(uint32_t index) const
  160. {
  161. assert(data && index < data->parameterCount);
  162. return (data && index < data->parameterCount) ? data->parameters[index].name : fallbackString;
  163. }
  164. const d_string& parameterSymbol(uint32_t index) const
  165. {
  166. assert(data && index < data->parameterCount);
  167. return (data && index < data->parameterCount) ? data->parameters[index].symbol : fallbackString;
  168. }
  169. const d_string& parameterUnit(uint32_t index) const
  170. {
  171. assert(data && index < data->parameterCount);
  172. return (data && index < data->parameterCount) ? data->parameters[index].unit : fallbackString;
  173. }
  174. const ParameterRanges* parameterRanges(uint32_t index) const
  175. {
  176. assert(data && index < data->parameterCount);
  177. return (data && index < data->parameterCount) ? &data->parameters[index].ranges : &fallbackRanges;
  178. }
  179. float parameterValue(uint32_t index)
  180. {
  181. assert(plugin && index < data->parameterCount);
  182. return (plugin && index < data->parameterCount) ? plugin->d_parameterValue(index) : 0.0f;
  183. }
  184. void setParameterValue(uint32_t index, float value)
  185. {
  186. assert(plugin && index < data->parameterCount);
  187. if (plugin && index < data->parameterCount)
  188. plugin->d_setParameterValue(index, value);
  189. }
  190. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  191. uint32_t programCount() const
  192. {
  193. assert(data);
  194. return data ? data->programCount : 0;
  195. }
  196. const d_string& programName(uint32_t index) const
  197. {
  198. assert(data && index < data->programCount);
  199. return (data && index < data->programCount) ? data->programNames[index] : fallbackString;
  200. }
  201. void setProgram(uint32_t index)
  202. {
  203. assert(plugin && index < data->programCount);
  204. if (plugin && index < data->programCount)
  205. plugin->d_setProgram(index);
  206. }
  207. #endif
  208. #if DISTRHO_PLUGIN_WANT_STATE
  209. uint32_t stateCount() const
  210. {
  211. assert(data);
  212. return data ? data->stateCount : 0;
  213. }
  214. const d_string& stateKey(uint32_t index) const
  215. {
  216. assert(data && index < data->stateCount);
  217. return (data && index < data->stateCount) ? data->stateKeys[index] : fallbackString;
  218. }
  219. void setState(const char* key, const char* value)
  220. {
  221. assert(plugin && key && value);
  222. if (plugin && key && value)
  223. plugin->d_setState(key, value);
  224. }
  225. #endif
  226. // ---------------------------------------------
  227. void activate()
  228. {
  229. assert(plugin);
  230. if (plugin)
  231. plugin->d_activate();
  232. }
  233. void deactivate()
  234. {
  235. assert(plugin);
  236. if (plugin)
  237. plugin->d_deactivate();
  238. }
  239. void run(const float** inputs, float** outputs, uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents)
  240. {
  241. assert(plugin && frames >= 2);
  242. if (plugin)
  243. plugin->d_run(inputs, outputs, frames, midiEventCount, midiEvents);
  244. }
  245. void setBufferSize(uint32_t bufferSize, bool callback = false)
  246. {
  247. assert(data && plugin && bufferSize >= 2);
  248. if (callback && data->bufferSize == bufferSize)
  249. callback = false;
  250. if (data)
  251. data->bufferSize = bufferSize;
  252. if (plugin && callback)
  253. {
  254. plugin->d_deactivate();
  255. plugin->d_bufferSizeChanged(bufferSize);
  256. plugin->d_activate();
  257. }
  258. }
  259. // ---------------------------------------------
  260. protected:
  261. Plugin* const plugin;
  262. PluginPrivateData* data;
  263. private:
  264. static const d_string fallbackString;
  265. static const ParameterRanges fallbackRanges;
  266. };
  267. // -------------------------------------------------
  268. END_NAMESPACE_DISTRHO
  269. #endif // __DISTRHO_PLUGIN_INTERNAL_H__