mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
MIDI: Add support for MIDI 2.0 I/O using Universal MIDI Packets
Includes support for communication with USB and Bluetooth devices, as well as virtual devices.
This commit is contained in:
parent
3636f2c666
commit
ba7593df26
65 changed files with 16609 additions and 5301 deletions
|
|
@ -35,144 +35,368 @@
|
|||
namespace juce
|
||||
{
|
||||
|
||||
MidiDeviceListConnection::~MidiDeviceListConnection() noexcept
|
||||
MidiDeviceListConnection MidiDeviceListConnection::make (std::function<void()> callback)
|
||||
{
|
||||
if (broadcaster != nullptr)
|
||||
broadcaster->remove (key);
|
||||
auto& broadcaster = MidiDeviceListConnectionBroadcaster::get();
|
||||
|
||||
const auto key = broadcaster.add (std::move (callback));
|
||||
|
||||
MidiDeviceListConnection result;
|
||||
result.token = ErasedScopeGuard { [&broadcaster, key] { broadcaster.remove (key); } };
|
||||
return result;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void MidiInputCallback::handlePartialSysexMessage ([[maybe_unused]] MidiInput* source,
|
||||
[[maybe_unused]] const uint8* messageData,
|
||||
[[maybe_unused]] int numBytesSoFar,
|
||||
[[maybe_unused]] double timestamp) {}
|
||||
static std::shared_ptr<ump::Session> getLegacySession()
|
||||
{
|
||||
static std::weak_ptr<ump::Session> weak;
|
||||
|
||||
if (auto strong = weak.lock())
|
||||
return strong;
|
||||
|
||||
if (auto session = ump::Endpoints::getInstance()->makeSession (ump::Endpoints::Impl::getGlobalMidiClientName()))
|
||||
{
|
||||
auto strong = std::make_shared<ump::Session> (std::move (session));
|
||||
weak = strong;
|
||||
return strong;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
class MidiInput::Impl : private ump::Consumer
|
||||
{
|
||||
public:
|
||||
void start()
|
||||
{
|
||||
const SpinLock::ScopedLockType lock { spinLock };
|
||||
active = true;
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
const SpinLock::ScopedLockType lock { spinLock };
|
||||
active = false;
|
||||
}
|
||||
|
||||
MidiDeviceInfo getDeviceInfo() const noexcept
|
||||
{
|
||||
return customName.has_value() ? storedInfo.withName (*customName) : storedInfo;
|
||||
}
|
||||
|
||||
void setName (String x)
|
||||
{
|
||||
customName = std::move (x);
|
||||
}
|
||||
|
||||
void addCallback (MidiInputCallback& cb)
|
||||
{
|
||||
callbacks.add (cb);
|
||||
}
|
||||
|
||||
void removeCallback (MidiInputCallback& cb)
|
||||
{
|
||||
callbacks.remove (cb);
|
||||
}
|
||||
|
||||
/* session may be null, in which case it's up to the caller to ensure that the session lives
|
||||
long enough for the connection to be useful.
|
||||
*/
|
||||
static std::unique_ptr<MidiInput> make (std::shared_ptr<ump::Session> session,
|
||||
ump::Input connection,
|
||||
uint8_t group,
|
||||
const MidiDeviceInfo& info,
|
||||
MidiInputCallback* cb,
|
||||
ump::LegacyVirtualInput virtualEndpoint)
|
||||
{
|
||||
auto result = rawToUniquePtr (new MidiInput);
|
||||
result->pimpl = rawToUniquePtr (new Impl (session,
|
||||
std::move (connection),
|
||||
group,
|
||||
result.get(),
|
||||
info,
|
||||
std::move (virtualEndpoint)));
|
||||
|
||||
if (cb != nullptr)
|
||||
result->addCallback (*cb);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t getGroup() const
|
||||
{
|
||||
return group;
|
||||
}
|
||||
|
||||
ump::EndpointId getEndpointId() const
|
||||
{
|
||||
return connection.getEndpointId();
|
||||
}
|
||||
|
||||
~Impl() override
|
||||
{
|
||||
connection.removeConsumer (*this);
|
||||
}
|
||||
|
||||
private:
|
||||
Impl (std::shared_ptr<ump::Session> s,
|
||||
ump::Input x,
|
||||
uint8_t g,
|
||||
MidiInput* o,
|
||||
MidiDeviceInfo i,
|
||||
ump::LegacyVirtualInput v)
|
||||
: session (s),
|
||||
virtualEndpoint (std::move (v)),
|
||||
connection (std::move (x)),
|
||||
storedInfo (i),
|
||||
group (g),
|
||||
owner (o)
|
||||
{
|
||||
connection.addConsumer (*this);
|
||||
}
|
||||
|
||||
void consume (ump::Iterator b, ump::Iterator e, double time) override
|
||||
{
|
||||
const SpinLock::ScopedTryLockType lock { spinLock };
|
||||
|
||||
if (! lock.isLocked() || ! active)
|
||||
return;
|
||||
|
||||
for (const auto& view : makeRange (b, e))
|
||||
{
|
||||
if (ump::Utils::getGroup (view[0]) != group)
|
||||
continue;
|
||||
|
||||
converter.convert (view, time, [this] (ump::BytesOnGroup v, double t)
|
||||
{
|
||||
const MidiMessage msg { v.bytes.data(), (int) v.bytes.size(), t };
|
||||
|
||||
callbacks.call ([&] (MidiInputCallback& l)
|
||||
{
|
||||
l.handleIncomingMidiMessage (owner, msg);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<ump::Session> session;
|
||||
ump::LegacyVirtualInput virtualEndpoint;
|
||||
std::optional<String> customName;
|
||||
ump::Input connection;
|
||||
MidiDeviceInfo storedInfo;
|
||||
ump::ToBytestreamConverter converter { 4096 };
|
||||
WaitFreeListeners<MidiInputCallback> callbacks;
|
||||
uint8_t group{};
|
||||
MidiInput* owner = nullptr;
|
||||
SpinLock spinLock;
|
||||
bool active = false;
|
||||
};
|
||||
|
||||
MidiInput::MidiInput() = default;
|
||||
MidiInput::~MidiInput() = default;
|
||||
|
||||
Array<MidiDeviceInfo> MidiInput::getAvailableDevices()
|
||||
{
|
||||
Array<MidiDeviceInfo> result;
|
||||
MidiDeviceListConnectionBroadcaster::get().getAllMidiDeviceInfo (ump::IOKind::src, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
MidiDeviceInfo MidiInput::getDefaultDevice()
|
||||
{
|
||||
return getAvailableDevices().getFirst();
|
||||
}
|
||||
|
||||
std::unique_ptr<MidiInput> MidiInput::openDevice (const String& deviceIdentifier, MidiInputCallback* callback)
|
||||
{
|
||||
const auto address = MidiDeviceListConnectionBroadcaster::get().getEndpointGroupForId (ump::IOKind::src, deviceIdentifier);
|
||||
|
||||
if (! address.has_value())
|
||||
return {};
|
||||
|
||||
const auto info = MidiDeviceListConnectionBroadcaster::get().getInfoForId (ump::IOKind::src, deviceIdentifier);
|
||||
|
||||
if (! info.has_value())
|
||||
return {};
|
||||
|
||||
auto session = getLegacySession();
|
||||
|
||||
if (session == nullptr)
|
||||
return {};
|
||||
|
||||
auto connection = session->connectInput (address->endpointId, ump::PacketProtocol::MIDI_1_0);
|
||||
|
||||
if (! connection.isAlive())
|
||||
return {};
|
||||
|
||||
return Impl::make (session, std::move (connection), address->group, *info, callback, {});
|
||||
}
|
||||
|
||||
static inline bool isValidMidi1VirtualEndpoint (const std::optional<ump::Endpoint>& ep,
|
||||
ump::BlockDirection dir)
|
||||
{
|
||||
if (! ep.has_value())
|
||||
return false;
|
||||
|
||||
if (! ep->hasMidi1Support())
|
||||
return false;
|
||||
|
||||
if (ep->getProtocol() != ump::PacketProtocol::MIDI_1_0)
|
||||
return false;
|
||||
|
||||
if (! ep->hasStaticBlocks())
|
||||
return false;
|
||||
|
||||
auto blocks = ep->getBlocks();
|
||||
const auto iter = std::find_if (blocks.begin(), blocks.end(), [&] (const ump::Block& b)
|
||||
{
|
||||
return b.getDirection() == dir
|
||||
&& b.getNumGroups() == 1
|
||||
&& b.isEnabled()
|
||||
&& b.getMIDI1ProxyKind() != ump::BlockMIDI1ProxyKind::inapplicable;
|
||||
});
|
||||
|
||||
if (iter == blocks.end())
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<MidiInput> MidiInput::createNewDevice (const String& name, MidiInputCallback* callback)
|
||||
{
|
||||
auto session = getLegacySession();
|
||||
|
||||
if (! session)
|
||||
return {};
|
||||
|
||||
auto port = session->createLegacyVirtualInput (name);
|
||||
|
||||
if (! port)
|
||||
return {};
|
||||
|
||||
jassert (isValidMidi1VirtualEndpoint (ump::Endpoints::getInstance()->getEndpoint (port.getId()),
|
||||
ump::BlockDirection::receiver));
|
||||
|
||||
auto connection = session->connectInput (port.getId(), ump::PacketProtocol::MIDI_1_0);
|
||||
|
||||
if (! connection)
|
||||
return {};
|
||||
|
||||
return Impl::make (session, std::move (connection), 0, { name, {} }, callback, std::move (port));
|
||||
}
|
||||
|
||||
void MidiInput::start()
|
||||
{
|
||||
pimpl->start();
|
||||
}
|
||||
|
||||
void MidiInput::stop()
|
||||
{
|
||||
pimpl->stop();
|
||||
}
|
||||
|
||||
MidiDeviceInfo MidiInput::getDeviceInfo() const noexcept
|
||||
{
|
||||
return pimpl->getDeviceInfo();
|
||||
}
|
||||
|
||||
void MidiInput::setName (const String& newName) noexcept
|
||||
{
|
||||
pimpl->setName (newName);
|
||||
}
|
||||
|
||||
uint8_t MidiInput::getGroup() const
|
||||
{
|
||||
return pimpl->getGroup();
|
||||
}
|
||||
|
||||
ump::EndpointId MidiInput::getEndpointId() const
|
||||
{
|
||||
return pimpl->getEndpointId();
|
||||
}
|
||||
|
||||
void MidiInput::addCallback (MidiInputCallback& callback)
|
||||
{
|
||||
pimpl->addCallback (callback);
|
||||
}
|
||||
|
||||
void MidiInput::removeCallback (MidiInputCallback& callback)
|
||||
{
|
||||
pimpl->removeCallback (callback);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
MidiOutput::MidiOutput (const String& deviceName, const String& deviceIdentifier)
|
||||
: Thread (SystemStats::getJUCEVersion() + ": midi out"), deviceInfo (deviceName, deviceIdentifier)
|
||||
MidiOutput::MidiOutput (std::shared_ptr<ump::Session> s,
|
||||
ump::Output x,
|
||||
uint8_t g,
|
||||
const MidiDeviceInfo& i,
|
||||
ump::LegacyVirtualOutput v)
|
||||
: session (s),
|
||||
virtualEndpoint (std::move (v)),
|
||||
connection (std::move (x)),
|
||||
storedInfo (i),
|
||||
group (g)
|
||||
{
|
||||
}
|
||||
|
||||
void MidiOutput::sendBlockOfMessagesNow (const MidiBuffer& buffer)
|
||||
Array<MidiDeviceInfo> MidiOutput::getAvailableDevices()
|
||||
{
|
||||
for (const auto metadata : buffer)
|
||||
sendMessageNow (metadata.getMessage());
|
||||
Array<MidiDeviceInfo> result;
|
||||
MidiDeviceListConnectionBroadcaster::get().getAllMidiDeviceInfo (ump::IOKind::dst, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void MidiOutput::sendBlockOfMessages (const MidiBuffer& buffer,
|
||||
double millisecondCounterToStartAt,
|
||||
double samplesPerSecondForBuffer)
|
||||
std::unique_ptr<MidiOutput> MidiOutput::openDevice (const String& deviceIdentifier)
|
||||
{
|
||||
// You've got to call startBackgroundThread() for this to actually work..
|
||||
jassert (isThreadRunning());
|
||||
const auto address = MidiDeviceListConnectionBroadcaster::get().getEndpointGroupForId (ump::IOKind::dst, deviceIdentifier);
|
||||
|
||||
// this needs to be a value in the future - RTFM for this method!
|
||||
jassert (millisecondCounterToStartAt > 0);
|
||||
if (! address.has_value())
|
||||
return {};
|
||||
|
||||
auto timeScaleFactor = 1000.0 / samplesPerSecondForBuffer;
|
||||
const auto info = MidiDeviceListConnectionBroadcaster::get().getInfoForId (ump::IOKind::dst, deviceIdentifier);
|
||||
|
||||
for (const auto metadata : buffer)
|
||||
{
|
||||
auto eventTime = millisecondCounterToStartAt + timeScaleFactor * metadata.samplePosition;
|
||||
auto* m = new PendingMessage (metadata.data, metadata.numBytes, eventTime);
|
||||
if (! info.has_value())
|
||||
return {};
|
||||
|
||||
const ScopedLock sl (lock);
|
||||
auto session = getLegacySession();
|
||||
|
||||
if (firstMessage == nullptr || firstMessage->message.getTimeStamp() > eventTime)
|
||||
{
|
||||
m->next = firstMessage;
|
||||
firstMessage = m;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto* mm = firstMessage;
|
||||
if (session == nullptr)
|
||||
return {};
|
||||
|
||||
while (mm->next != nullptr && mm->next->message.getTimeStamp() <= eventTime)
|
||||
mm = mm->next;
|
||||
auto connection = session->connectOutput (address->endpointId);
|
||||
|
||||
m->next = mm->next;
|
||||
mm->next = m;
|
||||
}
|
||||
}
|
||||
if (! connection.isAlive())
|
||||
return {};
|
||||
|
||||
notify();
|
||||
return rawToUniquePtr (new MidiOutput (session, std::move (connection), address->group, *info, {}));
|
||||
}
|
||||
|
||||
void MidiOutput::clearAllPendingMessages()
|
||||
std::unique_ptr<MidiOutput> MidiOutput::createNewDevice (const String& name)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
auto session = getLegacySession();
|
||||
|
||||
while (firstMessage != nullptr)
|
||||
{
|
||||
auto* m = firstMessage;
|
||||
firstMessage = firstMessage->next;
|
||||
delete m;
|
||||
}
|
||||
if (! session)
|
||||
return {};
|
||||
|
||||
auto port = session->createLegacyVirtualOutput (name);
|
||||
|
||||
if (! port)
|
||||
return {};
|
||||
|
||||
jassert (isValidMidi1VirtualEndpoint (ump::Endpoints::getInstance()->getEndpoint (port.getId()),
|
||||
ump::BlockDirection::sender));
|
||||
|
||||
auto connection = session->connectOutput (port.getId());
|
||||
|
||||
if (! connection)
|
||||
return {};
|
||||
|
||||
return rawToUniquePtr (new MidiOutput (session, std::move (connection), 0, { name, {} }, std::move (port)));
|
||||
}
|
||||
|
||||
void MidiOutput::startBackgroundThread()
|
||||
MidiDeviceInfo MidiOutput::getDeviceInfo() const noexcept
|
||||
{
|
||||
startThread (Priority::high);
|
||||
}
|
||||
|
||||
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<PendingMessage> 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();
|
||||
return customName.has_value() ? storedInfo.withName (*customName) : storedInfo;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue