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.

181 lines
6.4KB

  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. class MidiInput;
  20. //==============================================================================
  21. /**
  22. Receives incoming messages from a physical MIDI input device.
  23. This class is overridden to handle incoming midi messages. See the MidiInput
  24. class for more details.
  25. @see MidiInput
  26. @tags{Audio}
  27. */
  28. class JUCE_API MidiInputCallback
  29. {
  30. public:
  31. /** Destructor. */
  32. virtual ~MidiInputCallback() {}
  33. /** Receives an incoming message.
  34. A MidiInput object will call this method when a midi event arrives. It'll be
  35. called on a high-priority system thread, so avoid doing anything time-consuming
  36. in here, and avoid making any UI calls. You might find the MidiBuffer class helpful
  37. for queueing incoming messages for use later.
  38. @param source the MidiInput object that generated the message
  39. @param message the incoming message. The message's timestamp is set to a value
  40. equivalent to (Time::getMillisecondCounter() / 1000.0) to specify the
  41. time when the message arrived.
  42. */
  43. virtual void handleIncomingMidiMessage (MidiInput* source,
  44. const MidiMessage& message) = 0;
  45. /** Notification sent each time a packet of a multi-packet sysex message arrives.
  46. If a long sysex message is broken up into multiple packets, this callback is made
  47. for each packet that arrives until the message is finished, at which point
  48. the normal handleIncomingMidiMessage() callback will be made with the entire
  49. message.
  50. The message passed in will contain the start of a sysex, but won't be finished
  51. with the terminating 0xf7 byte.
  52. */
  53. virtual void handlePartialSysexMessage (MidiInput* source,
  54. const uint8* messageData,
  55. int numBytesSoFar,
  56. double timestamp)
  57. {
  58. ignoreUnused (source, messageData, numBytesSoFar, timestamp);
  59. }
  60. };
  61. //==============================================================================
  62. /**
  63. Represents a midi input device.
  64. To create one of these, use the static getDevices() method to find out what inputs are
  65. available, and then use the openDevice() method to try to open one.
  66. @see MidiOutput
  67. @tags{Audio}
  68. */
  69. class JUCE_API MidiInput
  70. {
  71. public:
  72. //==============================================================================
  73. /** Returns a list of the available midi input devices.
  74. You can open one of the devices by passing its index into the
  75. openDevice() method.
  76. @see getDefaultDeviceIndex, openDevice
  77. */
  78. static StringArray getDevices();
  79. /** Returns the index of the default midi input device to use.
  80. This refers to the index in the list returned by getDevices().
  81. */
  82. static int getDefaultDeviceIndex();
  83. /** Tries to open one of the midi input devices.
  84. This will return a MidiInput object if it manages to open it. You can then
  85. call start() and stop() on this device, and delete it when no longer needed.
  86. If the device can't be opened, this will return a null pointer.
  87. @param deviceIndex the index of a device from the list returned by getDevices()
  88. @param callback the object that will receive the midi messages from this device.
  89. @see MidiInputCallback, getDevices
  90. */
  91. static MidiInput* openDevice (int deviceIndex,
  92. MidiInputCallback* callback);
  93. #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
  94. /** This will try to create a new midi input device (Not available on Windows).
  95. This will attempt to create a new midi input device with the specified name,
  96. for other apps to connect to.
  97. Returns nullptr if a device can't be created.
  98. @param deviceName the name to use for the new device
  99. @param callback the object that will receive the midi messages from this device.
  100. */
  101. static MidiInput* createNewDevice (const String& deviceName,
  102. MidiInputCallback* callback);
  103. #endif
  104. //==============================================================================
  105. /** Destructor. */
  106. ~MidiInput();
  107. /** Returns the name of this device. */
  108. const String& getName() const noexcept { return name; }
  109. /** Allows you to set a custom name for the device, in case you don't like the name
  110. it was given when created.
  111. */
  112. void setName (const String& newName) noexcept { name = newName; }
  113. //==============================================================================
  114. /** Starts the device running.
  115. After calling this, the device will start sending midi messages to the
  116. MidiInputCallback object that was specified when the openDevice() method
  117. was called.
  118. @see stop
  119. */
  120. void start();
  121. /** Stops the device running.
  122. @see start
  123. */
  124. void stop();
  125. private:
  126. //==============================================================================
  127. String name;
  128. void* internal = nullptr;
  129. // The input objects are created with the openDevice() method.
  130. explicit MidiInput (const String&);
  131. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiInput)
  132. };
  133. } // namespace juce