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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. */
  24. class MidiDataConcatenator
  25. {
  26. public:
  27. //==============================================================================
  28. MidiDataConcatenator (int initialBufferSize)
  29. : pendingData ((size_t) initialBufferSize)
  30. {
  31. }
  32. void reset()
  33. {
  34. pendingBytes = 0;
  35. runningStatus = 0;
  36. pendingDataTime = 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. const uint8* d = static_cast<const uint8*> (inputData);
  43. while (numBytes > 0)
  44. {
  45. if (pendingBytes > 0 || d[0] == 0xf0)
  46. {
  47. processSysex (d, numBytes, time, input, callback);
  48. runningStatus = 0;
  49. }
  50. else
  51. {
  52. int len = 0;
  53. uint8 data[3];
  54. while (numBytes > 0)
  55. {
  56. // If there's a realtime message embedded in the middle of
  57. // the normal message, handle it now..
  58. if (*d >= 0xf8 && *d <= 0xfe)
  59. {
  60. callback.handleIncomingMidiMessage (input, MidiMessage (*d++, time));
  61. --numBytes;
  62. }
  63. else
  64. {
  65. if (len == 0 && *d < 0x80 && runningStatus >= 0x80)
  66. data[len++] = runningStatus;
  67. data[len++] = *d++;
  68. --numBytes;
  69. const uint8 firstByte = data[0];
  70. if (firstByte < 0x80 || firstByte == 0xf7)
  71. {
  72. len = 0;
  73. break; // ignore this malformed MIDI message..
  74. }
  75. if (len >= MidiMessage::getMessageLengthFromFirstByte (firstByte))
  76. break;
  77. }
  78. }
  79. if (len > 0)
  80. {
  81. int used = 0;
  82. const MidiMessage m (data, len, used, 0, time);
  83. if (used <= 0)
  84. break; // malformed message..
  85. jassert (used == len);
  86. callback.handleIncomingMidiMessage (input, m);
  87. runningStatus = data[0];
  88. }
  89. }
  90. }
  91. }
  92. private:
  93. template <typename UserDataType, typename CallbackType>
  94. void processSysex (const uint8*& d, int& numBytes, double time,
  95. UserDataType* input, CallbackType& callback)
  96. {
  97. if (*d == 0xf0)
  98. {
  99. pendingBytes = 0;
  100. pendingDataTime = time;
  101. }
  102. pendingData.ensureSize ((size_t) (pendingBytes + numBytes), false);
  103. uint8* totalMessage = static_cast<uint8*> (pendingData.getData());
  104. uint8* dest = totalMessage + pendingBytes;
  105. do
  106. {
  107. if (pendingBytes > 0 && *d >= 0x80)
  108. {
  109. if (*d == 0xf7)
  110. {
  111. *dest++ = *d++;
  112. ++pendingBytes;
  113. --numBytes;
  114. break;
  115. }
  116. if (*d >= 0xfa || *d == 0xf8)
  117. {
  118. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  119. ++d;
  120. --numBytes;
  121. }
  122. else
  123. {
  124. pendingBytes = 0;
  125. int used = 0;
  126. const MidiMessage m (d, numBytes, used, 0, time);
  127. if (used > 0)
  128. {
  129. callback.handleIncomingMidiMessage (input, m);
  130. numBytes -= used;
  131. d += used;
  132. }
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. *dest++ = *d++;
  139. ++pendingBytes;
  140. --numBytes;
  141. }
  142. }
  143. while (numBytes > 0);
  144. if (pendingBytes > 0)
  145. {
  146. if (totalMessage [pendingBytes - 1] == 0xf7)
  147. {
  148. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  149. pendingBytes = 0;
  150. }
  151. else
  152. {
  153. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  154. }
  155. }
  156. }
  157. MemoryBlock pendingData;
  158. double pendingDataTime = 0;
  159. int pendingBytes = 0;
  160. uint8 runningStatus = 0;
  161. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  162. };
  163. } // namespace juce