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.

88 lines
3.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. Utility functions for working with data formats used by property exchange
  22. messages.
  23. @tags{Audio}
  24. */
  25. struct Encodings
  26. {
  27. /** Text in ACK and NAK messages can't be utf-8 or ASCII because each byte only has 7 usable bits.
  28. The encoding rules are in section 5.10.4 of the CI spec.
  29. */
  30. static String stringFrom7BitText (Span<const std::byte> bytes);
  31. /** Text in ACK and NAK messages can't be utf-8 or ASCII because each byte only has 7 usable bits.
  32. The encoding rules are in section 5.10.4 of the CI spec.
  33. */
  34. static std::vector<std::byte> stringTo7BitText (const String& text);
  35. /** Converts a list of bytes representing a 7-bit ASCII string to JSON. */
  36. static var jsonFrom7BitText (Span<const std::byte> bytes)
  37. {
  38. return JSON::parse (stringFrom7BitText (bytes));
  39. }
  40. /** Converts a JSON object to a list of bytes in 7-bit ASCII format. */
  41. static std::vector<std::byte> jsonTo7BitText (const var& v)
  42. {
  43. return stringTo7BitText (JSON::toString (v, JSON::FormatOptions{}.withSpacing (JSON::Spacing::none)));
  44. }
  45. /** Each group of seven stored bytes is transmitted as eight bytes.
  46. First, the sign bits of the seven bytes are sent, followed by the low-order 7 bits of each byte.
  47. */
  48. static std::vector<std::byte> toMcoded7 (Span<const std::byte> bytes);
  49. /** Each group of seven stored bytes is transmitted as eight bytes.
  50. First, the sign bits of the seven bytes are sent, followed by the low-order 7 bits of each byte.
  51. */
  52. static std::vector<std::byte> fromMcoded7 (Span<const std::byte> bytes);
  53. /** Attempts to encode the provided byte span using the specified encoding.
  54. The ASCII encoding does not make any changes to the input stream, but
  55. encoding will fail if any byte has its most significant bit set.
  56. */
  57. static std::optional<std::vector<std::byte>> tryEncode (Span<const std::byte> bytes,
  58. Encoding mutualEncoding);
  59. /** Decodes the provided byte span using the specified encoding.
  60. All bytes of the input must be 7-bit values, i.e. all most-significant bits
  61. are unset.
  62. */
  63. static std::vector<std::byte> decode (Span<const std::byte> bytes, Encoding mutualEncoding);
  64. Encodings() = delete;
  65. };
  66. } // namespace juce::midi_ci