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.

266 lines
7.9KB

  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. 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. MidiBuffer::Iterator i (buffer);
  26. MidiMessage message;
  27. int samplePosition; // Note: Not actually used, so no need to initialise.
  28. while (i.getNextEvent (message, samplePosition))
  29. sendMessageNow (message);
  30. }
  31. void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
  32. double millisecondCounterToStartAt,
  33. double samplesPerSecondForBuffer)
  34. {
  35. // You've got to call startBackgroundThread() for this to actually work..
  36. jassert (isThreadRunning());
  37. // this needs to be a value in the future - RTFM for this method!
  38. jassert (millisecondCounterToStartAt > 0);
  39. auto timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;
  40. const uint8* data;
  41. int len, time;
  42. for (MidiBuffer::Iterator i (buffer); i.getNextEvent (data, len, time);)
  43. {
  44. auto eventTime = millisecondCounterToStartAt + timeScaleFactor * time;
  45. auto* m = new PendingMessage (data, len, eventTime);
  46. const ScopedLock sl (lock);
  47. if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime)
  48. {
  49. m->next = firstMessage;
  50. firstMessage = m;
  51. }
  52. else
  53. {
  54. auto* mm = firstMessage;
  55. while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime)
  56. mm = mm->next;
  57. m->next = mm->next;
  58. mm->next = m;
  59. }
  60. }
  61. notify();
  62. }
  63. void MidiOutput::clearAllPendingMessages()
  64. {
  65. const ScopedLock sl (lock);
  66. while (firstMessage != nullptr)
  67. {
  68. auto* m = firstMessage;
  69. firstMessage = firstMessage->next;
  70. delete m;
  71. }
  72. }
  73. void MidiOutput::startBackgroundThread()
  74. {
  75. startThread (9);
  76. }
  77. void MidiOutput::stopBackgroundThread()
  78. {
  79. stopThread (5000);
  80. }
  81. void MidiOutput::run()
  82. {
  83. while (! threadShouldExit())
  84. {
  85. auto now = Time::getMillisecondCounter();
  86. uint32 eventTime = 0;
  87. uint32 timeToWait = 500;
  88. PendingMessage* message;
  89. {
  90. const ScopedLock sl (lock);
  91. message = firstMessage;
  92. if (message != nullptr)
  93. {
  94. eventTime = (uint32) roundToInt (message->message.getTimeStamp());
  95. if (eventTime > now + 20)
  96. {
  97. timeToWait = eventTime - (now + 20);
  98. message = nullptr;
  99. }
  100. else
  101. {
  102. firstMessage = message->next;
  103. }
  104. }
  105. }
  106. if (message != nullptr)
  107. {
  108. std::unique_ptr<PendingMessage> messageDeleter (message);
  109. if (eventTime > now)
  110. {
  111. Time::waitForMillisecondCounter (eventTime);
  112. if (threadShouldExit())
  113. break;
  114. }
  115. if (eventTime > now - 200)
  116. sendMessageNow (message->message);
  117. }
  118. else
  119. {
  120. jassert (timeToWait < 1000 * 30);
  121. wait ((int) timeToWait);
  122. }
  123. }
  124. clearAllPendingMessages();
  125. }
  126. #if JUCE_UNIT_TESTS
  127. class MidiDevicesUnitTests : public UnitTest
  128. {
  129. public:
  130. MidiDevicesUnitTests() : UnitTest ("MidiInput/MidiOutput", "MIDI/MPE") {}
  131. void runTest() override
  132. {
  133. beginTest ("default device (input)");
  134. {
  135. auto devices = MidiInput::getAvailableDevices();
  136. auto defaultDevice = MidiInput::getDefaultDevice();
  137. if (devices.size() == 0)
  138. expect (defaultDevice == MidiDeviceInfo());
  139. else
  140. expect (devices.contains (defaultDevice));
  141. }
  142. beginTest ("default device (output)");
  143. {
  144. auto devices = MidiOutput::getAvailableDevices();
  145. auto defaultDevice = MidiOutput::getDefaultDevice();
  146. if (devices.size() == 0)
  147. expect (defaultDevice == MidiDeviceInfo());
  148. else
  149. expect (devices.contains (defaultDevice));
  150. }
  151. #if JUCE_MAC || JUCE_LINUX || JUCE_IOS
  152. String testDeviceName ("TestDevice");
  153. String testDeviceName2 ("TestDevice2");
  154. struct MessageCallbackHandler : public MidiInputCallback
  155. {
  156. void handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message) override
  157. {
  158. messageSource = source;
  159. messageReceived = message;
  160. }
  161. MidiInput* messageSource = nullptr;
  162. MidiMessage messageReceived;
  163. };
  164. MessageCallbackHandler handler;
  165. beginTest ("create device (input)");
  166. {
  167. std::unique_ptr<MidiInput> device (MidiInput::createNewDevice (testDeviceName, &handler));
  168. expect (device.get() != nullptr);
  169. expect (device->getName() == testDeviceName);
  170. device->setName (testDeviceName2);
  171. expect (device->getName() == testDeviceName2);
  172. }
  173. beginTest ("create device (output)");
  174. {
  175. std::unique_ptr<MidiOutput> device (MidiOutput::createNewDevice (testDeviceName));
  176. expect (device.get() != nullptr);
  177. expect (device->getName() == testDeviceName);
  178. }
  179. #if JUCE_MODAL_LOOPS_PERMITTED
  180. auto testMessage = MidiMessage::noteOn (5, 12, (uint8) 51);
  181. beginTest ("send messages");
  182. {
  183. std::unique_ptr<MidiInput> midiInput (MidiInput::createNewDevice (testDeviceName, &handler));
  184. expect (midiInput.get() != nullptr);
  185. midiInput->start();
  186. auto inputInfo = midiInput->getDeviceInfo();
  187. expect (MidiOutput::getAvailableDevices().contains (inputInfo));
  188. std::unique_ptr<MidiOutput> midiOutput (MidiOutput::openDevice (midiInput->getIdentifier()));
  189. expect (midiOutput.get() != nullptr);
  190. midiOutput->sendMessageNow (testMessage);
  191. // Pump the message thread for a bit to allow the message to be delivered
  192. MessageManager::getInstance()->runDispatchLoopUntil (100);
  193. expect (handler.messageSource == midiInput.get());
  194. expect (handler.messageReceived.getChannel() == testMessage.getChannel());
  195. expect (handler.messageReceived.getNoteNumber() == testMessage.getNoteNumber());
  196. expect (handler.messageReceived.getVelocity() == testMessage.getVelocity());
  197. midiInput->stop();
  198. }
  199. #endif
  200. #endif
  201. }
  202. };
  203. static MidiDevicesUnitTests MidiDevicesUnitTests;
  204. #endif
  205. } // namespace juce