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.

487 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. 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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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 uint64_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(" P_INT64 ", %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 uint64_t timePosFrame, const uint32_t frames)
  189. {
  190. return play(static_cast<long double>(timePosFrame), static_cast<double>(frames));
  191. }
  192. bool play(long double timePosFrame, const double frames, const double offset = 0.0)
  193. {
  194. long double ldtime;
  195. const CarlaMutexTryLocker cmtl(fReadMutex);
  196. if (cmtl.wasNotLocked())
  197. return false;
  198. if (fStartTime != 0)
  199. timePosFrame += static_cast<long 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<long double>(rawMidiEvent->time);
  205. if (ldtime < timePosFrame)
  206. continue;
  207. if (ldtime > timePosFrame + frames)
  208. break;
  209. kPlayer->writeMidiEvent(fMidiPort, ldtime + offset - timePosFrame, rawMidiEvent);
  210. }
  211. return true;
  212. }
  213. // -------------------------------------------------------------------
  214. // configure
  215. void setMidiPort(const uint8_t port) noexcept
  216. {
  217. fMidiPort = port;
  218. }
  219. void setStartTime(const uint64_t time) noexcept
  220. {
  221. fStartTime = time;
  222. }
  223. // -------------------------------------------------------------------
  224. // special
  225. const CarlaMutex& getWriteMutex() const noexcept
  226. {
  227. return fWriteMutex;
  228. }
  229. LinkedList<const RawMidiEvent*>::Itenerator iteneratorBegin() const noexcept
  230. {
  231. return fData.begin2();
  232. }
  233. // -------------------------------------------------------------------
  234. // state
  235. char* getState() const
  236. {
  237. static const std::size_t maxTimeSize = 20; // std::strlen("18446744073709551615");
  238. static const std::size_t maxDataSize = 4 + 4*MAX_EVENT_DATA_SIZE; // std::strlen("0xFF:127:127:127");
  239. static const std::size_t maxMsgSize = maxTimeSize + 3 /* sep + size + sep */ + maxDataSize + 1 /* newline */;
  240. const CarlaMutexLocker cmlw(fWriteMutex);
  241. if (fData.count() == 0)
  242. return nullptr;
  243. char* const data((char*)std::calloc(1, fData.count()*maxMsgSize+1));
  244. CARLA_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
  245. char* dataWrtn = data;
  246. int wrtn;
  247. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  248. {
  249. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  250. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  251. wrtn = std::snprintf(dataWrtn, maxTimeSize+6, P_UINT64 ":%u:", rawMidiEvent->time, rawMidiEvent->size);
  252. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  253. dataWrtn += wrtn;
  254. wrtn = std::snprintf(dataWrtn, 5, "0x%02X", rawMidiEvent->data[0]);
  255. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  256. dataWrtn += wrtn;
  257. for (uint8_t i=1, size=rawMidiEvent->size; i<size; ++i)
  258. {
  259. wrtn = std::snprintf(dataWrtn, 5, ":%03u", rawMidiEvent->data[i]);
  260. CARLA_SAFE_ASSERT_BREAK(wrtn > 0);
  261. dataWrtn += wrtn;
  262. }
  263. *dataWrtn++ = '\n';
  264. }
  265. *dataWrtn = '\0';
  266. return data;
  267. }
  268. void setState(const char* const data)
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  271. const size_t dataLen = std::strlen(data);
  272. const char* dataRead = data;
  273. const char* needle;
  274. RawMidiEvent midiEvent;
  275. char tmpBuf[24];
  276. ssize_t tmpSize;
  277. clear();
  278. const CarlaMutexLocker cmlr(fReadMutex);
  279. const CarlaMutexLocker cmlw(fWriteMutex);
  280. for (size_t dataPos=0; dataPos < dataLen && *dataRead != '\0';)
  281. {
  282. // get time
  283. needle = std::strchr(dataRead, ':');
  284. if (needle == nullptr)
  285. return;
  286. carla_zeroStruct(midiEvent);
  287. tmpSize = needle - dataRead;
  288. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0,);
  289. CARLA_SAFE_ASSERT_RETURN(tmpSize < 24,);
  290. {
  291. const size_t uSize = static_cast<size_t>(tmpSize);
  292. std::strncpy(tmpBuf, dataRead, uSize);
  293. tmpBuf[tmpSize] = '\0';
  294. dataRead += uSize+1U;
  295. dataPos += uSize+1U;
  296. }
  297. const long long time = std::atoll(tmpBuf);
  298. CARLA_SAFE_ASSERT_RETURN(time >= 0,);
  299. midiEvent.time = static_cast<uint64_t>(time);
  300. // get size
  301. needle = std::strchr(dataRead, ':');
  302. CARLA_SAFE_ASSERT_RETURN(needle != nullptr,);
  303. tmpSize = needle - dataRead;
  304. CARLA_SAFE_ASSERT_RETURN(tmpSize > 0 && tmpSize < 24,);
  305. {
  306. const size_t uSize = static_cast<size_t>(tmpSize);
  307. std::strncpy(tmpBuf, dataRead, uSize);
  308. tmpBuf[tmpSize] = '\0';
  309. dataRead += uSize+1U;
  310. dataPos += uSize+1U;
  311. }
  312. const int midiDataSize = std::atoi(tmpBuf);
  313. CARLA_SAFE_ASSERT_RETURN(midiDataSize > 0 && midiDataSize <= MAX_EVENT_DATA_SIZE,);
  314. midiEvent.size = static_cast<uint8_t>(midiDataSize);
  315. // get events
  316. for (int i=0; i<midiDataSize; ++i)
  317. {
  318. CARLA_SAFE_ASSERT_RETURN(dataRead-data >= 4,);
  319. tmpSize = i==0 ? 4 : 3;
  320. const size_t uSize = static_cast<size_t>(tmpSize);
  321. std::strncpy(tmpBuf, dataRead, uSize);
  322. tmpBuf[tmpSize] = '\0';
  323. dataRead += uSize+1U;
  324. dataPos += uSize+1U;
  325. long mdata;
  326. if (i == 0)
  327. {
  328. mdata = std::strtol(tmpBuf, nullptr, 16);
  329. CARLA_SAFE_ASSERT_RETURN(mdata >= 0x80 && mdata <= 0xFF,);
  330. }
  331. else
  332. {
  333. mdata = std::atoi(tmpBuf);
  334. CARLA_SAFE_ASSERT_RETURN(mdata >= 0 && mdata < MAX_MIDI_VALUE,);
  335. }
  336. midiEvent.data[i] = static_cast<uint8_t>(mdata);
  337. }
  338. for (int i=midiDataSize; i<MAX_EVENT_DATA_SIZE; ++i)
  339. midiEvent.data[i] = 0;
  340. RawMidiEvent* const event(new RawMidiEvent());
  341. carla_copyStruct(*event, midiEvent);
  342. fData.append(event);
  343. }
  344. }
  345. // -------------------------------------------------------------------
  346. private:
  347. AbstractMidiPlayer* const kPlayer;
  348. uint8_t fMidiPort;
  349. uint64_t fStartTime;
  350. CarlaMutex fReadMutex;
  351. CarlaMutex fWriteMutex;
  352. LinkedList<const RawMidiEvent*> fData;
  353. void appendSorted(const RawMidiEvent* const event)
  354. {
  355. const CarlaMutexLocker cmlw(fWriteMutex);
  356. if (fData.isEmpty())
  357. {
  358. fData.append(event);
  359. return;
  360. }
  361. for (LinkedList<const RawMidiEvent*>::Itenerator it = fData.begin2(); it.valid(); it.next())
  362. {
  363. const RawMidiEvent* const oldEvent(it.getValue(nullptr));
  364. CARLA_SAFE_ASSERT_CONTINUE(oldEvent != nullptr);
  365. if (event->time >= oldEvent->time)
  366. continue;
  367. fData.insertAt(event, it);
  368. return;
  369. }
  370. fData.append(event);
  371. }
  372. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPattern)
  373. };
  374. // -----------------------------------------------------------------------
  375. #endif // MIDI_BASE_HPP_INCLUDED