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.

168 lines
6.3KB

  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. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::midi_ci
  19. {
  20. //==============================================================================
  21. /**
  22. Configuration options for a Device.
  23. The options set here will remain constant over the lifetime of a Device.
  24. @tags{Audio}
  25. */
  26. class DeviceOptions
  27. {
  28. public:
  29. static constexpr auto beginValidAscii = 32; // inclusive
  30. static constexpr auto endValidAscii = 127; // exclusive
  31. /** Creates a random product instance ID.
  32. This isn't really recommended - it's probably better to have a unique ID that remains
  33. persistent after a restart.
  34. */
  35. static std::array<char, 16> makeProductInstanceId (Random& random)
  36. {
  37. std::array<char, 16> result{};
  38. for (auto& c : result)
  39. c = (char) random.nextInt ({ beginValidAscii, endValidAscii });
  40. return result;
  41. }
  42. /** One or more DeviceMessageHandlers that should receive callbacks with any messages that the
  43. device wishes to send.
  44. Referenced DeviceMessageHandlers *must* outlive any Device constructed from these options.
  45. */
  46. [[nodiscard]] DeviceOptions withOutputs (std::vector<DeviceMessageHandler*> x) const
  47. {
  48. return withMember (*this, &DeviceOptions::outputs, x);
  49. }
  50. /** The function block layout of this device. */
  51. [[nodiscard]] DeviceOptions withFunctionBlock (FunctionBlock x) const
  52. {
  53. return withMember (*this, &DeviceOptions::functionBlock, x);
  54. }
  55. /** Basic information about the device used to determine manufacturer, model, etc.
  56. In order to populate this correctly, you'll need to register with the MIDI association -
  57. otherwise you might accidentally end up using IDs that are already assigned to other
  58. companies/individuals.
  59. */
  60. [[nodiscard]] DeviceOptions withDeviceInfo (const ump::DeviceInfo& x) const
  61. {
  62. return withMember (*this, &DeviceOptions::deviceInfo, x);
  63. }
  64. /** The features that you want to enable on the device.
  65. If you enable property exchange, you may wish to supply a PropertyDelegate using
  66. withPropertyDelegate().
  67. If you enable profile configuration, you may wish to supply a ProfileDelegate using
  68. withProfileDelegate().
  69. Process inquiry is not currently supported.
  70. */
  71. [[nodiscard]] DeviceOptions withFeatures (DeviceFeatures x) const
  72. {
  73. return withMember (*this, &DeviceOptions::features, x);
  74. }
  75. /** The maximum size of sysex messages to accept and to produce. */
  76. [[nodiscard]] DeviceOptions withMaxSysExSize (size_t x) const
  77. {
  78. return withMember (*this, &DeviceOptions::maxSysExSize, x);
  79. }
  80. /** Specifies a profile delegate that can be used to respond to particular profile events.
  81. The referenced ProfileDelegate *must* outlive the Device.
  82. */
  83. [[nodiscard]] DeviceOptions withProfileDelegate (ProfileDelegate* x) const
  84. {
  85. return withMember (*this, &DeviceOptions::profileDelegate, x);
  86. }
  87. /** Specifies a property delegate that can be used to respond to particular property events.
  88. The referenced PropertyDelegate *must* outlive the Device.
  89. */
  90. [[nodiscard]] DeviceOptions withPropertyDelegate (PropertyDelegate* x) const
  91. {
  92. return withMember (*this, &DeviceOptions::propertyDelegate, x);
  93. }
  94. /** Specifies a product instance ID that will be returned in endpoint response messages. */
  95. [[nodiscard]] DeviceOptions withProductInstanceId (const std::array<char, 16>& x) const
  96. {
  97. const auto null = std::find (x.begin(), x.end(), 0);
  98. if (! std::all_of (x.begin(), null, [] (char c) { return beginValidAscii <= c && c < endValidAscii; }))
  99. {
  100. // The product instance ID must be made up of ASCII characters
  101. jassertfalse;
  102. return *this;
  103. }
  104. if (std::any_of (null, x.end(), [] (auto c) { return c != 0; }))
  105. {
  106. // All characters after the null terminator must be 0
  107. jassertfalse;
  108. return *this;
  109. }
  110. return withMember (*this, &DeviceOptions::productInstanceId, x);
  111. }
  112. /** @see withOutputs() */
  113. [[nodiscard]] const auto& getOutputs() const { return outputs; }
  114. /** @see withFunctionBlock() */
  115. [[nodiscard]] const auto& getFunctionBlock() const { return functionBlock; }
  116. /** @see withDeviceInfo() */
  117. [[nodiscard]] const auto& getDeviceInfo() const { return deviceInfo; }
  118. /** @see withFeatures() */
  119. [[nodiscard]] const auto& getFeatures() const { return features; }
  120. /** @see withMaxSysExSize() */
  121. [[nodiscard]] const auto& getMaxSysExSize() const { return maxSysExSize; }
  122. /** @see withProductInstanceId() */
  123. [[nodiscard]] const auto& getProductInstanceId() const { return productInstanceId; }
  124. /** @see withProfileDelegate() */
  125. [[nodiscard]] const auto& getProfileDelegate() const { return profileDelegate; }
  126. /** @see withPropertyDelegate() */
  127. [[nodiscard]] const auto& getPropertyDelegate() const { return propertyDelegate; }
  128. private:
  129. std::vector<DeviceMessageHandler*> outputs;
  130. FunctionBlock functionBlock;
  131. ump::DeviceInfo deviceInfo;
  132. DeviceFeatures features;
  133. size_t maxSysExSize = 512;
  134. std::array<char, 16> productInstanceId{};
  135. ProfileDelegate* profileDelegate = nullptr;
  136. PropertyDelegate* propertyDelegate = nullptr;
  137. };
  138. } // namespace juce::midi_ci