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.

572 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. TODO format to be defined
  79. */
  80. uint32_t getVersion() const override
  81. {
  82. return 0x1000;
  83. }
  84. /**
  85. Get the plugin unique Id.
  86. This value is used by LADSPA, DSSI and VST plugin formats.
  87. */
  88. int64_t getUniqueId() const override
  89. {
  90. return cconst('M', 'u', 't', 'e');
  91. }
  92. /* ----------------------------------------------------------------------------------------
  93. * This example has no parameters, so skip parameter stuff */
  94. void initParameter(uint32_t, Parameter&) override {}
  95. float getParameterValue(uint32_t) const override { return 0.0f; }
  96. void setParameterValue(uint32_t, float) override {}
  97. /* ----------------------------------------------------------------------------------------
  98. * Audio/MIDI Processing */
  99. /**
  100. Run/process function for plugins without MIDI input.
  101. NOTE: Some parameters might be null if there are no audio inputs or outputs.
  102. */
  103. void run(const float**, float** outputs, uint32_t frames) override
  104. {
  105. // get the left and right audio outputs
  106. float* const outL = outputs[0];
  107. float* const outR = outputs[1];
  108. // mute audio
  109. std::memset(outL, 0, sizeof(float)*frames);
  110. std::memset(outR, 0, sizeof(float)*frames);
  111. }
  112. };
  113. @endcode
  114. See the Plugin class for more information and to understand what each function does.
  115. @section Parameters
  116. A plugin is nothing without parameters.@n
  117. In DPF parameters can be inputs or outputs.@n
  118. They have hints to describe how they behave plus a name and a symbol identifying them.@n
  119. Parameters also have 'ranges' – a minimum, maximum and default value.
  120. Input parameters are "read-only": the plugin can read them but not change them.
  121. (the exception being when changing programs, more on that below)@n
  122. It's the host responsibility to save, restore and set input parameters.
  123. Output parameters can be changed at anytime by the plugin.@n
  124. The host will simply read their values and not change them.
  125. Here's an example of an audio plugin that has 1 input parameter:
  126. @code
  127. class GainPlugin : public Plugin
  128. {
  129. public:
  130. /**
  131. Plugin class constructor.
  132. You must set all parameter values to their defaults, matching ParameterRanges::def.
  133. */
  134. GainPlugin()
  135. : Plugin(1, 0, 0), // 1 parameter, 0 programs and 0 states
  136. fGain(1.0f)
  137. {
  138. }
  139. protected:
  140. /* ----------------------------------------------------------------------------------------
  141. * Information */
  142. const char* getLabel() const override
  143. {
  144. return "Gain";
  145. }
  146. const char* getMaker() const override
  147. {
  148. return "DPF";
  149. }
  150. const char* getLicense() const override
  151. {
  152. return "MIT";
  153. }
  154. uint32_t getVersion() const override
  155. {
  156. return 0x1000;
  157. }
  158. int64_t getUniqueId() const override
  159. {
  160. return cconst('G', 'a', 'i', 'n');
  161. }
  162. /* ----------------------------------------------------------------------------------------
  163. * Init */
  164. /**
  165. Initialize a parameter.
  166. This function will be called once, shortly after the plugin is created.
  167. */
  168. void initParameter(uint32_t index, Parameter& parameter) override
  169. {
  170. // we only have one parameter so we can skip checking the index
  171. parameter.hints = kParameterIsAutomable;
  172. parameter.name = "Gain";
  173. parameter.symbol = "gain";
  174. parameter.ranges.min = 0.0f;
  175. parameter.ranges.max = 2.0f;
  176. parameter.ranges.def = 1.0f;
  177. }
  178. /* ----------------------------------------------------------------------------------------
  179. * Internal data */
  180. /**
  181. Get the current value of a parameter.
  182. */
  183. float getParameterValue(uint32_t index) const override
  184. {
  185. // same as before, ignore index check
  186. return fGain;
  187. }
  188. /**
  189. Change a parameter value.
  190. */
  191. void setParameterValue(uint32_t index, float value) override
  192. {
  193. // same as before, ignore index check
  194. fGain = value;
  195. }
  196. /* ----------------------------------------------------------------------------------------
  197. * Audio/MIDI Processing */
  198. void run(const float**, float** outputs, uint32_t frames) override
  199. {
  200. // get the mono input and output
  201. const float* const in = inputs[0];
  202. /* */ float* const out = outputs[0];
  203. // apply gain against all samples
  204. for (uint32_t i=0; i < frames; ++i)
  205. out[i] = in[i] * fGain;
  206. }
  207. private:
  208. float fGain;
  209. };
  210. @endcode
  211. See the Parameter struct for more information about parameters.
  212. @section Programs
  213. Programs in DPF refer to plugin-side presets (usually called "factory presets"),
  214. an initial set of presets provided by plugin authors included in the actual plugin.
  215. To use programs you must first enable them by setting @ref DISTRHO_PLUGIN_WANT_PROGRAMS to 1 in your DistrhoPluginInfo.h file.@n
  216. When enabled you'll need to override 2 new function in your plugin code,
  217. Plugin::initProgramName(uint32_t, String&) and Plugin::loadProgram(uint32_t).
  218. Here's an example of a plugin with a "default" program:
  219. @code
  220. class PluginWithPresets : public Plugin
  221. {
  222. public:
  223. PluginWithPresets()
  224. : Plugin(2, 1, 0), // 2 parameters, 1 program and 0 states
  225. fGainL(1.0f),
  226. fGainR(1.0f),
  227. {
  228. }
  229. protected:
  230. /* ----------------------------------------------------------------------------------------
  231. * Information */
  232. const char* getLabel() const override
  233. {
  234. return "Prog";
  235. }
  236. const char* getMaker() const override
  237. {
  238. return "DPF";
  239. }
  240. const char* getLicense() const override
  241. {
  242. return "MIT";
  243. }
  244. uint32_t getVersion() const override
  245. {
  246. return 0x1000;
  247. }
  248. int64_t getUniqueId() const override
  249. {
  250. return cconst('P', 'r', 'o', 'g');
  251. }
  252. /* ----------------------------------------------------------------------------------------
  253. * Init */
  254. /**
  255. Initialize a parameter.
  256. This function will be called once, shortly after the plugin is created.
  257. */
  258. void initParameter(uint32_t index, Parameter& parameter) override
  259. {
  260. parameter.hints = kParameterIsAutomable;
  261. parameter.ranges.min = 0.0f;
  262. parameter.ranges.max = 2.0f;
  263. parameter.ranges.def = 1.0f;
  264. switch (index)
  265. {
  266. case 0;
  267. parameter.name = "Gain Right";
  268. parameter.symbol = "gainR";
  269. break;
  270. case 1;
  271. parameter.name = "Gain Left";
  272. parameter.symbol = "gainL";
  273. break;
  274. }
  275. }
  276. /**
  277. Set the name of the program @a index.
  278. This function will be called once, shortly after the plugin is created.
  279. */
  280. void initProgramName(uint32_t index, String& programName)
  281. {
  282. switch(index)
  283. {
  284. case 0:
  285. programName = "Default";
  286. break;
  287. }
  288. }
  289. /* ----------------------------------------------------------------------------------------
  290. * Internal data */
  291. /**
  292. Get the current value of a parameter.
  293. */
  294. float getParameterValue(uint32_t index) const override
  295. {
  296. switch (index)
  297. {
  298. case 0;
  299. return fGainL;
  300. case 1;
  301. return fGainR;
  302. }
  303. }
  304. /**
  305. Change a parameter value.
  306. */
  307. void setParameterValue(uint32_t index, float value) override
  308. {
  309. switch (index)
  310. {
  311. case 0;
  312. fGainL = value;
  313. break;
  314. case 1;
  315. fGainR = value;
  316. break;
  317. }
  318. }
  319. /**
  320. Load a program.
  321. */
  322. void loadProgram(uint32_t index)
  323. {
  324. switch(index)
  325. {
  326. case 0:
  327. fGainL = 1.0f;
  328. fGainR = 1.0f;
  329. break;
  330. }
  331. }
  332. /* ----------------------------------------------------------------------------------------
  333. * Audio/MIDI Processing */
  334. void run(const float**, float** outputs, uint32_t frames) override
  335. {
  336. // get the left and right audio buffers
  337. const float* const inL = inputs[0];
  338. const float* const inR = inputs[0];
  339. /* */ float* const outL = outputs[0];
  340. /* */ float* const outR = outputs[0];
  341. // apply gain against all samples
  342. for (uint32_t i=0; i < frames; ++i)
  343. {
  344. outL[i] = inL[i] * fGainL;
  345. outR[i] = inR[i] * fGainR;
  346. }
  347. }
  348. private:
  349. float fGainL, fGainR;
  350. };
  351. @endcode
  352. @section States
  353. describe them
  354. @section MIDI
  355. describe them
  356. @section Latency
  357. describe it
  358. @section Time-Position
  359. describe it
  360. @section UI
  361. describe them
  362. */
  363. /* ------------------------------------------------------------------------------------------------------------
  364. * Plugin Macros */
  365. /**
  366. @defgroup PluginMacros Plugin Macros
  367. C Macros that describe your plugin. (defined in the "DistrhoPluginInfo.h" file)
  368. With these macros you can tell the host what features your plugin requires.@n
  369. Depending on which macros you enable, new functions will be available to call and/or override.
  370. All values are either integer or strings.@n
  371. For boolean-like values 1 means 'on' and 0 means 'off'.
  372. The values defined in this group are for documentation purposes only.@n
  373. All macros are disabled by default.
  374. Only 4 macros are required, they are:
  375. - @ref DISTRHO_PLUGIN_NAME
  376. - @ref DISTRHO_PLUGIN_NUM_INPUTS
  377. - @ref DISTRHO_PLUGIN_NUM_OUTPUTS
  378. - @ref DISTRHO_PLUGIN_URI
  379. @{
  380. */
  381. /**
  382. The plugin name.@n
  383. This is used to identify your plugin before a Plugin instance can be created.
  384. @note This macro is required.
  385. */
  386. #define DISTRHO_PLUGIN_NAME "Plugin Name"
  387. /**
  388. Number of audio inputs the plugin has.
  389. @note This macro is required.
  390. */
  391. #define DISTRHO_PLUGIN_NUM_INPUTS 2
  392. /**
  393. Number of audio outputs the plugin has.
  394. @note This macro is required.
  395. */
  396. #define DISTRHO_PLUGIN_NUM_OUTPUTS 2
  397. /**
  398. The plugin URI when exporting in LV2 format.
  399. @note This macro is required.
  400. */
  401. #define DISTRHO_PLUGIN_URI "urn:distrho:name"
  402. /**
  403. Wherever the plugin has a custom %UI.
  404. @see DISTRHO_UI_USE_NANOVG
  405. @see UI
  406. */
  407. #define DISTRHO_PLUGIN_HAS_UI 1
  408. /**
  409. Wherever the plugin processing is realtime-safe.@n
  410. TODO - list rtsafe requirements
  411. */
  412. #define DISTRHO_PLUGIN_IS_RT_SAFE 1
  413. /**
  414. Wherever the plugin is a synth.@n
  415. @ref DISTRHO_PLUGIN_WANT_MIDI_INPUT is automatically enabled when this is too.
  416. @see DISTRHO_PLUGIN_WANT_MIDI_INPUT
  417. */
  418. #define DISTRHO_PLUGIN_IS_SYNTH 1
  419. /**
  420. Enable direct access between the %UI and plugin code.
  421. @see UI::getPluginInstancePointer()
  422. @note DO NOT USE THIS UNLESS STRICTLY NECESSARY!!
  423. Try to avoid it at all costs!
  424. */
  425. #define DISTRHO_PLUGIN_WANT_DIRECT_ACCESS 0
  426. /**
  427. Wherever the plugin introduces latency during audio or midi processing.
  428. @see Plugin::setLatency(uint32_t)
  429. */
  430. #define DISTRHO_PLUGIN_WANT_LATENCY 1
  431. /**
  432. Wherever the plugin wants MIDI input.@n
  433. This is automatically enabled if @ref DISTRHO_PLUGIN_IS_SYNTH is true.
  434. */
  435. #define DISTRHO_PLUGIN_WANT_MIDI_INPUT 1
  436. /**
  437. Wherever the plugin wants MIDI output.
  438. @see Plugin::writeMidiEvent(const MidiEvent&)
  439. */
  440. #define DISTRHO_PLUGIN_WANT_MIDI_OUTPUT 1
  441. /**
  442. Wherever the plugin provides its own internal programs.
  443. @see Plugin::initProgramName(uint32_t, String&)
  444. @see Plugin::loadProgram(uint32_t)
  445. */
  446. #define DISTRHO_PLUGIN_WANT_PROGRAMS 1
  447. /**
  448. Wherever the plugin uses internal non-parameter data.
  449. @see Plugin::initState(uint32_t, String&, String&)
  450. @see Plugin::setState(const char*, const char*)
  451. */
  452. #define DISTRHO_PLUGIN_WANT_STATE 1
  453. /**
  454. Wherever the plugin wants time position information from the host.
  455. @see Plugin::getTimePosition()
  456. */
  457. #define DISTRHO_PLUGIN_WANT_TIMEPOS 1
  458. /**
  459. Wherever the %UI uses NanoVG for drawing instead of the default raw OpenGL calls.@n
  460. When enabled your %UI instance will subclass @ref NanoWidget instead of @ref Widget.
  461. */
  462. #define DISTRHO_UI_USE_NANOVG 1
  463. /**
  464. The %UI URI when exporting in LV2 format.@n
  465. By default this is set to @ref DISTRHO_PLUGIN_URI with "#UI" as suffix.
  466. */
  467. #define DISTRHO_UI_URI DISTRHO_PLUGIN_URI "#UI"
  468. /** @} */
  469. // -----------------------------------------------------------------------------------------------------------
  470. END_NAMESPACE_DISTRHO
  471. #endif // DOXYGEN