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.

201 lines
6.7KB

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