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.6KB

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