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_MidiDevices.cpp 4.2KB

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