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.

241 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. 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. MidiBufferIterator& MidiBufferIterator::operator++() noexcept
  65. {
  66. data += sizeof (int32) + sizeof (uint16) + size_t (MidiBufferHelpers::getEventDataSize (data));
  67. return *this;
  68. }
  69. MidiBufferIterator MidiBufferIterator::operator++ (int) noexcept
  70. {
  71. auto copy = *this;
  72. ++(*this);
  73. return copy;
  74. }
  75. MidiBufferIterator::reference MidiBufferIterator::operator*() const noexcept
  76. {
  77. return { data + sizeof (int32) + sizeof (uint16),
  78. MidiBufferHelpers::getEventDataSize (data),
  79. MidiBufferHelpers::getEventTime (data) };
  80. }
  81. //==============================================================================
  82. MidiBuffer::MidiBuffer (const MidiMessage& message) noexcept
  83. {
  84. addEvent (message, 0);
  85. }
  86. void MidiBuffer::swapWith (MidiBuffer& other) noexcept { data.swapWith (other.data); }
  87. void MidiBuffer::clear() noexcept { data.clearQuick(); }
  88. void MidiBuffer::ensureSize (size_t minimumNumBytes) { data.ensureStorageAllocated ((int) minimumNumBytes); }
  89. bool MidiBuffer::isEmpty() const noexcept { return data.size() == 0; }
  90. void MidiBuffer::clear (int startSample, int numSamples)
  91. {
  92. auto start = MidiBufferHelpers::findEventAfter (data.begin(), data.end(), startSample - 1);
  93. auto end = MidiBufferHelpers::findEventAfter (start, data.end(), startSample + numSamples - 1);
  94. data.removeRange ((int) (start - data.begin()), (int) (end - data.begin()));
  95. }
  96. void MidiBuffer::addEvent (const MidiMessage& m, int sampleNumber)
  97. {
  98. addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
  99. }
  100. void MidiBuffer::addEvent (const void* newData, int maxBytes, int sampleNumber)
  101. {
  102. auto numBytes = MidiBufferHelpers::findActualEventLength (static_cast<const uint8*> (newData), maxBytes);
  103. if (numBytes > 0)
  104. {
  105. auto newItemSize = (size_t) numBytes + sizeof (int32) + sizeof (uint16);
  106. auto offset = (int) (MidiBufferHelpers::findEventAfter (data.begin(), data.end(), sampleNumber) - data.begin());
  107. data.insertMultiple (offset, 0, (int) newItemSize);
  108. auto* d = data.begin() + offset;
  109. writeUnaligned<int32> (d, sampleNumber);
  110. d += sizeof (int32);
  111. writeUnaligned<uint16> (d, static_cast<uint16> (numBytes));
  112. d += sizeof (uint16);
  113. memcpy (d, newData, (size_t) numBytes);
  114. }
  115. }
  116. void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
  117. int startSample, int numSamples, int sampleDeltaToAdd)
  118. {
  119. for (auto i = otherBuffer.findNextSamplePosition (startSample); i != otherBuffer.cend(); ++i)
  120. {
  121. const auto metadata = *i;
  122. if (metadata.samplePosition >= startSample + numSamples && numSamples >= 0)
  123. break;
  124. addEvent (metadata.data, metadata.numBytes, metadata.samplePosition + sampleDeltaToAdd);
  125. }
  126. }
  127. int MidiBuffer::getNumEvents() const noexcept
  128. {
  129. int n = 0;
  130. auto end = data.end();
  131. for (auto d = data.begin(); d < end; ++n)
  132. d += MidiBufferHelpers::getEventTotalSize (d);
  133. return n;
  134. }
  135. int MidiBuffer::getFirstEventTime() const noexcept
  136. {
  137. return data.size() > 0 ? MidiBufferHelpers::getEventTime (data.begin()) : 0;
  138. }
  139. int MidiBuffer::getLastEventTime() const noexcept
  140. {
  141. if (data.size() == 0)
  142. return 0;
  143. auto endData = data.end();
  144. for (auto d = data.begin();;)
  145. {
  146. auto nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
  147. if (nextOne >= endData)
  148. return MidiBufferHelpers::getEventTime (d);
  149. d = nextOne;
  150. }
  151. }
  152. MidiBufferIterator MidiBuffer::findNextSamplePosition (int samplePosition) const noexcept
  153. {
  154. return std::find_if (cbegin(), cend(), [&] (const MidiMessageMetadata& metadata) noexcept
  155. {
  156. return metadata.samplePosition >= samplePosition;
  157. });
  158. }
  159. //==============================================================================
  160. MidiBuffer::Iterator::Iterator (const MidiBuffer& b) noexcept
  161. : buffer (b), iterator (b.data.begin())
  162. {
  163. }
  164. MidiBuffer::Iterator::~Iterator() noexcept {}
  165. void MidiBuffer::Iterator::setNextSamplePosition (int samplePosition) noexcept
  166. {
  167. iterator = buffer.findNextSamplePosition (samplePosition);
  168. }
  169. bool MidiBuffer::Iterator::getNextEvent (const uint8*& midiData, int& numBytes, int& samplePosition) noexcept
  170. {
  171. if (iterator == buffer.cend())
  172. return false;
  173. const auto metadata = *iterator++;
  174. midiData = metadata.data;
  175. numBytes = metadata.numBytes;
  176. samplePosition = metadata.samplePosition;
  177. return true;
  178. }
  179. bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) noexcept
  180. {
  181. if (iterator == buffer.cend())
  182. return false;
  183. const auto metadata = *iterator++;
  184. result = metadata.getMessage();
  185. samplePosition = metadata.samplePosition;
  186. return true;
  187. }
  188. } // namespace juce