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.

175 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MIDIDATACONCATENATOR_JUCEHEADER__
  19. #define __JUCE_MIDIDATACONCATENATOR_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. Helper class that takes chunks of incoming midi bytes, packages them into
  23. messages, and dispatches them to a midi callback.
  24. */
  25. class MidiDataConcatenator
  26. {
  27. public:
  28. //==============================================================================
  29. MidiDataConcatenator (const int initialBufferSize)
  30. : pendingData ((size_t) initialBufferSize),
  31. pendingDataTime (0), pendingBytes (0), runningStatus (0)
  32. {
  33. }
  34. void reset()
  35. {
  36. pendingBytes = 0;
  37. runningStatus = 0;
  38. pendingDataTime = 0;
  39. }
  40. void pushMidiData (const void* data, int numBytes, double time,
  41. MidiInput* input, MidiInputCallback& callback)
  42. {
  43. const uint8* d = static_cast <const uint8*> (data);
  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. void processSysex (const uint8*& d, int& numBytes, double time,
  90. MidiInput* input, MidiInputCallback& callback)
  91. {
  92. if (*d == 0xf0)
  93. {
  94. pendingBytes = 0;
  95. pendingDataTime = time;
  96. }
  97. pendingData.ensureSize ((size_t) (pendingBytes + numBytes), false);
  98. uint8* totalMessage = static_cast<uint8*> (pendingData.getData());
  99. uint8* dest = totalMessage + pendingBytes;
  100. do
  101. {
  102. if (pendingBytes > 0 && *d >= 0x80)
  103. {
  104. if (*d >= 0xfa || *d == 0xf8)
  105. {
  106. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  107. ++d;
  108. --numBytes;
  109. }
  110. else
  111. {
  112. if (*d == 0xf7)
  113. {
  114. *dest++ = *d++;
  115. pendingBytes++;
  116. --numBytes;
  117. }
  118. break;
  119. }
  120. }
  121. else
  122. {
  123. *dest++ = *d++;
  124. pendingBytes++;
  125. --numBytes;
  126. }
  127. }
  128. while (numBytes > 0);
  129. if (pendingBytes > 0)
  130. {
  131. if (totalMessage [pendingBytes - 1] == 0xf7)
  132. {
  133. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingBytes, pendingDataTime));
  134. pendingBytes = 0;
  135. }
  136. else
  137. {
  138. callback.handlePartialSysexMessage (input, totalMessage, pendingBytes, pendingDataTime);
  139. }
  140. }
  141. }
  142. MemoryBlock pendingData;
  143. double pendingDataTime;
  144. int pendingBytes;
  145. uint8 runningStatus;
  146. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator);
  147. };
  148. #endif // __JUCE_MIDIDATACONCATENATOR_JUCEHEADER__