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.

216 lines
7.1KB

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