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.

174 lines
6.3KB

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