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.

185 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  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. @tags{Audio}
  24. */
  25. class MidiDataConcatenator
  26. {
  27. public:
  28. MidiDataConcatenator (int initialBufferSize)
  29. : pendingSysexData ((size_t) initialBufferSize)
  30. {
  31. }
  32. void reset()
  33. {
  34. currentMessageLen = 0;
  35. pendingSysexSize = 0;
  36. pendingSysexTime = 0;
  37. }
  38. template <typename UserDataType, typename CallbackType>
  39. void pushMidiData (const void* inputData, int numBytes, double time,
  40. UserDataType* input, CallbackType& callback)
  41. {
  42. auto d = static_cast<const uint8*> (inputData);
  43. while (numBytes > 0)
  44. {
  45. auto nextByte = *d;
  46. if (pendingSysexSize != 0 || nextByte == 0xf0)
  47. {
  48. processSysex (d, numBytes, time, input, callback);
  49. currentMessageLen = 0;
  50. continue;
  51. }
  52. ++d;
  53. --numBytes;
  54. if (nextByte >= 0xf8 && nextByte <= 0xfe) // realtime message
  55. {
  56. callback.handleIncomingMidiMessage (input, MidiMessage (nextByte, time));
  57. // These can be embedded in the middle of a normal message, so we won't
  58. // reset the currentMessageLen here.
  59. continue;
  60. }
  61. if (nextByte < 0x80)
  62. {
  63. if (currentMessageLen == 3)
  64. {
  65. currentMessageLen = 0; // message is too long - abandon it and start again with the next byte
  66. continue;
  67. }
  68. currentMessage[currentMessageLen++] = nextByte;
  69. }
  70. else
  71. {
  72. currentMessage[0] = nextByte;
  73. currentMessageLen = 1;
  74. }
  75. auto expectedLength = MidiMessage::getMessageLengthFromFirstByte (currentMessage[0]);
  76. if (expectedLength == currentMessageLen)
  77. {
  78. callback.handleIncomingMidiMessage (input, MidiMessage (currentMessage, expectedLength, time));
  79. currentMessageLen = 1; // reset, but leave the first byte to use as the running status byte
  80. }
  81. }
  82. }
  83. private:
  84. template <typename UserDataType, typename CallbackType>
  85. void processSysex (const uint8*& d, int& numBytes, double time,
  86. UserDataType* input, CallbackType& callback)
  87. {
  88. if (*d == 0xf0)
  89. {
  90. pendingSysexSize = 0;
  91. pendingSysexTime = time;
  92. }
  93. pendingSysexData.ensureSize ((size_t) (pendingSysexSize + numBytes), false);
  94. auto totalMessage = static_cast<uint8*> (pendingSysexData.getData());
  95. auto dest = totalMessage + pendingSysexSize;
  96. do
  97. {
  98. if (pendingSysexSize > 0 && *d >= 0x80)
  99. {
  100. if (*d == 0xf7)
  101. {
  102. *dest++ = *d++;
  103. ++pendingSysexSize;
  104. --numBytes;
  105. break;
  106. }
  107. if (*d >= 0xfa || *d == 0xf8)
  108. {
  109. callback.handleIncomingMidiMessage (input, MidiMessage (*d, time));
  110. ++d;
  111. --numBytes;
  112. }
  113. else
  114. {
  115. pendingSysexSize = 0;
  116. int used = 0;
  117. const MidiMessage m (d, numBytes, used, 0, time);
  118. if (used > 0)
  119. {
  120. callback.handleIncomingMidiMessage (input, m);
  121. numBytes -= used;
  122. d += used;
  123. }
  124. break;
  125. }
  126. }
  127. else
  128. {
  129. *dest++ = *d++;
  130. ++pendingSysexSize;
  131. --numBytes;
  132. }
  133. }
  134. while (numBytes > 0);
  135. if (pendingSysexSize > 0)
  136. {
  137. if (totalMessage [pendingSysexSize - 1] == 0xf7)
  138. {
  139. callback.handleIncomingMidiMessage (input, MidiMessage (totalMessage, pendingSysexSize, pendingSysexTime));
  140. pendingSysexSize = 0;
  141. }
  142. else
  143. {
  144. callback.handlePartialSysexMessage (input, totalMessage, pendingSysexSize, pendingSysexTime);
  145. }
  146. }
  147. }
  148. uint8 currentMessage[3];
  149. int currentMessageLen = 0;
  150. MemoryBlock pendingSysexData;
  151. double pendingSysexTime = 0;
  152. int pendingSysexSize = 0;
  153. JUCE_DECLARE_NON_COPYABLE (MidiDataConcatenator)
  154. };
  155. } // namespace juce