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.

392 lines
15KB

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