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.

84 lines
2.6KB

  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 28-bit ID that uniquely identifies a device taking part in a series of
  22. MIDI-CI transactions.
  23. @tags{Audio}
  24. */
  25. class MUID
  26. {
  27. constexpr explicit MUID (uint32_t v) : value (v) {}
  28. // 0x0fffff00 to 0x0ffffffe are reserved, 0x0fffffff is 'broadcast'
  29. static constexpr uint32_t userMuidEnd = 0x0fffff00;
  30. static constexpr uint32_t mask = 0x0fffffff;
  31. uint32_t value{};
  32. public:
  33. /** Returns the ID as a plain integer. */
  34. constexpr uint32_t get() const { return value; }
  35. /** Converts the provided integer to a MUID without validation that it
  36. is within the allowed range.
  37. */
  38. static MUID makeUnchecked (uint32_t v)
  39. {
  40. // If this is hit, the MUID has too many bits set!
  41. jassert ((v & mask) == v);
  42. return MUID (v);
  43. }
  44. /** Returns a MUID if the provided value is within the valid range for
  45. MUID values; otherwise returns nullopt.
  46. */
  47. static std::optional<MUID> make (uint32_t v)
  48. {
  49. if ((v & mask) == v)
  50. return makeUnchecked (v);
  51. return {};
  52. }
  53. /** Makes a random MUID using the provided random engine. */
  54. static MUID makeRandom (Random& r)
  55. {
  56. return makeUnchecked ((uint32_t) r.nextInt (userMuidEnd));
  57. }
  58. bool operator== (const MUID other) const { return value == other.value; }
  59. bool operator!= (const MUID other) const { return value != other.value; }
  60. bool operator< (const MUID other) const { return value < other.value; }
  61. /** Returns the special MUID representing the broadcast address. */
  62. static constexpr MUID getBroadcast() { return MUID { mask }; }
  63. };
  64. } // namespace juce::midi_ci