Loading...
Searching...
No Matches
juce_MidiDevices.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
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
{
class MidiDeviceListConnectionBroadcaster;
class MidiDeviceListConnection
{
public:
using Key = uint64_t;
MidiDeviceListConnection() = default;
MidiDeviceListConnection (const MidiDeviceListConnection&) = delete;
MidiDeviceListConnection (MidiDeviceListConnection&& other) noexcept
: broadcaster (std::exchange (other.broadcaster, nullptr)),
key (std::exchange (other.key, Key{}))
{
}
MidiDeviceListConnection& operator= (const MidiDeviceListConnection&) = delete;
MidiDeviceListConnection& operator= (MidiDeviceListConnection&& other) noexcept
{
MidiDeviceListConnection (std::move (other)).swap (*this);
return *this;
}
~MidiDeviceListConnection() noexcept;
void reset() noexcept
{
MidiDeviceListConnection().swap (*this);
}
static MidiDeviceListConnection make (std::function<void()>);
private:
MidiDeviceListConnection (MidiDeviceListConnectionBroadcaster* b, const Key k)
: broadcaster (b), key (k) {}
void swap (MidiDeviceListConnection& other) noexcept
{
std::swap (other.broadcaster, broadcaster);
std::swap (other.key, key);
}
MidiDeviceListConnectionBroadcaster* broadcaster = nullptr;
Key key = {};
};
//==============================================================================
struct MidiDeviceInfo
{
MidiDeviceInfo() = default;
MidiDeviceInfo (const String& deviceName, const String& deviceIdentifier)
: name (deviceName), identifier (deviceIdentifier)
{
}
String name;
String identifier;
//==============================================================================
auto tie() const { return std::tie (name, identifier); }
bool operator== (const MidiDeviceInfo& other) const noexcept { return tie() == other.tie(); }
bool operator!= (const MidiDeviceInfo& other) const noexcept { return tie() != other.tie(); }
};
class MidiInputCallback;
//==============================================================================
class JUCE_API MidiInput final
{
public:
//==============================================================================
static Array<MidiDeviceInfo> getAvailableDevices();
static MidiDeviceInfo getDefaultDevice();
static std::unique_ptr<MidiInput> openDevice (const String& deviceIdentifier, MidiInputCallback* callback);
#if JUCE_LINUX || JUCE_BSD || JUCE_MAC || JUCE_IOS || DOXYGEN
static std::unique_ptr<MidiInput> createNewDevice (const String& deviceName, MidiInputCallback* callback);
#endif
//==============================================================================
~MidiInput();
void start();
void stop();
MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
String getIdentifier() const noexcept { return deviceInfo.identifier; }
String getName() const noexcept { return deviceInfo.name; }
void setName (const String& newName) noexcept { deviceInfo.name = newName; }
//==============================================================================
#ifndef DOXYGEN
[[deprecated ("Use getAvailableDevices instead.")]]
static StringArray getDevices();
[[deprecated ("Use getDefaultDevice instead.")]]
static int getDefaultDeviceIndex();
[[deprecated ("Use openDevice that takes a device identifier instead.")]]
static std::unique_ptr<MidiInput> openDevice (int, MidiInputCallback*);
#endif
class Pimpl;
private:
//==============================================================================
explicit MidiInput (const String&, const String&);
MidiDeviceInfo deviceInfo;
std::unique_ptr<Pimpl> internal;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInput)
};
//==============================================================================
class JUCE_API MidiInputCallback
{
public:
virtual ~MidiInputCallback() = default;
virtual void handleIncomingMidiMessage (MidiInput* source,
const MidiMessage& message) = 0;
virtual void handlePartialSysexMessage (MidiInput* source,
const uint8* messageData,
int numBytesSoFar,
double timestamp);
};
//==============================================================================
class JUCE_API MidiOutput final : private Thread
{
public:
//==============================================================================
static Array<MidiDeviceInfo> getAvailableDevices();
static MidiDeviceInfo getDefaultDevice();
static std::unique_ptr<MidiOutput> openDevice (const String& deviceIdentifier);
#if JUCE_LINUX || JUCE_BSD || JUCE_MAC || JUCE_IOS || DOXYGEN
static std::unique_ptr<MidiOutput> createNewDevice (const String& deviceName);
#endif
//==============================================================================
~MidiOutput() override;
MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
String getIdentifier() const noexcept { return deviceInfo.identifier; }
String getName() const noexcept { return deviceInfo.name; }
void setName (const String& newName) noexcept { deviceInfo.name = newName; }
//==============================================================================
void sendMessageNow (const MidiMessage& message);
void sendBlockOfMessagesNow (const MidiBuffer& buffer);
void sendBlockOfMessages (const MidiBuffer& buffer,
double millisecondCounterToStartAt,
double samplesPerSecondForBuffer);
void clearAllPendingMessages();
void startBackgroundThread();
void stopBackgroundThread();
bool isBackgroundThreadRunning() const noexcept { return isThreadRunning(); }
//==============================================================================
#ifndef DOXYGEN
[[deprecated ("Use getAvailableDevices instead.")]]
static StringArray getDevices();
[[deprecated ("Use getDefaultDevice instead.")]]
static int getDefaultDeviceIndex();
[[deprecated ("Use openDevice that takes a device identifier instead.")]]
static std::unique_ptr<MidiOutput> openDevice (int);
#endif
class Pimpl;
private:
//==============================================================================
struct PendingMessage
{
PendingMessage (const void* data, int len, double timeStamp)
: message (data, len, timeStamp)
{
}
MidiMessage message;
PendingMessage* next;
};
//==============================================================================
explicit MidiOutput (const String&, const String&);
void run() override;
MidiDeviceInfo deviceInfo;
std::unique_ptr<Pimpl> internal;
CriticalSection lock;
PendingMessage* firstMessage = nullptr;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiOutput)
};
} // namespace juce
juce::Array
Definition juce_Array.h:56
juce::MidiBuffer
Definition juce_MidiBuffer.h:145
juce::MidiDeviceListConnection
Definition juce_MidiDevices.h:50
juce::MidiDeviceListConnection::make
static MidiDeviceListConnection make(std::function< void()>)
juce::MidiDeviceListConnection::MidiDeviceListConnection
MidiDeviceListConnection()=default
juce::MidiDeviceListConnection::reset
void reset() noexcept
Definition juce_MidiDevices.h:79
juce::MidiInputCallback
Definition juce_MidiDevices.h:277
juce::MidiInputCallback::~MidiInputCallback
virtual ~MidiInputCallback()=default
juce::MidiInputCallback::handlePartialSysexMessage
virtual void handlePartialSysexMessage(MidiInput *source, const uint8 *messageData, int numBytesSoFar, double timestamp)
Definition juce_MidiDevices.cpp:101
juce::MidiInputCallback::handleIncomingMidiMessage
virtual void handleIncomingMidiMessage(MidiInput *source, const MidiMessage &message)=0
juce::MidiInput
Definition juce_MidiDevices.h:164
juce::MidiInput::setName
void setName(const String &newName) noexcept
Definition juce_MidiDevices.h:239
juce::MidiInput::~MidiInput
~MidiInput()
juce::MidiInput::getAvailableDevices
static Array< MidiDeviceInfo > getAvailableDevices()
juce::MidiInput::openDevice
static std::unique_ptr< MidiInput > openDevice(const String &deviceIdentifier, MidiInputCallback *callback)
juce::MidiInput::stop
void stop()
juce::MidiInput::getName
String getName() const noexcept
Definition juce_MidiDevices.h:236
juce::MidiInput::getDeviceInfo
MidiDeviceInfo getDeviceInfo() const noexcept
Definition juce_MidiDevices.h:230
juce::MidiInput::getDefaultDevice
static MidiDeviceInfo getDefaultDevice()
juce::MidiInput::start
void start()
juce::MidiInput::getIdentifier
String getIdentifier() const noexcept
Definition juce_MidiDevices.h:233
juce::MidiMessage
Definition juce_MidiMessage.h:35
juce::MidiOutput::getDefaultDevice
static MidiDeviceInfo getDefaultDevice()
juce::MidiOutput::sendBlockOfMessagesNow
void sendBlockOfMessagesNow(const MidiBuffer &buffer)
Definition juce_MidiDevices.cpp:112
juce::MidiOutput::stopBackgroundThread
void stopBackgroundThread()
Definition juce_MidiDevices.cpp:174
juce::MidiOutput::getName
String getName() const noexcept
Definition juce_MidiDevices.h:379
juce::MidiOutput::setName
void setName(const String &newName) noexcept
Definition juce_MidiDevices.h:382
juce::MidiOutput::getIdentifier
String getIdentifier() const noexcept
Definition juce_MidiDevices.h:376
juce::MidiOutput::clearAllPendingMessages
void clearAllPendingMessages()
Definition juce_MidiDevices.cpp:157
juce::MidiOutput::openDevice
static std::unique_ptr< MidiOutput > openDevice(const String &deviceIdentifier)
juce::MidiOutput::getDeviceInfo
MidiDeviceInfo getDeviceInfo() const noexcept
Definition juce_MidiDevices.h:373
juce::MidiOutput::startBackgroundThread
void startBackgroundThread()
Definition juce_MidiDevices.cpp:169
juce::MidiOutput::isBackgroundThreadRunning
bool isBackgroundThreadRunning() const noexcept
Definition juce_MidiDevices.h:428
juce::MidiOutput::sendMessageNow
void sendMessageNow(const MidiMessage &message)
juce::MidiOutput::~MidiOutput
~MidiOutput() override
juce::MidiOutput::sendBlockOfMessages
void sendBlockOfMessages(const MidiBuffer &buffer, double millisecondCounterToStartAt, double samplesPerSecondForBuffer)
Definition juce_MidiDevices.cpp:118
juce::MidiOutput::getAvailableDevices
static Array< MidiDeviceInfo > getAvailableDevices()
juce::StringArray
Definition juce_StringArray.h:35
juce::String
Definition juce_String.h:53
juce::Thread::Thread
Thread(const String &threadName, size_t threadStackSize=osDefaultStackSize)
Definition juce_Thread.cpp:26
juce::Thread::isThreadRunning
bool isThreadRunning() const
Definition juce_Thread.cpp:180
juce::MidiDeviceInfo
Definition juce_MidiDevices.h:118
juce::MidiDeviceInfo::name
String name
Definition juce_MidiDevices.h:135
juce::MidiDeviceInfo::identifier
String identifier
Definition juce_MidiDevices.h:142