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.

334 lines
10KB

  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.ensureStorageAllocated (other.list.size());
  23. for (int i = 0; i < other.list.size(); ++i)
  24. list.add (new MidiEventHolder (other.list.getUnchecked(i)->message));
  25. }
  26. MidiMessageSequence& MidiMessageSequence::operator= (const MidiMessageSequence& other)
  27. {
  28. MidiMessageSequence otherCopy (other);
  29. swapWith (otherCopy);
  30. return *this;
  31. }
  32. void MidiMessageSequence::swapWith (MidiMessageSequence& other) noexcept
  33. {
  34. list.swapWith (other.list);
  35. }
  36. MidiMessageSequence::~MidiMessageSequence()
  37. {
  38. }
  39. void MidiMessageSequence::clear()
  40. {
  41. list.clear();
  42. }
  43. int MidiMessageSequence::getNumEvents() const
  44. {
  45. return list.size();
  46. }
  47. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::getEventPointer (const int index) const
  48. {
  49. return list [index];
  50. }
  51. double MidiMessageSequence::getTimeOfMatchingKeyUp (const int index) const
  52. {
  53. if (const MidiEventHolder* const meh = list [index])
  54. if (meh->noteOffObject != nullptr)
  55. return meh->noteOffObject->message.getTimeStamp();
  56. return 0.0;
  57. }
  58. int MidiMessageSequence::getIndexOfMatchingKeyUp (const int index) const
  59. {
  60. if (const MidiEventHolder* const meh = list [index])
  61. return list.indexOf (meh->noteOffObject);
  62. return -1;
  63. }
  64. int MidiMessageSequence::getIndexOf (MidiEventHolder* const event) const
  65. {
  66. return list.indexOf (event);
  67. }
  68. int MidiMessageSequence::getNextIndexAtTime (const double timeStamp) const
  69. {
  70. const int numEvents = list.size();
  71. int i;
  72. for (i = 0; i < numEvents; ++i)
  73. if (list.getUnchecked(i)->message.getTimeStamp() >= timeStamp)
  74. break;
  75. return i;
  76. }
  77. //==============================================================================
  78. double MidiMessageSequence::getStartTime() const
  79. {
  80. return getEventTime (0);
  81. }
  82. double MidiMessageSequence::getEndTime() const
  83. {
  84. return getEventTime (list.size() - 1);
  85. }
  86. double MidiMessageSequence::getEventTime (const int index) const
  87. {
  88. if (const MidiEventHolder* const meh = list [index])
  89. return meh->message.getTimeStamp();
  90. return 0.0;
  91. }
  92. //==============================================================================
  93. MidiMessageSequence::MidiEventHolder* MidiMessageSequence::addEvent (const MidiMessage& newMessage,
  94. double timeAdjustment)
  95. {
  96. MidiEventHolder* const newOne = new MidiEventHolder (newMessage);
  97. timeAdjustment += newMessage.getTimeStamp();
  98. newOne->message.setTimeStamp (timeAdjustment);
  99. int i;
  100. for (i = list.size(); --i >= 0;)
  101. if (list.getUnchecked(i)->message.getTimeStamp() <= timeAdjustment)
  102. break;
  103. list.insert (i + 1, newOne);
  104. return newOne;
  105. }
  106. void MidiMessageSequence::deleteEvent (const int index,
  107. const bool deleteMatchingNoteUp)
  108. {
  109. if (isPositiveAndBelow (index, list.size()))
  110. {
  111. if (deleteMatchingNoteUp)
  112. deleteEvent (getIndexOfMatchingKeyUp (index), false);
  113. list.remove (index);
  114. }
  115. }
  116. struct MidiMessageSequenceSorter
  117. {
  118. static int compareElements (const MidiMessageSequence::MidiEventHolder* const first,
  119. const MidiMessageSequence::MidiEventHolder* const second) noexcept
  120. {
  121. const double diff = first->message.getTimeStamp() - second->message.getTimeStamp();
  122. return (diff > 0) - (diff < 0);
  123. }
  124. };
  125. void MidiMessageSequence::addSequence (const MidiMessageSequence& other,
  126. double timeAdjustment,
  127. double firstAllowableTime,
  128. double endOfAllowableDestTimes)
  129. {
  130. firstAllowableTime -= timeAdjustment;
  131. endOfAllowableDestTimes -= timeAdjustment;
  132. for (int i = 0; i < other.list.size(); ++i)
  133. {
  134. const MidiMessage& m = other.list.getUnchecked(i)->message;
  135. const double t = m.getTimeStamp();
  136. if (t >= firstAllowableTime && t < endOfAllowableDestTimes)
  137. {
  138. MidiEventHolder* const newOne = new MidiEventHolder (m);
  139. newOne->message.setTimeStamp (timeAdjustment + t);
  140. list.add (newOne);
  141. }
  142. }
  143. sort();
  144. }
  145. //==============================================================================
  146. void MidiMessageSequence::sort()
  147. {
  148. MidiMessageSequenceSorter sorter;
  149. list.sort (sorter, true);
  150. }
  151. void MidiMessageSequence::updateMatchedPairs()
  152. {
  153. for (int i = 0; i < list.size(); ++i)
  154. {
  155. MidiEventHolder* const meh = list.getUnchecked(i);
  156. const MidiMessage& m1 = meh->message;
  157. if (m1.isNoteOn())
  158. {
  159. meh->noteOffObject = nullptr;
  160. const int note = m1.getNoteNumber();
  161. const int chan = m1.getChannel();
  162. const int len = list.size();
  163. for (int j = i + 1; j < len; ++j)
  164. {
  165. const MidiMessage& m = list.getUnchecked(j)->message;
  166. if (m.getNoteNumber() == note && m.getChannel() == chan)
  167. {
  168. if (m.isNoteOff())
  169. {
  170. meh->noteOffObject = list[j];
  171. break;
  172. }
  173. else if (m.isNoteOn())
  174. {
  175. MidiEventHolder* const newEvent = new MidiEventHolder (MidiMessage::noteOff (chan, note));
  176. list.insert (j, newEvent);
  177. newEvent->message.setTimeStamp (m.getTimeStamp());
  178. meh->noteOffObject = newEvent;
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. void MidiMessageSequence::addTimeToMessages (const double delta)
  187. {
  188. for (int i = list.size(); --i >= 0;)
  189. {
  190. MidiMessage& mm = list.getUnchecked(i)->message;
  191. mm.setTimeStamp (mm.getTimeStamp() + delta);
  192. }
  193. }
  194. //==============================================================================
  195. void MidiMessageSequence::extractMidiChannelMessages (const int channelNumberToExtract,
  196. MidiMessageSequence& destSequence,
  197. const bool alsoIncludeMetaEvents) const
  198. {
  199. for (int i = 0; i < list.size(); ++i)
  200. {
  201. const MidiMessage& mm = list.getUnchecked(i)->message;
  202. if (mm.isForChannel (channelNumberToExtract) || (alsoIncludeMetaEvents && mm.isMetaEvent()))
  203. destSequence.addEvent (mm);
  204. }
  205. }
  206. void MidiMessageSequence::extractSysExMessages (MidiMessageSequence& destSequence) const
  207. {
  208. for (int i = 0; i < list.size(); ++i)
  209. {
  210. const MidiMessage& mm = list.getUnchecked(i)->message;
  211. if (mm.isSysEx())
  212. destSequence.addEvent (mm);
  213. }
  214. }
  215. void MidiMessageSequence::deleteMidiChannelMessages (const int channelNumberToRemove)
  216. {
  217. for (int i = list.size(); --i >= 0;)
  218. if (list.getUnchecked(i)->message.isForChannel (channelNumberToRemove))
  219. list.remove(i);
  220. }
  221. void MidiMessageSequence::deleteSysExMessages()
  222. {
  223. for (int i = list.size(); --i >= 0;)
  224. if (list.getUnchecked(i)->message.isSysEx())
  225. list.remove(i);
  226. }
  227. //==============================================================================
  228. void MidiMessageSequence::createControllerUpdatesForTime (const int channelNumber,
  229. const double time,
  230. OwnedArray<MidiMessage>& dest)
  231. {
  232. bool doneProg = false;
  233. bool donePitchWheel = false;
  234. Array<int> doneControllers;
  235. doneControllers.ensureStorageAllocated (32);
  236. for (int i = list.size(); --i >= 0;)
  237. {
  238. const MidiMessage& mm = list.getUnchecked(i)->message;
  239. if (mm.isForChannel (channelNumber) && mm.getTimeStamp() <= time)
  240. {
  241. if (mm.isProgramChange())
  242. {
  243. if (! doneProg)
  244. {
  245. dest.add (new MidiMessage (mm, 0.0));
  246. doneProg = true;
  247. }
  248. }
  249. else if (mm.isController())
  250. {
  251. if (! doneControllers.contains (mm.getControllerNumber()))
  252. {
  253. dest.add (new MidiMessage (mm, 0.0));
  254. doneControllers.add (mm.getControllerNumber());
  255. }
  256. }
  257. else if (mm.isPitchWheel())
  258. {
  259. if (! donePitchWheel)
  260. {
  261. dest.add (new MidiMessage (mm, 0.0));
  262. donePitchWheel = true;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. //==============================================================================
  269. MidiMessageSequence::MidiEventHolder::MidiEventHolder (const MidiMessage& mm)
  270. : message (mm), noteOffObject (nullptr)
  271. {
  272. }
  273. MidiMessageSequence::MidiEventHolder::~MidiEventHolder()
  274. {
  275. }