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.

195 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MIDIDATACONCATENATOR_H_INCLUDED
  18. #define JUCE_MIDIDATACONCATENATOR_H_INCLUDED
  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 (const int initialBufferSize)
  29. : pendingData ((size_t) initialBufferSize),
  30. pendingDataTime (0), pendingBytes (0), runningStatus (0)
  31. {
  32. }
  33. void reset()
  34. {
  35. pendingBytes = 0;
  36. runningStatus = 0;
  37. pendingDataTime = 0;
  38. }
  39. template <typename UserDataType, typename CallbackType>
  40. void pushMidiData (const void* inputData, int numBytes, double time,
  41. UserDataType* input, CallbackType& callback)
  42. {
  43. const uint8* d = static_cast<const uint8*> (inputData);
  44. while (numBytes > 0)
  45. {
  46. if (pendingBytes > 0 || d[0] == 0xf0)
  47. {
  48. processSysex (d, numBytes, time, input, callback);
  49. runningStatus = 0;
  50. }
  51. else
  52. {
  53. int len = 0;
  54. uint8 data[3];
  55. while (numBytes > 0)
  56. {
  57. // If there's a realtime message embedded in the middle of
  58. // the normal message, handle it now..
  59. if (*d >= 0xf8 && *d <= 0xfe)
  60. {
  61. callback.handleIncomingMidiMessage (input, MidiMessage (*d++, time));
  62. --numBytes;
  63. }
  64. else
  65. {
  66. if (len == 0 && *d < 0x80 && runningStatus >= 0x80)
  67. data[len++] = runningStatus;
  68. data[len++] = *d++;
  69. --numBytes;
  70. const uint8 firstByte = data[0];
  71. if (firstByte < 0x80 || firstByte == 0xf7)
  72. {
  73. len = 0;
  74. break; // ignore this malformed MIDI message..
  75. }
  76. if (len >= MidiMessage::getMessageLengthFromFirstByte (firstByte))
  77. break;
  78. }
  79. }
  80. if (len > 0)
  81. {
  82. int used = 0;
  83. const MidiMessage m (data, len, used, 0, time);
  84. if (used <= 0)
  85. break; // malformed message..
  86. jassert (used == len);
  87. callback.handleIncomingMidiMessage (input, m);
  88. runningStatus = data[0];
  89. }
  90. }
  91. }
  92. }
  93. private:
  94. template <typename UserDataType, typename CallbackType>
  95. void processSysex (const uint8*& d, int& numBytes, double time,
  96. UserDataType* input, CallbackType& callback)
  97. {
  98. if (*d == 0xf0)
  99. {
  100. pendingBytes = 0;
  101. pendingDataTime = time;
  102. }
  103. pendingData.ensureSize ((size_t) (pendingBytes + numBytes), false);
  104. uint8* totalMessage = static_cast<uint8*> (pendingData.getData());
  105. uint8* dest = totalMessage + pendingBytes;
  106. do
  107. {
  108. if (pendingBytes > 0 && *d >= 0x80)
  109. {
  110. if (*d == 0xf7)
  111. {
  112. *dest++ = *d++;
  113. ++pendingBytes;
  114. --numBytes;
  115. break;
  116. }
  117. if (*d >= 0xfa || *d == 0xf8)
  118. {
  119. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  120. ++d;
  121. --numBytes;
  122. }
  123. else
  124. {
  125. pendingBytes = 0;
  126. int used = 0;
  127. const MidiMessage m (d, numBytes, used, 0, time);
  128. if (used > 0)
  129. {
  130. callback.handleIncomingMidiMessage (input, m);
  131. numBytes -= used;
  132. d += used;
  133. }
  134. break;
  135. }
  136. }
  137. else
  138. {
  139. *dest++ = *d++;
  140. ++pendingBytes;
  141. --numBytes;
  142. }
  143. }
  144. while (numBytes > 0);
  145. if (pendingBytes > 0)
  146. {
  147. if (totalMessage [pendingBytes - 1] == 0xf7)
  148. {
  149. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  150. pendingBytes = 0;
  151. }
  152. else
  153. {
  154. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  155. }
  156. }
  157. }
  158. MemoryBlock pendingData;
  159. double pendingDataTime;
  160. int pendingBytes;
  161. uint8 runningStatus;
  162. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  163. };
  164. #endif // JUCE_MIDIDATACONCATENATOR_H_INCLUDED