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.

372 lines
14KB

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