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.

188 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. const MidiMessage m (*d++, time);
  62. callback.handleIncomingMidiMessage (input, m);
  63. --numBytes;
  64. }
  65. else
  66. {
  67. if (len == 0 && *d < 0x80 && runningStatus >= 0x80)
  68. data[len++] = runningStatus;
  69. data[len++] = *d++;
  70. --numBytes;
  71. if (len >= MidiMessage::getMessageLengthFromFirstByte (data[0]))
  72. break;
  73. }
  74. }
  75. if (len > 0)
  76. {
  77. int used = 0;
  78. const MidiMessage m (data, len, used, 0, time);
  79. if (used <= 0)
  80. break; // malformed message..
  81. jassert (used == len);
  82. callback.handleIncomingMidiMessage (input, m);
  83. runningStatus = data[0];
  84. }
  85. }
  86. }
  87. }
  88. private:
  89. template <typename UserDataType, typename CallbackType>
  90. void processSysex (const uint8*& d, int& numBytes, double time,
  91. UserDataType* input, CallbackType& callback)
  92. {
  93. if (*d == 0xf0)
  94. {
  95. pendingBytes = 0;
  96. pendingDataTime = time;
  97. }
  98. pendingData.ensureSize ((size_t) (pendingBytes + numBytes), false);
  99. uint8* totalMessage = static_cast<uint8*> (pendingData.getData());
  100. uint8* dest = totalMessage + pendingBytes;
  101. do
  102. {
  103. if (pendingBytes > 0 && *d >= 0x80)
  104. {
  105. if (*d == 0xf7)
  106. {
  107. *dest++ = *d++;
  108. ++pendingBytes;
  109. --numBytes;
  110. break;
  111. }
  112. if (*d >= 0xfa || *d == 0xf8)
  113. {
  114. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  115. ++d;
  116. --numBytes;
  117. }
  118. else
  119. {
  120. pendingBytes = 0;
  121. int used = 0;
  122. const MidiMessage m (d, numBytes, used, 0, time);
  123. if (used > 0)
  124. {
  125. callback.handleIncomingMidiMessage (input, m);
  126. numBytes -= used;
  127. d += used;
  128. }
  129. break;
  130. }
  131. }
  132. else
  133. {
  134. *dest++ = *d++;
  135. ++pendingBytes;
  136. --numBytes;
  137. }
  138. }
  139. while (numBytes > 0);
  140. if (pendingBytes > 0)
  141. {
  142. if (totalMessage [pendingBytes - 1] == 0xf7)
  143. {
  144. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  145. pendingBytes = 0;
  146. }
  147. else
  148. {
  149. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  150. }
  151. }
  152. }
  153. MemoryBlock pendingData;
  154. double pendingDataTime;
  155. int pendingBytes;
  156. uint8 runningStatus;
  157. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  158. };
  159. #endif // JUCE_MIDIDATACONCATENATOR_H_INCLUDED