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.

531 lines
17KB

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