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.

458 lines
14KB

  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. };
  34. // -----------------------------------------------------------------------
  35. class AbstractMidiPlayer
  36. {
  37. public:
  38. virtual ~AbstractMidiPlayer() {}
  39. virtual void writeMidiEvent(const uint8_t port, const long 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. fMutex(),
  50. fData()
  51. {
  52. CARLA_SAFE_ASSERT(kPlayer != nullptr);
  53. }
  54. ~MidiPattern() noexcept
  55. {
  56. clear();
  57. }
  58. // -------------------------------------------------------------------
  59. // add data, time always counts from 0
  60. void addControl(const uint64_t time, const uint8_t channel, const uint8_t control, const uint8_t value)
  61. {
  62. RawMidiEvent* const ctrlEvent(new RawMidiEvent());
  63. ctrlEvent->time = time;
  64. ctrlEvent->size = 3;
  65. ctrlEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  66. ctrlEvent->data[1] = control;
  67. ctrlEvent->data[2] = value;
  68. appendSorted(ctrlEvent);
  69. }
  70. void addChannelPressure(const uint64_t time, const uint8_t channel, const uint8_t pressure)
  71. {
  72. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  73. pressureEvent->time = time;
  74. pressureEvent->size = 2;
  75. pressureEvent->data[0] = uint8_t(MIDI_STATUS_CHANNEL_PRESSURE | (channel & MIDI_CHANNEL_BIT));
  76. pressureEvent->data[1] = pressure;
  77. appendSorted(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* const noteOnEvent(new RawMidiEvent());
  87. noteOnEvent->time = time;
  88. noteOnEvent->size = 3;
  89. noteOnEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_ON | (channel & MIDI_CHANNEL_BIT));
  90. noteOnEvent->data[1] = pitch;
  91. noteOnEvent->data[2] = velocity;
  92. appendSorted(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* const noteOffEvent(new RawMidiEvent());
  97. noteOffEvent->time = time;
  98. noteOffEvent->size = 3;
  99. noteOffEvent->data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (channel & MIDI_CHANNEL_BIT));
  100. noteOffEvent->data[1] = pitch;
  101. noteOffEvent->data[2] = velocity;
  102. appendSorted(noteOffEvent);
  103. }
  104. void addNoteAftertouch(const uint64_t time, const uint8_t channel, const uint8_t pitch, const uint8_t pressure)
  105. {
  106. RawMidiEvent* const noteAfterEvent(new RawMidiEvent());
  107. noteAfterEvent->time = time;
  108. noteAfterEvent->size = 3;
  109. noteAfterEvent->data[0] = uint8_t(MIDI_STATUS_POLYPHONIC_AFTERTOUCH | (channel & MIDI_CHANNEL_BIT));
  110. noteAfterEvent->data[1] = pitch;
  111. noteAfterEvent->data[2] = pressure;
  112. appendSorted(noteAfterEvent);
  113. }
  114. void addProgram(const uint64_t time, const uint8_t channel, const uint8_t bank, const uint8_t program)
  115. {
  116. RawMidiEvent* const bankEvent(new RawMidiEvent());
  117. bankEvent->time = time;
  118. bankEvent->size = 3;
  119. bankEvent->data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  120. bankEvent->data[1] = MIDI_CONTROL_BANK_SELECT;
  121. bankEvent->data[2] = bank;
  122. RawMidiEvent* const programEvent(new RawMidiEvent());
  123. programEvent->time = time;
  124. programEvent->size = 2;
  125. programEvent->data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (channel & MIDI_CHANNEL_BIT));
  126. programEvent->data[1] = program;
  127. appendSorted(bankEvent);
  128. appendSorted(programEvent);
  129. }
  130. void addPitchbend(const uint64_t time, const uint8_t channel, const uint8_t lsb, const uint8_t msb)
  131. {
  132. RawMidiEvent* const pressureEvent(new RawMidiEvent());
  133. pressureEvent->time = time;
  134. pressureEvent->size = 3;
  135. pressureEvent->data[0] = uint8_t(MIDI_STATUS_PITCH_WHEEL_CONTROL | (channel & MIDI_CHANNEL_BIT));
  136. pressureEvent->data[1] = lsb;
  137. pressureEvent->data[2] = msb;
  138. appendSorted(pressureEvent);
  139. }
  140. void addRaw(const uint64_t time, const uint8_t* const data, const uint8_t size)
  141. {
  142. RawMidiEvent* const rawEvent(new RawMidiEvent());
  143. rawEvent->time = time;
  144. rawEvent->size = size;
  145. carla_copy<uint8_t>(rawEvent->data, data, size);
  146. appendSorted(rawEvent);
  147. }
  148. // -------------------------------------------------------------------
  149. // remove data
  150. void removeRaw(const uint64_t time, const uint8_t* const data, const uint8_t size)
  151. {
  152. const CarlaMutexLocker sl(fMutex);
  153. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  154. {
  155. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  156. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  157. if (rawMidiEvent->time != time)
  158. continue;
  159. if (rawMidiEvent->size != size)
  160. continue;
  161. if (std::memcmp(rawMidiEvent->data, data, size) != 0)
  162. continue;
  163. delete rawMidiEvent;
  164. fData.remove(it);
  165. return;
  166. }
  167. carla_stderr("MidiPattern::removeRaw(" P_INT64 ", %p, %i) - unable to find event to remove", time, data, size);
  168. }
  169. // -------------------------------------------------------------------
  170. // clear
  171. void clear() noexcept
  172. {
  173. const CarlaMutexLocker sl(fMutex);
  174. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  175. delete it.getValue(nullptr);
  176. fData.clear();
  177. }
  178. // -------------------------------------------------------------------
  179. // play on time
  180. void play(const uint64_t timePosFrame, const uint32_t frames)
  181. {
  182. play(static_cast<long double>(timePosFrame), static_cast<double>(frames));
  183. }
  184. void play(long double timePosFrame, const double frames)
  185. {
  186. if (! fMutex.tryLock())
  187. return;
  188. if (fStartTime != 0)
  189. timePosFrame += static_cast<long double>(fStartTime);
  190. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  191. {
  192. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  193. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  194. if (timePosFrame > rawMidiEvent->time)
  195. continue;
  196. if (timePosFrame + frames <= rawMidiEvent->time)
  197. continue;
  198. kPlayer->writeMidiEvent(fMidiPort, static_cast<long double>(rawMidiEvent->time)-timePosFrame, rawMidiEvent);
  199. }
  200. fMutex.unlock();
  201. }
  202. // -------------------------------------------------------------------
  203. // configure
  204. void setMidiPort(const uint8_t port) noexcept
  205. {
  206. fMidiPort = port;
  207. }
  208. void setStartTime(const uint64_t time) noexcept
  209. {
  210. fStartTime = time;
  211. }
  212. // -------------------------------------------------------------------
  213. // special
  214. const CarlaMutex& getLock() const noexcept
  215. {
  216. return fMutex;
  217. }
  218. LinkedList<const RawMidiEvent*>::Itenerator iteneratorBegin() const noexcept
  219. {
  220. return fData.begin2();
  221. }
  222. // -------------------------------------------------------------------
  223. // state
  224. char* getState() const
  225. {
  226. static const std::size_t maxTimeSize = 20; // std::strlen("18446744073709551615");
  227. static const std::size_t maxDataSize = 4 + 4*MAX_EVENT_DATA_SIZE; // std::strlen("0xFF:127:127:127");
  228. static const std::size_t maxMsgSize = maxTimeSize + 3 /* sep + size + sep */ + maxDataSize + 1 /* newline */;
  229. const CarlaMutexLocker sl(fMutex);
  230. if (fData.count() == 0)
  231. return nullptr;
  232. char* const data((char*)std::calloc(1, fData.count()*maxMsgSize));
  233. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  234. char* dataWrtn = data;
  235. int wrtn;
  236. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  237. {
  238. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  239. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  240. wrtn = std::snprintf(dataWrtn, maxTimeSize+4, P_INT64 ":%i:", rawMidiEvent->time, rawMidiEvent->size);
  241. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  242. dataWrtn += wrtn;
  243. wrtn = std::snprintf(dataWrtn, 5, "0x%02X", rawMidiEvent->data[0]);
  244. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  245. dataWrtn += wrtn;
  246. for (uint8_t i=1, size=rawMidiEvent->size; i<size; ++i)
  247. {
  248. wrtn = std::snprintf(dataWrtn, 5, ":%03u", rawMidiEvent->data[i]);
  249. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  250. dataWrtn += wrtn;
  251. }
  252. *dataWrtn++ = '\n';
  253. }
  254. *dataWrtn = '\0';
  255. return data;
  256. }
  257. void setState(const char* const data)
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  260. const char* dataRead = data;
  261. const char* needle;
  262. RawMidiEvent midiEvent;
  263. char tmpBuf[24];
  264. ssize_t tmpSize;
  265. clear();
  266. const CarlaMutexLocker sl(fMutex);
  267. for (; *dataRead != '\0';)
  268. {
  269. // get time
  270. needle = std::strchr(dataRead, ':');
  271. if (needle == nullptr)
  272. return;
  273. carla_zeroStruct(midiEvent);
  274. tmpSize = needle - dataRead;
  275. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0 && tmpSize < 24,);
  276. std::strncpy(tmpBuf, dataRead, static_cast<size_t>(tmpSize));
  277. tmpBuf[tmpSize] = '\0';
  278. dataRead += tmpSize+1;
  279. const long long time = std::atoll(tmpBuf);
  280. CARLA_SAFE_ASSERT_RETURN(time >= 0,);
  281. midiEvent.time = static_cast<uint64_t>(time);
  282. // get size
  283. needle = std::strchr(dataRead, ':');
  284. CARLA_SAFE_ASSERT_RETURN(needle != nullptr,);
  285. tmpSize = needle - dataRead;
  286. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0 && tmpSize < 24,);
  287. std::strncpy(tmpBuf, dataRead, static_cast<size_t>(tmpSize));
  288. tmpBuf[tmpSize] = '\0';
  289. dataRead += tmpSize+1;
  290. const int size = std::atoi(tmpBuf);
  291. CARLA_SAFE_ASSERT_RETURN(size > 0 && size <= MAX_EVENT_DATA_SIZE,);
  292. midiEvent.size = static_cast<uint8_t>(size);
  293. // get events
  294. for (int i=0; i<size; ++i)
  295. {
  296. CARLA_SAFE_ASSERT_RETURN(dataRead-data >= 4,);
  297. tmpSize = i==0 ? 4 : 3;
  298. std::strncpy(tmpBuf, dataRead, static_cast<size_t>(tmpSize));
  299. tmpBuf[tmpSize] = '\0';
  300. dataRead += tmpSize+1;
  301. long mdata;
  302. if (i == 0)
  303. {
  304. mdata = std::strtol(tmpBuf, nullptr, 16);
  305. CARLA_SAFE_ASSERT_RETURN(mdata >= 0x80 && mdata <= 0xFF,);
  306. }
  307. else
  308. {
  309. mdata = std::atoi(tmpBuf);
  310. CARLA_SAFE_ASSERT_RETURN(mdata >= 0 && mdata < MAX_MIDI_VALUE,);
  311. }
  312. midiEvent.data[i] = static_cast<uint8_t>(mdata);
  313. }
  314. for (int i=size; i<MAX_EVENT_DATA_SIZE; ++i)
  315. midiEvent.data[i] = 0;
  316. RawMidiEvent* const event(new RawMidiEvent());
  317. carla_copyStruct(*event, midiEvent);
  318. fData.append(event);
  319. }
  320. }
  321. // -------------------------------------------------------------------
  322. private:
  323. AbstractMidiPlayer* const kPlayer;
  324. uint8_t fMidiPort;
  325. uint64_t fStartTime;
  326. CarlaMutex fMutex;
  327. LinkedList<const RawMidiEvent*> fData;
  328. void appendSorted(const RawMidiEvent* const event)
  329. {
  330. const CarlaMutexLocker sl(fMutex);
  331. if (fData.isEmpty())
  332. {
  333. fData.append(event);
  334. return;
  335. }
  336. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  337. {
  338. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  339. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  340. if (event->time >= oldEvent->time)
  341. continue;
  342. fData.insertAt(event, it);
  343. return;
  344. }
  345. fData.append(event);
  346. }
  347. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  348. };
  349. // -----------------------------------------------------------------------
  350. #endif // MIDI_BASE_HPP_INCLUDED