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-sequencer.cpp 14KB

9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #include "CarlaNativeExtUI.hpp"
  18. #include "RtLinkedList.hpp"
  19. #include "midi-base.hpp"
  20. // -----------------------------------------------------------------------
  21. class MidiSequencerPlugin : public NativePluginAndUiClass,
  22. public AbstractMidiPlayer
  23. {
  24. public:
  25. MidiSequencerPlugin(const NativeHostDescriptor* const host)
  26. : NativePluginAndUiClass(host, CARLA_OS_SEP_STR "midiseq-ui"),
  27. fWantInEvents(false),
  28. fWasPlayingBefore(false),
  29. fInEvents(),
  30. fMidiOut(this),
  31. fTimeInfo(),
  32. leakDetector_MidiSequencerPlugin()
  33. {
  34. // TEST SONG (unsorted to test RtList API)
  35. uint32_t m = 44;
  36. fMidiOut.addControl(0*m, 0, 7, 99);
  37. fMidiOut.addControl(0*m, 0, 10, 63);
  38. fMidiOut.addProgram(0*m, 0, 0, 0);
  39. // 6912 On ch=1 n=60 v=90
  40. // 7237 Off ch=1 n=60 v=90
  41. // 7296 On ch=1 n=62 v=90
  42. // 7621 Off ch=1 n=62 v=90
  43. // 7680 On ch=1 n=64 v=90
  44. // 8005 Off ch=1 n=64 v=90
  45. fMidiOut.addNote(6912*m, 0, 60, 90, 325*m);
  46. fMidiOut.addNote(7680*m, 0, 64, 90, 325*m);
  47. fMidiOut.addNote(7296*m, 0, 62, 90, 325*m);
  48. // 1152 On ch=1 n=62 v=90
  49. // 1477 Off ch=1 n=62 v=90
  50. // 1536 On ch=1 n=64 v=90
  51. // 1861 Off ch=1 n=64 v=90
  52. // 1920 On ch=1 n=64 v=90
  53. // 2245 Off ch=1 n=64 v=90
  54. fMidiOut.addNote(1152*m, 0, 62, 90, 325*m);
  55. fMidiOut.addNote(1920*m, 0, 64, 90, 325*m);
  56. fMidiOut.addNote(1536*m, 0, 64, 90, 325*m);
  57. // 3840 On ch=1 n=62 v=90
  58. // 4491 Off ch=1 n=62 v=90
  59. // 4608 On ch=1 n=64 v=90
  60. // 4933 Off ch=1 n=64 v=90
  61. // 4992 On ch=1 n=67 v=90
  62. // 5317 Off ch=1 n=67 v=90
  63. fMidiOut.addNote(3840*m, 0, 62, 90, 650*m);
  64. fMidiOut.addNote(4992*m, 0, 67, 90, 325*m);
  65. fMidiOut.addNote(4608*m, 0, 64, 90, 325*m);
  66. // 0 On ch=1 n=64 v=90
  67. // 325 Off ch=1 n=64 v=90
  68. // 384 On ch=1 n=62 v=90
  69. // 709 Off ch=1 n=62 v=90
  70. // 768 On ch=1 n=60 v=90
  71. //1093 Off ch=1 n=60 v=90
  72. fMidiOut.addNote( 0*m, 0, 64, 90, 325*m);
  73. fMidiOut.addNote(768*m, 0, 60, 90, 325*m);
  74. fMidiOut.addNote(384*m, 0, 62, 90, 325*m);
  75. // 10752 On ch=1 n=60 v=90
  76. // 12056 Off ch=1 n=60 v=90
  77. fMidiOut.addNote(10752*m, 0, 60, 90, 650*m);
  78. // 5376 On ch=1 n=67 v=90
  79. // 6027 Off ch=1 n=67 v=90
  80. // 6144 On ch=1 n=64 v=90
  81. // 6469 Off ch=1 n=64 v=90
  82. // 6528 On ch=1 n=62 v=90
  83. // 6853 Off ch=1 n=62 v=90
  84. fMidiOut.addNote(5376*m, 0, 67, 90, 650*m);
  85. fMidiOut.addNote(6144*m, 0, 64, 90, 325*m);
  86. fMidiOut.addNote(6528*m, 0, 62, 90, 325*m);
  87. // 8064 On ch=1 n=64 v=90
  88. // 8389 Off ch=1 n=64 v=90
  89. // 8448 On ch=1 n=64 v=90
  90. // 9099 Off ch=1 n=64 v=90
  91. // 9216 On ch=1 n=62 v=90
  92. // 9541 Off ch=1 n=62 v=90
  93. fMidiOut.addNote(8064*m, 0, 64, 90, 325*m);
  94. fMidiOut.addNote(8448*m, 0, 64, 90, 650*m);
  95. fMidiOut.addNote(9216*m, 0, 62, 90, 325*m);
  96. // 9600 On ch=1 n=62 v=90
  97. // 9925 Off ch=1 n=62 v=90
  98. // 9984 On ch=1 n=64 v=90
  99. // 10309 Off ch=1 n=64 v=90
  100. // 10368 On ch=1 n=62 v=90
  101. // 10693 Off ch=1 n=62 v=90
  102. fMidiOut.addNote(9600*m, 0, 62, 90, 325*m);
  103. fMidiOut.addNote(9984*m, 0, 64, 90, 325*m);
  104. fMidiOut.addNote(10368*m, 0, 62, 90, 325*m);
  105. // 2304 On ch=1 n=64 v=90
  106. // 2955 Off ch=1 n=64 v=90
  107. // 3072 On ch=1 n=62 v=90
  108. // 3397 Off ch=1 n=62 v=90
  109. // 3456 On ch=1 n=62 v=90
  110. // 3781 Off ch=1 n=62 v=90
  111. fMidiOut.addNote(2304*m, 0, 64, 90, 650*m);
  112. fMidiOut.addNote(3072*m, 0, 62, 90, 325*m);
  113. fMidiOut.addNote(3456*m, 0, 62, 90, 325*m);
  114. carla_zeroStruct(fTimeInfo);
  115. }
  116. protected:
  117. // -------------------------------------------------------------------
  118. // Plugin process calls
  119. void process(float**, float**, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  120. {
  121. const NativeTimeInfo* const timePos = getTimeInfo();
  122. if (fWantInEvents)
  123. {
  124. if (midiEventCount > 0)
  125. {
  126. RawMidiEvent rawMidiEvent;
  127. for (uint32_t i=0; i < midiEventCount; ++i)
  128. {
  129. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  130. rawMidiEvent.data[0] = midiEvent->data[0];
  131. rawMidiEvent.data[1] = midiEvent->data[1];
  132. rawMidiEvent.data[2] = midiEvent->data[2];
  133. rawMidiEvent.data[3] = midiEvent->data[3];
  134. rawMidiEvent.size = midiEvent->size;
  135. rawMidiEvent.time = timePos->playing ? timePos->frame + midiEvent->time : 0;
  136. fInEvents.appendRT(rawMidiEvent);
  137. }
  138. }
  139. fInEvents.trySplice();
  140. }
  141. if (const NativeTimeInfo* const timeInfo = getTimeInfo())
  142. fTimeInfo = *timeInfo;
  143. if (timePos->playing)
  144. {
  145. fMidiOut.play(timePos->frame, frames);
  146. }
  147. else if (fWasPlayingBefore)
  148. {
  149. NativeMidiEvent midiEvent;
  150. midiEvent.port = 0;
  151. midiEvent.time = 0;
  152. midiEvent.data[0] = MIDI_STATUS_CONTROL_CHANGE;
  153. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  154. midiEvent.data[2] = 0;
  155. midiEvent.data[3] = 0;
  156. midiEvent.size = 3;
  157. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  158. {
  159. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE+i);
  160. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  161. }
  162. carla_stdout("WAS PLAYING BEFORE, NOW STOPPED");
  163. }
  164. fWasPlayingBefore = timePos->playing;
  165. }
  166. // -------------------------------------------------------------------
  167. // Plugin UI calls
  168. void uiShow(const bool show) override
  169. {
  170. NativePluginAndUiClass::uiShow(show);
  171. if (show)
  172. _sendEventsToUI();
  173. }
  174. void uiIdle() override
  175. {
  176. NativePluginAndUiClass::uiIdle();
  177. // send transport
  178. if (isPipeRunning())
  179. {
  180. char strBuf[0xff+1];
  181. strBuf[0xff] = '\0';
  182. const CarlaMutexLocker cml(getPipeLock());
  183. const ScopedLocale csl;
  184. writeAndFixMessage("transport");
  185. writeMessage(fTimeInfo.playing ? "true\n" : "false\n");
  186. if (fTimeInfo.bbt.valid)
  187. {
  188. std::sprintf(strBuf, P_UINT64 ":%i:%i:%i\n", fTimeInfo.frame, fTimeInfo.bbt.bar, fTimeInfo.bbt.beat, fTimeInfo.bbt.tick);
  189. writeMessage(strBuf);
  190. std::sprintf(strBuf, "%f:%f:%f\n", fTimeInfo.bbt.beatsPerMinute, fTimeInfo.bbt.beatsPerBar, fTimeInfo.bbt.beatType);
  191. writeMessage(strBuf);
  192. }
  193. else
  194. {
  195. std::sprintf(strBuf, P_UINT64 ":0:0:0\n", fTimeInfo.frame);
  196. writeMessage(strBuf);
  197. writeMessage("120.0:4.0:4.0\n");
  198. }
  199. flushMessages();
  200. }
  201. }
  202. // -------------------------------------------------------------------
  203. // Plugin state calls
  204. char* getState() const override
  205. {
  206. // TODO: malloc list of events
  207. return nullptr;
  208. }
  209. void setState(const char* const data) override
  210. {
  211. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  212. return;
  213. fMidiOut.clear();
  214. // TODO: set events according to data
  215. _sendEventsToUI();
  216. }
  217. // -------------------------------------------------------------------
  218. // AbstractMidiPlayer calls
  219. void writeMidiEvent(const uint8_t port, const uint64_t timePosFrame, const RawMidiEvent* const event) override
  220. {
  221. NativeMidiEvent midiEvent;
  222. midiEvent.port = port;
  223. midiEvent.time = uint32_t(event->time-timePosFrame);
  224. midiEvent.data[0] = event->data[0];
  225. midiEvent.data[1] = event->data[1];
  226. midiEvent.data[2] = event->data[2];
  227. midiEvent.data[3] = event->data[3];
  228. midiEvent.size = event->size;
  229. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  230. }
  231. // -------------------------------------------------------------------
  232. // Pipe Server calls
  233. bool msgReceived(const char* const msg) noexcept override
  234. {
  235. if (NativePluginAndUiClass::msgReceived(msg))
  236. return true;
  237. if (std::strcmp(msg, "midi-clear-all") == 0)
  238. {
  239. fMidiOut.clear();
  240. return true;
  241. }
  242. if (std::strcmp(msg, "midievent-add") == 0)
  243. {
  244. uint64_t time;
  245. uint8_t size;
  246. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  247. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  248. uint8_t data[size], dvalue;
  249. for (uint8_t i=0; i<size; ++i)
  250. {
  251. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  252. data[i] = dvalue;
  253. }
  254. fMidiOut.addRaw(time, data, size);
  255. return true;
  256. }
  257. if (std::strcmp(msg, "midievent-remove") == 0)
  258. {
  259. uint64_t time;
  260. uint8_t size;
  261. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  262. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  263. uint8_t data[size], dvalue;
  264. for (uint8_t i=0; i<size; ++i)
  265. {
  266. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  267. data[i] = dvalue;
  268. }
  269. fMidiOut.removeRaw(time, data, size);
  270. return true;
  271. }
  272. return false;
  273. }
  274. // -------------------------------------------------------------------
  275. private:
  276. bool fWantInEvents;
  277. bool fWasPlayingBefore;
  278. struct InRtEvents {
  279. CarlaMutex mutex;
  280. RtLinkedList<RawMidiEvent>::Pool dataPool;
  281. RtLinkedList<RawMidiEvent> data;
  282. RtLinkedList<RawMidiEvent> dataPendingRT;
  283. InRtEvents() noexcept
  284. : mutex(),
  285. dataPool(MIN_PREALLOCATED_EVENT_COUNT, MAX_PREALLOCATED_EVENT_COUNT),
  286. data(dataPool),
  287. dataPendingRT(dataPool) {}
  288. ~InRtEvents() noexcept
  289. {
  290. clear();
  291. }
  292. void appendRT(const RawMidiEvent& event) noexcept
  293. {
  294. dataPendingRT.append(event);
  295. }
  296. void clear() noexcept
  297. {
  298. mutex.lock();
  299. data.clear();
  300. dataPendingRT.clear();
  301. mutex.unlock();
  302. }
  303. void trySplice() noexcept
  304. {
  305. if (mutex.tryLock())
  306. {
  307. if (dataPendingRT.count() > 0)
  308. dataPendingRT.moveTo(data, true);
  309. mutex.unlock();
  310. }
  311. }
  312. CARLA_DECLARE_NON_COPY_STRUCT(InRtEvents);
  313. } fInEvents;
  314. MidiPattern fMidiOut;
  315. NativeTimeInfo fTimeInfo;
  316. void _sendEventsToUI() const noexcept
  317. {
  318. char strBuf[0xff+1];
  319. strBuf[0xff] = '\0';
  320. const CarlaMutexLocker cml1(getPipeLock());
  321. const CarlaMutexLocker cml2(fMidiOut.getLock());
  322. writeMessage("midi-clear-all\n", 15);
  323. for (LinkedList<const RawMidiEvent*>::Itenerator it = fMidiOut.iteneratorBegin(); it.valid(); it.next())
  324. {
  325. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  326. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  327. writeMessage("midievent-add\n", 14);
  328. std::snprintf(strBuf, 0xff, P_INT64 "\n", rawMidiEvent->time);
  329. writeMessage(strBuf);
  330. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->size);
  331. writeMessage(strBuf);
  332. for (uint8_t i=0, size=rawMidiEvent->size; i<size; ++i)
  333. {
  334. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->data[i]);
  335. writeMessage(strBuf);
  336. }
  337. }
  338. }
  339. PluginClassEND(MidiSequencerPlugin)
  340. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiSequencerPlugin)
  341. };
  342. // -----------------------------------------------------------------------
  343. static const NativePluginDescriptor midisequencerDesc = {
  344. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  345. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  346. |NATIVE_PLUGIN_HAS_UI
  347. |NATIVE_PLUGIN_USES_STATE
  348. |NATIVE_PLUGIN_USES_TIME),
  349. /* supports */ NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  350. /* audioIns */ 0,
  351. /* audioOuts */ 0,
  352. /* midiIns */ 1,
  353. /* midiOuts */ 1,
  354. /* paramIns */ 0,
  355. /* paramOuts */ 0,
  356. /* name */ "MIDI Sequencer",
  357. /* label */ "midisequencer",
  358. /* maker */ "falkTX, tatch",
  359. /* copyright */ "GNU GPL v2+",
  360. PluginDescriptorFILL(MidiSequencerPlugin)
  361. };
  362. // -----------------------------------------------------------------------
  363. CARLA_EXPORT
  364. void carla_register_native_plugin_midisequencer();
  365. CARLA_EXPORT
  366. void carla_register_native_plugin_midisequencer()
  367. {
  368. carla_register_native_plugin(&midisequencerDesc);
  369. }
  370. // -----------------------------------------------------------------------