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.

238 lines
7.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2025 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. #ifndef DISTRHO_PLUGIN_UTILS_HPP_INCLUDED
  17. #define DISTRHO_PLUGIN_UTILS_HPP_INCLUDED
  18. #include "DistrhoPlugin.hpp"
  19. START_NAMESPACE_DISTRHO
  20. /* ------------------------------------------------------------------------------------------------------------
  21. * Plugin related utilities */
  22. /**
  23. @defgroup PluginRelatedUtilities Plugin related utilities
  24. @{
  25. */
  26. /**
  27. Get the absolute filename of the plugin DSP/UI binary.@n
  28. Under certain systems or plugin formats the binary will be inside the plugin bundle.@n
  29. Also, in some formats or setups, the DSP and UI binaries are in different files.
  30. */
  31. const char* getBinaryFilename();
  32. /**
  33. Get an OS-specific directory intended to store persistent configuration data about the plugin.@n
  34. Calling this function will ensure the dictory exists on the filesystem.@n
  35. The returned path already includes DISTRHO_PLUGIN_NAME and final OS separator.
  36. */
  37. const char* getConfigDir();
  38. /**
  39. Get an OS-specific directory intended to store "documents" for the plugin.@n
  40. Calling this function will ensure the dictory exists on the filesystem.@n
  41. The returned path already includes DISTRHO_PLUGIN_NAME and final OS separator.
  42. */
  43. const char* getDocumentsDir();
  44. /**
  45. Get the user "home" directory.@n
  46. This function is provided only for convenience, it should not be needed under normal circunstances.
  47. */
  48. const char* getHomeDir();
  49. /**
  50. Get a string representation of the current plugin format we are building against.@n
  51. This can be "AudioUnit", "JACK/Standalone", "LADSPA", "DSSI", "LV2", "VST2" or "VST3" or "CLAP".@n
  52. This string is purely informational and must not be used to tweak plugin behaviour.
  53. @note DO NOT CHANGE PLUGIN BEHAVIOUR BASED ON PLUGIN FORMAT.
  54. */
  55. const char* getPluginFormatName() noexcept;
  56. /**
  57. Get the path to where resources are stored within the plugin bundle.@n
  58. Requires a valid plugin bundle path.
  59. Returns a path inside the bundle where the plugin is meant to store its resources in.@n
  60. This path varies between systems and plugin formats, like so:
  61. - AU: <bundle>/Contents/Resources
  62. - CLAP+VST2 macOS: <bundle>/Contents/Resources
  63. - CLAP+VST2 non-macOS: <bundle>/resources (see note)
  64. - LV2: <bundle>/resources (can be stored anywhere inside the bundle really, DPF just uses this one)
  65. - VST3: <bundle>/Contents/Resources
  66. The other non-mentioned formats do not support bundles.@n
  67. @note For CLAP and VST2 on non-macOS systems, this assumes you have your plugin inside a dedicated directory
  68. rather than only shipping with the binary (e.g. <myplugin.vst>/myplugin.dll)
  69. */
  70. const char* getResourcePath(const char* bundlePath) noexcept;
  71. /** @} */
  72. /* ------------------------------------------------------------------------------------------------------------
  73. * Plugin helper classes */
  74. /**
  75. @defgroup PluginHelperClasses Plugin helper classes
  76. @{
  77. */
  78. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  79. /**
  80. Handy class to help keep audio buffer in sync with incoming MIDI events.
  81. To use it, create a local variable (on the stack) and call nextEvent() until it returns false.
  82. @code
  83. for (AudioMidiSyncHelper amsh(outputs, frames, midiEvents, midiEventCount); amsh.nextEvent();)
  84. {
  85. float* const outL = amsh.outputs[0];
  86. float* const outR = amsh.outputs[1];
  87. for (uint32_t i=0; i<amsh.midiEventCount; ++i)
  88. {
  89. const MidiEvent& ev(amsh.midiEvents[i]);
  90. // ... do something with the midi event
  91. }
  92. renderSynth(outL, outR, amsh.frames);
  93. }
  94. @endcode
  95. Some important notes when using this class:
  96. 1. MidiEvent::frame retains its original value, but it is useless, do not use it.
  97. 2. The class variable names are the same as the default ones in the run function.
  98. Keep that in mind and try to avoid typos. :)
  99. */
  100. struct AudioMidiSyncHelper
  101. {
  102. /** Parameters from the run function, adjusted for event sync */
  103. float* outputs[DISTRHO_PLUGIN_NUM_OUTPUTS];
  104. uint32_t frames;
  105. const MidiEvent* midiEvents;
  106. uint32_t midiEventCount;
  107. /**
  108. Constructor, using values from the run function.
  109. */
  110. AudioMidiSyncHelper(float** const o, uint32_t f, const MidiEvent* m, uint32_t mc)
  111. : outputs(),
  112. frames(0),
  113. midiEvents(m),
  114. midiEventCount(0),
  115. remainingFrames(f),
  116. remainingMidiEventCount(mc),
  117. totalFramesUsed(0)
  118. {
  119. for (uint i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  120. outputs[i] = o[i];
  121. }
  122. /**
  123. Process a batch of events untill no more are available.
  124. You must not read any more values from this class after this function returns false.
  125. */
  126. bool nextEvent()
  127. {
  128. // nothing else to do
  129. if (remainingFrames == 0)
  130. return false;
  131. // initial setup, need to find first MIDI event
  132. if (totalFramesUsed == 0)
  133. {
  134. // no MIDI events at all in this process cycle
  135. if (remainingMidiEventCount == 0)
  136. {
  137. frames = remainingFrames;
  138. remainingFrames = 0;
  139. totalFramesUsed += frames;
  140. return true;
  141. }
  142. // render audio until first midi event, if needed
  143. if (const uint32_t firstEventFrame = midiEvents[0].frame)
  144. {
  145. DISTRHO_SAFE_ASSERT_UINT2_RETURN(firstEventFrame < remainingFrames,
  146. firstEventFrame, remainingFrames, false);
  147. frames = firstEventFrame;
  148. remainingFrames -= firstEventFrame;
  149. totalFramesUsed += firstEventFrame;
  150. return true;
  151. }
  152. }
  153. else
  154. {
  155. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  156. outputs[i] += frames;
  157. }
  158. // no more MIDI events available
  159. if (remainingMidiEventCount == 0)
  160. {
  161. frames = remainingFrames;
  162. midiEvents = nullptr;
  163. midiEventCount = 0;
  164. remainingFrames = 0;
  165. totalFramesUsed += frames;
  166. return true;
  167. }
  168. // if there were midi events before, increment pointer
  169. if (midiEventCount != 0)
  170. midiEvents += midiEventCount;
  171. const uint32_t firstEventFrame = midiEvents[0].frame;
  172. DISTRHO_SAFE_ASSERT_UINT2_RETURN(firstEventFrame >= totalFramesUsed,
  173. firstEventFrame, totalFramesUsed, false);
  174. midiEventCount = 1;
  175. while (midiEventCount < remainingMidiEventCount)
  176. {
  177. if (midiEvents[midiEventCount].frame == firstEventFrame)
  178. ++midiEventCount;
  179. else
  180. break;
  181. }
  182. frames = firstEventFrame - totalFramesUsed;
  183. remainingFrames -= frames;
  184. remainingMidiEventCount -= midiEventCount;
  185. totalFramesUsed += frames;
  186. return true;
  187. }
  188. private:
  189. /** @internal */
  190. uint32_t remainingFrames;
  191. uint32_t remainingMidiEventCount;
  192. uint32_t totalFramesUsed;
  193. };
  194. #endif
  195. /** @} */
  196. // -----------------------------------------------------------------------------------------------------------
  197. END_NAMESPACE_DISTRHO
  198. #endif // DISTRHO_PLUGIN_UTILS_HPP_INCLUDED