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.

508 lines
16KB

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