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.

MidiBuffer.cpp 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "MidiBuffer.h"
  21. #include "MidiMessage.h"
  22. namespace water {
  23. namespace MidiBufferHelpers
  24. {
  25. inline int getEventTime (const void* const d) noexcept
  26. {
  27. return readUnaligned<int32> (d);
  28. }
  29. inline uint16 getEventDataSize (const void* const d) noexcept
  30. {
  31. return readUnaligned<uint16> (static_cast<const char*> (d) + sizeof (int32));
  32. }
  33. inline uint16 getEventTotalSize (const void* const d) noexcept
  34. {
  35. return getEventDataSize (d) + sizeof (int32) + sizeof (uint16);
  36. }
  37. static int findActualEventLength (const uint8* const data, const int maxBytes) noexcept
  38. {
  39. unsigned int byte = (unsigned int) *data;
  40. int size = 0;
  41. if (byte == 0xf0 || byte == 0xf7)
  42. {
  43. const uint8* d = data + 1;
  44. while (d < data + maxBytes)
  45. if (*d++ == 0xf7)
  46. break;
  47. size = (int) (d - data);
  48. }
  49. else if (byte == 0xff)
  50. {
  51. int n;
  52. const int bytesLeft = MidiMessage::readVariableLengthVal (data + 1, n);
  53. size = jmin (maxBytes, n + 2 + bytesLeft);
  54. }
  55. else if (byte >= 0x80)
  56. {
  57. size = jmin (maxBytes, MidiMessage::getMessageLengthFromFirstByte ((uint8) byte));
  58. }
  59. return size;
  60. }
  61. static uint8* findEventAfter (uint8* d, uint8* endData, const int samplePosition) noexcept
  62. {
  63. while (d < endData && getEventTime (d) <= samplePosition)
  64. d += getEventTotalSize (d);
  65. return d;
  66. }
  67. }
  68. //==============================================================================
  69. MidiBuffer::MidiBuffer() noexcept {}
  70. MidiBuffer::~MidiBuffer() {}
  71. MidiBuffer::MidiBuffer (const MidiBuffer& other) noexcept : data (other.data) {}
  72. MidiBuffer& MidiBuffer::operator= (const MidiBuffer& other) noexcept
  73. {
  74. data = other.data;
  75. return *this;
  76. }
  77. MidiBuffer::MidiBuffer (const MidiMessage& message) noexcept
  78. {
  79. addEvent (message, 0);
  80. }
  81. void MidiBuffer::swapWith (MidiBuffer& other) noexcept { data.swapWith (other.data); }
  82. void MidiBuffer::clear() noexcept { data.clearQuick(); }
  83. void MidiBuffer::ensureSize (size_t minimumNumBytes) { data.ensureStorageAllocated ((int) minimumNumBytes); }
  84. bool MidiBuffer::isEmpty() const noexcept { return data.size() == 0; }
  85. void MidiBuffer::clear (const int startSample, const int numSamples)
  86. {
  87. uint8* const start = MidiBufferHelpers::findEventAfter (data.begin(), data.end(), startSample - 1);
  88. uint8* const end = MidiBufferHelpers::findEventAfter (start, data.end(), startSample + numSamples - 1);
  89. data.removeRange ((int) (start - data.begin()), (int) (end - data.begin()));
  90. }
  91. void MidiBuffer::addEvent (const MidiMessage& m, const int sampleNumber)
  92. {
  93. addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
  94. }
  95. void MidiBuffer::addEvent (const void* const newData, const int maxBytes, const int sampleNumber)
  96. {
  97. const int numBytes = MidiBufferHelpers::findActualEventLength (static_cast<const uint8*> (newData), maxBytes);
  98. if (numBytes > 0)
  99. {
  100. const size_t newItemSize = (size_t) numBytes + sizeof (int32) + sizeof (uint16);
  101. const int offset = (int) (MidiBufferHelpers::findEventAfter (data.begin(), data.end(), sampleNumber) - data.begin());
  102. data.insertMultiple (offset, 0, (int) newItemSize);
  103. uint8* const d = data.begin() + offset;
  104. writeUnaligned<int32> (d, sampleNumber);
  105. writeUnaligned<uint16> (d + 4, static_cast<uint16> (numBytes));
  106. memcpy (d + 6, newData, (size_t) numBytes);
  107. }
  108. }
  109. void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
  110. const int startSample,
  111. const int numSamples,
  112. const int sampleDeltaToAdd)
  113. {
  114. Iterator i (otherBuffer);
  115. i.setNextSamplePosition (startSample);
  116. const uint8* eventData;
  117. int eventSize, position;
  118. while (i.getNextEvent (eventData, eventSize, position)
  119. && (position < startSample + numSamples || numSamples < 0))
  120. {
  121. addEvent (eventData, eventSize, position + sampleDeltaToAdd);
  122. }
  123. }
  124. int MidiBuffer::getNumEvents() const noexcept
  125. {
  126. int n = 0;
  127. const uint8* const end = data.end();
  128. for (const uint8* d = data.begin(); d < end; ++n)
  129. d += MidiBufferHelpers::getEventTotalSize (d);
  130. return n;
  131. }
  132. int MidiBuffer::getFirstEventTime() const noexcept
  133. {
  134. return data.size() > 0 ? MidiBufferHelpers::getEventTime (data.begin()) : 0;
  135. }
  136. int MidiBuffer::getLastEventTime() const noexcept
  137. {
  138. if (data.size() == 0)
  139. return 0;
  140. const uint8* const endData = data.end();
  141. for (const uint8* d = data.begin();;)
  142. {
  143. const uint8* const nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
  144. if (nextOne >= endData)
  145. return MidiBufferHelpers::getEventTime (d);
  146. d = nextOne;
  147. }
  148. }
  149. //==============================================================================
  150. MidiBuffer::Iterator::Iterator (const MidiBuffer& b) noexcept
  151. : buffer (b), data (b.data.begin())
  152. {
  153. }
  154. MidiBuffer::Iterator::~Iterator() noexcept
  155. {
  156. }
  157. void MidiBuffer::Iterator::setNextSamplePosition (const int samplePosition) noexcept
  158. {
  159. data = buffer.data.begin();
  160. const uint8* const dataEnd = buffer.data.end();
  161. while (data < dataEnd && MidiBufferHelpers::getEventTime (data) < samplePosition)
  162. data += MidiBufferHelpers::getEventTotalSize (data);
  163. }
  164. bool MidiBuffer::Iterator::getNextEvent (const uint8* &midiData, int& numBytes, int& samplePosition) noexcept
  165. {
  166. if (data >= buffer.data.end())
  167. return false;
  168. samplePosition = MidiBufferHelpers::getEventTime (data);
  169. const int itemSize = MidiBufferHelpers::getEventDataSize (data);
  170. numBytes = itemSize;
  171. midiData = data + sizeof (int32) + sizeof (uint16);
  172. data += sizeof (int32) + sizeof (uint16) + (size_t) itemSize;
  173. return true;
  174. }
  175. bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) noexcept
  176. {
  177. if (data >= buffer.data.end())
  178. return false;
  179. samplePosition = MidiBufferHelpers::getEventTime (data);
  180. const int itemSize = MidiBufferHelpers::getEventDataSize (data);
  181. result = MidiMessage (data + sizeof (int32) + sizeof (uint16), itemSize, samplePosition);
  182. data += sizeof (int32) + sizeof (uint16) + (size_t) itemSize;
  183. return true;
  184. }
  185. }