The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

172 lines
4.6KB

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