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.

508 lines
16KB

  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 float beatsPerBar = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerBar : 4.0f;
  215. const double beatsPerMinute = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerMinute : 120.0;
  216. const float beatType = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatType : 4.0f;
  217. const double ticksPerBeat = 48.0;
  218. const double ticksPerFrame = ticksPerBeat / (60.0 / beatsPerMinute * getSampleRate());
  219. const long double fullTicks = ticksPerFrame*static_cast<long double>(fTimeInfo.frame);
  220. const double fullBeats = fullTicks/ticksPerBeat;
  221. const uint32_t tick = static_cast<uint32_t>(std::floor(std::fmod(fullTicks, ticksPerBeat)));
  222. const uint32_t beat = static_cast<uint32_t>(std::floor(std::fmod(fullBeats, beatsPerBar)));
  223. const uint32_t bar = static_cast<uint32_t>(std::floor(fullBeats/beatsPerBar));
  224. const CarlaMutexLocker cml(getPipeLock());
  225. const ScopedLocale csl;
  226. writeAndFixMessage("transport");
  227. writeMessage(fTimeInfo.playing ? "true\n" : "false\n");
  228. std::sprintf(strBuf, P_UINT64 ":%i:%i:%i\n", fTimeInfo.frame, bar, beat, tick);
  229. writeMessage(strBuf);
  230. std::sprintf(strBuf, "%f:%f:%f\n", beatsPerMinute, beatsPerBar, beatType);
  231. writeMessage(strBuf);
  232. flushMessages();
  233. }
  234. }
  235. // -------------------------------------------------------------------
  236. // Plugin state calls
  237. char* getState() const override
  238. {
  239. // TODO: malloc list of events
  240. return nullptr;
  241. }
  242. void setState(const char* const data) override
  243. {
  244. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  245. return;
  246. fMidiOut.clear();
  247. // TODO: set events according to data
  248. _sendEventsToUI();
  249. }
  250. // -------------------------------------------------------------------
  251. // AbstractMidiPlayer calls
  252. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  253. {
  254. NativeMidiEvent midiEvent;
  255. midiEvent.port = port;
  256. midiEvent.time = uint32_t(timePosFrame/fTicksPerFrame);
  257. midiEvent.data[0] = event->data[0];
  258. midiEvent.data[1] = event->data[1];
  259. midiEvent.data[2] = event->data[2];
  260. midiEvent.data[3] = event->data[3];
  261. midiEvent.size = event->size;
  262. carla_stdout("Playing at %i :: %03X:%03i:%03i",
  263. midiEvent.time, midiEvent.data[0], midiEvent.data[1], midiEvent.data[2]);
  264. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  265. }
  266. // -------------------------------------------------------------------
  267. // Pipe Server calls
  268. bool msgReceived(const char* const msg) noexcept override
  269. {
  270. if (NativePluginAndUiClass::msgReceived(msg))
  271. return true;
  272. if (std::strcmp(msg, "midi-clear-all") == 0)
  273. {
  274. fMidiOut.clear();
  275. return true;
  276. }
  277. if (std::strcmp(msg, "midievent-add") == 0)
  278. {
  279. uint64_t time;
  280. uint8_t size;
  281. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  282. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  283. uint8_t data[size], dvalue;
  284. for (uint8_t i=0; i<size; ++i)
  285. {
  286. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  287. data[i] = dvalue;
  288. }
  289. fMidiOut.addRaw(time, data, size);
  290. return true;
  291. }
  292. if (std::strcmp(msg, "midievent-remove") == 0)
  293. {
  294. uint64_t time;
  295. uint8_t size;
  296. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  297. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  298. uint8_t data[size], dvalue;
  299. for (uint8_t i=0; i<size; ++i)
  300. {
  301. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  302. data[i] = dvalue;
  303. }
  304. fMidiOut.removeRaw(time, data, size);
  305. return true;
  306. }
  307. return false;
  308. }
  309. // -------------------------------------------------------------------
  310. private:
  311. bool fWantInEvents;
  312. bool fWasPlayingBefore;
  313. double fTicksPerFrame;
  314. struct InRtEvents {
  315. CarlaMutex mutex;
  316. RtLinkedList<RawMidiEvent>::Pool dataPool;
  317. RtLinkedList<RawMidiEvent> data;
  318. RtLinkedList<RawMidiEvent> dataPendingRT;
  319. InRtEvents() noexcept
  320. : mutex(),
  321. dataPool(MIN_PREALLOCATED_EVENT_COUNT, MAX_PREALLOCATED_EVENT_COUNT),
  322. data(dataPool),
  323. dataPendingRT(dataPool) {}
  324. ~InRtEvents() noexcept
  325. {
  326. clear();
  327. }
  328. void appendRT(const RawMidiEvent& event) noexcept
  329. {
  330. dataPendingRT.append(event);
  331. }
  332. void clear() noexcept
  333. {
  334. mutex.lock();
  335. data.clear();
  336. dataPendingRT.clear();
  337. mutex.unlock();
  338. }
  339. void trySplice() noexcept
  340. {
  341. if (mutex.tryLock())
  342. {
  343. if (dataPendingRT.count() > 0)
  344. dataPendingRT.moveTo(data, true);
  345. mutex.unlock();
  346. }
  347. }
  348. CARLA_DECLARE_NON_COPY_STRUCT(InRtEvents);
  349. } fInEvents;
  350. MidiPattern fMidiOut;
  351. NativeTimeInfo fTimeInfo;
  352. void _sendEventsToUI() const noexcept
  353. {
  354. char strBuf[0xff+1];
  355. strBuf[0xff] = '\0';
  356. const CarlaMutexLocker cml1(getPipeLock());
  357. const CarlaMutexLocker cml2(fMidiOut.getLock());
  358. writeMessage("midi-clear-all\n", 15);
  359. for (LinkedList<const RawMidiEvent*>::Itenerator it = fMidiOut.iteneratorBegin(); it.valid(); it.next())
  360. {
  361. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  362. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  363. writeMessage("midievent-add\n", 14);
  364. std::snprintf(strBuf, 0xff, P_INT64 "\n", rawMidiEvent->time);
  365. writeMessage(strBuf);
  366. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->size);
  367. writeMessage(strBuf);
  368. for (uint8_t i=0, size=rawMidiEvent->size; i<size; ++i)
  369. {
  370. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->data[i]);
  371. writeMessage(strBuf);
  372. }
  373. }
  374. }
  375. PluginClassEND(MidiSequencerPlugin)
  376. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiSequencerPlugin)
  377. };
  378. // -----------------------------------------------------------------------
  379. static const NativePluginDescriptor midisequencerDesc = {
  380. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  381. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  382. |NATIVE_PLUGIN_HAS_UI
  383. |NATIVE_PLUGIN_USES_STATE
  384. |NATIVE_PLUGIN_USES_TIME),
  385. /* supports */ NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  386. /* audioIns */ 0,
  387. /* audioOuts */ 0,
  388. /* midiIns */ 1,
  389. /* midiOuts */ 1,
  390. /* paramIns */ MidiSequencerPlugin::kParameterCount,
  391. /* paramOuts */ 0,
  392. /* name */ "MIDI Sequencer",
  393. /* label */ "midisequencer",
  394. /* maker */ "falkTX, tatch",
  395. /* copyright */ "GNU GPL v2+",
  396. PluginDescriptorFILL(MidiSequencerPlugin)
  397. };
  398. // -----------------------------------------------------------------------
  399. CARLA_EXPORT
  400. void carla_register_native_plugin_midisequencer();
  401. CARLA_EXPORT
  402. void carla_register_native_plugin_midisequencer()
  403. {
  404. carla_register_native_plugin(&midisequencerDesc);
  405. }
  406. // -----------------------------------------------------------------------