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.

163 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. struct MidiOutput::PendingMessage
  18. {
  19. PendingMessage (const void* const data, const int len, const double timeStamp)
  20. : message (data, len, timeStamp)
  21. {}
  22. MidiMessage message;
  23. PendingMessage* next;
  24. };
  25. MidiOutput::MidiOutput()
  26. : Thread ("midi out"),
  27. internal (nullptr),
  28. firstMessage (nullptr)
  29. {
  30. }
  31. void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
  32. const double millisecondCounterToStartAt,
  33. double samplesPerSecondForBuffer)
  34. {
  35. // You've got to call startBackgroundThread() for this to actually work..
  36. jassert (isThreadRunning());
  37. // this needs to be a value in the future - RTFM for this method!
  38. jassert (millisecondCounterToStartAt > 0);
  39. const double timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;
  40. MidiBuffer::Iterator i (buffer);
  41. const uint8* data;
  42. int len, time;
  43. while (i.getNextEvent (data, len, time))
  44. {
  45. const double eventTime = millisecondCounterToStartAt + timeScaleFactor * time;
  46. PendingMessage* const m = new PendingMessage (data, len, eventTime);
  47. const ScopedLock sl (lock);
  48. if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime)
  49. {
  50. m->next = firstMessage;
  51. firstMessage = m;
  52. }
  53. else
  54. {
  55. PendingMessage* mm = firstMessage;
  56. while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime)
  57. mm = mm->next;
  58. m->next = mm->next;
  59. mm->next = m;
  60. }
  61. }
  62. notify();
  63. }
  64. void MidiOutput::clearAllPendingMessages()
  65. {
  66. const ScopedLock sl (lock);
  67. while (firstMessage != nullptr)
  68. {
  69. PendingMessage* const m = firstMessage;
  70. firstMessage = firstMessage->next;
  71. delete m;
  72. }
  73. }
  74. void MidiOutput::startBackgroundThread()
  75. {
  76. startThread (9);
  77. }
  78. void MidiOutput::stopBackgroundThread()
  79. {
  80. stopThread (5000);
  81. }
  82. void MidiOutput::run()
  83. {
  84. while (! threadShouldExit())
  85. {
  86. uint32 now = Time::getMillisecondCounter();
  87. uint32 eventTime = 0;
  88. uint32 timeToWait = 500;
  89. PendingMessage* message;
  90. {
  91. const ScopedLock sl (lock);
  92. message = firstMessage;
  93. if (message != nullptr)
  94. {
  95. eventTime = (uint32) roundToInt (message->message.getTimeStamp());
  96. if (eventTime > now + 20)
  97. {
  98. timeToWait = eventTime - (now + 20);
  99. message = nullptr;
  100. }
  101. else
  102. {
  103. firstMessage = message->next;
  104. }
  105. }
  106. }
  107. if (message != nullptr)
  108. {
  109. const ScopedPointer<PendingMessage> messageDeleter (message);
  110. if (eventTime > now)
  111. {
  112. Time::waitForMillisecondCounter (eventTime);
  113. if (threadShouldExit())
  114. break;
  115. }
  116. if (eventTime > now - 200)
  117. sendMessageNow (message->message);
  118. }
  119. else
  120. {
  121. jassert (timeToWait < 1000 * 30);
  122. wait ((int) timeToWait);
  123. }
  124. }
  125. clearAllPendingMessages();
  126. }