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.

571 lines
16KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifdef DOXYGEN
  17. #include "src/DistrhoDefines.h"
  18. START_NAMESPACE_DISTRHO
  19. /* ------------------------------------------------------------------------------------------------------------
  20. * Intro */
  21. /**
  22. @mainpage DISTRHO %Plugin Framework
  23. DISTRHO %Plugin Framework (or @b DPF for short)
  24. is a plugin framework designed to make development of new plugins an easy and enjoyable task.@n
  25. It allows developers to create plugins with custom UIs using a simple C++ API.@n
  26. The framework facilitates exporting various different plugin formats from the same code-base.
  27. DPF can build for LADSPA, DSSI, LV2 and VST2 formats.@n
  28. A JACK/Standalone mode is also available, allowing you to quickly test plugins.
  29. @section Macros
  30. You start by creating a "DistrhoPluginInfo.h" file describing the plugin via macros, see @ref PluginMacros.@n
  31. This file is included in the main DPF code to select which features to activate for each plugin format.
  32. For example, a plugin (with %UI) that use states will require LV2 hosts to support Atom and Worker extensions for
  33. message passing from the %UI to the plugin.@n
  34. If your plugin does not make use of states, the Worker extension is not set as a required feature.
  35. @section Plugin
  36. The next step is to create your plugin code by subclassing DPF's Plugin class.@n
  37. You need to pass the number of parameters in the constructor and also the number of programs and states, if any.
  38. Here's an example of an audio plugin that simply mutes the host output:
  39. @code
  40. class MutePlugin : public Plugin
  41. {
  42. public:
  43. /**
  44. Plugin class constructor.
  45. */
  46. MutePlugin()
  47. : Plugin(0, 0, 0) // 0 parameters, 0 programs and 0 states
  48. {
  49. }
  50. protected:
  51. /* ----------------------------------------------------------------------------------------
  52. * Information */
  53. /**
  54. Get the plugin label.
  55. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  56. */
  57. const char* getLabel() const override
  58. {
  59. return "Mute";
  60. }
  61. /**
  62. Get the plugin author/maker.
  63. */
  64. const char* getMaker() const override
  65. {
  66. return "DPF";
  67. }
  68. /**
  69. Get the plugin license name (a single line of text).
  70. For commercial plugins this should return some short copyright information.
  71. */
  72. const char* getLicense() const override
  73. {
  74. return "MIT";
  75. }
  76. /**
  77. Get the plugin version, in hexadecimal.
  78. */
  79. uint32_t getVersion() const override
  80. {
  81. return d_version(1, 0, 0);
  82. }
  83. /**
  84. Get the plugin unique Id.
  85. This value is used by LADSPA, DSSI and VST plugin formats.
  86. */
  87. int64_t getUniqueId() const override
  88. {
  89. return d_cconst('M', 'u', 't', 'e');
  90. }
  91. /* ----------------------------------------------------------------------------------------
  92. * This example has no parameters, so skip parameter stuff */
  93. void initParameter(uint32_t, Parameter&) override {}
  94. float getParameterValue(uint32_t) const override { return 0.0f; }
  95. void setParameterValue(uint32_t, float) override {}
  96. /* ----------------------------------------------------------------------------------------
  97. * Audio/MIDI Processing */
  98. /**
  99. Run/process function for plugins without MIDI input.
  100. NOTE: Some parameters might be null if there are no audio inputs or outputs.
  101. */
  102. void run(const float**, float** outputs, uint32_t frames) override
  103. {
  104. // get the left and right audio outputs
  105. float* const outL = outputs[0];
  106. float* const outR = outputs[1];
  107. // mute audio
  108. std::memset(outL, 0, sizeof(float)*frames);
  109. std::memset(outR, 0, sizeof(float)*frames);
  110. }
  111. };
  112. @endcode
  113. See the Plugin class for more information and to understand what each function does.
  114. @section Parameters
  115. A plugin is nothing without parameters.@n
  116. In DPF parameters can be inputs or outputs.@n
  117. They have hints to describe how they behave plus a name and a symbol identifying them.@n
  118. Parameters also have 'ranges' – a minimum, maximum and default value.
  119. Input parameters are "read-only": the plugin can read them but not change them.
  120. (the exception being when changing programs, more on that below)@n
  121. It's the host responsibility to save, restore and set input parameters.
  122. Output parameters can be changed at anytime by the plugin.@n
  123. The host will simply read their values and not change them.
  124. Here's an example of an audio plugin that has 1 input parameter:
  125. @code
  126. class GainPlugin : public Plugin
  127. {
  128. public:
  129. /**
  130. Plugin class constructor.
  131. You must set all parameter values to their defaults, matching ParameterRanges::def.
  132. */
  133. GainPlugin()
  134. : Plugin(1, 0, 0), // 1 parameter, 0 programs and 0 states
  135. fGain(1.0f)
  136. {
  137. }
  138. protected:
  139. /* ----------------------------------------------------------------------------------------
  140. * Information */
  141. const char* getLabel() const override
  142. {
  143. return "Gain";
  144. }
  145. const char* getMaker() const override
  146. {
  147. return "DPF";
  148. }
  149. const char* getLicense() const override
  150. {
  151. return "MIT";
  152. }
  153. uint32_t getVersion() const override
  154. {
  155. return d_version(1, 0, 0);
  156. }
  157. int64_t getUniqueId() const override
  158. {
  159. return d_cconst('G', 'a', 'i', 'n');
  160. }
  161. /* ----------------------------------------------------------------------------------------
  162. * Init */
  163. /**
  164. Initialize a parameter.
  165. This function will be called once, shortly after the plugin is created.
  166. */
  167. void initParameter(uint32_t index, Parameter& parameter) override
  168. {
  169. // we only have one parameter so we can skip checking the index
  170. parameter.hints = kParameterIsAutomable;
  171. parameter.name = "Gain";
  172. parameter.symbol = "gain";
  173. parameter.ranges.min = 0.0f;
  174. parameter.ranges.max = 2.0f;
  175. parameter.ranges.def = 1.0f;
  176. }
  177. /* ----------------------------------------------------------------------------------------
  178. * Internal data */
  179. /**
  180. Get the current value of a parameter.
  181. */
  182. float getParameterValue(uint32_t index) const override
  183. {
  184. // same as before, ignore index check
  185. return fGain;
  186. }
  187. /**
  188. Change a parameter value.
  189. */
  190. void setParameterValue(uint32_t index, float value) override
  191. {
  192. // same as before, ignore index check
  193. fGain = value;
  194. }
  195. /* ----------------------------------------------------------------------------------------
  196. * Audio/MIDI Processing */
  197. void run(const float**, float** outputs, uint32_t frames) override
  198. {
  199. // get the mono input and output
  200. const float* const in = inputs[0];
  201. /* */ float* const out = outputs[0];
  202. // apply gain against all samples
  203. for (uint32_t i=0; i < frames; ++i)
  204. out[i] = in[i] * fGain;
  205. }
  206. private:
  207. float fGain;
  208. };
  209. @endcode
  210. See the Parameter struct for more information about parameters.
  211. @section Programs
  212. Programs in DPF refer to plugin-side presets (usually called "factory presets"),
  213. an initial set of presets provided by plugin authors included in the actual plugin.
  214. To use programs you must first enable them by setting @ref DISTRHO_PLUGIN_WANT_PROGRAMS to 1 in your DistrhoPluginInfo.h file.@n
  215. When enabled you'll need to override 2 new function in your plugin code,
  216. Plugin::initProgramName(uint32_t, String&) and Plugin::loadProgram(uint32_t).
  217. Here's an example of a plugin with a "default" program:
  218. @code
  219. class PluginWithPresets : public Plugin
  220. {
  221. public:
  222. PluginWithPresets()
  223. : Plugin(2, 1, 0), // 2 parameters, 1 program and 0 states
  224. fGainL(1.0f),
  225. fGainR(1.0f),
  226. {
  227. }
  228. protected:
  229. /* ----------------------------------------------------------------------------------------
  230. * Information */
  231. const char* getLabel() const override
  232. {
  233. return "Prog";
  234. }
  235. const char* getMaker() const override
  236. {
  237. return "DPF";
  238. }
  239. const char* getLicense() const override
  240. {
  241. return "MIT";
  242. }
  243. uint32_t getVersion() const override
  244. {
  245. return d_version(1, 0, 0);
  246. }
  247. int64_t getUniqueId() const override
  248. {
  249. return d_cconst('P', 'r', 'o', 'g');
  250. }
  251. /* ----------------------------------------------------------------------------------------
  252. * Init */
  253. /**
  254. Initialize a parameter.
  255. This function will be called once, shortly after the plugin is created.
  256. */
  257. void initParameter(uint32_t index, Parameter& parameter) override
  258. {
  259. parameter.hints = kParameterIsAutomable;
  260. parameter.ranges.min = 0.0f;
  261. parameter.ranges.max = 2.0f;
  262. parameter.ranges.def = 1.0f;
  263. switch (index)
  264. {
  265. case 0;
  266. parameter.name = "Gain Right";
  267. parameter.symbol = "gainR";
  268. break;
  269. case 1;
  270. parameter.name = "Gain Left";
  271. parameter.symbol = "gainL";
  272. break;
  273. }
  274. }
  275. /**
  276. Set the name of the program @a index.
  277. This function will be called once, shortly after the plugin is created.
  278. */
  279. void initProgramName(uint32_t index, String& programName)
  280. {
  281. switch(index)
  282. {
  283. case 0:
  284. programName = "Default";
  285. break;
  286. }
  287. }
  288. /* ----------------------------------------------------------------------------------------
  289. * Internal data */
  290. /**
  291. Get the current value of a parameter.
  292. */
  293. float getParameterValue(uint32_t index) const override
  294. {
  295. switch (index)
  296. {
  297. case 0;
  298. return fGainL;
  299. case 1;
  300. return fGainR;
  301. }
  302. }
  303. /**
  304. Change a parameter value.
  305. */
  306. void setParameterValue(uint32_t index, float value) override
  307. {
  308. switch (index)
  309. {
  310. case 0;
  311. fGainL = value;
  312. break;
  313. case 1;
  314. fGainR = value;
  315. break;
  316. }
  317. }
  318. /**
  319. Load a program.
  320. */
  321. void loadProgram(uint32_t index)
  322. {
  323. switch(index)
  324. {
  325. case 0:
  326. fGainL = 1.0f;
  327. fGainR = 1.0f;
  328. break;
  329. }
  330. }
  331. /* ----------------------------------------------------------------------------------------
  332. * Audio/MIDI Processing */
  333. void run(const float**, float** outputs, uint32_t frames) override
  334. {
  335. // get the left and right audio buffers
  336. const float* const inL = inputs[0];
  337. const float* const inR = inputs[0];
  338. /* */ float* const outL = outputs[0];
  339. /* */ float* const outR = outputs[0];
  340. // apply gain against all samples
  341. for (uint32_t i=0; i < frames; ++i)
  342. {
  343. outL[i] = inL[i] * fGainL;
  344. outR[i] = inR[i] * fGainR;
  345. }
  346. }
  347. private:
  348. float fGainL, fGainR;
  349. };
  350. @endcode
  351. @section States
  352. describe them
  353. @section MIDI
  354. describe them
  355. @section Latency
  356. describe it
  357. @section Time-Position
  358. describe it
  359. @section UI
  360. describe them
  361. */
  362. /* ------------------------------------------------------------------------------------------------------------
  363. * Plugin Macros */
  364. /**
  365. @defgroup PluginMacros Plugin Macros
  366. C Macros that describe your plugin. (defined in the "DistrhoPluginInfo.h" file)
  367. With these macros you can tell the host what features your plugin requires.@n
  368. Depending on which macros you enable, new functions will be available to call and/or override.
  369. All values are either integer or strings.@n
  370. For boolean-like values 1 means 'on' and 0 means 'off'.
  371. The values defined in this group are for documentation purposes only.@n
  372. All macros are disabled by default.
  373. Only 4 macros are required, they are:
  374. - @ref DISTRHO_PLUGIN_NAME
  375. - @ref DISTRHO_PLUGIN_NUM_INPUTS
  376. - @ref DISTRHO_PLUGIN_NUM_OUTPUTS
  377. - @ref DISTRHO_PLUGIN_URI
  378. @{
  379. */
  380. /**
  381. The plugin name.@n
  382. This is used to identify your plugin before a Plugin instance can be created.
  383. @note This macro is required.
  384. */
  385. #define DISTRHO_PLUGIN_NAME "Plugin Name"
  386. /**
  387. Number of audio inputs the plugin has.
  388. @note This macro is required.
  389. */
  390. #define DISTRHO_PLUGIN_NUM_INPUTS 2
  391. /**
  392. Number of audio outputs the plugin has.
  393. @note This macro is required.
  394. */
  395. #define DISTRHO_PLUGIN_NUM_OUTPUTS 2
  396. /**
  397. The plugin URI when exporting in LV2 format.
  398. @note This macro is required.
  399. */
  400. #define DISTRHO_PLUGIN_URI "urn:distrho:name"
  401. /**
  402. Wherever the plugin has a custom %UI.
  403. @see DISTRHO_UI_USE_NANOVG
  404. @see UI
  405. */
  406. #define DISTRHO_PLUGIN_HAS_UI 1
  407. /**
  408. Wherever the plugin processing is realtime-safe.@n
  409. TODO - list rtsafe requirements
  410. */
  411. #define DISTRHO_PLUGIN_IS_RT_SAFE 1
  412. /**
  413. Wherever the plugin is a synth.@n
  414. @ref DISTRHO_PLUGIN_WANT_MIDI_INPUT is automatically enabled when this is too.
  415. @see DISTRHO_PLUGIN_WANT_MIDI_INPUT
  416. */
  417. #define DISTRHO_PLUGIN_IS_SYNTH 1
  418. /**
  419. Enable direct access between the %UI and plugin code.
  420. @see UI::getPluginInstancePointer()
  421. @note DO NOT USE THIS UNLESS STRICTLY NECESSARY!!
  422. Try to avoid it at all costs!
  423. */
  424. #define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 0
  425. /**
  426. Wherever the plugin introduces latency during audio or midi processing.
  427. @see Plugin::setLatency(uint32_t)
  428. */
  429. #define DISTRHO_PLUGIN_WANT_LATENCY 1
  430. /**
  431. Wherever the plugin wants MIDI input.@n
  432. This is automatically enabled if @ref DISTRHO_PLUGIN_IS_SYNTH is true.
  433. */
  434. #define DISTRHO_PLUGIN_WANT_MIDI_INPUT 1
  435. /**
  436. Wherever the plugin wants MIDI output.
  437. @see Plugin::writeMidiEvent(const MidiEvent&)
  438. */
  439. #define DISTRHO_PLUGIN_WANT_MIDI_OUTPUT 1
  440. /**
  441. Wherever the plugin provides its own internal programs.
  442. @see Plugin::initProgramName(uint32_t, String&)
  443. @see Plugin::loadProgram(uint32_t)
  444. */
  445. #define DISTRHO_PLUGIN_WANT_PROGRAMS 1
  446. /**
  447. Wherever the plugin uses internal non-parameter data.
  448. @see Plugin::initState(uint32_t, String&, String&)
  449. @see Plugin::setState(const char*, const char*)
  450. */
  451. #define DISTRHO_PLUGIN_WANT_STATE 1
  452. /**
  453. Wherever the plugin wants time position information from the host.
  454. @see Plugin::getTimePosition()
  455. */
  456. #define DISTRHO_PLUGIN_WANT_TIMEPOS 1
  457. /**
  458. Wherever the %UI uses NanoVG for drawing instead of the default raw OpenGL calls.@n
  459. When enabled your %UI instance will subclass @ref NanoWidget instead of @ref Widget.
  460. */
  461. #define DISTRHO_UI_USE_NANOVG 1
  462. /**
  463. The %UI URI when exporting in LV2 format.@n
  464. By default this is set to @ref DISTRHO_PLUGIN_URI with "#UI" as suffix.
  465. */
  466. #define DISTRHO_UI_URI DISTRHO_PLUGIN_URI "#UI"
  467. /** @} */
  468. // -----------------------------------------------------------------------------------------------------------
  469. END_NAMESPACE_DISTRHO
  470. #endif // DOXYGEN