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.

534 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. 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. }
  247. // -------------------------------------------------------------------
  248. // Plugin UI calls
  249. void uiShow(const bool show) override
  250. {
  251. NativePluginAndUiClass::uiShow(show);
  252. if (show)
  253. _sendEventsToUI();
  254. }
  255. void uiIdle() override
  256. {
  257. NativePluginAndUiClass::uiIdle();
  258. // send transport
  259. if (isPipeRunning())
  260. {
  261. char strBuf[0xff+1];
  262. carla_zeroChars(strBuf, 0xff+1);
  263. const double beatsPerBar = fTimeSigNum;
  264. const double beatsPerMinute = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerMinute : 120.0;
  265. const double ticksPerBeat = TICKS_PER_BEAT;
  266. const double fullTicks = fLastPosition;
  267. const double fullBeats = fullTicks / ticksPerBeat;
  268. const uint32_t tick = static_cast<uint32_t>(std::floor(std::fmod(fullTicks, ticksPerBeat)));
  269. const uint32_t beat = static_cast<uint32_t>(std::floor(std::fmod(fullBeats, beatsPerBar)));
  270. const uint32_t bar = static_cast<uint32_t>(std::floor(fullBeats/beatsPerBar));
  271. const CarlaMutexLocker cml(getPipeLock());
  272. CARLA_SAFE_ASSERT_RETURN(writeMessage("transport\n"),);
  273. std::snprintf(strBuf, 0xff, "%i:" P_UINT64 ":%i:%i:%i\n", int(fTimeInfo.playing), fTimeInfo.frame, bar, beat, tick);
  274. CARLA_SAFE_ASSERT_RETURN(writeMessage(strBuf),);
  275. {
  276. const CarlaScopedLocale csl;
  277. std::snprintf(strBuf, 0xff, "%.12g\n", beatsPerMinute);
  278. }
  279. CARLA_SAFE_ASSERT_RETURN(writeMessage(strBuf),);
  280. flushMessages();
  281. }
  282. }
  283. // -------------------------------------------------------------------
  284. // Plugin state calls
  285. char* getState() const override
  286. {
  287. return fMidiOut.getState();
  288. }
  289. void setState(const char* const data) override
  290. {
  291. fMidiOut.setState(data);
  292. if (isPipeRunning())
  293. _sendEventsToUI();
  294. }
  295. // -------------------------------------------------------------------
  296. // AbstractMidiPlayer calls
  297. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  298. {
  299. NativeMidiEvent midiEvent;
  300. midiEvent.port = port;
  301. midiEvent.time = uint32_t(timePosFrame/fTicksPerFrame);
  302. midiEvent.data[0] = event->data[0];
  303. midiEvent.data[1] = event->data[1];
  304. midiEvent.data[2] = event->data[2];
  305. midiEvent.data[3] = event->data[3];
  306. midiEvent.size = event->size;
  307. #ifdef DEBUG
  308. carla_stdout("Playing at %f|%u :: %03X:%03i:%03i",
  309. midiEvent.time,
  310. static_cast<double>(midiEvent.time)*fTicksPerFrame,
  311. midiEvent.data[0], midiEvent.data[1], midiEvent.data[2]);
  312. #endif
  313. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  314. }
  315. // -------------------------------------------------------------------
  316. // Pipe Server calls
  317. bool msgReceived(const char* const msg) noexcept override
  318. {
  319. if (NativePluginAndUiClass::msgReceived(msg))
  320. return true;
  321. if (std::strcmp(msg, "midi-clear-all") == 0)
  322. {
  323. fMidiOut.clear();
  324. fNeedsAllNotesOff = true;
  325. return true;
  326. }
  327. if (std::strcmp(msg, "midievent-add") == 0)
  328. {
  329. uint64_t time;
  330. uint8_t size;
  331. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  332. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  333. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  334. uint8_t data[size], dvalue;
  335. for (uint8_t i=0; i<size; ++i)
  336. {
  337. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  338. data[i] = dvalue;
  339. }
  340. fMidiOut.addRaw(time /* * TICKS_PER_BEAT */, data, size);
  341. return true;
  342. }
  343. if (std::strcmp(msg, "midievent-remove") == 0)
  344. {
  345. uint64_t time;
  346. uint8_t size;
  347. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  348. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  349. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  350. uint8_t data[size], dvalue;
  351. for (uint8_t i=0; i<size; ++i)
  352. {
  353. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  354. data[i] = dvalue;
  355. }
  356. fMidiOut.removeRaw(time /* * TICKS_PER_BEAT */, data, size);
  357. return true;
  358. }
  359. return false;
  360. }
  361. // -------------------------------------------------------------------
  362. private:
  363. bool fNeedsAllNotesOff;
  364. bool fWasPlayingBefore;
  365. int fTimeSigNum;
  366. float fLastPosition;
  367. uint64_t fLastFrame;
  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.getWriteMutex());
  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. // -----------------------------------------------------------------------