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.

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