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.

388 lines
15KB

  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. /** @internal */
  123. class Pimpl;
  124. private:
  125. //==============================================================================
  126. explicit MidiInput (const String&, const String&);
  127. MidiDeviceInfo deviceInfo;
  128. std::unique_ptr<Pimpl> internal;
  129. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInput)
  130. };
  131. //==============================================================================
  132. /**
  133. Receives incoming messages from a physical MIDI input device.
  134. This class is overridden to handle incoming midi messages. See the MidiInput
  135. class for more details.
  136. @see MidiInput
  137. @tags{Audio}
  138. */
  139. class JUCE_API MidiInputCallback
  140. {
  141. public:
  142. /** Destructor. */
  143. virtual ~MidiInputCallback() = default;
  144. /** Receives an incoming message.
  145. A MidiInput object will call this method when a midi event arrives. It'll be
  146. called on a high-priority system thread, so avoid doing anything time-consuming
  147. in here, and avoid making any UI calls. You might find the MidiBuffer class helpful
  148. for queueing incoming messages for use later.
  149. @param source the MidiInput object that generated the message
  150. @param message the incoming message. The message's timestamp is set to a value
  151. equivalent to (Time::getMillisecondCounter() / 1000.0) to specify the
  152. time when the message arrived
  153. */
  154. virtual void handleIncomingMidiMessage (MidiInput* source,
  155. const MidiMessage& message) = 0;
  156. /** Notification sent each time a packet of a multi-packet sysex message arrives.
  157. If a long sysex message is broken up into multiple packets, this callback is made
  158. for each packet that arrives until the message is finished, at which point
  159. the normal handleIncomingMidiMessage() callback will be made with the entire
  160. message.
  161. The message passed in will contain the start of a sysex, but won't be finished
  162. with the terminating 0xf7 byte.
  163. */
  164. virtual void handlePartialSysexMessage (MidiInput* source,
  165. const uint8* messageData,
  166. int numBytesSoFar,
  167. double timestamp)
  168. {
  169. ignoreUnused (source, messageData, numBytesSoFar, timestamp);
  170. }
  171. };
  172. //==============================================================================
  173. /**
  174. Represents a midi output device.
  175. To create one of these, use the static getAvailableDevices() method to find out what
  176. outputs are available, and then use the openDevice() method to try to open one.
  177. @see MidiInput
  178. @tags{Audio}
  179. */
  180. class JUCE_API MidiOutput final : private Thread
  181. {
  182. public:
  183. //==============================================================================
  184. /** Returns a list of the available midi output devices.
  185. You can open one of the devices by passing its identifier into the openDevice() method.
  186. @see MidiDeviceInfo, getDevices, getDefaultDeviceIndex, openDevice
  187. */
  188. static Array<MidiDeviceInfo> getAvailableDevices();
  189. /** Returns the MidiDeviceInfo of the default midi output device to use. */
  190. static MidiDeviceInfo getDefaultDevice();
  191. /** Tries to open one of the midi output devices.
  192. This will return a MidiOutput object if it manages to open it, you can then
  193. send messages to this device.
  194. If the device can't be opened, this will return an empty object.
  195. @param deviceIdentifier the ID of the device to open - use the getAvailableDevices() method to
  196. find the available devices that can be opened
  197. @see getDevices
  198. */
  199. static std::unique_ptr<MidiOutput> openDevice (const String& deviceIdentifier);
  200. #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
  201. /** This will try to create a new midi output device (only available on Linux, macOS and iOS).
  202. This will attempt to create a new midi output device with the specified name that other
  203. apps can connect to and use as their midi input.
  204. NB - if you are calling this method on iOS you must have enabled the "Audio Background Capability"
  205. setting in the iOS exporter otherwise this method will fail.
  206. Returns an empty object if a device can't be created.
  207. @param deviceName the name of the device to create
  208. */
  209. static std::unique_ptr<MidiOutput> createNewDevice (const String& deviceName);
  210. #endif
  211. //==============================================================================
  212. /** Destructor. */
  213. ~MidiOutput() override;
  214. /** Returns the MidiDeviceInfo struct containing some information about this device. */
  215. MidiDeviceInfo getDeviceInfo() const noexcept { return deviceInfo; }
  216. /** Returns the identifier of this device. */
  217. String getIdentifier() const noexcept { return deviceInfo.identifier; }
  218. /** Returns the name of this device. */
  219. String getName() const noexcept { return deviceInfo.name; }
  220. /** Sets a custom name for the device. */
  221. void setName (const String& newName) noexcept { deviceInfo.name = newName; }
  222. //==============================================================================
  223. /** Sends out a MIDI message immediately. */
  224. void sendMessageNow (const MidiMessage& message);
  225. /** Sends out a sequence of MIDI messages immediately. */
  226. void sendBlockOfMessagesNow (const MidiBuffer& buffer);
  227. /** This lets you supply a block of messages that will be sent out at some point
  228. in the future.
  229. The MidiOutput class has an internal thread that can send out timestamped
  230. messages - this appends a set of messages to its internal buffer, ready for
  231. sending.
  232. This will only work if you've already started the thread with startBackgroundThread().
  233. A time is specified, at which the block of messages should be sent. This time uses
  234. the same time base as Time::getMillisecondCounter(), and must be in the future.
  235. The samplesPerSecondForBuffer parameter indicates the number of samples per second
  236. used by the MidiBuffer. Each event in a MidiBuffer has a sample position, and the
  237. samplesPerSecondForBuffer value is needed to convert this sample position to a
  238. real time.
  239. */
  240. void sendBlockOfMessages (const MidiBuffer& buffer,
  241. double millisecondCounterToStartAt,
  242. double samplesPerSecondForBuffer);
  243. /** Gets rid of any midi messages that had been added by sendBlockOfMessages(). */
  244. void clearAllPendingMessages();
  245. /** Starts up a background thread so that the device can send blocks of data.
  246. Call this to get the device ready, before using sendBlockOfMessages().
  247. */
  248. void startBackgroundThread();
  249. /** Stops the background thread, and clears any pending midi events.
  250. @see startBackgroundThread
  251. */
  252. void stopBackgroundThread();
  253. /** Returns true if the background thread used to send blocks of data is running.
  254. @see startBackgroundThread, stopBackgroundThread
  255. */
  256. bool isBackgroundThreadRunning() const noexcept { return isThreadRunning(); }
  257. //==============================================================================
  258. /** Deprecated. */
  259. static StringArray getDevices();
  260. /** Deprecated. */
  261. static int getDefaultDeviceIndex();
  262. /** Deprecated. */
  263. static std::unique_ptr<MidiOutput> openDevice (int);
  264. /** @internal */
  265. class Pimpl;
  266. private:
  267. //==============================================================================
  268. struct PendingMessage
  269. {
  270. PendingMessage (const void* data, int len, double timeStamp)
  271. : message (data, len, timeStamp)
  272. {
  273. }
  274. MidiMessage message;
  275. PendingMessage* next;
  276. };
  277. //==============================================================================
  278. explicit MidiOutput (const String&, const String&);
  279. void run() override;
  280. MidiDeviceInfo deviceInfo;
  281. std::unique_ptr<Pimpl> internal;
  282. CriticalSection lock;
  283. PendingMessage* firstMessage = nullptr;
  284. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiOutput)
  285. };
  286. } // namespace juce