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.

344 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2019 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #include "MidiMessageSequence.h"
  21. namespace water {
  22. MidiMessageSequence::MidiMessageSequence()
  23. {
  24. }
  25. MidiMessageSequence::MidiMessageSequence (const MidiMessageSequence& other)
  26. {
  27. addSequence(other, 0.0);
  28. updateMatchedPairs();
  29. }
  30. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  31. {
  32. MidiMessageSequence otherCopy (other);
  33. swapWith (otherCopy);
  34. return *this;
  35. }
  36. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  37. {
  38. list.swapWith (other.list);
  39. }
  40. MidiMessageSequence::~MidiMessageSequence()
  41. {
  42. }
  43. void MidiMessageSequence::clear()
  44. {
  45. list.clear();
  46. }
  47. int MidiMessageSequence::getNumEvents() const noexcept
  48. {
  49. return list.size();
  50. }
  51. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const noexcept
  52. {
  53. return list [index];
  54. }
  55. double MidiMessageSequence::getTimeOfMatchingKeyUp (const int index) const noexcept
  56. {
  57. if (const MidiEventHolder* const meh = list [index])
  58. if (meh->noteOffObject != nullptr)
  59. return meh->noteOffObject->message.getTimeStamp();
  60. return 0.0;
  61. }
  62. int MidiMessageSequence::getIndexOfMatchingKeyUp (const int index) const noexcept
  63. {
  64. if (const MidiEventHolder* const meh = list [index])
  65. return list.indexOf (meh->noteOffObject);
  66. return -1;
  67. }
  68. int MidiMessageSequence::getIndexOf (const MidiEventHolder* const event) const noexcept
  69. {
  70. return list.indexOf (event);
  71. }
  72. int MidiMessageSequence::getNextIndexAtTime (const double timeStamp) const noexcept
  73. {
  74. const int numEvents = list.size();
  75. int i;
  76. for (i = 0; i < numEvents; ++i)
  77. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  78. break;
  79. return i;
  80. }
  81. //==============================================================================
  82. double MidiMessageSequence::getStartTime() const noexcept
  83. {
  84. return getEventTime (0);
  85. }
  86. double MidiMessageSequence::getEndTime() const noexcept
  87. {
  88. return getEventTime (list.size() - 1);
  89. }
  90. double MidiMessageSequence::getEventTime (const int index) const noexcept
  91. {
  92. if (const MidiEventHolder* const meh = list [index])
  93. return meh->message.getTimeStamp();
  94. return 0.0;
  95. }
  96. //==============================================================================
  97. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage,
  98. double timeAdjustment)
  99. {
  100. MidiEventHolder* const newOne = new MidiEventHolder (newMessage);
  101. timeAdjustment += newMessage.getTimeStamp();
  102. newOne->message.setTimeStamp (timeAdjustment);
  103. int i;
  104. for (i = list.size(); --i >= 0;)
  105. if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
  106. break;
  107. list.insert (i + 1, newOne);
  108. return newOne;
  109. }
  110. void MidiMessageSequence::deleteEvent (const int index,
  111. const bool deleteMatchingNoteUp)
  112. {
  113. if (isPositiveAndBelow (index, static_cast<int>(list.size())))
  114. {
  115. if (deleteMatchingNoteUp)
  116. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  117. list.remove (index);
  118. }
  119. }
  120. struct MidiMessageSequenceSorter
  121. {
  122. static int compareElements (const MidiMessageSequence::MidiEventHolder* const first,
  123. const MidiMessageSequence::MidiEventHolder* const second) noexcept
  124. {
  125. const double diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  126. return (diff > 0) - (diff < 0);
  127. }
  128. };
  129. void MidiMessageSequence::addSequence (const MidiMessageSequence& other, double timeAdjustment)
  130. {
  131. for (int i = 0; i < static_cast<int>(other.list.size()); ++i)
  132. {
  133. const MidiMessage& m = other.list.getUnchecked(i)->message;
  134. MidiEventHolder* const newOne = new MidiEventHolder (m);
  135. newOne->message.addToTimeStamp (timeAdjustment);
  136. list.add (newOne);
  137. }
  138. sort();
  139. }
  140. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  141. double timeAdjustment,
  142. double firstAllowableTime,
  143. double endOfAllowableDestTimes)
  144. {
  145. for (int i = 0; i < static_cast<int>(other.list.size()); ++i)
  146. {
  147. const MidiMessage& m = other.list.getUnchecked(i)->message;
  148. const double t = m.getTimeStamp() + timeAdjustment;
  149. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  150. {
  151. MidiEventHolder* const newOne = new MidiEventHolder (m);
  152. newOne->message.setTimeStamp (t);
  153. list.add (newOne);
  154. }
  155. }
  156. sort();
  157. }
  158. //==============================================================================
  159. void MidiMessageSequence::sort() noexcept
  160. {
  161. MidiMessageSequenceSorter sorter;
  162. list.sort (sorter, true);
  163. }
  164. void MidiMessageSequence::updateMatchedPairs() noexcept
  165. {
  166. for (int i = 0; i < static_cast<int>(list.size()); ++i)
  167. {
  168. MidiEventHolder* const meh = list.getUnchecked(i);
  169. const MidiMessage& m1 = meh->message;
  170. if (m1.isNoteOn())
  171. {
  172. meh->noteOffObject = nullptr;
  173. const int note = m1.getNoteNumber();
  174. const int chan = m1.getChannel();
  175. const int len = list.size();
  176. for (int j = i + 1; j < len; ++j)
  177. {
  178. const MidiMessage& m = list.getUnchecked(j)->message;
  179. if (m.getNoteNumber() == note && m.getChannel() == chan)
  180. {
  181. if (m.isNoteOff())
  182. {
  183. meh->noteOffObject = list[j];
  184. break;
  185. }
  186. else if (m.isNoteOn())
  187. {
  188. MidiEventHolder* const newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  189. list.insert (j, newEvent);
  190. newEvent->message.setTimeStamp (m.getTimeStamp());
  191. meh->noteOffObject = newEvent;
  192. break;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. void MidiMessageSequence::addTimeToMessages (const double delta) noexcept
  200. {
  201. for (int i = static_cast<int>(list.size()); --i >= 0;)
  202. {
  203. MidiMessage& mm = list.getUnchecked(i)->message;
  204. mm.setTimeStamp (mm.getTimeStamp() + delta);
  205. }
  206. }
  207. //==============================================================================
  208. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  209. MidiMessageSequence& destSequence,
  210. const bool alsoIncludeMetaEvents) const
  211. {
  212. for (int i = 0; i < static_cast<int>(list.size()); ++i)
  213. {
  214. const MidiMessage& mm = list.getUnchecked(i)->message;
  215. if (mm.isForChannel (channelNumberToExtract) || (alsoIncludeMetaEvents && mm.isMetaEvent()))
  216. destSequence.addEvent (mm);
  217. }
  218. }
  219. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  220. {
  221. for (int i = 0; i < static_cast<int>(list.size()); ++i)
  222. {
  223. const MidiMessage& mm = list.getUnchecked(i)->message;
  224. if (mm.isSysEx())
  225. destSequence.addEvent (mm);
  226. }
  227. }
  228. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  229. {
  230. for (int i = list.size(); --i >= 0;)
  231. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  232. list.remove(i);
  233. }
  234. void MidiMessageSequence::deleteSysExMessages()
  235. {
  236. for (int i = list.size(); --i >= 0;)
  237. if (list.getUnchecked(i)->message.isSysEx())
  238. list.remove(i);
  239. }
  240. //==============================================================================
  241. void MidiMessageSequence::createControllerUpdatesForTime (const int channelNumber, const double time, Array<MidiMessage>& dest)
  242. {
  243. bool doneProg = false;
  244. bool donePitchWheel = false;
  245. bool doneControllers[128] = { 0 };
  246. for (int i = list.size(); --i >= 0;)
  247. {
  248. const MidiMessage& mm = list.getUnchecked(i)->message;
  249. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  250. {
  251. if (mm.isProgramChange() && ! doneProg)
  252. {
  253. doneProg = true;
  254. dest.add (MidiMessage (mm, 0.0));
  255. }
  256. else if (mm.isPitchWheel() && ! donePitchWheel)
  257. {
  258. donePitchWheel = true;
  259. dest.add (MidiMessage (mm, 0.0));
  260. }
  261. else if (mm.isController())
  262. {
  263. const int controllerNumber = mm.getControllerNumber();
  264. wassert (isPositiveAndBelow (controllerNumber, 128));
  265. if (! doneControllers[controllerNumber])
  266. {
  267. doneControllers[controllerNumber] = true;
  268. dest.add (MidiMessage (mm, 0.0));
  269. }
  270. }
  271. }
  272. }
  273. }
  274. //==============================================================================
  275. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm)
  276. : message (mm), noteOffObject (nullptr)
  277. {
  278. }
  279. MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
  280. {
  281. }
  282. }