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.

283 lines
8.5KB

  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. // clear
  157. void clear() noexcept
  158. {
  159. const CarlaMutexLocker sl(fMutex);
  160. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  161. delete it.getValue(nullptr);
  162. fData.clear();
  163. }
  164. // -------------------------------------------------------------------
  165. // play on time
  166. void play(uint64_t timePosFrame, const uint32_t frames)
  167. {
  168. if (! fMutex.tryLock())
  169. return;
  170. timePosFrame += fStartTime;
  171. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  172. {
  173. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  174. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  175. if (timePosFrame > rawMidiEvent->time)
  176. continue;
  177. if (timePosFrame + frames <= rawMidiEvent->time)
  178. continue;
  179. kPlayer->writeMidiEvent(fMidiPort, timePosFrame, rawMidiEvent);
  180. }
  181. fMutex.unlock();
  182. }
  183. // -------------------------------------------------------------------
  184. // configure
  185. void setMidiPort(const uint8_t port) noexcept
  186. {
  187. fMidiPort = port;
  188. }
  189. void setStartTime(const uint64_t time) noexcept
  190. {
  191. fStartTime = time;
  192. }
  193. // -------------------------------------------------------------------
  194. private:
  195. AbstractMidiPlayer* const kPlayer;
  196. uint8_t fMidiPort;
  197. uint64_t fStartTime;
  198. CarlaMutex fMutex;
  199. LinkedList<const RawMidiEvent*> fData;
  200. void appendSorted(const RawMidiEvent* const event)
  201. {
  202. const CarlaMutexLocker sl(fMutex);
  203. if (fData.isEmpty())
  204. {
  205. fData.append(event);
  206. return;
  207. }
  208. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  209. {
  210. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  211. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  212. if (event->time >= oldEvent->time)
  213. continue;
  214. fData.insertAt(event, it);
  215. return;
  216. }
  217. fData.append(event);
  218. }
  219. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  220. };
  221. // -----------------------------------------------------------------------
  222. #endif // MIDI_BASE_HPP_INCLUDED