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_MidiDataConcatenator.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Helper class that takes chunks of incoming midi bytes, packages them into
  22. messages, and dispatches them to a midi callback.
  23. @tags{Audio}
  24. */
  25. class MidiDataConcatenator
  26. {
  27. public:
  28. MidiDataConcatenator (int initialBufferSize)
  29. : pendingSysexData ((size_t) initialBufferSize)
  30. {
  31. }
  32. void reset()
  33. {
  34. currentMessageLen = 0;
  35. pendingSysexSize = 0;
  36. pendingSysexTime = 0;
  37. }
  38. template <typename UserDataType, typename CallbackType>
  39. void pushMidiData (const void* inputData, int numBytes, double time,
  40. UserDataType* input, CallbackType& callback)
  41. {
  42. auto d = static_cast<const uint8*> (inputData);
  43. while (numBytes > 0)
  44. {
  45. auto nextByte = *d;
  46. if (pendingSysexSize != 0 || nextByte == 0xf0)
  47. {
  48. processSysex (d, numBytes, time, input, callback);
  49. currentMessageLen = 0;
  50. continue;
  51. }
  52. ++d;
  53. --numBytes;
  54. if (isRealtimeMessage (nextByte))
  55. {
  56. callback.handleIncomingMidiMessage (input, MidiMessage (nextByte, time));
  57. // These can be embedded in the middle of a normal message, so we won't
  58. // reset the currentMessageLen here.
  59. continue;
  60. }
  61. if (isInitialByte (nextByte))
  62. {
  63. currentMessage[0] = nextByte;
  64. currentMessageLen = 1;
  65. }
  66. else if (currentMessageLen > 0 && currentMessageLen < 3)
  67. {
  68. currentMessage[currentMessageLen++] = nextByte;
  69. }
  70. else
  71. {
  72. // message is too long or invalid MIDI - abandon it and start again with the next byte
  73. currentMessageLen = 0;
  74. continue;
  75. }
  76. auto expectedLength = MidiMessage::getMessageLengthFromFirstByte (currentMessage[0]);
  77. if (expectedLength == currentMessageLen)
  78. {
  79. callback.handleIncomingMidiMessage (input, MidiMessage (currentMessage, expectedLength, time));
  80. currentMessageLen = 1; // reset, but leave the first byte to use as the running status byte
  81. }
  82. }
  83. }
  84. private:
  85. template <typename UserDataType, typename CallbackType>
  86. void processSysex (const uint8*& d, int& numBytes, double time,
  87. UserDataType* input, CallbackType& callback)
  88. {
  89. if (*d == 0xf0)
  90. {
  91. pendingSysexSize = 0;
  92. pendingSysexTime = time;
  93. }
  94. pendingSysexData.ensureSize ((size_t) (pendingSysexSize + numBytes), false);
  95. auto totalMessage = static_cast<uint8*> (pendingSysexData.getData());
  96. auto dest = totalMessage + pendingSysexSize;
  97. do
  98. {
  99. if (pendingSysexSize > 0 && isStatusByte (*d))
  100. {
  101. if (*d == 0xf7)
  102. {
  103. *dest++ = *d++;
  104. ++pendingSysexSize;
  105. --numBytes;
  106. break;
  107. }
  108. if (*d >= 0xfa || *d == 0xf8)
  109. {
  110. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  111. ++d;
  112. --numBytes;
  113. }
  114. else
  115. {
  116. pendingSysexSize = 0;
  117. int used = 0;
  118. const MidiMessage m (d, numBytes, used, 0, time);
  119. if (used > 0)
  120. {
  121. callback.handleIncomingMidiMessage (input, m);
  122. numBytes -= used;
  123. d += used;
  124. }
  125. break;
  126. }
  127. }
  128. else
  129. {
  130. *dest++ = *d++;
  131. ++pendingSysexSize;
  132. --numBytes;
  133. }
  134. }
  135. while (numBytes > 0);
  136. if (pendingSysexSize > 0)
  137. {
  138. if (totalMessage [pendingSysexSize - 1] == 0xf7)
  139. {
  140. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingSysexSize, pendingSysexTime));
  141. pendingSysexSize = 0;
  142. }
  143. else
  144. {
  145. callback.handlePartialSysexMessage (input, totalMessage, pendingSysexSize, pendingSysexTime);
  146. }
  147. }
  148. }
  149. static bool isRealtimeMessage (uint8 byte) { return byte >= 0xf8 && byte <= 0xfe; }
  150. static bool isStatusByte (uint8 byte) { return byte >= 0x80; }
  151. static bool isInitialByte (uint8 byte) { return isStatusByte (byte) && byte != 0xf7; }
  152. uint8 currentMessage[3];
  153. int currentMessageLen = 0;
  154. MemoryBlock pendingSysexData;
  155. double pendingSysexTime = 0;
  156. int pendingSysexSize = 0;
  157. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  158. };
  159. } // namespace juce