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.

321 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. const auto var = MidiMessage::readVariableLengthValue (data + 1, maxBytes - 1);
  49. return jmin (maxBytes, var.value + 2 + var.bytesUsed);
  50. }
  51. if (byte >= 0x80)
  52. return jmin (maxBytes, MidiMessage::getMessageLengthFromFirstByte ((uint8) byte));
  53. return 0;
  54. }
  55. static uint8* findEventAfter (uint8* d, uint8* endData, int samplePosition) noexcept
  56. {
  57. while (d < endData && getEventTime (d) <= samplePosition)
  58. d += getEventTotalSize (d);
  59. return d;
  60. }
  61. }
  62. //==============================================================================
  63. MidiBufferIterator& MidiBufferIterator::operator++() noexcept
  64. {
  65. data += sizeof (int32) + sizeof (uint16) + size_t (MidiBufferHelpers::getEventDataSize (data));
  66. return *this;
  67. }
  68. MidiBufferIterator MidiBufferIterator::operator++ (int) noexcept
  69. {
  70. auto copy = *this;
  71. ++(*this);
  72. return copy;
  73. }
  74. MidiBufferIterator::reference MidiBufferIterator::operator*() const noexcept
  75. {
  76. return { data + sizeof (int32) + sizeof (uint16),
  77. MidiBufferHelpers::getEventDataSize (data),
  78. MidiBufferHelpers::getEventTime (data) };
  79. }
  80. //==============================================================================
  81. MidiBuffer::MidiBuffer (const MidiMessage& message) noexcept
  82. {
  83. addEvent (message, 0);
  84. }
  85. void MidiBuffer::swapWith (MidiBuffer& other) noexcept { data.swapWith (other.data); }
  86. void MidiBuffer::clear() noexcept { data.clearQuick(); }
  87. void MidiBuffer::ensureSize (size_t minimumNumBytes) { data.ensureStorageAllocated ((int) minimumNumBytes); }
  88. bool MidiBuffer::isEmpty() const noexcept { return data.size() == 0; }
  89. void MidiBuffer::clear (int startSample, int numSamples)
  90. {
  91. auto start = MidiBufferHelpers::findEventAfter (data.begin(), data.end(), startSample - 1);
  92. auto end = MidiBufferHelpers::findEventAfter (start, data.end(), startSample + numSamples - 1);
  93. data.removeRange ((int) (start - data.begin()), (int) (end - start));
  94. }
  95. bool MidiBuffer::addEvent (const MidiMessage& m, int sampleNumber)
  96. {
  97. return addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
  98. }
  99. bool MidiBuffer::addEvent (const void* newData, int maxBytes, int sampleNumber)
  100. {
  101. auto numBytes = MidiBufferHelpers::findActualEventLength (static_cast<const uint8*> (newData), maxBytes);
  102. if (numBytes <= 0)
  103. return true;
  104. if (std::numeric_limits<uint16>::max() < numBytes)
  105. {
  106. // This method only supports messages smaller than (1 << 16) bytes
  107. return false;
  108. }
  109. auto newItemSize = (size_t) numBytes + sizeof (int32) + sizeof (uint16);
  110. auto offset = (int) (MidiBufferHelpers::findEventAfter (data.begin(), data.end(), sampleNumber) - data.begin());
  111. data.insertMultiple (offset, 0, (int) newItemSize);
  112. auto* d = data.begin() + offset;
  113. writeUnaligned<int32> (d, sampleNumber);
  114. d += sizeof (int32);
  115. writeUnaligned<uint16> (d, static_cast<uint16> (numBytes));
  116. d += sizeof (uint16);
  117. memcpy (d, newData, (size_t) numBytes);
  118. return true;
  119. }
  120. void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
  121. int startSample, int numSamples, int sampleDeltaToAdd)
  122. {
  123. for (auto i = otherBuffer.findNextSamplePosition (startSample); i != otherBuffer.cend(); ++i)
  124. {
  125. const auto metadata = *i;
  126. if (metadata.samplePosition >= startSample + numSamples && numSamples >= 0)
  127. break;
  128. addEvent (metadata.data, metadata.numBytes, metadata.samplePosition + sampleDeltaToAdd);
  129. }
  130. }
  131. int MidiBuffer::getNumEvents() const noexcept
  132. {
  133. int n = 0;
  134. auto end = data.end();
  135. for (auto d = data.begin(); d < end; ++n)
  136. d += MidiBufferHelpers::getEventTotalSize (d);
  137. return n;
  138. }
  139. int MidiBuffer::getFirstEventTime() const noexcept
  140. {
  141. return data.size() > 0 ? MidiBufferHelpers::getEventTime (data.begin()) : 0;
  142. }
  143. int MidiBuffer::getLastEventTime() const noexcept
  144. {
  145. if (data.size() == 0)
  146. return 0;
  147. auto endData = data.end();
  148. for (auto d = data.begin();;)
  149. {
  150. auto nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
  151. if (nextOne >= endData)
  152. return MidiBufferHelpers::getEventTime (d);
  153. d = nextOne;
  154. }
  155. }
  156. MidiBufferIterator MidiBuffer::findNextSamplePosition (int samplePosition) const noexcept
  157. {
  158. return std::find_if (cbegin(), cend(), [&] (const MidiMessageMetadata& metadata) noexcept
  159. {
  160. return metadata.samplePosition >= samplePosition;
  161. });
  162. }
  163. //==============================================================================
  164. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
  165. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (4996)
  166. MidiBuffer::Iterator::Iterator (const MidiBuffer& b) noexcept
  167. : buffer (b), iterator (b.data.begin())
  168. {
  169. }
  170. void MidiBuffer::Iterator::setNextSamplePosition (int samplePosition) noexcept
  171. {
  172. iterator = buffer.findNextSamplePosition (samplePosition);
  173. }
  174. bool MidiBuffer::Iterator::getNextEvent (const uint8*& midiData, int& numBytes, int& samplePosition) noexcept
  175. {
  176. if (iterator == buffer.cend())
  177. return false;
  178. const auto metadata = *iterator++;
  179. midiData = metadata.data;
  180. numBytes = metadata.numBytes;
  181. samplePosition = metadata.samplePosition;
  182. return true;
  183. }
  184. bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) noexcept
  185. {
  186. if (iterator == buffer.cend())
  187. return false;
  188. const auto metadata = *iterator++;
  189. result = metadata.getMessage();
  190. samplePosition = metadata.samplePosition;
  191. return true;
  192. }
  193. JUCE_END_IGNORE_WARNINGS_MSVC
  194. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  195. //==============================================================================
  196. //==============================================================================
  197. #if JUCE_UNIT_TESTS
  198. struct MidiBufferTest : public UnitTest
  199. {
  200. MidiBufferTest()
  201. : UnitTest ("MidiBuffer", UnitTestCategories::midi)
  202. {}
  203. void runTest() override
  204. {
  205. beginTest ("Clear messages");
  206. {
  207. const auto message = MidiMessage::noteOn (1, 64, 0.5f);
  208. const auto testBuffer = [&]
  209. {
  210. MidiBuffer buffer;
  211. buffer.addEvent (message, 0);
  212. buffer.addEvent (message, 10);
  213. buffer.addEvent (message, 20);
  214. buffer.addEvent (message, 30);
  215. return buffer;
  216. }();
  217. {
  218. auto buffer = testBuffer;
  219. buffer.clear (10, 0);
  220. expectEquals (buffer.getNumEvents(), 4);
  221. }
  222. {
  223. auto buffer = testBuffer;
  224. buffer.clear (10, 1);
  225. expectEquals (buffer.getNumEvents(), 3);
  226. }
  227. {
  228. auto buffer = testBuffer;
  229. buffer.clear (10, 10);
  230. expectEquals (buffer.getNumEvents(), 3);
  231. }
  232. {
  233. auto buffer = testBuffer;
  234. buffer.clear (10, 20);
  235. expectEquals (buffer.getNumEvents(), 2);
  236. }
  237. {
  238. auto buffer = testBuffer;
  239. buffer.clear (10, 30);
  240. expectEquals (buffer.getNumEvents(), 1);
  241. }
  242. {
  243. auto buffer = testBuffer;
  244. buffer.clear (10, 300);
  245. expectEquals (buffer.getNumEvents(), 1);
  246. }
  247. }
  248. }
  249. };
  250. static MidiBufferTest midiBufferTest;
  251. #endif
  252. } // namespace juce