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.

547 lines
17KB

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