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.

juce_MidiInput.h 6.4KB

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