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 16KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2020 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. uint32_t time;
  31. uint8_t size;
  32. uint8_t data[MAX_EVENT_DATA_SIZE];
  33. };
  34. // -----------------------------------------------------------------------
  35. class AbstractMidiPlayer
  36. {
  37. public:
  38. virtual ~AbstractMidiPlayer() {}
  39. virtual void writeMidiEvent(const uint8_t port, const double timePosFrame, const RawMidiEvent* const event) = 0;
  40. };
  41. // -----------------------------------------------------------------------
  42. class MidiPattern
  43. {
  44. public:
  45. MidiPattern(AbstractMidiPlayer* const player) noexcept
  46. : kPlayer(player),
  47. fMidiPort(0),
  48. fStartTime(0),
  49. fReadMutex(),
  50. fWriteMutex(),
  51. fData()
  52. {
  53. CARLA_SAFE_ASSERT(kPlayer != nullptr);
  54. }
  55. ~MidiPattern() noexcept
  56. {
  57. clear();
  58. }
  59. // -------------------------------------------------------------------
  60. // add data, time always counts from 0
  61. void addControl(const uint32_t time, const uint8_t channel, const uint8_t control, const uint8_t value)
  62. {
  63. RawMidiEvent* const ctrlEvent(new RawMidiEvent());
  64. ctrlEvent->time = time;
  65. ctrlEvent->size = 3;
  66. ctrlEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  67. ctrlEvent->data[1] = control;
  68. ctrlEvent->data[2] = value;
  69. appendSorted(ctrlEvent);
  70. }
  71. void addChannelPressure(const uint32_t time, const uint8_t channel, const uint8_t pressure)
  72. {
  73. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  74. pressureEvent->time = time;
  75. pressureEvent->size = 2;
  76. pressureEvent->data[0] = uint8_t(MIDI_STATUS_CHANNEL_PRESSURE | (channel & MIDI_CHANNEL_BIT));
  77. pressureEvent->data[1] = pressure;
  78. appendSorted(pressureEvent);
  79. }
  80. void addNote(const uint32_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity, const uint32_t duration)
  81. {
  82. addNoteOn(time, channel, pitch, velocity);
  83. addNoteOff(time+duration, channel, pitch, velocity);
  84. }
  85. void addNoteOn(const uint32_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity)
  86. {
  87. RawMidiEvent* const noteOnEvent(new RawMidiEvent());
  88. noteOnEvent->time = time;
  89. noteOnEvent->size = 3;
  90. noteOnEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  91. noteOnEvent->data[1] = pitch;
  92. noteOnEvent->data[2] = velocity;
  93. appendSorted(noteOnEvent);
  94. }
  95. void addNoteOff(const uint32_t time, const uint8_t channel, const uint8_t pitch, const uint8_t velocity = 0)
  96. {
  97. RawMidiEvent* const noteOffEvent(new RawMidiEvent());
  98. noteOffEvent->time = time;
  99. noteOffEvent->size = 3;
  100. noteOffEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  101. noteOffEvent->data[1] = pitch;
  102. noteOffEvent->data[2] = velocity;
  103. appendSorted(noteOffEvent);
  104. }
  105. void addNoteAftertouch(const uint32_t time, const uint8_t channel, const uint8_t pitch, const uint8_t pressure)
  106. {
  107. RawMidiEvent* const noteAfterEvent(new RawMidiEvent());
  108. noteAfterEvent->time = time;
  109. noteAfterEvent->size = 3;
  110. noteAfterEvent->data[0] = uint8_t(MIDI_STATUS_POLYPHONIC_AFTERTOUCH | (channel & MIDI_CHANNEL_BIT));
  111. noteAfterEvent->data[1] = pitch;
  112. noteAfterEvent->data[2] = pressure;
  113. appendSorted(noteAfterEvent);
  114. }
  115. void addProgram(const uint32_t time, const uint8_t channel, const uint8_t bank, const uint8_t program)
  116. {
  117. RawMidiEvent* const bankEvent(new RawMidiEvent());
  118. bankEvent->time = time;
  119. bankEvent->size = 3;
  120. bankEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  121. bankEvent->data[1] = MIDI_CONTROL_BANK_SELECT;
  122. bankEvent->data[2] = bank;
  123. RawMidiEvent* const programEvent(new RawMidiEvent());
  124. programEvent->time = time;
  125. programEvent->size = 2;
  126. programEvent->data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
  127. programEvent->data[1] = program;
  128. appendSorted(bankEvent);
  129. appendSorted(programEvent);
  130. }
  131. void addPitchbend(const uint32_t time, const uint8_t channel, const uint8_t lsb, const uint8_t msb)
  132. {
  133. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  134. pressureEvent->time = time;
  135. pressureEvent->size = 3;
  136. pressureEvent->data[0] = uint8_t(MIDI_STATUS_PITCH_WHEEL_CONTROL | (channel & MIDI_CHANNEL_BIT));
  137. pressureEvent->data[1] = lsb;
  138. pressureEvent->data[2] = msb;
  139. appendSorted(pressureEvent);
  140. }
  141. void addRaw(const uint32_t time, const uint8_t* const data, const uint8_t size)
  142. {
  143. RawMidiEvent* const rawEvent(new RawMidiEvent());
  144. rawEvent->time = time;
  145. rawEvent->size = size;
  146. carla_copy<uint8_t>(rawEvent->data, data, size);
  147. // Fix zero-velocity note-ons
  148. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  149. rawEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (data[0] & MIDI_CHANNEL_BIT));
  150. appendSorted(rawEvent);
  151. }
  152. // -------------------------------------------------------------------
  153. // remove data
  154. void removeRaw(const uint32_t time, const uint8_t* const data, const uint8_t size)
  155. {
  156. const CarlaMutexLocker cmlw(fWriteMutex);
  157. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  158. {
  159. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  160. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  161. if (rawMidiEvent->time != time)
  162. continue;
  163. if (rawMidiEvent->size != size)
  164. continue;
  165. if (std::memcmp(rawMidiEvent->data, data, size) != 0)
  166. continue;
  167. {
  168. const CarlaMutexLocker cmlr(fReadMutex);
  169. fData.remove(it);
  170. }
  171. delete rawMidiEvent;
  172. return;
  173. }
  174. carla_stderr("MidiPattern::removeRaw(%u, %p, %i) - unable to find event to remove", time, data, size);
  175. }
  176. // -------------------------------------------------------------------
  177. // clear
  178. void clear() noexcept
  179. {
  180. const CarlaMutexLocker cmlr(fReadMutex);
  181. const CarlaMutexLocker cmlw(fWriteMutex);
  182. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  183. delete it.getValue(nullptr);
  184. fData.clear();
  185. }
  186. // -------------------------------------------------------------------
  187. // play on time
  188. bool play(const uint32_t timePosFrame, const uint32_t frames)
  189. {
  190. return play(static_cast<double>(timePosFrame), static_cast<double>(frames));
  191. }
  192. bool play(double timePosFrame, const double frames, const double offset = 0.0)
  193. {
  194. double ldtime;
  195. const CarlaMutexTryLocker cmtl(fReadMutex);
  196. if (cmtl.wasNotLocked())
  197. return false;
  198. if (fStartTime != 0)
  199. timePosFrame += static_cast<double>(fStartTime);
  200. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  201. {
  202. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  203. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  204. ldtime = static_cast<double>(rawMidiEvent->time);
  205. if (ldtime < timePosFrame)
  206. continue;
  207. if (ldtime > timePosFrame + frames)
  208. break;
  209. if (carla_isEqual(ldtime, timePosFrame + frames))
  210. {
  211. // only allow a few events to pass through in this special case
  212. if (! MIDI_IS_STATUS_NOTE_OFF(rawMidiEvent->data[0]))
  213. continue;
  214. }
  215. kPlayer->writeMidiEvent(fMidiPort, ldtime + offset - timePosFrame, rawMidiEvent);
  216. }
  217. return true;
  218. }
  219. // -------------------------------------------------------------------
  220. // configure
  221. void setMidiPort(const uint8_t port) noexcept
  222. {
  223. fMidiPort = port;
  224. }
  225. void setStartTime(const uint32_t time) noexcept
  226. {
  227. fStartTime = time;
  228. }
  229. // -------------------------------------------------------------------
  230. // special
  231. const CarlaMutex& getWriteMutex() const noexcept
  232. {
  233. return fWriteMutex;
  234. }
  235. LinkedList<const RawMidiEvent*>::Itenerator iteneratorBegin() const noexcept
  236. {
  237. return fData.begin2();
  238. }
  239. // -------------------------------------------------------------------
  240. // state
  241. char* getState() const
  242. {
  243. static const std::size_t maxTimeSize = 20; // std::strlen("18446744073709551615");
  244. static const std::size_t maxDataSize = 4 + 4*MAX_EVENT_DATA_SIZE; // std::strlen("0xFF:127:127:127");
  245. static const std::size_t maxMsgSize = maxTimeSize + 3 /* sep + size + sep */ + maxDataSize + 1 /* newline */;
  246. const CarlaMutexLocker cmlw(fWriteMutex);
  247. char* const data((char*)std::calloc(1, fData.count() * maxMsgSize + 1));
  248. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  249. if (fData.count() == 0)
  250. {
  251. *data = '\0';
  252. return data;
  253. }
  254. char* dataWrtn = data;
  255. int wrtn;
  256. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  257. {
  258. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  259. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  260. wrtn = std::snprintf(dataWrtn, maxTimeSize+6, "%u:%u:", rawMidiEvent->time, rawMidiEvent->size);
  261. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  262. dataWrtn += wrtn;
  263. wrtn = std::snprintf(dataWrtn, 5, "0x%02X", rawMidiEvent->data[0]);
  264. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  265. dataWrtn += wrtn;
  266. for (uint8_t i=1, size=rawMidiEvent->size; i<size; ++i)
  267. {
  268. wrtn = std::snprintf(dataWrtn, 5, ":%03u", rawMidiEvent->data[i]);
  269. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  270. dataWrtn += wrtn;
  271. }
  272. *dataWrtn++ = '\n';
  273. }
  274. *dataWrtn = '\0';
  275. return data;
  276. }
  277. void setState(const char* const data)
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  280. const size_t dataLen = std::strlen(data);
  281. const char* dataRead = data;
  282. const char* needle;
  283. RawMidiEvent midiEvent;
  284. char tmpBuf[24];
  285. ssize_t tmpSize;
  286. clear();
  287. const CarlaMutexLocker cmlr(fReadMutex);
  288. const CarlaMutexLocker cmlw(fWriteMutex);
  289. for (size_t dataPos=0; dataPos < dataLen && *dataRead != '\0';)
  290. {
  291. // get time
  292. needle = std::strchr(dataRead, ':');
  293. if (needle == nullptr)
  294. return;
  295. carla_zeroStruct(midiEvent);
  296. tmpSize = needle - dataRead;
  297. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0,);
  298. CARLA_SAFE_ASSERT_RETURN(tmpSize < 24,);
  299. {
  300. const size_t uSize = static_cast<size_t>(tmpSize);
  301. std::strncpy(tmpBuf, dataRead, uSize);
  302. tmpBuf[tmpSize] = '\0';
  303. dataRead += uSize+1U;
  304. dataPos += uSize+1U;
  305. }
  306. const long long time = std::atoll(tmpBuf);
  307. CARLA_SAFE_ASSERT_RETURN(time >= 0,);
  308. midiEvent.time = static_cast<uint32_t>(time);
  309. // get size
  310. needle = std::strchr(dataRead, ':');
  311. CARLA_SAFE_ASSERT_RETURN(needle != nullptr,);
  312. tmpSize = needle - dataRead;
  313. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0 && tmpSize < 24,);
  314. {
  315. const size_t uSize = static_cast<size_t>(tmpSize);
  316. std::strncpy(tmpBuf, dataRead, uSize);
  317. tmpBuf[tmpSize] = '\0';
  318. dataRead += uSize+1U;
  319. dataPos += uSize+1U;
  320. }
  321. const int midiDataSize = std::atoi(tmpBuf);
  322. CARLA_SAFE_ASSERT_RETURN(midiDataSize > 0 && midiDataSize <= MAX_EVENT_DATA_SIZE,);
  323. midiEvent.size = static_cast<uint8_t>(midiDataSize);
  324. // get events
  325. for (int i=0; i<midiDataSize; ++i)
  326. {
  327. CARLA_SAFE_ASSERT_RETURN(dataRead-data >= 4,);
  328. tmpSize = i==0 ? 4 : 3;
  329. const size_t uSize = static_cast<size_t>(tmpSize);
  330. std::strncpy(tmpBuf, dataRead, uSize);
  331. tmpBuf[tmpSize] = '\0';
  332. dataRead += uSize+1U;
  333. dataPos += uSize+1U;
  334. long mdata;
  335. if (i == 0)
  336. {
  337. mdata = std::strtol(tmpBuf, nullptr, 16);
  338. CARLA_SAFE_ASSERT_RETURN(mdata >= 0x80 && mdata <= 0xFF,);
  339. }
  340. else
  341. {
  342. mdata = std::atoi(tmpBuf);
  343. CARLA_SAFE_ASSERT_RETURN(mdata >= 0 && mdata < MAX_MIDI_VALUE,);
  344. }
  345. midiEvent.data[i] = static_cast<uint8_t>(mdata);
  346. }
  347. for (int i=midiDataSize; i<MAX_EVENT_DATA_SIZE; ++i)
  348. midiEvent.data[i] = 0;
  349. RawMidiEvent* const event(new RawMidiEvent());
  350. carla_copyStruct(*event, midiEvent);
  351. fData.append(event);
  352. }
  353. }
  354. // -------------------------------------------------------------------
  355. private:
  356. AbstractMidiPlayer* const kPlayer;
  357. uint8_t fMidiPort;
  358. uint32_t fStartTime;
  359. CarlaMutex fReadMutex;
  360. CarlaMutex fWriteMutex;
  361. LinkedList<const RawMidiEvent*> fData;
  362. void appendSorted(const RawMidiEvent* const event)
  363. {
  364. const CarlaMutexLocker cmlw(fWriteMutex);
  365. if (fData.isEmpty())
  366. {
  367. fData.append(event);
  368. return;
  369. }
  370. if (const RawMidiEvent* const lastEvent = fData.getLast(nullptr))
  371. {
  372. if (event->time >= lastEvent->time)
  373. {
  374. fData.append(event);
  375. return;
  376. }
  377. }
  378. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  379. {
  380. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  381. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  382. if (event->time >= oldEvent->time)
  383. continue;
  384. fData.insertAt(event, it);
  385. return;
  386. }
  387. fData.append(event);
  388. }
  389. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  390. };
  391. // -----------------------------------------------------------------------
  392. #endif // MIDI_BASE_HPP_INCLUDED