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.

144 lines
5.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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Controls a physical MIDI output device.
  22. To create one of these, use the static getDevices() method to get a list of the
  23. available output devices, then use the openDevice() method to try to open one.
  24. @see MidiInput
  25. */
  26. class JUCE_API MidiOutput : private Thread
  27. {
  28. public:
  29. //==============================================================================
  30. /** Returns a list of the available midi output devices.
  31. You can open one of the devices by passing its index into the
  32. openDevice() method.
  33. @see getDefaultDeviceIndex, openDevice
  34. */
  35. static StringArray getDevices();
  36. /** Returns the index of the default midi output device to use.
  37. This refers to the index in the list returned by getDevices().
  38. */
  39. static int getDefaultDeviceIndex();
  40. /** Tries to open one of the midi output devices.
  41. This will return a MidiOutput object if it manages to open it. You can then
  42. send messages to this device, and delete it when no longer needed.
  43. If the device can't be opened, this will return a null pointer.
  44. @param deviceIndex the index of a device from the list returned by getDevices()
  45. @see getDevices
  46. */
  47. static MidiOutput* openDevice (int deviceIndex);
  48. #if JUCE_LINUX || JUCE_MAC || JUCE_IOS || DOXYGEN
  49. /** This will try to create a new midi output device (Not available on Windows).
  50. This will attempt to create a new midi output device that other apps can connect
  51. to and use as their midi input.
  52. Returns nullptr if a device can't be created.
  53. @param deviceName the name to use for the new device
  54. */
  55. static MidiOutput* createNewDevice (const String& deviceName);
  56. #endif
  57. //==============================================================================
  58. /** Destructor. */
  59. ~MidiOutput();
  60. /** Returns the name of this device. */
  61. const String& getName() const noexcept { return name; }
  62. /** Sends out a MIDI message immediately. */
  63. void sendMessageNow (const MidiMessage& message);
  64. /** Sends out a sequence of MIDI messages immediately. */
  65. void sendBlockOfMessagesNow (const MidiBuffer& buffer);
  66. //==============================================================================
  67. /** This lets you supply a block of messages that will be sent out at some point
  68. in the future.
  69. The MidiOutput class has an internal thread that can send out timestamped
  70. messages - this appends a set of messages to its internal buffer, ready for
  71. sending.
  72. This will only work if you've already started the thread with startBackgroundThread().
  73. A time is specified, at which the block of messages should be sent. This time uses
  74. the same time base as Time::getMillisecondCounter(), and must be in the future.
  75. The samplesPerSecondForBuffer parameter indicates the number of samples per second
  76. used by the MidiBuffer. Each event in a MidiBuffer has a sample position, and the
  77. samplesPerSecondForBuffer value is needed to convert this sample position to a
  78. real time.
  79. */
  80. void sendBlockOfMessages (const MidiBuffer& buffer,
  81. double millisecondCounterToStartAt,
  82. double samplesPerSecondForBuffer);
  83. /** Gets rid of any midi messages that had been added by sendBlockOfMessages(). */
  84. void clearAllPendingMessages();
  85. /** Starts up a background thread so that the device can send blocks of data.
  86. Call this to get the device ready, before using sendBlockOfMessages().
  87. */
  88. void startBackgroundThread();
  89. /** Stops the background thread, and clears any pending midi events.
  90. @see startBackgroundThread
  91. */
  92. void stopBackgroundThread();
  93. private:
  94. //==============================================================================
  95. void* internal = nullptr;
  96. CriticalSection lock;
  97. struct PendingMessage;
  98. PendingMessage* firstMessage;
  99. String name;
  100. MidiOutput (const String& midiName); // These objects are created with the openDevice() method.
  101. void run() override;
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiOutput)
  103. };
  104. } // namespace juce