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_MidiBuffer.cpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace MidiBufferHelpers
  20. {
  21. inline int getEventTime (const void* const d) noexcept
  22. {
  23. return readUnaligned<int32> (d);
  24. }
  25. inline uint16 getEventDataSize (const void* const d) noexcept
  26. {
  27. return readUnaligned<uint16> (static_cast<const char*> (d) + sizeof (int32));
  28. }
  29. inline uint16 getEventTotalSize (const void* const d) noexcept
  30. {
  31. return (uint16) (getEventDataSize (d) + sizeof (int32) + sizeof (uint16));
  32. }
  33. static int findActualEventLength (const uint8* const data, const int maxBytes) noexcept
  34. {
  35. unsigned int byte = (unsigned int) *data;
  36. int size = 0;
  37. if (byte == 0xf0 || byte == 0xf7)
  38. {
  39. const uint8* d = data + 1;
  40. while (d < data + maxBytes)
  41. if (*d++ == 0xf7)
  42. break;
  43. size = (int) (d - data);
  44. }
  45. else if (byte == 0xff)
  46. {
  47. int n;
  48. const int bytesLeft = MidiMessage::readVariableLengthVal (data + 1, n);
  49. size = jmin (maxBytes, n + 2 + bytesLeft);
  50. }
  51. else if (byte >= 0x80)
  52. {
  53. size = jmin (maxBytes, MidiMessage::getMessageLengthFromFirstByte ((uint8) byte));
  54. }
  55. return size;
  56. }
  57. static uint8* findEventAfter (uint8* d, uint8* endData, const int samplePosition) noexcept
  58. {
  59. while (d < endData && getEventTime (d) <= samplePosition)
  60. d += getEventTotalSize (d);
  61. return d;
  62. }
  63. }
  64. //==============================================================================
  65. MidiBuffer::MidiBuffer() noexcept {}
  66. MidiBuffer::~MidiBuffer() {}
  67. MidiBuffer::MidiBuffer (const MidiBuffer& other) noexcept : data (other.data) {}
  68. MidiBuffer& MidiBuffer::operator= (const MidiBuffer& other) noexcept
  69. {
  70. data = other.data;
  71. return *this;
  72. }
  73. MidiBuffer::MidiBuffer (const MidiMessage& message) noexcept
  74. {
  75. addEvent (message, 0);
  76. }
  77. void MidiBuffer::swapWith (MidiBuffer& other) noexcept { data.swapWith (other.data); }
  78. void MidiBuffer::clear() noexcept { data.clearQuick(); }
  79. void MidiBuffer::ensureSize (size_t minimumNumBytes) { data.ensureStorageAllocated ((int) minimumNumBytes); }
  80. bool MidiBuffer::isEmpty() const noexcept { return data.size() == 0; }
  81. void MidiBuffer::clear (const int startSample, const int numSamples)
  82. {
  83. uint8* const start = MidiBufferHelpers::findEventAfter (data.begin(), data.end(), startSample - 1);
  84. uint8* const end = MidiBufferHelpers::findEventAfter (start, data.end(), startSample + numSamples - 1);
  85. data.removeRange ((int) (start - data.begin()), (int) (end - data.begin()));
  86. }
  87. void MidiBuffer::addEvent (const MidiMessage& m, const int sampleNumber)
  88. {
  89. addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
  90. }
  91. void MidiBuffer::addEvent (const void* const newData, const int maxBytes, const int sampleNumber)
  92. {
  93. const int numBytes = MidiBufferHelpers::findActualEventLength (static_cast<const uint8*> (newData), maxBytes);
  94. if (numBytes > 0)
  95. {
  96. const size_t newItemSize = (size_t) numBytes + sizeof (int32) + sizeof (uint16);
  97. const int offset = (int) (MidiBufferHelpers::findEventAfter (data.begin(), data.end(), sampleNumber) - data.begin());
  98. data.insertMultiple (offset, 0, (int) newItemSize);
  99. uint8* const d = data.begin() + offset;
  100. writeUnaligned<int32> (d, sampleNumber);
  101. writeUnaligned<uint16> (d + 4, static_cast<uint16> (numBytes));
  102. memcpy (d + 6, newData, (size_t) numBytes);
  103. }
  104. }
  105. void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
  106. const int startSample,
  107. const int numSamples,
  108. const int sampleDeltaToAdd)
  109. {
  110. Iterator i (otherBuffer);
  111. i.setNextSamplePosition (startSample);
  112. const uint8* eventData;
  113. int eventSize, position;
  114. while (i.getNextEvent (eventData, eventSize, position)
  115. && (position < startSample + numSamples || numSamples < 0))
  116. {
  117. addEvent (eventData, eventSize, position + sampleDeltaToAdd);
  118. }
  119. }
  120. int MidiBuffer::getNumEvents() const noexcept
  121. {
  122. int n = 0;
  123. const uint8* const end = data.end();
  124. for (const uint8* d = data.begin(); d < end; ++n)
  125. d += MidiBufferHelpers::getEventTotalSize (d);
  126. return n;
  127. }
  128. int MidiBuffer::getFirstEventTime() const noexcept
  129. {
  130. return data.size() > 0 ? MidiBufferHelpers::getEventTime (data.begin()) : 0;
  131. }
  132. int MidiBuffer::getLastEventTime() const noexcept
  133. {
  134. if (data.size() == 0)
  135. return 0;
  136. const uint8* const endData = data.end();
  137. for (const uint8* d = data.begin();;)
  138. {
  139. const uint8* const nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
  140. if (nextOne >= endData)
  141. return MidiBufferHelpers::getEventTime (d);
  142. d = nextOne;
  143. }
  144. }
  145. //==============================================================================
  146. MidiBuffer::Iterator::Iterator (const MidiBuffer& b) noexcept
  147. : buffer (b), data (b.data.begin())
  148. {
  149. }
  150. MidiBuffer::Iterator::~Iterator() noexcept
  151. {
  152. }
  153. void MidiBuffer::Iterator::setNextSamplePosition (const int samplePosition) noexcept
  154. {
  155. data = buffer.data.begin();
  156. const uint8* const dataEnd = buffer.data.end();
  157. while (data < dataEnd && MidiBufferHelpers::getEventTime (data) < samplePosition)
  158. data += MidiBufferHelpers::getEventTotalSize (data);
  159. }
  160. bool MidiBuffer::Iterator::getNextEvent (const uint8* &midiData, int& numBytes, int& samplePosition) noexcept
  161. {
  162. if (data >= buffer.data.end())
  163. return false;
  164. samplePosition = MidiBufferHelpers::getEventTime (data);
  165. const int itemSize = MidiBufferHelpers::getEventDataSize (data);
  166. numBytes = itemSize;
  167. midiData = data + sizeof (int32) + sizeof (uint16);
  168. data += sizeof (int32) + sizeof (uint16) + (size_t) itemSize;
  169. return true;
  170. }
  171. bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) noexcept
  172. {
  173. if (data >= buffer.data.end())
  174. return false;
  175. samplePosition = MidiBufferHelpers::getEventTime (data);
  176. const int itemSize = MidiBufferHelpers::getEventDataSize (data);
  177. result = MidiMessage (data + sizeof (int32) + sizeof (uint16), itemSize, samplePosition);
  178. data += sizeof (int32) + sizeof (uint16) + (size_t) itemSize;
  179. return true;
  180. }
  181. } // namespace juce