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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. struct MidiOutput::PendingMessage
  20. {
  21. PendingMessage (const void* const data, const int len, const double timeStamp)
  22. : message (data, len, timeStamp)
  23. {}
  24. MidiMessage message;
  25. PendingMessage* next;
  26. };
  27. MidiOutput::MidiOutput (const String& midiName)
  28. : Thread ("midi out"),
  29. internal (nullptr),
  30. firstMessage (nullptr),
  31. name (midiName)
  32. {
  33. }
  34. void MidiOutput::sendBlockOfMessagesNow (const MidiBuffer& buffer)
  35. {
  36. MidiBuffer::Iterator i (buffer);
  37. MidiMessage message;
  38. int samplePosition; // Note: not actually used, so no need to initialise.
  39. while (i.getNextEvent (message, samplePosition))
  40. sendMessageNow (message);
  41. }
  42. void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
  43. const double millisecondCounterToStartAt,
  44. double samplesPerSecondForBuffer)
  45. {
  46. // You've got to call startBackgroundThread() for this to actually work..
  47. jassert (isThreadRunning());
  48. // this needs to be a value in the future - RTFM for this method!
  49. jassert (millisecondCounterToStartAt > 0);
  50. const double timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;
  51. MidiBuffer::Iterator i (buffer);
  52. const uint8* data;
  53. int len, time;
  54. while (i.getNextEvent (data, len, time))
  55. {
  56. const double eventTime = millisecondCounterToStartAt + timeScaleFactor * time;
  57. PendingMessage* const m = new PendingMessage (data, len, eventTime);
  58. const ScopedLock sl (lock);
  59. if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime)
  60. {
  61. m->next = firstMessage;
  62. firstMessage = m;
  63. }
  64. else
  65. {
  66. PendingMessage* mm = firstMessage;
  67. while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime)
  68. mm = mm->next;
  69. m->next = mm->next;
  70. mm->next = m;
  71. }
  72. }
  73. notify();
  74. }
  75. void MidiOutput::clearAllPendingMessages()
  76. {
  77. const ScopedLock sl (lock);
  78. while (firstMessage != nullptr)
  79. {
  80. PendingMessage* const m = firstMessage;
  81. firstMessage = firstMessage->next;
  82. delete m;
  83. }
  84. }
  85. void MidiOutput::startBackgroundThread()
  86. {
  87. startThread (9);
  88. }
  89. void MidiOutput::stopBackgroundThread()
  90. {
  91. stopThread (5000);
  92. }
  93. void MidiOutput::run()
  94. {
  95. while (! threadShouldExit())
  96. {
  97. uint32 now = Time::getMillisecondCounter();
  98. uint32 eventTime = 0;
  99. uint32 timeToWait = 500;
  100. PendingMessage* message;
  101. {
  102. const ScopedLock sl (lock);
  103. message = firstMessage;
  104. if (message != nullptr)
  105. {
  106. eventTime = (uint32) roundToInt (message->message.getTimeStamp());
  107. if (eventTime > now + 20)
  108. {
  109. timeToWait = eventTime - (now + 20);
  110. message = nullptr;
  111. }
  112. else
  113. {
  114. firstMessage = message->next;
  115. }
  116. }
  117. }
  118. if (message != nullptr)
  119. {
  120. const ScopedPointer<PendingMessage> messageDeleter (message);
  121. if (eventTime > now)
  122. {
  123. Time::waitForMillisecondCounter (eventTime);
  124. if (threadShouldExit())
  125. break;
  126. }
  127. if (eventTime > now - 200)
  128. sendMessageNow (message->message);
  129. }
  130. else
  131. {
  132. jassert (timeToWait < 1000 * 30);
  133. wait ((int) timeToWait);
  134. }
  135. }
  136. clearAllPendingMessages();
  137. }
  138. } // namespace juce