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.

233 lines
7.1KB

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