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