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.

470 lines
15KB

  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 size_t dataLen = std::strlen(data);
  261. const char* dataRead = data;
  262. const char* needle;
  263. RawMidiEvent midiEvent;
  264. char tmpBuf[24];
  265. ssize_t tmpSize;
  266. clear();
  267. const CarlaMutexLocker sl(fMutex);
  268. for (size_t dataPos=0; dataPos < dataLen && *dataRead != '\0';)
  269. {
  270. // get time
  271. needle = std::strchr(dataRead, ':');
  272. if (needle == nullptr)
  273. return;
  274. carla_zeroStruct(midiEvent);
  275. tmpSize = needle - dataRead;
  276. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0,);
  277. CARLA_SAFE_ASSERT_RETURN(tmpSize < 24,);
  278. {
  279. const size_t uSize = static_cast<size_t>(tmpSize);
  280. std::strncpy(tmpBuf, dataRead, uSize);
  281. tmpBuf[tmpSize] = '\0';
  282. dataRead += uSize+1U;
  283. dataPos += uSize+1U;
  284. }
  285. const long long time = std::atoll(tmpBuf);
  286. CARLA_SAFE_ASSERT_RETURN(time >= 0,);
  287. midiEvent.time = static_cast<uint64_t>(time);
  288. // get size
  289. needle = std::strchr(dataRead, ':');
  290. CARLA_SAFE_ASSERT_RETURN(needle != nullptr,);
  291. tmpSize = needle - dataRead;
  292. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0 && tmpSize < 24,);
  293. {
  294. const size_t uSize = static_cast<size_t>(tmpSize);
  295. std::strncpy(tmpBuf, dataRead, uSize);
  296. tmpBuf[tmpSize] = '\0';
  297. dataRead += uSize+1U;
  298. dataPos += uSize+1U;
  299. }
  300. const int midiDataSize = std::atoi(tmpBuf);
  301. CARLA_SAFE_ASSERT_RETURN(midiDataSize > 0 && midiDataSize <= MAX_EVENT_DATA_SIZE,);
  302. midiEvent.size = static_cast<uint8_t>(midiDataSize);
  303. // get events
  304. for (int i=0; i<midiDataSize; ++i)
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(dataRead-data >= 4,);
  307. tmpSize = i==0 ? 4 : 3;
  308. const size_t uSize = static_cast<size_t>(tmpSize);
  309. std::strncpy(tmpBuf, dataRead, uSize);
  310. tmpBuf[tmpSize] = '\0';
  311. dataRead += uSize+1U;
  312. dataPos += uSize+1U;
  313. long mdata;
  314. if (i == 0)
  315. {
  316. mdata = std::strtol(tmpBuf, nullptr, 16);
  317. CARLA_SAFE_ASSERT_RETURN(mdata >= 0x80 && mdata <= 0xFF,);
  318. }
  319. else
  320. {
  321. mdata = std::atoi(tmpBuf);
  322. CARLA_SAFE_ASSERT_RETURN(mdata >= 0 && mdata < MAX_MIDI_VALUE,);
  323. }
  324. midiEvent.data[i] = static_cast<uint8_t>(mdata);
  325. }
  326. for (int i=midiDataSize; i<MAX_EVENT_DATA_SIZE; ++i)
  327. midiEvent.data[i] = 0;
  328. RawMidiEvent* const event(new RawMidiEvent());
  329. carla_copyStruct(*event, midiEvent);
  330. fData.append(event);
  331. }
  332. }
  333. // -------------------------------------------------------------------
  334. private:
  335. AbstractMidiPlayer* const kPlayer;
  336. uint8_t fMidiPort;
  337. uint64_t fStartTime;
  338. CarlaMutex fMutex;
  339. LinkedList<const RawMidiEvent*> fData;
  340. void appendSorted(const RawMidiEvent* const event)
  341. {
  342. const CarlaMutexLocker sl(fMutex);
  343. if (fData.isEmpty())
  344. {
  345. fData.append(event);
  346. return;
  347. }
  348. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  349. {
  350. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  351. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  352. if (event->time >= oldEvent->time)
  353. continue;
  354. fData.insertAt(event, it);
  355. return;
  356. }
  357. fData.append(event);
  358. }
  359. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  360. };
  361. // -----------------------------------------------------------------------
  362. #endif // MIDI_BASE_HPP_INCLUDED