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.

juce_UMPUtils.h 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 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. #ifndef DOXYGEN
  18. namespace juce
  19. {
  20. namespace universal_midi_packets
  21. {
  22. /**
  23. Helpful types and functions for interacting with Universal MIDI Packets.
  24. @tags{Audio}
  25. */
  26. struct Utils
  27. {
  28. /** Joins 4 bytes into a single 32-bit word. */
  29. static constexpr uint32_t bytesToWord (uint8_t a, uint8_t b, uint8_t c, uint8_t d)
  30. {
  31. return uint32_t (a << 0x18 | b << 0x10 | c << 0x08 | d << 0x00);
  32. }
  33. /** Returns the expected number of 32-bit words in a Universal MIDI Packet, given
  34. the first word of the packet.
  35. The result will be between 1 and 4 inclusive.
  36. A result of 1 means that the word is itself a complete packet.
  37. */
  38. static uint32_t getNumWordsForMessageType (uint32_t);
  39. /**
  40. Helper functions for setting/getting 4-bit ranges inside a 32-bit word.
  41. */
  42. template <size_t Index>
  43. struct U4
  44. {
  45. static constexpr uint32_t shift = (uint32_t) 0x1c - Index * 4;
  46. static constexpr uint32_t set (uint32_t word, uint8_t value)
  47. {
  48. return (word & ~((uint32_t) 0xf << shift)) | (uint32_t) ((value & 0xf) << shift);
  49. }
  50. static constexpr uint8_t get (uint32_t word)
  51. {
  52. return (uint8_t) ((word >> shift) & 0xf);
  53. }
  54. };
  55. /**
  56. Helper functions for setting/getting 8-bit ranges inside a 32-bit word.
  57. */
  58. template <size_t Index>
  59. struct U8
  60. {
  61. static constexpr uint32_t shift = (uint32_t) 0x18 - Index * 8;
  62. static constexpr uint32_t set (uint32_t word, uint8_t value)
  63. {
  64. return (word & ~((uint32_t) 0xff << shift)) | (uint32_t) (value << shift);
  65. }
  66. static constexpr uint8_t get (uint32_t word)
  67. {
  68. return (uint8_t) ((word >> shift) & 0xff);
  69. }
  70. };
  71. /**
  72. Helper functions for setting/getting 16-bit ranges inside a 32-bit word.
  73. */
  74. template <size_t Index>
  75. struct U16
  76. {
  77. static constexpr uint32_t shift = (uint32_t) 0x10 - Index * 16;
  78. static constexpr uint32_t set (uint32_t word, uint16_t value)
  79. {
  80. return (word & ~((uint32_t) 0xffff << shift)) | (uint32_t) (value << shift);
  81. }
  82. static constexpr uint16_t get (uint32_t word)
  83. {
  84. return (uint16_t) ((word >> shift) & 0xffff);
  85. }
  86. };
  87. static constexpr uint8_t getMessageType (uint32_t w) noexcept { return U4<0>::get (w); }
  88. static constexpr uint8_t getGroup (uint32_t w) noexcept { return U4<1>::get (w); }
  89. static constexpr uint8_t getStatus (uint32_t w) noexcept { return U4<2>::get (w); }
  90. static constexpr uint8_t getChannel (uint32_t w) noexcept { return U4<3>::get (w); }
  91. };
  92. }
  93. }
  94. #endif