/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2017 - ROLI Ltd. JUCE is an open source library subject to commercial or open-source licensing. The code included in this file is provided under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license. Permission To use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted provided that the above copyright notice and this permission notice appear in all copies. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ namespace juce { MidiOutput::MidiOutput (const String& deviceName, const String& deviceIdentifier) : Thread ("midi out"), deviceInfo (deviceName, deviceIdentifier) { } void MidiOutput::sendBlockOfMessagesNow (const MidiBuffer& buffer) { MidiBuffer::Iterator i (buffer); MidiMessage message; int samplePosition; // Note: Not actually used, so no need to initialise. while (i.getNextEvent (message, samplePosition)) sendMessageNow (message); } void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer, double millisecondCounterToStartAt, double samplesPerSecondForBuffer) { // You've got to call startBackgroundThread() for this to actually work.. jassert (isThreadRunning()); // this needs to be a value in the future - RTFM for this method! jassert (millisecondCounterToStartAt > 0); auto timeScaleFactor = 1000.0 / samplesPerSecondForBuffer; const uint8* data; int len, time; for (MidiBuffer::Iterator i (buffer); i.getNextEvent (data, len, time);) { auto eventTime = millisecondCounterToStartAt + timeScaleFactor * time; auto* m = new PendingMessage (data, len, eventTime); const ScopedLock sl (lock); if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime) { m->next = firstMessage; firstMessage = m; } else { auto* mm = firstMessage; while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime) mm = mm->next; m->next = mm->next; mm->next = m; } } notify(); } void MidiOutput::clearAllPendingMessages() { const ScopedLock sl (lock); while (firstMessage != nullptr) { auto* m = firstMessage; firstMessage = firstMessage->next; delete m; } } void MidiOutput::startBackgroundThread() { startThread (9); } void MidiOutput::stopBackgroundThread() { stopThread (5000); } void MidiOutput::run() { while (! threadShouldExit()) { auto now = Time::getMillisecondCounter(); uint32 eventTime = 0; uint32 timeToWait = 500; PendingMessage* message; { const ScopedLock sl (lock); message = firstMessage; if (message != nullptr) { eventTime = (uint32) roundToInt (message->message.getTimeStamp()); if (eventTime > now + 20) { timeToWait = eventTime - (now + 20); message = nullptr; } else { firstMessage = message->next; } } } if (message != nullptr) { std::unique_ptr messageDeleter (message); if (eventTime > now) { Time::waitForMillisecondCounter (eventTime); if (threadShouldExit()) break; } if (eventTime > now - 200) sendMessageNow (message->message); } else { jassert (timeToWait < 1000 * 30); wait ((int) timeToWait); } } clearAllPendingMessages(); } #if JUCE_UNIT_TESTS class MidiDevicesUnitTests : public UnitTest { public: MidiDevicesUnitTests() : UnitTest ("MidiInput/MidiOutput", "MIDI/MPE") {} void runTest() override { beginTest ("default device (input)"); { auto devices = MidiInput::getAvailableDevices(); auto defaultDevice = MidiInput::getDefaultDevice(); if (devices.size() == 0) expect (defaultDevice == MidiDeviceInfo()); else expect (devices.contains (defaultDevice)); } beginTest ("default device (output)"); { auto devices = MidiOutput::getAvailableDevices(); auto defaultDevice = MidiOutput::getDefaultDevice(); if (devices.size() == 0) expect (defaultDevice == MidiDeviceInfo()); else expect (devices.contains (defaultDevice)); } #if JUCE_MAC || JUCE_LINUX || JUCE_IOS String testDeviceName ("TestDevice"); String testDeviceName2 ("TestDevice2"); struct MessageCallbackHandler : public MidiInputCallback { void handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message) override { messageSource = source; messageReceived = message; } MidiInput* messageSource = nullptr; MidiMessage messageReceived; }; MessageCallbackHandler handler; beginTest ("create device (input)"); { std::unique_ptr device (MidiInput::createNewDevice (testDeviceName, &handler)); expect (device.get() != nullptr); expect (device->getName() == testDeviceName); device->setName (testDeviceName2); expect (device->getName() == testDeviceName2); } beginTest ("create device (output)"); { std::unique_ptr device (MidiOutput::createNewDevice (testDeviceName)); expect (device.get() != nullptr); expect (device->getName() == testDeviceName); } #if JUCE_MODAL_LOOPS_PERMITTED auto testMessage = MidiMessage::noteOn (5, 12, (uint8) 51); beginTest ("send messages"); { std::unique_ptr midiInput (MidiInput::createNewDevice (testDeviceName, &handler)); expect (midiInput.get() != nullptr); midiInput->start(); auto inputInfo = midiInput->getDeviceInfo(); expect (MidiOutput::getAvailableDevices().contains (inputInfo)); std::unique_ptr midiOutput (MidiOutput::openDevice (midiInput->getIdentifier())); expect (midiOutput.get() != nullptr); midiOutput->sendMessageNow (testMessage); // Pump the message thread for a bit to allow the message to be delivered MessageManager::getInstance()->runDispatchLoopUntil (100); expect (handler.messageSource == midiInput.get()); expect (handler.messageReceived.getChannel() == testMessage.getChannel()); expect (handler.messageReceived.getNoteNumber() == testMessage.getNoteNumber()); expect (handler.messageReceived.getVelocity() == testMessage.getVelocity()); midiInput->stop(); } #endif #endif } }; static MidiDevicesUnitTests MidiDevicesUnitTests; #endif } // namespace juce