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.

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