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 16KB

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
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 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*fParameters[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. else if (value > 2.5f)
  175. fTimeSigNum = 3;
  176. else if (value > 1.5f)
  177. fTimeSigNum = 2;
  178. else
  179. fTimeSigNum = 1;
  180. // fall through
  181. case kParameterMeasures:
  182. fMaxTicks = 48.0*fTimeSigNum*fParameters[kParameterMeasures] /2; // FIXME: why /2 ?
  183. break;
  184. }
  185. }
  186. // -------------------------------------------------------------------
  187. // Plugin process calls
  188. void process(float**, float**, const uint32_t frames, const NativeMidiEvent*, uint32_t) override
  189. {
  190. if (const NativeTimeInfo* const timeInfo = getTimeInfo())
  191. fTimeInfo = *timeInfo;
  192. if (fWasPlayingBefore != fTimeInfo.playing)
  193. {
  194. fNeedsAllNotesOff = true;
  195. fWasPlayingBefore = fTimeInfo.playing;
  196. }
  197. if (fNeedsAllNotesOff)
  198. {
  199. NativeMidiEvent midiEvent;
  200. midiEvent.port = 0;
  201. midiEvent.time = 0;
  202. midiEvent.data[0] = 0;
  203. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  204. midiEvent.data[2] = 0;
  205. midiEvent.data[3] = 0;
  206. midiEvent.size = 3;
  207. for (int channel=MAX_MIDI_CHANNELS; --channel >= 0;)
  208. {
  209. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  210. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  211. }
  212. fNeedsAllNotesOff = false;
  213. }
  214. if (fTimeInfo.playing)
  215. {
  216. if (! fTimeInfo.bbt.valid)
  217. fTimeInfo.bbt.beatsPerMinute = 120.0;
  218. fTicksPerFrame = 48.0 / (60.0 / fTimeInfo.bbt.beatsPerMinute * getSampleRate());
  219. /* */ double playPos = fTicksPerFrame*static_cast<double>(fTimeInfo.frame);
  220. const double endPos = playPos + fTicksPerFrame*static_cast<double>(frames);
  221. const double loopedEndPos = std::fmod(endPos, fMaxTicks);
  222. for (; playPos < endPos; playPos += fMaxTicks)
  223. {
  224. const double loopedPlayPos = std::fmod(playPos, fMaxTicks);
  225. if (loopedEndPos >= loopedPlayPos)
  226. {
  227. fMidiOut.play(loopedPlayPos, loopedEndPos-loopedPlayPos);
  228. }
  229. else
  230. {
  231. fMidiOut.play(loopedPlayPos, fMaxTicks-loopedPlayPos);
  232. fMidiOut.play(0.0, loopedEndPos);
  233. }
  234. }
  235. }
  236. }
  237. // -------------------------------------------------------------------
  238. // Plugin UI calls
  239. void uiShow(const bool show) override
  240. {
  241. NativePluginAndUiClass::uiShow(show);
  242. if (show)
  243. _sendEventsToUI();
  244. }
  245. void uiIdle() override
  246. {
  247. NativePluginAndUiClass::uiIdle();
  248. // send transport
  249. if (isPipeRunning())
  250. {
  251. char strBuf[0xff+1];
  252. strBuf[0xff] = '\0';
  253. const float beatsPerBar = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerBar : 4.0f;
  254. const double beatsPerMinute = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerMinute : 120.0;
  255. const float beatType = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatType : 4.0f;
  256. const double ticksPerBeat = 48.0;
  257. const double ticksPerFrame = ticksPerBeat / (60.0 / beatsPerMinute * getSampleRate());
  258. const double fullTicks = static_cast<double>(ticksPerFrame * static_cast<long double>(fTimeInfo.frame));
  259. const double fullBeats = fullTicks / ticksPerBeat;
  260. const uint32_t tick = static_cast<uint32_t>(std::floor(std::fmod(fullTicks, ticksPerBeat)));
  261. const uint32_t beat = static_cast<uint32_t>(std::floor(std::fmod(fullBeats, static_cast<double>(beatsPerBar))));
  262. const uint32_t bar = static_cast<uint32_t>(std::floor(fullBeats/beatsPerBar));
  263. const CarlaMutexLocker cml(getPipeLock());
  264. const ScopedLocale csl;
  265. if (! writeAndFixMessage("transport"))
  266. return;
  267. if (! writeMessage(fTimeInfo.playing ? "true\n" : "false\n"))
  268. return;
  269. std::sprintf(strBuf, P_UINT64 ":%i:%i:%i\n", fTimeInfo.frame, bar, beat, tick);
  270. if (! writeMessage(strBuf))
  271. return;
  272. std::sprintf(strBuf, "%f:%f:%f\n", beatsPerMinute, beatsPerBar, beatType);
  273. if (! writeMessage(strBuf))
  274. return;
  275. flushMessages();
  276. }
  277. }
  278. // -------------------------------------------------------------------
  279. // Plugin state calls
  280. char* getState() const override
  281. {
  282. return fMidiOut.getState();
  283. }
  284. void setState(const char* const data) override
  285. {
  286. fMidiOut.setState(data);
  287. if (isPipeRunning())
  288. _sendEventsToUI();
  289. }
  290. // -------------------------------------------------------------------
  291. // AbstractMidiPlayer calls
  292. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  293. {
  294. NativeMidiEvent midiEvent;
  295. midiEvent.port = port;
  296. midiEvent.time = uint32_t(timePosFrame/fTicksPerFrame);
  297. midiEvent.data[0] = event->data[0];
  298. midiEvent.data[1] = event->data[1];
  299. midiEvent.data[2] = event->data[2];
  300. midiEvent.data[3] = event->data[3];
  301. midiEvent.size = event->size;
  302. #ifdef DEBUG
  303. carla_stdout("Playing at %f :: %03X:%03i:%03i",
  304. float(double(midiEvent.time)*fTicksPerFrame),
  305. midiEvent.data[0], midiEvent.data[1], midiEvent.data[2]);
  306. #endif
  307. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  308. }
  309. // -------------------------------------------------------------------
  310. // Pipe Server calls
  311. bool msgReceived(const char* const msg) noexcept override
  312. {
  313. if (NativePluginAndUiClass::msgReceived(msg))
  314. return true;
  315. if (std::strcmp(msg, "midi-clear-all") == 0)
  316. {
  317. fMidiOut.clear();
  318. fNeedsAllNotesOff = true;
  319. return true;
  320. }
  321. if (std::strcmp(msg, "midievent-add") == 0)
  322. {
  323. uint64_t time;
  324. uint8_t size;
  325. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  326. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  327. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  328. uint8_t data[size], dvalue;
  329. for (uint8_t i=0; i<size; ++i)
  330. {
  331. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  332. data[i] = dvalue;
  333. }
  334. fMidiOut.addRaw(time, data, size);
  335. return true;
  336. }
  337. if (std::strcmp(msg, "midievent-remove") == 0)
  338. {
  339. uint64_t time;
  340. uint8_t size;
  341. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  342. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  343. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  344. uint8_t data[size], dvalue;
  345. for (uint8_t i=0; i<size; ++i)
  346. {
  347. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  348. data[i] = dvalue;
  349. }
  350. fMidiOut.removeRaw(time, data, size);
  351. return true;
  352. }
  353. return false;
  354. }
  355. // -------------------------------------------------------------------
  356. private:
  357. bool fNeedsAllNotesOff;
  358. bool fWasPlayingBefore;
  359. int fTimeSigNum;
  360. double fTicksPerFrame;
  361. double fMaxTicks;
  362. MidiPattern fMidiOut;
  363. NativeTimeInfo fTimeInfo;
  364. float fParameters[kParameterCount];
  365. void _sendEventsToUI() const noexcept
  366. {
  367. char strBuf[0xff+1];
  368. strBuf[0xff] = '\0';
  369. const CarlaMutexLocker cml1(getPipeLock());
  370. const CarlaMutexLocker cml2(fMidiOut.getLock());
  371. writeMessage("midi-clear-all\n", 15);
  372. for (LinkedList<const RawMidiEvent*>::Itenerator it = fMidiOut.iteneratorBegin(); it.valid(); it.next())
  373. {
  374. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  375. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  376. writeMessage("midievent-add\n", 14);
  377. std::snprintf(strBuf, 0xff, P_INT64 "\n", rawMidiEvent->time);
  378. writeMessage(strBuf);
  379. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->size);
  380. writeMessage(strBuf);
  381. for (uint8_t i=0, size=rawMidiEvent->size; i<size; ++i)
  382. {
  383. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->data[i]);
  384. writeMessage(strBuf);
  385. }
  386. }
  387. }
  388. PluginClassEND(MidiPatternPlugin)
  389. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPatternPlugin)
  390. };
  391. // -----------------------------------------------------------------------
  392. static const NativePluginDescriptor midipatternDesc = {
  393. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  394. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  395. |NATIVE_PLUGIN_HAS_UI
  396. |NATIVE_PLUGIN_USES_STATE
  397. |NATIVE_PLUGIN_USES_TIME),
  398. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  399. /* audioIns */ 0,
  400. /* audioOuts */ 0,
  401. /* midiIns */ 0,
  402. /* midiOuts */ 1,
  403. /* paramIns */ MidiPatternPlugin::kParameterCount,
  404. /* paramOuts */ 0,
  405. /* name */ "MIDI Pattern",
  406. /* label */ "midipattern",
  407. /* maker */ "falkTX, tatch",
  408. /* copyright */ "GNU GPL v2+",
  409. PluginDescriptorFILL(MidiPatternPlugin)
  410. };
  411. // -----------------------------------------------------------------------
  412. CARLA_EXPORT
  413. void carla_register_native_plugin_midipattern();
  414. CARLA_EXPORT
  415. void carla_register_native_plugin_midipattern()
  416. {
  417. carla_register_native_plugin(&midipatternDesc);
  418. }
  419. // -----------------------------------------------------------------------