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.

324 lines
9.7KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef MIDI_BASE_HPP_INCLUDED
  18. #define MIDI_BASE_HPP_INCLUDED
  19. #include "CarlaMIDI.h"
  20. #include "CarlaMutex.hpp"
  21. #include "LinkedList.hpp"
  22. #include "CarlaJuceUtils.hpp"
  23. #include "CarlaMathUtils.hpp"
  24. // -----------------------------------------------------------------------
  25. #define MAX_EVENT_DATA_SIZE 4
  26. #define MIN_PREALLOCATED_EVENT_COUNT 100
  27. #define MAX_PREALLOCATED_EVENT_COUNT 1000
  28. // -----------------------------------------------------------------------
  29. struct RawMidiEvent {
  30. uint64_t time;
  31. uint8_t size;
  32. uint8_t data[MAX_EVENT_DATA_SIZE];
  33. RawMidiEvent() noexcept
  34. : time(0),
  35. size(0)
  36. {
  37. carla_zeroBytes(data, MAX_EVENT_DATA_SIZE);
  38. }
  39. };
  40. // -----------------------------------------------------------------------
  41. class AbstractMidiPlayer
  42. {
  43. public:
  44. virtual ~AbstractMidiPlayer() {}
  45. virtual void writeMidiEvent(const uint8_t port, const uint64_t timePosFrame, const RawMidiEvent* const event) = 0;
  46. };
  47. // -----------------------------------------------------------------------
  48. class MidiPattern
  49. {
  50. public:
  51. MidiPattern(AbstractMidiPlayer* const player) noexcept
  52. : kPlayer(player),
  53. fMidiPort(0),
  54. fStartTime(0),
  55. fMutex(),
  56. fData(),
  57. leakDetector_MidiPattern()
  58. {
  59. CARLA_SAFE_ASSERT(kPlayer != nullptr);
  60. }
  61. ~MidiPattern() noexcept
  62. {
  63. clear();
  64. }
  65. // -------------------------------------------------------------------
  66. // add data, time always counts from 0
  67. void addControl(const uint64_t time, const uint8_t channel, const uint8_t control, const uint8_t value)
  68. {
  69. RawMidiEvent* const ctrlEvent(new RawMidiEvent());
  70. ctrlEvent->time = time;
  71. ctrlEvent->size = 3;
  72. ctrlEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & 0x0F));
  73. ctrlEvent->data[1] = control;
  74. ctrlEvent->data[2] = value;
  75. appendSorted(ctrlEvent);
  76. }
  77. void addChannelPressure(const uint64_t time, const uint8_t channel, const uint8_t pressure)
  78. {
  79. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  80. pressureEvent->time = time;
  81. pressureEvent->size = 2;
  82. pressureEvent->data[0] = uint8_t(MIDI_STATUS_CHANNEL_PRESSURE | (channel & 0x0F));
  83. pressureEvent->data[1] = pressure;
  84. appendSorted(pressureEvent);
  85. }
  86. void addNote(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity, const uint32_t duration)
  87. {
  88. addNoteOn(time, channel, pitch, velocity);
  89. addNoteOff(time+duration, channel, pitch, velocity);
  90. }
  91. void addNoteOn(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity)
  92. {
  93. RawMidiEvent* const noteOnEvent(new RawMidiEvent());
  94. noteOnEvent->time = time;
  95. noteOnEvent->size = 3;
  96. noteOnEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & 0x0F));
  97. noteOnEvent->data[1] = pitch;
  98. noteOnEvent->data[2] = velocity;
  99. appendSorted(noteOnEvent);
  100. }
  101. void addNoteOff(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity = 0)
  102. {
  103. RawMidiEvent* const noteOffEvent(new RawMidiEvent());
  104. noteOffEvent->time = time;
  105. noteOffEvent->size = 3;
  106. noteOffEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & 0x0F));
  107. noteOffEvent->data[1] = pitch;
  108. noteOffEvent->data[2] = velocity;
  109. appendSorted(noteOffEvent);
  110. }
  111. void addNoteAftertouch(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t pressure)
  112. {
  113. RawMidiEvent* const noteAfterEvent(new RawMidiEvent());
  114. noteAfterEvent->time = time;
  115. noteAfterEvent->size = 3;
  116. noteAfterEvent->data[0] = uint8_t(MIDI_STATUS_POLYPHONIC_AFTERTOUCH | (channel & 0x0F));
  117. noteAfterEvent->data[1] = pitch;
  118. noteAfterEvent->data[2] = pressure;
  119. appendSorted(noteAfterEvent);
  120. }
  121. void addProgram(const uint64_t time, const uint8_t channel, const uint8_t bank, const uint8_t program)
  122. {
  123. RawMidiEvent* const bankEvent(new RawMidiEvent());
  124. bankEvent->time = time;
  125. bankEvent->size = 3;
  126. bankEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & 0x0F));
  127. bankEvent->data[1] = MIDI_CONTROL_BANK_SELECT;
  128. bankEvent->data[2] = bank;
  129. RawMidiEvent* const programEvent(new RawMidiEvent());
  130. programEvent->time = time;
  131. programEvent->size = 2;
  132. programEvent->data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (channel & 0x0F));
  133. programEvent->data[1] = program;
  134. appendSorted(bankEvent);
  135. appendSorted(programEvent);
  136. }
  137. void addPitchbend(const uint64_t time, const uint8_t channel, const uint8_t lsb, const uint8_t msb)
  138. {
  139. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  140. pressureEvent->time = time;
  141. pressureEvent->size = 3;
  142. pressureEvent->data[0] = uint8_t(MIDI_STATUS_PITCH_WHEEL_CONTROL | (channel & 0x0F));
  143. pressureEvent->data[1] = lsb;
  144. pressureEvent->data[2] = msb;
  145. appendSorted(pressureEvent);
  146. }
  147. void addRaw(const uint64_t time, const uint8_t* const data, const uint8_t size)
  148. {
  149. RawMidiEvent* const rawEvent(new RawMidiEvent());
  150. rawEvent->time = time;
  151. rawEvent->size = size;
  152. carla_copy<uint8_t>(rawEvent->data, data, size);
  153. appendSorted(rawEvent);
  154. }
  155. // -------------------------------------------------------------------
  156. // remove data
  157. void removeRaw(const uint64_t time, const uint8_t* const data, const uint8_t size)
  158. {
  159. const CarlaMutexLocker sl(fMutex);
  160. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  161. {
  162. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  163. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  164. if (rawMidiEvent->time != time)
  165. continue;
  166. if (rawMidiEvent->size != size)
  167. continue;
  168. if (std::memcmp(rawMidiEvent->data, data, size) != 0)
  169. continue;
  170. delete rawMidiEvent;
  171. fData.remove(it);
  172. return;
  173. }
  174. carla_stderr("MidiPattern::removeRaw(" P_INT64 ", %p, %i) - unable to find event to remove", time, data, size);
  175. }
  176. // -------------------------------------------------------------------
  177. // clear
  178. void clear() noexcept
  179. {
  180. const CarlaMutexLocker sl(fMutex);
  181. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  182. delete it.getValue(nullptr);
  183. fData.clear();
  184. }
  185. // -------------------------------------------------------------------
  186. // play on time
  187. void play(uint64_t timePosFrame, const uint32_t frames)
  188. {
  189. if (! fMutex.tryLock())
  190. return;
  191. timePosFrame += fStartTime;
  192. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  193. {
  194. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  195. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  196. if (timePosFrame > rawMidiEvent->time)
  197. continue;
  198. if (timePosFrame + frames <= rawMidiEvent->time)
  199. continue;
  200. kPlayer->writeMidiEvent(fMidiPort, timePosFrame, rawMidiEvent);
  201. }
  202. fMutex.unlock();
  203. }
  204. // -------------------------------------------------------------------
  205. // configure
  206. void setMidiPort(const uint8_t port) noexcept
  207. {
  208. fMidiPort = port;
  209. }
  210. void setStartTime(const uint64_t time) noexcept
  211. {
  212. fStartTime = time;
  213. }
  214. // -------------------------------------------------------------------
  215. // special
  216. const CarlaMutex& getLock() const noexcept
  217. {
  218. return fMutex;
  219. }
  220. LinkedList<const RawMidiEvent*>::Itenerator iteneratorBegin() const noexcept
  221. {
  222. return fData.begin();
  223. }
  224. // -------------------------------------------------------------------
  225. private:
  226. AbstractMidiPlayer* const kPlayer;
  227. uint8_t fMidiPort;
  228. uint64_t fStartTime;
  229. CarlaMutex fMutex;
  230. LinkedList<const RawMidiEvent*> fData;
  231. void appendSorted(const RawMidiEvent* const event)
  232. {
  233. const CarlaMutexLocker sl(fMutex);
  234. if (fData.isEmpty())
  235. {
  236. fData.append(event);
  237. return;
  238. }
  239. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  240. {
  241. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  242. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  243. if (event->time >= oldEvent->time)
  244. continue;
  245. fData.insertAt(event, it);
  246. return;
  247. }
  248. fData.append(event);
  249. }
  250. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  251. };
  252. // -----------------------------------------------------------------------
  253. #endif // MIDI_BASE_HPP_INCLUDED