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.

midi-base.hpp 7.1KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013-2014 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 "CarlaMathUtils.hpp"
  23. #define MAX_EVENT_DATA_SIZE 4
  24. #define MIN_PREALLOCATED_EVENT_COUNT 100
  25. #define MAX_PREALLOCATED_EVENT_COUNT 1000
  26. struct RawMidiEvent {
  27. uint64_t time;
  28. uint8_t size;
  29. uint8_t data[MAX_EVENT_DATA_SIZE];
  30. RawMidiEvent()
  31. : time(0),
  32. size(0)
  33. {
  34. carla_fill<uint8_t>(data, 0, MAX_EVENT_DATA_SIZE);
  35. }
  36. };
  37. class AbstractMidiPlayer
  38. {
  39. public:
  40. virtual ~AbstractMidiPlayer() {}
  41. virtual void writeMidiEvent(const uint64_t timePosFrame, const RawMidiEvent* const event) = 0;
  42. };
  43. class MidiPattern
  44. {
  45. public:
  46. MidiPattern(AbstractMidiPlayer* const player)
  47. : kPlayer(player),
  48. fMutex(),
  49. fData(),
  50. leakDetector_MidiPattern()
  51. //fStartTime(0),
  52. //fDuration(0)
  53. {
  54. CARLA_ASSERT(kPlayer != nullptr);
  55. }
  56. ~MidiPattern()
  57. {
  58. fData.clear();
  59. }
  60. void addControl(const uint64_t time, const uint8_t channel, const uint8_t control, const uint8_t value)
  61. {
  62. RawMidiEvent* ctrlEvent(new RawMidiEvent());
  63. ctrlEvent->time = time;
  64. ctrlEvent->size = 3;
  65. ctrlEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & 0x0F));
  66. ctrlEvent->data[1] = control;
  67. ctrlEvent->data[2] = value;
  68. append(ctrlEvent);
  69. }
  70. void addChannelPressure(const uint64_t time, const uint8_t channel, const uint8_t pressure)
  71. {
  72. RawMidiEvent* pressureEvent(new RawMidiEvent());
  73. pressureEvent->time = time;
  74. pressureEvent->size = 2;
  75. pressureEvent->data[0] = uint8_t(MIDI_STATUS_CHANNEL_PRESSURE | (channel & 0x0F));
  76. pressureEvent->data[1] = pressure;
  77. append(pressureEvent);
  78. }
  79. void addNote(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity, const uint32_t duration)
  80. {
  81. addNoteOn(time, channel, pitch, velocity);
  82. addNoteOff(time+duration, channel, pitch, velocity);
  83. }
  84. void addNoteOn(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity)
  85. {
  86. RawMidiEvent* noteOnEvent(new RawMidiEvent());
  87. noteOnEvent->time = time;
  88. noteOnEvent->size = 3;
  89. noteOnEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & 0x0F));
  90. noteOnEvent->data[1] = pitch;
  91. noteOnEvent->data[2] = velocity;
  92. append(noteOnEvent);
  93. }
  94. void addNoteOff(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity = 0)
  95. {
  96. RawMidiEvent* noteOffEvent(new RawMidiEvent());
  97. noteOffEvent->time = time;
  98. noteOffEvent->size = 3;
  99. noteOffEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & 0x0F));
  100. noteOffEvent->data[1] = pitch;
  101. noteOffEvent->data[2] = velocity;
  102. append(noteOffEvent);
  103. }
  104. void addNoteAftertouch(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t pressure)
  105. {
  106. RawMidiEvent* noteAfterEvent(new RawMidiEvent());
  107. noteAfterEvent->time = time;
  108. noteAfterEvent->size = 3;
  109. noteAfterEvent->data[0] = uint8_t(MIDI_STATUS_POLYPHONIC_AFTERTOUCH | (channel & 0x0F));
  110. noteAfterEvent->data[1] = pitch;
  111. noteAfterEvent->data[2] = pressure;
  112. append(noteAfterEvent);
  113. }
  114. void addProgram(const uint64_t time, const uint8_t channel, const uint8_t bank, const uint8_t program)
  115. {
  116. RawMidiEvent* bankEvent(new RawMidiEvent());
  117. bankEvent->time = time;
  118. bankEvent->size = 3;
  119. bankEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & 0x0F));
  120. bankEvent->data[1] = MIDI_CONTROL_BANK_SELECT;
  121. bankEvent->data[2] = bank;
  122. RawMidiEvent* programEvent(new RawMidiEvent());
  123. programEvent->time = time;
  124. programEvent->size = 2;
  125. programEvent->data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (channel & 0x0F));
  126. programEvent->data[1] = program;
  127. append(bankEvent);
  128. append(programEvent);
  129. }
  130. void addPitchbend(const uint64_t time, const uint8_t channel, const uint8_t lsb, const uint8_t msb)
  131. {
  132. RawMidiEvent* pressureEvent(new RawMidiEvent());
  133. pressureEvent->time = time;
  134. pressureEvent->size = 3;
  135. pressureEvent->data[0] = uint8_t(MIDI_STATUS_PITCH_WHEEL_CONTROL | (channel & 0x0F));
  136. pressureEvent->data[1] = lsb;
  137. pressureEvent->data[2] = msb;
  138. append(pressureEvent);
  139. }
  140. void addRaw(const uint64_t time, const uint8_t* data, const uint8_t size)
  141. {
  142. RawMidiEvent* rawEvent(new RawMidiEvent());
  143. rawEvent->time = time;
  144. rawEvent->size = size;
  145. carla_copy<uint8_t>(rawEvent->data, data, size);
  146. append(rawEvent);
  147. }
  148. void play(uint64_t timePosFrame, uint32_t frames)
  149. {
  150. if (! fMutex.tryLock())
  151. return;
  152. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  153. {
  154. const RawMidiEvent* const rawMidiEvent(it.getValue());
  155. if (timePosFrame > rawMidiEvent->time)
  156. continue;
  157. if (timePosFrame + frames <= rawMidiEvent->time)
  158. continue;
  159. kPlayer->writeMidiEvent(timePosFrame, rawMidiEvent);
  160. }
  161. fMutex.unlock();
  162. }
  163. void clear()
  164. {
  165. const CarlaMutexLocker sl(fMutex);
  166. fData.clear();
  167. }
  168. private:
  169. AbstractMidiPlayer* const kPlayer;
  170. //uint32_t fStartTime; // unused
  171. //uint32_t fDuration; // unused
  172. CarlaMutex fMutex;
  173. LinkedList<const RawMidiEvent*> fData;
  174. void append(const RawMidiEvent* const event)
  175. {
  176. if (fData.isEmpty())
  177. {
  178. const CarlaMutexLocker sl(fMutex);
  179. fData.append(event);
  180. return;
  181. }
  182. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin(); it.valid(); it.next())
  183. {
  184. const RawMidiEvent* const oldEvent(it.getValue());
  185. if (event->time >= oldEvent->time)
  186. continue;
  187. const CarlaMutexLocker sl(fMutex);
  188. fData.insertAt(event, it);
  189. return;
  190. }
  191. const CarlaMutexLocker sl(fMutex);
  192. fData.append(event);
  193. }
  194. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  195. };
  196. #endif // MIDI_BASE_HPP_INCLUDED