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.

juce_MidiOutput.cpp 4.3KB

9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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(const String& midiName)
  26. : Thread ("midi out"),
  27. internal (nullptr),
  28. firstMessage (nullptr),
  29. name (midiName)
  30. {
  31. }
  32. void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
  33. const double millisecondCounterToStartAt,
  34. double samplesPerSecondForBuffer)
  35. {
  36. // You've got to call startBackgroundThread() for this to actually work..
  37. jassert (isThreadRunning());
  38. // this needs to be a value in the future - RTFM for this method!
  39. jassert (millisecondCounterToStartAt > 0);
  40. const double timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;
  41. MidiBuffer::Iterator i (buffer);
  42. const uint8* data;
  43. int len, time;
  44. while (i.getNextEvent (data, len, time))
  45. {
  46. const double eventTime = millisecondCounterToStartAt + timeScaleFactor * time;
  47. PendingMessage* const m = new PendingMessage (data, len, eventTime);
  48. const ScopedLock sl (lock);
  49. if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime)
  50. {
  51. m->next = firstMessage;
  52. firstMessage = m;
  53. }
  54. else
  55. {
  56. PendingMessage* mm = firstMessage;
  57. while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime)
  58. mm = mm->next;
  59. m->next = mm->next;
  60. mm->next = m;
  61. }
  62. }
  63. notify();
  64. }
  65. void MidiOutput::clearAllPendingMessages()
  66. {
  67. const ScopedLock sl (lock);
  68. while (firstMessage != nullptr)
  69. {
  70. PendingMessage* const m = firstMessage;
  71. firstMessage = firstMessage->next;
  72. delete m;
  73. }
  74. }
  75. void MidiOutput::startBackgroundThread()
  76. {
  77. startThread (9);
  78. }
  79. void MidiOutput::stopBackgroundThread()
  80. {
  81. stopThread (5000);
  82. }
  83. void MidiOutput::run()
  84. {
  85. while (! threadShouldExit())
  86. {
  87. uint32 now = Time::getMillisecondCounter();
  88. uint32 eventTime = 0;
  89. uint32 timeToWait = 500;
  90. PendingMessage* message;
  91. {
  92. const ScopedLock sl (lock);
  93. message = firstMessage;
  94. if (message != nullptr)
  95. {
  96. eventTime = (uint32) roundToInt (message->message.getTimeStamp());
  97. if (eventTime > now + 20)
  98. {
  99. timeToWait = eventTime - (now + 20);
  100. message = nullptr;
  101. }
  102. else
  103. {
  104. firstMessage = message->next;
  105. }
  106. }
  107. }
  108. if (message != nullptr)
  109. {
  110. const ScopedPointer<PendingMessage> messageDeleter (message);
  111. if (eventTime > now)
  112. {
  113. Time::waitForMillisecondCounter (eventTime);
  114. if (threadShouldExit())
  115. break;
  116. }
  117. if (eventTime > now - 200)
  118. sendMessageNow (message->message);
  119. }
  120. else
  121. {
  122. jassert (timeToWait < 1000 * 30);
  123. wait ((int) timeToWait);
  124. }
  125. }
  126. clearAllPendingMessages();
  127. }