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.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. //==============================================================================
  20. /**
  21. This struct contains information about a MIDI input or output device.
  22. You can get one of these structs by calling the static getAvailableDevices() or
  23. getDefaultDevice() methods of MidiInput and MidiOutput or by calling getDeviceInfo()
  24. on an instance of these classes. Devices can be opened by passing the identifier to
  25. the openDevice() method.
  26. @tags{Audio}
  27. */
  28. struct MidiDeviceInfo
  29. {
  30. MidiDeviceInfo() = default;
  31. MidiDeviceInfo (const String& deviceName, const String& deviceIdentifier)
  32. : name (deviceName), identifier (deviceIdentifier)
  33. {
  34. }
  35. /** The name of this device.
  36. This will be provided by the OS unless the device has been created with the
  37. createNewDevice() method.
  38. Note that the name is not guaranteed to be unique and two devices with the
  39. same name will be indistinguishable. If you want to address a specific device
  40. it is better to use the identifier.
  41. */
  42. String name;
  43. /** The identifier for this device.
  44. This will be provided by the OS and it's format will differ on different systems
  45. e.g. on macOS it will be a number whereas on Windows it will be a long alphanumeric string.
  46. */
  47. String identifier;
  48. //==============================================================================
  49. bool operator== (const MidiDeviceInfo& other) const noexcept { return name == other.name && identifier == other.identifier; }
  50. bool operator!= (const MidiDeviceInfo& other) const noexcept { return ! operator== (other); }
  51. };
  52. class MidiInputCallback;
  53. //==============================================================================
  54. /**
  55. Represents a midi input device.
  56. To create one of these, use the static getAvailableDevices() method to find out what
  57. inputs are available, and then use the openDevice() method to try to open one.
  58. @see MidiOutput
  59. @tags{Audio}
  60. */
  61. class JUCE_API MidiInput final
  62. {
  63. public:
  64. //==============================================================================
  65. /** Returns a list of the available midi input devices.
  66. You can open one of the devices by passing its identifier into the openDevice() method.
  67. @see MidiDeviceInfo, getDevices, getDefaultDeviceIndex, openDevice
  68. */
  69. static Array<MidiDeviceInfo> getAvailableDevices();
  70. /** Returns the MidiDeviceInfo of the default midi input device to use. */
  71. static MidiDeviceInfo getDefaultDevice();
  72. /** Tries to open one of the midi input devices.
  73. This will return a MidiInput object if it manages to open it, you can then
  74. call start() and stop() on this device.
  75. If the device can't be opened, this will return an empty object.
  76. @param deviceIdentifier the ID of the device to open - use the getAvailableDevices() method to
  77. find the available devices that can be opened
  78. @param callback the object that will receive the midi messages from this device
  79. @see MidiInputCallback, getDevices
  80. */
  81. static std::unique_ptr<MidiInput> openDevice (const String& deviceIdentifier, MidiInputCallback* callback);
  82. #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
  83. /** This will try to create a new midi input device (only available on Linux, macOS and iOS).
  84. This will attempt to create a new midi input device with the specified name for other
  85. apps to connect to.
  86. NB - if you are calling this method on iOS you must have enabled the "Audio Background Capability"
  87. setting in the iOS exporter otherwise this method will fail.
  88. Returns an empty object if a device can't be created.
  89. @param deviceName the name of the device to create
  90. @param callback the object that will receive the midi messages from this device
  91. */
  92. static std::unique_ptr<MidiInput> createNewDevice (const String& deviceName, MidiInputCallback* callback);
  93. #endif
  94. //==============================================================================
  95. /** Destructor. */
  96. ~MidiInput();
  97. /** Starts the device running.
  98. After calling this, the device will start sending midi messages to the MidiInputCallback
  99. object that was specified when the openDevice() method was called.
  100. @see stop
  101. */
  102. void start();
  103. /** Stops the device running.
  104. @see start
  105. */
  106. void stop();
  107. /** Returns the MidiDeviceInfo struct containing some information about this device. */
  108. MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
  109. /** Returns the identifier of this device. */
  110. String getIdentifier() const noexcept { return deviceInfo.identifier; }
  111. /** Returns the name of this device. */
  112. String getName() const noexcept { return deviceInfo.name; }
  113. /** Sets a custom name for the device. */
  114. void setName (const String& newName) noexcept { deviceInfo.name = newName; }
  115. //==============================================================================
  116. /** Deprecated. */
  117. static StringArray getDevices();
  118. /** Deprecated. */
  119. static int getDefaultDeviceIndex();
  120. /** Deprecated. */
  121. static std::unique_ptr<MidiInput> openDevice (int, MidiInputCallback*);
  122. private:
  123. //==============================================================================
  124. explicit MidiInput (const String&, const String&);
  125. MidiDeviceInfo deviceInfo;
  126. void* internal = nullptr;
  127. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInput)
  128. };
  129. //==============================================================================
  130. /**
  131. Receives incoming messages from a physical MIDI input device.
  132. This class is overridden to handle incoming midi messages. See the MidiInput
  133. class for more details.
  134. @see MidiInput
  135. @tags{Audio}
  136. */
  137. class JUCE_API MidiInputCallback
  138. {
  139. public:
  140. /** Destructor. */
  141. virtual ~MidiInputCallback() = default;
  142. /** Receives an incoming message.
  143. A MidiInput object will call this method when a midi event arrives. It'll be
  144. called on a high-priority system thread, so avoid doing anything time-consuming
  145. in here, and avoid making any UI calls. You might find the MidiBuffer class helpful
  146. for queueing incoming messages for use later.
  147. @param source the MidiInput object that generated the message
  148. @param message the incoming message. The message's timestamp is set to a value
  149. equivalent to (Time::getMillisecondCounter() / 1000.0) to specify the
  150. time when the message arrived
  151. */
  152. virtual void handleIncomingMidiMessage (MidiInput* source,
  153. const MidiMessage& message) = 0;
  154. /** Notification sent each time a packet of a multi-packet sysex message arrives.
  155. If a long sysex message is broken up into multiple packets, this callback is made
  156. for each packet that arrives until the message is finished, at which point
  157. the normal handleIncomingMidiMessage() callback will be made with the entire
  158. message.
  159. The message passed in will contain the start of a sysex, but won't be finished
  160. with the terminating 0xf7 byte.
  161. */
  162. virtual void handlePartialSysexMessage (MidiInput* source,
  163. const uint8* messageData,
  164. int numBytesSoFar,
  165. double timestamp)
  166. {
  167. ignoreUnused (source, messageData, numBytesSoFar, timestamp);
  168. }
  169. };
  170. //==============================================================================
  171. /**
  172. Represents a midi output device.
  173. To create one of these, use the static getAvailableDevices() method to find out what
  174. outputs are available, and then use the openDevice() method to try to open one.
  175. @see MidiInput
  176. @tags{Audio}
  177. */
  178. class JUCE_API MidiOutput final : private Thread
  179. {
  180. public:
  181. //==============================================================================
  182. /** Returns a list of the available midi output devices.
  183. You can open one of the devices by passing its identifier into the openDevice() method.
  184. @see MidiDeviceInfo, getDevices, getDefaultDeviceIndex, openDevice
  185. */
  186. static Array<MidiDeviceInfo> getAvailableDevices();
  187. /** Returns the MidiDeviceInfo of the default midi output device to use. */
  188. static MidiDeviceInfo getDefaultDevice();
  189. /** Tries to open one of the midi output devices.
  190. This will return a MidiOutput object if it manages to open it, you can then
  191. send messages to this device.
  192. If the device can't be opened, this will return an empty object.
  193. @param deviceIdentifier the ID of the device to open - use the getAvailableDevices() method to
  194. find the available devices that can be opened
  195. @see getDevices
  196. */
  197. static std::unique_ptr<MidiOutput> openDevice (const String& deviceIdentifier);
  198. #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
  199. /** This will try to create a new midi output device (only available on Linux, macOS and iOS).
  200. This will attempt to create a new midi output device with the specified name that other
  201. apps can connect to and use as their midi input.
  202. NB - if you are calling this method on iOS you must have enabled the "Audio Background Capability"
  203. setting in the iOS exporter otherwise this method will fail.
  204. Returns an empty object if a device can't be created.
  205. @param deviceName the name of the device to create
  206. */
  207. static std::unique_ptr<MidiOutput> createNewDevice (const String& deviceName);
  208. #endif
  209. //==============================================================================
  210. /** Destructor. */
  211. ~MidiOutput() override;
  212. /** Returns the MidiDeviceInfo struct containing some information about this device. */
  213. MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
  214. /** Returns the identifier of this device. */
  215. String getIdentifier() const noexcept { return deviceInfo.identifier; }
  216. /** Returns the name of this device. */
  217. String getName() const noexcept { return deviceInfo.name; }
  218. /** Sets a custom name for the device. */
  219. void setName (const String& newName) noexcept { deviceInfo.name = newName; }
  220. //==============================================================================
  221. /** Sends out a MIDI message immediately. */
  222. void sendMessageNow (const MidiMessage& message);
  223. /** Sends out a sequence of MIDI messages immediately. */
  224. void sendBlockOfMessagesNow (const MidiBuffer& buffer);
  225. /** This lets you supply a block of messages that will be sent out at some point
  226. in the future.
  227. The MidiOutput class has an internal thread that can send out timestamped
  228. messages - this appends a set of messages to its internal buffer, ready for
  229. sending.
  230. This will only work if you've already started the thread with startBackgroundThread().
  231. A time is specified, at which the block of messages should be sent. This time uses
  232. the same time base as Time::getMillisecondCounter(), and must be in the future.
  233. The samplesPerSecondForBuffer parameter indicates the number of samples per second
  234. used by the MidiBuffer. Each event in a MidiBuffer has a sample position, and the
  235. samplesPerSecondForBuffer value is needed to convert this sample position to a
  236. real time.
  237. */
  238. void sendBlockOfMessages (const MidiBuffer& buffer,
  239. double millisecondCounterToStartAt,
  240. double samplesPerSecondForBuffer);
  241. /** Gets rid of any midi messages that had been added by sendBlockOfMessages(). */
  242. void clearAllPendingMessages();
  243. /** Starts up a background thread so that the device can send blocks of data.
  244. Call this to get the device ready, before using sendBlockOfMessages().
  245. */
  246. void startBackgroundThread();
  247. /** Stops the background thread, and clears any pending midi events.
  248. @see startBackgroundThread
  249. */
  250. void stopBackgroundThread();
  251. /** Returns true if the background thread used to send blocks of data is running.
  252. @see startBackgroundThread, stopBackgroundThread
  253. */
  254. bool isBackgroundThreadRunning() const noexcept { return isThreadRunning(); }
  255. //==============================================================================
  256. /** Deprecated. */
  257. static StringArray getDevices();
  258. /** Deprecated. */
  259. static int getDefaultDeviceIndex();
  260. /** Deprecated. */
  261. static std::unique_ptr<MidiOutput> openDevice (int);
  262. private:
  263. //==============================================================================
  264. struct PendingMessage
  265. {
  266. PendingMessage (const void* data, int len, double timeStamp)
  267. : message (data, len, timeStamp)
  268. {
  269. }
  270. MidiMessage message;
  271. PendingMessage* next;
  272. };
  273. //==============================================================================
  274. explicit MidiOutput (const String&, const String&);
  275. void run() override;
  276. MidiDeviceInfo deviceInfo;
  277. void* internal = nullptr;
  278. CriticalSection lock;
  279. PendingMessage* firstMessage = nullptr;
  280. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiOutput)
  281. };
  282. } // namespace juce