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.

240 lines
7.7KB

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