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.

230 lines
7.1KB

  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. A strongly-typed identifier for a 7-bit request ID with a nullable state.
  22. @tags{Audio}
  23. */
  24. class RequestID
  25. {
  26. public:
  27. /** Constructs a RequestID if the provided value is valid, i.e. its most significant bit is
  28. not set. Otherwise, returns nullopt.
  29. */
  30. static std::optional<RequestID> create (uint8_t v)
  31. {
  32. if (v < 128)
  33. return RequestID { v };
  34. return {};
  35. }
  36. /** Constructs a RequestID if the provided value is valid, i.e. its most significant bit is
  37. not set. Otherwise, returns nullopt.
  38. */
  39. static std::optional<RequestID> create (std::byte value)
  40. {
  41. return create (static_cast<uint8_t> (value));
  42. }
  43. /** Returns the byte corresponding to this ID. */
  44. std::byte asByte() const
  45. {
  46. return std::byte { value };
  47. }
  48. /** Returns the int value of this ID. */
  49. uint8_t asInt() const
  50. {
  51. return value;
  52. }
  53. /** Equality operator. */
  54. bool operator== (RequestID other) const
  55. {
  56. return value == other.value;
  57. }
  58. /** Inequality operator. */
  59. bool operator!= (RequestID other) const
  60. {
  61. return ! operator== (other);
  62. }
  63. private:
  64. /* Constructs a non-null request ID.
  65. The argument must not have its most significant bit set.
  66. */
  67. explicit RequestID (uint8_t index)
  68. : value (index)
  69. {
  70. // IDs must only use the lowest 7 bits
  71. jassert (value < 128);
  72. }
  73. uint8_t value{};
  74. };
  75. /** A strongly-typed 64-bit identifier. */
  76. enum class Token64 : uint64_t {};
  77. /** Compares Token64 instances. */
  78. constexpr bool operator< (Token64 a, Token64 b)
  79. {
  80. return toUnderlyingType (a) < toUnderlyingType (b);
  81. }
  82. /**
  83. Accumulates message chunks that have been sent by another device in response
  84. to a transaction initiated by a local device.
  85. @tags{Audio}
  86. */
  87. class InitiatorPropertyExchangeCache
  88. {
  89. public:
  90. InitiatorPropertyExchangeCache();
  91. ~InitiatorPropertyExchangeCache();
  92. InitiatorPropertyExchangeCache (InitiatorPropertyExchangeCache&&) noexcept;
  93. InitiatorPropertyExchangeCache& operator= (InitiatorPropertyExchangeCache&&) noexcept;
  94. JUCE_DECLARE_NON_COPYABLE (InitiatorPropertyExchangeCache)
  95. /** Picks an unused request ID, and prepares the cache for that ID to accumulate message chunks.
  96. Incoming chunks added with addChunk are generated by another device acting as a responder.
  97. */
  98. std::optional<Token64> primeCache (uint8_t maxSimultaneousRequests,
  99. std::function<void (const PropertyExchangeResult&)> onDone);
  100. /** Terminates/cancels an ongoing transaction.
  101. Returns true if the termination had an effect (i.e. the transaction was still ongoing), or
  102. false otherwise (the transaction already ended or never started).
  103. */
  104. bool terminate (Token64);
  105. /** If there's a transaction ongoing with the given request id, returns the token uniquely
  106. identifying that transaction, otherwise returns nullopt.
  107. */
  108. std::optional<Token64> getTokenForRequestId (RequestID) const;
  109. /** If the token refers to an ongoing transaction, returns the request id of that transaction.
  110. Otherwise, returns an invalid request id.
  111. */
  112. std::optional<RequestID> getRequestIdForToken (Token64) const;
  113. /** Adds a message chunk for the provided transaction id. */
  114. void addChunk (RequestID, const Message::DynamicSizePropertyExchange& chunk);
  115. /** Updates the transaction state based on the contents of the provided notification. */
  116. void notify (RequestID, Span<const std::byte> header);
  117. /** Returns all ongoing transactions. */
  118. std::vector<Token64> getOngoingTransactions() const;
  119. private:
  120. class Impl;
  121. std::unique_ptr<Impl> pimpl;
  122. };
  123. //==============================================================================
  124. /**
  125. Accumulates message chunks that form a request initiated by a remote device.
  126. @tags{Audio}
  127. */
  128. class ResponderPropertyExchangeCache
  129. {
  130. public:
  131. ResponderPropertyExchangeCache();
  132. ~ResponderPropertyExchangeCache();
  133. ResponderPropertyExchangeCache (ResponderPropertyExchangeCache&&) noexcept;
  134. ResponderPropertyExchangeCache& operator= (ResponderPropertyExchangeCache&&) noexcept;
  135. JUCE_DECLARE_NON_COPYABLE (ResponderPropertyExchangeCache)
  136. /** Prepares the cache for the given requestID to accumulate message chunks.
  137. Incoming chunks added with addChunk are generated by another device acting as an initiator.
  138. */
  139. void primeCache (uint8_t maxSimultaneousTransactions,
  140. std::function<void (const PropertyExchangeResult&)> onDone,
  141. RequestID id);
  142. /** Adds a message chunk for the provided transaction id. */
  143. void addChunk (RequestID, const Message::DynamicSizePropertyExchange& chunk);
  144. /** Updates the transaction state based on the contents of the provided notification. */
  145. void notify (RequestID, Span<const std::byte> header);
  146. /** Returns the number of transactions that have been started but not finished. */
  147. int countOngoingTransactions() const;
  148. private:
  149. class Impl;
  150. std::unique_ptr<Impl> pimpl;
  151. };
  152. //==============================================================================
  153. /**
  154. An interface for objects that provide resources for property exchange
  155. transactions.
  156. @tags{Audio}
  157. */
  158. class CacheProvider
  159. {
  160. public:
  161. virtual ~CacheProvider() = default;
  162. /** Returns a set containing all of the MUIDs currently known to the provider. */
  163. virtual std::set<MUID> getDiscoveredMuids() const = 0;
  164. /** Returns a property exchange cache for accumulating replies to transactions
  165. we initiated.
  166. */
  167. virtual InitiatorPropertyExchangeCache* getCacheForMuidAsInitiator (MUID m) = 0;
  168. /** Returns a property exchange cache for accumulating requests initiated
  169. by other devices.
  170. */
  171. virtual ResponderPropertyExchangeCache* getCacheForMuidAsResponder (MUID m) = 0;
  172. /** Returns the maximum sysex size supported by the device with the
  173. given MUID.
  174. */
  175. virtual int getMaxSysexSizeForMuid (MUID m) const = 0;
  176. };
  177. } // namespace juce::midi_ci