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.

331 lines
10.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MidiMessageSequence::MidiMessageSequence()
  18. {
  19. }
  20. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  21. {
  22. list.addCopiesOf (other.list);
  23. }
  24. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  25. {
  26. MidiMessageSequence otherCopy (other);
  27. swapWith (otherCopy);
  28. return *this;
  29. }
  30. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  31. {
  32. list.swapWith (other.list);
  33. }
  34. MidiMessageSequence::~MidiMessageSequence()
  35. {
  36. }
  37. void MidiMessageSequence::clear()
  38. {
  39. list.clear();
  40. }
  41. int MidiMessageSequence::getNumEvents() const
  42. {
  43. return list.size();
  44. }
  45. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const
  46. {
  47. return list [index];
  48. }
  49. double MidiMessageSequence::getTimeOfMatchingKeyUp (const int index) const
  50. {
  51. if (const MidiEventHolder* const meh = list [index])
  52. if (meh->noteOffObject != nullptr)
  53. return meh->noteOffObject->message.getTimeStamp();
  54. return 0.0;
  55. }
  56. int MidiMessageSequence::getIndexOfMatchingKeyUp (const int index) const
  57. {
  58. if (const MidiEventHolder* const meh = list [index])
  59. return list.indexOf (meh->noteOffObject);
  60. return -1;
  61. }
  62. int MidiMessageSequence::getIndexOf (MidiEventHolder* const event) const
  63. {
  64. return list.indexOf (event);
  65. }
  66. int MidiMessageSequence::getNextIndexAtTime (const double timeStamp) const
  67. {
  68. const int numEvents = list.size();
  69. int i;
  70. for (i = 0; i < numEvents; ++i)
  71. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  72. break;
  73. return i;
  74. }
  75. //==============================================================================
  76. double MidiMessageSequence::getStartTime() const
  77. {
  78. return getEventTime (0);
  79. }
  80. double MidiMessageSequence::getEndTime() const
  81. {
  82. return getEventTime (list.size() - 1);
  83. }
  84. double MidiMessageSequence::getEventTime (const int index) const
  85. {
  86. if (const MidiEventHolder* const meh = list [index])
  87. return meh->message.getTimeStamp();
  88. return 0.0;
  89. }
  90. //==============================================================================
  91. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage,
  92. double timeAdjustment)
  93. {
  94. MidiEventHolder* const newOne = new MidiEventHolder (newMessage);
  95. timeAdjustment += newMessage.getTimeStamp();
  96. newOne->message.setTimeStamp (timeAdjustment);
  97. int i;
  98. for (i = list.size(); --i >= 0;)
  99. if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
  100. break;
  101. list.insert (i + 1, newOne);
  102. return newOne;
  103. }
  104. void MidiMessageSequence::deleteEvent (const int index,
  105. const bool deleteMatchingNoteUp)
  106. {
  107. if (isPositiveAndBelow (index, list.size()))
  108. {
  109. if (deleteMatchingNoteUp)
  110. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  111. list.remove (index);
  112. }
  113. }
  114. struct MidiMessageSequenceSorter
  115. {
  116. static int compareElements (const MidiMessageSequence::MidiEventHolder* const first,
  117. const MidiMessageSequence::MidiEventHolder* const second) noexcept
  118. {
  119. const double diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  120. return (diff > 0) - (diff < 0);
  121. }
  122. };
  123. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  124. double timeAdjustment,
  125. double firstAllowableTime,
  126. double endOfAllowableDestTimes)
  127. {
  128. firstAllowableTime -= timeAdjustment;
  129. endOfAllowableDestTimes -= timeAdjustment;
  130. for (int i = 0; i < other.list.size(); ++i)
  131. {
  132. const MidiMessage& m = other.list.getUnchecked(i)->message;
  133. const double t = m.getTimeStamp();
  134. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  135. {
  136. MidiEventHolder* const newOne = new MidiEventHolder (m);
  137. newOne->message.setTimeStamp (timeAdjustment + t);
  138. list.add (newOne);
  139. }
  140. }
  141. sort();
  142. }
  143. //==============================================================================
  144. void MidiMessageSequence::sort()
  145. {
  146. MidiMessageSequenceSorter sorter;
  147. list.sort (sorter, true);
  148. }
  149. void MidiMessageSequence::updateMatchedPairs()
  150. {
  151. for (int i = 0; i < list.size(); ++i)
  152. {
  153. MidiEventHolder* const meh = list.getUnchecked(i);
  154. const MidiMessage& m1 = meh->message;
  155. if (m1.isNoteOn())
  156. {
  157. meh->noteOffObject = nullptr;
  158. const int note = m1.getNoteNumber();
  159. const int chan = m1.getChannel();
  160. const int len = list.size();
  161. for (int j = i + 1; j < len; ++j)
  162. {
  163. const MidiMessage& m = list.getUnchecked(j)->message;
  164. if (m.getNoteNumber() == note && m.getChannel() == chan)
  165. {
  166. if (m.isNoteOff())
  167. {
  168. meh->noteOffObject = list[j];
  169. break;
  170. }
  171. else if (m.isNoteOn())
  172. {
  173. MidiEventHolder* const newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  174. list.insert (j, newEvent);
  175. newEvent->message.setTimeStamp (m.getTimeStamp());
  176. meh->noteOffObject = newEvent;
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. void MidiMessageSequence::addTimeToMessages (const double delta)
  185. {
  186. for (int i = list.size(); --i >= 0;)
  187. {
  188. MidiMessage& mm = list.getUnchecked(i)->message;
  189. mm.setTimeStamp (mm.getTimeStamp() + delta);
  190. }
  191. }
  192. //==============================================================================
  193. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  194. MidiMessageSequence& destSequence,
  195. const bool alsoIncludeMetaEvents) const
  196. {
  197. for (int i = 0; i < list.size(); ++i)
  198. {
  199. const MidiMessage& mm = list.getUnchecked(i)->message;
  200. if (mm.isForChannel (channelNumberToExtract) || (alsoIncludeMetaEvents && mm.isMetaEvent()))
  201. destSequence.addEvent (mm);
  202. }
  203. }
  204. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  205. {
  206. for (int i = 0; i < list.size(); ++i)
  207. {
  208. const MidiMessage& mm = list.getUnchecked(i)->message;
  209. if (mm.isSysEx())
  210. destSequence.addEvent (mm);
  211. }
  212. }
  213. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  214. {
  215. for (int i = list.size(); --i >= 0;)
  216. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  217. list.remove(i);
  218. }
  219. void MidiMessageSequence::deleteSysExMessages()
  220. {
  221. for (int i = list.size(); --i >= 0;)
  222. if (list.getUnchecked(i)->message.isSysEx())
  223. list.remove(i);
  224. }
  225. //==============================================================================
  226. void MidiMessageSequence::createControllerUpdatesForTime (const int channelNumber,
  227. const double time,
  228. OwnedArray<MidiMessage>& dest)
  229. {
  230. bool doneProg = false;
  231. bool donePitchWheel = false;
  232. Array<int> doneControllers;
  233. doneControllers.ensureStorageAllocated (32);
  234. for (int i = list.size(); --i >= 0;)
  235. {
  236. const MidiMessage& mm = list.getUnchecked(i)->message;
  237. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  238. {
  239. if (mm.isProgramChange())
  240. {
  241. if (! doneProg)
  242. {
  243. dest.add (new MidiMessage (mm, 0.0));
  244. doneProg = true;
  245. }
  246. }
  247. else if (mm.isController())
  248. {
  249. if (! doneControllers.contains (mm.getControllerNumber()))
  250. {
  251. dest.add (new MidiMessage (mm, 0.0));
  252. doneControllers.add (mm.getControllerNumber());
  253. }
  254. }
  255. else if (mm.isPitchWheel())
  256. {
  257. if (! donePitchWheel)
  258. {
  259. dest.add (new MidiMessage (mm, 0.0));
  260. donePitchWheel = true;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. //==============================================================================
  267. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm)
  268. : message (mm), noteOffObject (nullptr)
  269. {
  270. }
  271. MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
  272. {
  273. }