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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 >= 0xfa || *d == 0xf8)
  106. {
  107. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  108. ++d;
  109. --numBytes;
  110. }
  111. else
  112. {
  113. if (*d == 0xf7)
  114. {
  115. *dest++ = *d++;
  116. pendingBytes++;
  117. --numBytes;
  118. }
  119. break;
  120. }
  121. }
  122. else
  123. {
  124. *dest++ = *d++;
  125. pendingBytes++;
  126. --numBytes;
  127. }
  128. }
  129. while (numBytes > 0);
  130. if (pendingBytes > 0)
  131. {
  132. if (totalMessage [pendingBytes - 1] == 0xf7)
  133. {
  134. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  135. pendingBytes = 0;
  136. }
  137. else
  138. {
  139. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  140. }
  141. }
  142. }
  143. MemoryBlock pendingData;
  144. double pendingDataTime;
  145. int pendingBytes;
  146. uint8 runningStatus;
  147. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  148. };
  149. #endif // JUCE_MIDIDATACONCATENATOR_H_INCLUDED