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.

160 lines
5.3KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 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. /**
  22. Handy class to help keep audio buffer in sync with incoming MIDI events.
  23. To use it, create a local variable (on the stack) and call nextEvent() until it returns false.
  24. @code
  25. for (AudioMidiSyncHelper amsh(outputs, frames, midiEvents, midiEventCount); amsh.nextEvent();)
  26. {
  27. float* const outL = amsh.outputs[0];
  28. float* const outR = amsh.outputs[1];
  29. for (uint32_t i=0; i<amsh.midiEventCount; ++i)
  30. {
  31. const MidiEvent& ev(amsh.midiEvents[i]);
  32. // ... do something with the midi event
  33. }
  34. renderSynth(outL, outR, amsh.frames);
  35. }
  36. @endcode
  37. Some important notes when using this class:
  38. 1. MidiEvent::frame retains its original value, but it is useless, do not use it.
  39. 2. The class variables names are be the same as the default ones in the run function.
  40. Keep that in mind and try to avoid typos. :)
  41. */
  42. class AudioMidiSyncHelper {
  43. public:
  44. /** Parameters from the run function, adjusted for event sync */
  45. float* outputs[DISTRHO_PLUGIN_NUM_OUTPUTS];
  46. uint32_t frames;
  47. const MidiEvent* midiEvents;
  48. uint32_t midiEventCount;
  49. /**
  50. Constructor, using values from the run function.
  51. */
  52. AudioMidiSyncHelper(float** const o, uint32_t f, const MidiEvent* m, uint32_t mc)
  53. : outputs(),
  54. frames(0),
  55. midiEvents(m),
  56. midiEventCount(0),
  57. remainingFrames(f),
  58. remainingMidiEventCount(mc),
  59. totalFramesUsed(0)
  60. {
  61. for (uint i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  62. outputs[i] = o[i];
  63. }
  64. /**
  65. Process a batch of events untill no more are available.
  66. You must not read any more values from this class after this function returns false.
  67. */
  68. bool nextEvent()
  69. {
  70. // nothing else to do
  71. if (remainingFrames == 0)
  72. return false;
  73. // initial setup, need to find first MIDI event
  74. if (totalFramesUsed == 0)
  75. {
  76. // no MIDI events at all in this process cycle
  77. if (remainingMidiEventCount == 0)
  78. {
  79. frames = remainingFrames;
  80. remainingFrames = 0;
  81. totalFramesUsed += frames;
  82. return true;
  83. }
  84. // render audio until first midi event, if needed
  85. if (const uint32_t firstEventFrame = midiEvents[0].frame)
  86. {
  87. DISTRHO_SAFE_ASSERT_UINT2_RETURN(firstEventFrame < remainingFrames,
  88. firstEventFrame, remainingFrames, false);
  89. frames = firstEventFrame;
  90. remainingFrames -= firstEventFrame;
  91. totalFramesUsed += firstEventFrame;
  92. return true;
  93. }
  94. }
  95. else
  96. {
  97. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  98. outputs[i] += frames;
  99. }
  100. // no more MIDI events available
  101. if (remainingMidiEventCount == 0)
  102. {
  103. frames = remainingFrames;
  104. midiEvents = nullptr;
  105. midiEventCount = 0;
  106. remainingFrames = 0;
  107. totalFramesUsed += frames;
  108. return true;
  109. }
  110. // if there were midi events before, increment pointer
  111. if (midiEventCount != 0)
  112. midiEvents += midiEventCount;
  113. const uint32_t firstEventFrame = midiEvents[0].frame;
  114. DISTRHO_SAFE_ASSERT_UINT2_RETURN(firstEventFrame >= totalFramesUsed,
  115. firstEventFrame, totalFramesUsed, false);
  116. midiEventCount = 1;
  117. while (midiEventCount < remainingMidiEventCount)
  118. {
  119. if (midiEvents[midiEventCount].frame == firstEventFrame)
  120. ++midiEventCount;
  121. else
  122. break;
  123. }
  124. frames = firstEventFrame - totalFramesUsed;
  125. remainingFrames -= frames;
  126. remainingMidiEventCount -= midiEventCount;
  127. totalFramesUsed += frames;
  128. return true;
  129. }
  130. private:
  131. /** @internal */
  132. uint32_t remainingFrames;
  133. uint32_t remainingMidiEventCount;
  134. uint32_t totalFramesUsed;
  135. };
  136. // -----------------------------------------------------------------------------------------------------------
  137. END_NAMESPACE_DISTRHO
  138. #endif // DISTRHO_PLUGIN_UTILS_HPP_INCLUDED