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