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.

180 lines
5.1KB

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