DISTRHO Plugin Framework
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.

258 lines
7.4KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2020 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. #include "DistrhoPlugin.hpp"
  17. START_NAMESPACE_DISTRHO
  18. // -----------------------------------------------------------------------------------------------------------
  19. /**
  20. Plugin to demonstrate File handling within DPF.
  21. */
  22. class FileHandlingExamplePlugin : public Plugin
  23. {
  24. public:
  25. FileHandlingExamplePlugin()
  26. : Plugin(kParameterCount, 0, kStateCount)
  27. {
  28. std::memset(fParameters, 0, sizeof(fParameters));
  29. }
  30. protected:
  31. /* --------------------------------------------------------------------------------------------------------
  32. * Information */
  33. /**
  34. Get the plugin label.
  35. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  36. */
  37. const char* getLabel() const override
  38. {
  39. return "FileHandling";
  40. }
  41. /**
  42. Get an extensive comment/description about the plugin.
  43. */
  44. const char* getDescription() const override
  45. {
  46. return "Plugin to demonstrate File handling.";
  47. }
  48. /**
  49. Get the plugin author/maker.
  50. */
  51. const char* getMaker() const override
  52. {
  53. return "DISTRHO";
  54. }
  55. /**
  56. Get the plugin homepage.
  57. */
  58. const char* getHomePage() const override
  59. {
  60. return "https://github.com/DISTRHO/DPF";
  61. }
  62. /**
  63. Get the plugin license name (a single line of text).
  64. For commercial plugins this should return some short copyright information.
  65. */
  66. const char* getLicense() const override
  67. {
  68. return "ISC";
  69. }
  70. /**
  71. Get the plugin version, in hexadecimal.
  72. */
  73. uint32_t getVersion() const override
  74. {
  75. return d_version(0, 0, 0);
  76. }
  77. /**
  78. Get the plugin unique Id.
  79. This value is used by LADSPA, DSSI and VST plugin formats.
  80. */
  81. int64_t getUniqueId() const override
  82. {
  83. return d_cconst('d', 'F', 'i', 'H');
  84. }
  85. /* --------------------------------------------------------------------------------------------------------
  86. * Init */
  87. /**
  88. Initialize the parameter @a index.
  89. This function will be called once, shortly after the plugin is created.
  90. */
  91. void initParameter(uint32_t index, Parameter& param) override
  92. {
  93. param.hints = kParameterIsOutput|kParameterIsInteger;
  94. switch (index)
  95. {
  96. case kParameterFileSize1:
  97. param.name = "Size #1";
  98. param.symbol = "size1";
  99. break;
  100. case kParameterFileSize2:
  101. param.name = "Size #2";
  102. param.symbol = "size2";
  103. break;
  104. case kParameterFileSize3:
  105. param.name = "Size #3";
  106. param.symbol = "size3";
  107. break;
  108. }
  109. }
  110. /**
  111. Set the state key and default value of @a index.@n
  112. This function will be called once, shortly after the plugin is created.@n
  113. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  114. */
  115. void initState(uint32_t index, String& stateKey, String& defaultStateValue) override
  116. {
  117. switch (index)
  118. {
  119. case kStateFile1:
  120. stateKey = "file1";
  121. break;
  122. case kStateFile2:
  123. stateKey = "file2";
  124. break;
  125. case kStateFile3:
  126. stateKey = "file3";
  127. break;
  128. }
  129. defaultStateValue = "";
  130. }
  131. /**
  132. TODO API under construction
  133. */
  134. bool isStateFile(uint32_t index) override
  135. {
  136. switch (index)
  137. {
  138. case kStateFile1:
  139. case kStateFile2:
  140. case kStateFile3:
  141. return true;
  142. }
  143. return false;
  144. }
  145. /* --------------------------------------------------------------------------------------------------------
  146. * Internal data */
  147. /**
  148. Get the current value of a parameter.
  149. The host may call this function from any context, including realtime processing.
  150. We have no parameters in this plugin example, so we do nothing with the function.
  151. */
  152. float getParameterValue(uint32_t index) const override
  153. {
  154. return fParameters[index];
  155. }
  156. /**
  157. Change a parameter value.@n
  158. The host may call this function from any context, including realtime processing.
  159. This function will only be called for parameter inputs.
  160. Since we have no parameters inputs in this example, so we do nothing with the function.
  161. */
  162. void setParameterValue(uint32_t, float) override {}
  163. /**
  164. Change an internal state @a key to @a value.
  165. */
  166. void setState(const char* key, const char* value) override
  167. {
  168. d_stdout("DSP setState %s %s", key, value);
  169. int fileId = -1;
  170. /**/ if (std::strcmp(key, "file1") == 0)
  171. fileId = 0;
  172. else if (std::strcmp(key, "file2") == 0)
  173. fileId = 1;
  174. else if (std::strcmp(key, "file3") == 0)
  175. fileId = 2;
  176. if (fileId == -1)
  177. return;
  178. if (FILE* const fh = fopen(value, "r"))
  179. {
  180. fseek(fh, 0, SEEK_END);
  181. // filesize
  182. const long int size = ftell(fh);
  183. d_stdout("size of %s is %li", value, size);
  184. fParameters[kParameterFileSize1 + fileId] = static_cast<float>(size) / 1000.0f;
  185. fclose(fh);
  186. }
  187. }
  188. /* --------------------------------------------------------------------------------------------------------
  189. * Audio/MIDI Processing */
  190. /**
  191. Run/process function for plugins without MIDI input.
  192. @note Some parameters might be null if there are no audio inputs or outputs.
  193. */
  194. void run(const float** inputs, float** outputs, uint32_t frames) override
  195. {
  196. /**
  197. This plugin doesn't do audio, it just demonstrates file handling usage.
  198. So here we directly copy inputs over outputs, leaving the audio untouched.
  199. We need to be careful in case the host re-uses the same buffer for both ins and outs.
  200. */
  201. if (outputs[0] != inputs[0])
  202. std::memcpy(outputs[0], inputs[0], sizeof(float)*frames);
  203. }
  204. // -------------------------------------------------------------------------------------------------------
  205. private:
  206. float fParameters[kParameterCount];
  207. /**
  208. Set our plugin class as non-copyable and add a leak detector just in case.
  209. */
  210. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileHandlingExamplePlugin)
  211. };
  212. /* ------------------------------------------------------------------------------------------------------------
  213. * Plugin entry point, called by DPF to create a new plugin instance. */
  214. Plugin* createPlugin()
  215. {
  216. return new FileHandlingExamplePlugin();
  217. }
  218. // -----------------------------------------------------------------------------------------------------------
  219. END_NAMESPACE_DISTRHO