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.

198 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #pragma once
  24. //==============================================================================
  25. /**
  26. Helper class that takes chunks of incoming midi bytes, packages them into
  27. messages, and dispatches them to a midi callback.
  28. */
  29. class MidiDataConcatenator
  30. {
  31. public:
  32. //==============================================================================
  33. MidiDataConcatenator (const int initialBufferSize)
  34. : pendingData ((size_t) initialBufferSize),
  35. pendingDataTime (0), pendingBytes (0), runningStatus (0)
  36. {
  37. }
  38. void reset()
  39. {
  40. pendingBytes = 0;
  41. runningStatus = 0;
  42. pendingDataTime = 0;
  43. }
  44. template <typename UserDataType, typename CallbackType>
  45. void pushMidiData (const void* inputData, int numBytes, double time,
  46. UserDataType* input, CallbackType& callback)
  47. {
  48. const uint8* d = static_cast<const uint8*> (inputData);
  49. while (numBytes > 0)
  50. {
  51. if (pendingBytes > 0 || d[0] == 0xf0)
  52. {
  53. processSysex (d, numBytes, time, input, callback);
  54. runningStatus = 0;
  55. }
  56. else
  57. {
  58. int len = 0;
  59. uint8 data[3];
  60. while (numBytes > 0)
  61. {
  62. // If there's a realtime message embedded in the middle of
  63. // the normal message, handle it now..
  64. if (*d >= 0xf8 && *d <= 0xfe)
  65. {
  66. callback.handleIncomingMidiMessage (input, MidiMessage (*d++, time));
  67. --numBytes;
  68. }
  69. else
  70. {
  71. if (len == 0 && *d < 0x80 && runningStatus >= 0x80)
  72. data[len++] = runningStatus;
  73. data[len++] = *d++;
  74. --numBytes;
  75. const uint8 firstByte = data[0];
  76. if (firstByte < 0x80 || firstByte == 0xf7)
  77. {
  78. len = 0;
  79. break; // ignore this malformed MIDI message..
  80. }
  81. if (len >= MidiMessage::getMessageLengthFromFirstByte (firstByte))
  82. break;
  83. }
  84. }
  85. if (len > 0)
  86. {
  87. int used = 0;
  88. const MidiMessage m (data, len, used, 0, time);
  89. if (used <= 0)
  90. break; // malformed message..
  91. jassert (used == len);
  92. callback.handleIncomingMidiMessage (input, m);
  93. runningStatus = data[0];
  94. }
  95. }
  96. }
  97. }
  98. private:
  99. template <typename UserDataType, typename CallbackType>
  100. void processSysex (const uint8*& d, int& numBytes, double time,
  101. UserDataType* input, CallbackType& callback)
  102. {
  103. if (*d == 0xf0)
  104. {
  105. pendingBytes = 0;
  106. pendingDataTime = time;
  107. }
  108. pendingData.ensureSize ((size_t) (pendingBytes + numBytes), false);
  109. uint8* totalMessage = static_cast<uint8*> (pendingData.getData());
  110. uint8* dest = totalMessage + pendingBytes;
  111. do
  112. {
  113. if (pendingBytes > 0 && *d >= 0x80)
  114. {
  115. if (*d == 0xf7)
  116. {
  117. *dest++ = *d++;
  118. ++pendingBytes;
  119. --numBytes;
  120. break;
  121. }
  122. if (*d >= 0xfa || *d == 0xf8)
  123. {
  124. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  125. ++d;
  126. --numBytes;
  127. }
  128. else
  129. {
  130. pendingBytes = 0;
  131. int used = 0;
  132. const MidiMessage m (d, numBytes, used, 0, time);
  133. if (used > 0)
  134. {
  135. callback.handleIncomingMidiMessage (input, m);
  136. numBytes -= used;
  137. d += used;
  138. }
  139. break;
  140. }
  141. }
  142. else
  143. {
  144. *dest++ = *d++;
  145. ++pendingBytes;
  146. --numBytes;
  147. }
  148. }
  149. while (numBytes > 0);
  150. if (pendingBytes > 0)
  151. {
  152. if (totalMessage [pendingBytes - 1] == 0xf7)
  153. {
  154. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  155. pendingBytes = 0;
  156. }
  157. else
  158. {
  159. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  160. }
  161. }
  162. }
  163. MemoryBlock pendingData;
  164. double pendingDataTime;
  165. int pendingBytes;
  166. uint8 runningStatus;
  167. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  168. };