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.

419 lines
13KB

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