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-pattern.cpp 17KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2019 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 MidiPatternPlugin : public NativePluginAndUiClass,
  22. public AbstractMidiPlayer
  23. {
  24. public:
  25. enum Parameters {
  26. kParameterTimeSig = 0,
  27. kParameterMeasures,
  28. kParameterDefLength,
  29. kParameterQuantize,
  30. kParameterCount
  31. };
  32. MidiPatternPlugin(const NativeHostDescriptor* const host)
  33. : NativePluginAndUiClass(host, "midipattern-ui"),
  34. fNeedsAllNotesOff(false),
  35. fWasPlayingBefore(false),
  36. fTimeSigNum(4),
  37. fTicksPerFrame(0.0),
  38. fMaxTicks(0.0),
  39. fMidiOut(this),
  40. fTimeInfo()
  41. {
  42. carla_zeroStruct(fTimeInfo);
  43. // set default param values
  44. fParameters[kParameterTimeSig] = 3.0f;
  45. fParameters[kParameterMeasures] = 4.0f;
  46. fParameters[kParameterDefLength] = 4.0f;
  47. fParameters[kParameterQuantize] = 4.0f;
  48. fMaxTicks = 48.0 * fTimeSigNum * 4 /* kParameterMeasures */ / 2; // FIXME: why / 2 ?
  49. }
  50. protected:
  51. // -------------------------------------------------------------------
  52. // Plugin parameter calls
  53. uint32_t getParameterCount() const override
  54. {
  55. return kParameterCount;
  56. }
  57. const NativeParameter* getParameterInfo(const uint32_t index) const override
  58. {
  59. CARLA_SAFE_ASSERT_RETURN(index < kParameterCount, nullptr);
  60. static NativeParameter param;
  61. static NativeParameterScalePoint scalePoints[10];
  62. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE|NATIVE_PARAMETER_IS_INTEGER;
  63. switch (index)
  64. {
  65. case 0:
  66. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  67. param.name = "Time Signature";
  68. param.ranges.def = 3.0f;
  69. param.ranges.min = 0.0f;
  70. param.ranges.max = 5.0f;
  71. scalePoints[0].value = 0.0f;
  72. scalePoints[0].label = "1/4";
  73. scalePoints[1].value = 1.0f;
  74. scalePoints[1].label = "2/4";
  75. scalePoints[2].value = 2.0f;
  76. scalePoints[2].label = "3/4";
  77. scalePoints[3].value = 3.0f;
  78. scalePoints[3].label = "4/4";
  79. scalePoints[4].value = 4.0f;
  80. scalePoints[4].label = "5/4";
  81. scalePoints[5].value = 5.0f;
  82. scalePoints[5].label = "6/4";
  83. param.scalePointCount = 6;
  84. param.scalePoints = scalePoints;
  85. break;
  86. case 1:
  87. param.name = "Measures";
  88. param.ranges.def = 4.0f;
  89. param.ranges.min = 1.0f;
  90. param.ranges.max = 16.0f;
  91. break;
  92. case 2:
  93. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  94. param.name = "Default Length";
  95. param.ranges.def = 4.0f;
  96. param.ranges.min = 0.0f;
  97. param.ranges.max = 9.0f;
  98. scalePoints[0].value = 0.0f;
  99. scalePoints[0].label = "1/16";
  100. scalePoints[1].value = 1.0f;
  101. scalePoints[1].label = "1/15";
  102. scalePoints[2].value = 2.0f;
  103. scalePoints[2].label = "1/12";
  104. scalePoints[3].value = 3.0f;
  105. scalePoints[3].label = "1/9";
  106. scalePoints[4].value = 4.0f;
  107. scalePoints[4].label = "1/8";
  108. scalePoints[5].value = 5.0f;
  109. scalePoints[5].label = "1/6";
  110. scalePoints[6].value = 6.0f;
  111. scalePoints[6].label = "1/4";
  112. scalePoints[7].value = 7.0f;
  113. scalePoints[7].label = "1/3";
  114. scalePoints[8].value = 8.0f;
  115. scalePoints[8].label = "1/2";
  116. scalePoints[9].value = 9.0f;
  117. scalePoints[9].label = "1";
  118. param.scalePointCount = 10;
  119. param.scalePoints = scalePoints;
  120. break;
  121. case 3:
  122. hints |= NATIVE_PARAMETER_USES_SCALEPOINTS;
  123. param.name = "Quantize";
  124. param.ranges.def = 4.0f;
  125. param.ranges.min = 0.0f;
  126. param.ranges.max = 9.0f;
  127. scalePoints[0].value = 0.0f;
  128. scalePoints[0].label = "1/16";
  129. scalePoints[1].value = 1.0f;
  130. scalePoints[1].label = "1/15";
  131. scalePoints[2].value = 2.0f;
  132. scalePoints[2].label = "1/12";
  133. scalePoints[3].value = 3.0f;
  134. scalePoints[3].label = "1/9";
  135. scalePoints[4].value = 4.0f;
  136. scalePoints[4].label = "1/8";
  137. scalePoints[5].value = 5.0f;
  138. scalePoints[5].label = "1/6";
  139. scalePoints[6].value = 6.0f;
  140. scalePoints[6].label = "1/4";
  141. scalePoints[7].value = 7.0f;
  142. scalePoints[7].label = "1/3";
  143. scalePoints[8].value = 8.0f;
  144. scalePoints[8].label = "1/2";
  145. scalePoints[9].value = 9.0f;
  146. scalePoints[9].label = "1";
  147. param.scalePointCount = 10;
  148. param.scalePoints = scalePoints;
  149. break;
  150. }
  151. param.hints = static_cast<NativeParameterHints>(hints);
  152. return &param;
  153. }
  154. float getParameterValue(const uint32_t index) const override
  155. {
  156. CARLA_SAFE_ASSERT_RETURN(index < kParameterCount, 0.0f);
  157. return fParameters[index];
  158. }
  159. // -------------------------------------------------------------------
  160. // Plugin state calls
  161. void setParameterValue(const uint32_t index, const float value) override
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(index < kParameterCount,);
  164. fParameters[index] = value;
  165. switch (index)
  166. {
  167. case kParameterTimeSig:
  168. /**/ if (value > 4.5f)
  169. fTimeSigNum = 6;
  170. else if (value > 3.5f)
  171. fTimeSigNum = 5;
  172. else if (value > 2.5f)
  173. fTimeSigNum = 4;
  174. /* FIXME
  175. else if (value > 2.5f)
  176. fTimeSigNum = 3;
  177. */
  178. else if (value > 1.5f)
  179. fTimeSigNum = 2;
  180. else
  181. fTimeSigNum = 1;
  182. // fall through
  183. case kParameterMeasures:
  184. fMaxTicks = 48.0 * fTimeSigNum * static_cast<double>(fParameters[kParameterMeasures]) /2; // FIXME: why /2 ?
  185. break;
  186. }
  187. }
  188. // -------------------------------------------------------------------
  189. // Plugin process calls
  190. void process(const float**, float**, const uint32_t frames, const NativeMidiEvent*, uint32_t) override
  191. {
  192. if (const NativeTimeInfo* const timeInfo = getTimeInfo())
  193. fTimeInfo = *timeInfo;
  194. if (fWasPlayingBefore != fTimeInfo.playing)
  195. {
  196. fNeedsAllNotesOff = true;
  197. fWasPlayingBefore = fTimeInfo.playing;
  198. }
  199. if (fNeedsAllNotesOff)
  200. {
  201. NativeMidiEvent midiEvent;
  202. midiEvent.port = 0;
  203. midiEvent.time = 0;
  204. midiEvent.data[0] = 0;
  205. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  206. midiEvent.data[2] = 0;
  207. midiEvent.data[3] = 0;
  208. midiEvent.size = 3;
  209. for (int channel=MAX_MIDI_CHANNELS; --channel >= 0;)
  210. {
  211. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  212. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  213. }
  214. fNeedsAllNotesOff = false;
  215. }
  216. if (fTimeInfo.playing)
  217. {
  218. if (! fTimeInfo.bbt.valid)
  219. fTimeInfo.bbt.beatsPerMinute = 120.0;
  220. fTicksPerFrame = 48.0 / (60.0 / fTimeInfo.bbt.beatsPerMinute * getSampleRate());
  221. /* */ double playPos = fTicksPerFrame*static_cast<double>(fTimeInfo.frame);
  222. const double endPos = playPos + fTicksPerFrame*static_cast<double>(frames);
  223. const double loopedEndPos = std::fmod(endPos, fMaxTicks);
  224. for (; playPos < endPos; playPos += fMaxTicks)
  225. {
  226. const double loopedPlayPos = std::fmod(playPos, fMaxTicks);
  227. if (loopedEndPos >= loopedPlayPos)
  228. {
  229. fMidiOut.play(loopedPlayPos, loopedEndPos-loopedPlayPos);
  230. }
  231. else
  232. {
  233. fMidiOut.play(loopedPlayPos, fMaxTicks-loopedPlayPos);
  234. fMidiOut.play(0.0, loopedEndPos);
  235. }
  236. }
  237. }
  238. }
  239. // -------------------------------------------------------------------
  240. // Plugin UI calls
  241. void uiShow(const bool show) override
  242. {
  243. NativePluginAndUiClass::uiShow(show);
  244. if (show)
  245. _sendEventsToUI();
  246. }
  247. void uiIdle() override
  248. {
  249. NativePluginAndUiClass::uiIdle();
  250. // send transport
  251. if (isPipeRunning())
  252. {
  253. char strBuf[0xff+1];
  254. strBuf[0xff] = '\0';
  255. const double beatsPerBar = fTimeInfo.bbt.valid ? static_cast<double>(fTimeInfo.bbt.beatsPerBar) : 4.0;
  256. const double beatsPerMinute = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerMinute : 120.0;
  257. const float beatType = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatType : 4.0f;
  258. const double ticksPerBeat = 48.0;
  259. const double ticksPerFrame = ticksPerBeat / (60.0 / beatsPerMinute * getSampleRate());
  260. const double fullTicks = static_cast<double>(ticksPerFrame * static_cast<long double>(fTimeInfo.frame));
  261. const double fullBeats = fullTicks / ticksPerBeat;
  262. const uint32_t tick = static_cast<uint32_t>(std::floor(std::fmod(fullTicks, ticksPerBeat)));
  263. const uint32_t beat = static_cast<uint32_t>(std::floor(std::fmod(fullBeats, beatsPerBar)));
  264. const uint32_t bar = static_cast<uint32_t>(std::floor(fullBeats/beatsPerBar));
  265. const CarlaMutexLocker cml(getPipeLock());
  266. if (! writeAndFixMessage("transport"))
  267. return;
  268. if (! writeMessage(fTimeInfo.playing ? "true\n" : "false\n"))
  269. return;
  270. std::sprintf(strBuf, P_UINT64 ":%i:%i:%i\n", fTimeInfo.frame, bar, beat, tick);
  271. if (! writeMessage(strBuf))
  272. return;
  273. {
  274. const CarlaScopedLocale csl;
  275. std::sprintf(strBuf, "%f:%f:%f\n",
  276. static_cast<double>(beatsPerMinute),
  277. static_cast<double>(beatsPerBar),
  278. static_cast<double>(beatType));
  279. }
  280. if (! writeMessage(strBuf))
  281. return;
  282. flushMessages();
  283. }
  284. }
  285. // -------------------------------------------------------------------
  286. // Plugin state calls
  287. char* getState() const override
  288. {
  289. return fMidiOut.getState();
  290. }
  291. void setState(const char* const data) override
  292. {
  293. fMidiOut.setState(data);
  294. if (isPipeRunning())
  295. _sendEventsToUI();
  296. }
  297. // -------------------------------------------------------------------
  298. // AbstractMidiPlayer calls
  299. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  300. {
  301. NativeMidiEvent midiEvent;
  302. midiEvent.port = port;
  303. midiEvent.time = uint32_t(timePosFrame/fTicksPerFrame);
  304. midiEvent.data[0] = event->data[0];
  305. midiEvent.data[1] = event->data[1];
  306. midiEvent.data[2] = event->data[2];
  307. midiEvent.data[3] = event->data[3];
  308. midiEvent.size = event->size;
  309. #ifdef DEBUG
  310. carla_stdout("Playing at %f :: %03X:%03i:%03i",
  311. static_cast<double>(midiEvent.time)*fTicksPerFrame,
  312. midiEvent.data[0], midiEvent.data[1], midiEvent.data[2]);
  313. #endif
  314. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  315. }
  316. // -------------------------------------------------------------------
  317. // Pipe Server calls
  318. bool msgReceived(const char* const msg) noexcept override
  319. {
  320. if (NativePluginAndUiClass::msgReceived(msg))
  321. return true;
  322. if (std::strcmp(msg, "midi-clear-all") == 0)
  323. {
  324. fMidiOut.clear();
  325. fNeedsAllNotesOff = true;
  326. return true;
  327. }
  328. if (std::strcmp(msg, "midievent-add") == 0)
  329. {
  330. uint64_t time;
  331. uint8_t size;
  332. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  333. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  334. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  335. uint8_t data[size], dvalue;
  336. for (uint8_t i=0; i<size; ++i)
  337. {
  338. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  339. data[i] = dvalue;
  340. }
  341. fMidiOut.addRaw(time, data, size);
  342. return true;
  343. }
  344. if (std::strcmp(msg, "midievent-remove") == 0)
  345. {
  346. uint64_t time;
  347. uint8_t size;
  348. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  349. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  350. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  351. uint8_t data[size], dvalue;
  352. for (uint8_t i=0; i<size; ++i)
  353. {
  354. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  355. data[i] = dvalue;
  356. }
  357. fMidiOut.removeRaw(time, data, size);
  358. return true;
  359. }
  360. return false;
  361. }
  362. // -------------------------------------------------------------------
  363. private:
  364. bool fNeedsAllNotesOff;
  365. bool fWasPlayingBefore;
  366. int fTimeSigNum;
  367. double fTicksPerFrame;
  368. double fMaxTicks;
  369. MidiPattern fMidiOut;
  370. NativeTimeInfo fTimeInfo;
  371. float fParameters[kParameterCount];
  372. void _sendEventsToUI() const noexcept
  373. {
  374. char strBuf[0xff+1];
  375. strBuf[0xff] = '\0';
  376. const CarlaMutexLocker cml1(getPipeLock());
  377. const CarlaMutexLocker cml2(fMidiOut.getLock());
  378. writeMessage("midi-clear-all\n", 15);
  379. for (LinkedList<const RawMidiEvent*>::Itenerator it = fMidiOut.iteneratorBegin(); it.valid(); it.next())
  380. {
  381. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  382. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  383. writeMessage("midievent-add\n", 14);
  384. std::snprintf(strBuf, 0xff, P_INT64 "\n", rawMidiEvent->time);
  385. writeMessage(strBuf);
  386. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->size);
  387. writeMessage(strBuf);
  388. for (uint8_t i=0, size=rawMidiEvent->size; i<size; ++i)
  389. {
  390. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->data[i]);
  391. writeMessage(strBuf);
  392. }
  393. }
  394. }
  395. PluginClassEND(MidiPatternPlugin)
  396. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPatternPlugin)
  397. };
  398. // -----------------------------------------------------------------------
  399. static const NativePluginDescriptor midipatternDesc = {
  400. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  401. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  402. |NATIVE_PLUGIN_HAS_UI
  403. |NATIVE_PLUGIN_USES_STATE
  404. |NATIVE_PLUGIN_USES_TIME),
  405. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  406. /* audioIns */ 0,
  407. /* audioOuts */ 0,
  408. /* midiIns */ 0,
  409. /* midiOuts */ 1,
  410. /* paramIns */ MidiPatternPlugin::kParameterCount,
  411. /* paramOuts */ 0,
  412. /* name */ "MIDI Pattern",
  413. /* label */ "midipattern",
  414. /* maker */ "falkTX, tatch",
  415. /* copyright */ "GNU GPL v2+",
  416. PluginDescriptorFILL(MidiPatternPlugin)
  417. };
  418. // -----------------------------------------------------------------------
  419. CARLA_EXPORT
  420. void carla_register_native_plugin_midipattern();
  421. CARLA_EXPORT
  422. void carla_register_native_plugin_midipattern()
  423. {
  424. carla_register_native_plugin(&midipatternDesc);
  425. }
  426. // -----------------------------------------------------------------------