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.

240 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. 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 - data.begin()));
  94. }
  95. void MidiBuffer::addEvent (const MidiMessage& m, int sampleNumber)
  96. {
  97. addEvent (m.getRawData(), m.getRawDataSize(), sampleNumber);
  98. }
  99. void 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. {
  104. auto newItemSize = (size_t) numBytes + sizeof (int32) + sizeof (uint16);
  105. auto offset = (int) (MidiBufferHelpers::findEventAfter (data.begin(), data.end(), sampleNumber) - data.begin());
  106. data.insertMultiple (offset, 0, (int) newItemSize);
  107. auto* d = data.begin() + offset;
  108. writeUnaligned<int32> (d, sampleNumber);
  109. d += sizeof (int32);
  110. writeUnaligned<uint16> (d, static_cast<uint16> (numBytes));
  111. d += sizeof (uint16);
  112. memcpy (d, newData, (size_t) numBytes);
  113. }
  114. }
  115. void MidiBuffer::addEvents (const MidiBuffer& otherBuffer,
  116. int startSample, int numSamples, int sampleDeltaToAdd)
  117. {
  118. for (auto i = otherBuffer.findNextSamplePosition (startSample); i != otherBuffer.cend(); ++i)
  119. {
  120. const auto metadata = *i;
  121. if (metadata.samplePosition >= startSample + numSamples && numSamples >= 0)
  122. break;
  123. addEvent (metadata.data, metadata.numBytes, metadata.samplePosition + sampleDeltaToAdd);
  124. }
  125. }
  126. int MidiBuffer::getNumEvents() const noexcept
  127. {
  128. int n = 0;
  129. auto end = data.end();
  130. for (auto d = data.begin(); d < end; ++n)
  131. d += MidiBufferHelpers::getEventTotalSize (d);
  132. return n;
  133. }
  134. int MidiBuffer::getFirstEventTime() const noexcept
  135. {
  136. return data.size() > 0 ? MidiBufferHelpers::getEventTime (data.begin()) : 0;
  137. }
  138. int MidiBuffer::getLastEventTime() const noexcept
  139. {
  140. if (data.size() == 0)
  141. return 0;
  142. auto endData = data.end();
  143. for (auto d = data.begin();;)
  144. {
  145. auto nextOne = d + MidiBufferHelpers::getEventTotalSize (d);
  146. if (nextOne >= endData)
  147. return MidiBufferHelpers::getEventTime (d);
  148. d = nextOne;
  149. }
  150. }
  151. MidiBufferIterator MidiBuffer::findNextSamplePosition (int samplePosition) const noexcept
  152. {
  153. return std::find_if (cbegin(), cend(), [&] (const MidiMessageMetadata& metadata) noexcept
  154. {
  155. return metadata.samplePosition >= samplePosition;
  156. });
  157. }
  158. //==============================================================================
  159. MidiBuffer::Iterator::Iterator (const MidiBuffer& b) noexcept
  160. : buffer (b), iterator (b.data.begin())
  161. {
  162. }
  163. MidiBuffer::Iterator::~Iterator() noexcept {}
  164. void MidiBuffer::Iterator::setNextSamplePosition (int samplePosition) noexcept
  165. {
  166. iterator = buffer.findNextSamplePosition (samplePosition);
  167. }
  168. bool MidiBuffer::Iterator::getNextEvent (const uint8*& midiData, int& numBytes, int& samplePosition) noexcept
  169. {
  170. if (iterator == buffer.cend())
  171. return false;
  172. const auto metadata = *iterator++;
  173. midiData = metadata.data;
  174. numBytes = metadata.numBytes;
  175. samplePosition = metadata.samplePosition;
  176. return true;
  177. }
  178. bool MidiBuffer::Iterator::getNextEvent (MidiMessage& result, int& samplePosition) noexcept
  179. {
  180. if (iterator == buffer.cend())
  181. return false;
  182. const auto metadata = *iterator++;
  183. result = metadata.getMessage();
  184. samplePosition = metadata.samplePosition;
  185. return true;
  186. }
  187. } // namespace juce