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.

321 lines
10.0KB

  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. kParameterCount = 0
  27. };
  28. MidiPatternPlugin(const NativeHostDescriptor* const host)
  29. : NativePluginAndUiClass(host, "midipattern-ui"),
  30. fNeedsAllNotesOff(false),
  31. fWasPlayingBefore(false),
  32. fTicksPerFrame(0.0),
  33. fMidiOut(this),
  34. fTimeInfo()
  35. {
  36. carla_zeroStruct(fTimeInfo);
  37. }
  38. protected:
  39. // -------------------------------------------------------------------
  40. // Plugin process calls
  41. void process(float**, float**, const uint32_t frames, const NativeMidiEvent*, uint32_t) override
  42. {
  43. if (const NativeTimeInfo* const timeInfo = getTimeInfo())
  44. fTimeInfo = *timeInfo;
  45. if (fWasPlayingBefore != fTimeInfo.playing)
  46. {
  47. fNeedsAllNotesOff = true;
  48. fWasPlayingBefore = fTimeInfo.playing;
  49. }
  50. if (fNeedsAllNotesOff)
  51. {
  52. NativeMidiEvent midiEvent;
  53. midiEvent.port = 0;
  54. midiEvent.time = 0;
  55. midiEvent.data[0] = 0;
  56. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  57. midiEvent.data[2] = 0;
  58. midiEvent.data[3] = 0;
  59. midiEvent.size = 3;
  60. for (int channel=MAX_MIDI_CHANNELS; --channel >= 0;)
  61. {
  62. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  63. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  64. }
  65. fNeedsAllNotesOff = false;
  66. }
  67. if (fTimeInfo.playing)
  68. {
  69. if (! fTimeInfo.bbt.valid)
  70. fTimeInfo.bbt.beatsPerMinute = 120.0;
  71. fTicksPerFrame = 48.0 / (60.0 / fTimeInfo.bbt.beatsPerMinute * getSampleRate());
  72. fMidiOut.play(fTicksPerFrame*static_cast<long double>(fTimeInfo.frame),
  73. fTicksPerFrame*static_cast<double>(frames));
  74. }
  75. }
  76. // -------------------------------------------------------------------
  77. // Plugin UI calls
  78. void uiShow(const bool show) override
  79. {
  80. NativePluginAndUiClass::uiShow(show);
  81. if (show)
  82. _sendEventsToUI();
  83. }
  84. void uiIdle() override
  85. {
  86. NativePluginAndUiClass::uiIdle();
  87. // send transport
  88. if (isPipeRunning())
  89. {
  90. char strBuf[0xff+1];
  91. strBuf[0xff] = '\0';
  92. const float beatsPerBar = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerBar : 4.0f;
  93. const double beatsPerMinute = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatsPerMinute : 120.0;
  94. const float beatType = fTimeInfo.bbt.valid ? fTimeInfo.bbt.beatType : 4.0f;
  95. const double ticksPerBeat = 48.0;
  96. const double ticksPerFrame = ticksPerBeat / (60.0 / beatsPerMinute * getSampleRate());
  97. const double fullTicks = static_cast<double>(ticksPerFrame*static_cast<long double>(fTimeInfo.frame));
  98. const double fullBeats = fullTicks/ticksPerBeat;
  99. const uint32_t tick = static_cast<uint32_t>(std::floor(std::fmod(fullTicks, ticksPerBeat)));
  100. const uint32_t beat = static_cast<uint32_t>(std::floor(std::fmod(fullBeats, static_cast<double>(beatsPerBar))));
  101. const uint32_t bar = static_cast<uint32_t>(std::floor(fullBeats/beatsPerBar));
  102. const CarlaMutexLocker cml(getPipeLock());
  103. const ScopedLocale csl;
  104. writeAndFixMessage("transport");
  105. writeMessage(fTimeInfo.playing ? "true\n" : "false\n");
  106. std::sprintf(strBuf, P_UINT64 ":%i:%i:%i\n", fTimeInfo.frame, bar, beat, tick);
  107. writeMessage(strBuf);
  108. std::sprintf(strBuf, "%f:%f:%f\n", beatsPerMinute, beatsPerBar, beatType);
  109. writeMessage(strBuf);
  110. flushMessages();
  111. }
  112. }
  113. // -------------------------------------------------------------------
  114. // Plugin state calls
  115. char* getState() const override
  116. {
  117. return fMidiOut.getState();
  118. }
  119. void setState(const char* const data) override
  120. {
  121. fMidiOut.setState(data);
  122. if (isPipeRunning())
  123. _sendEventsToUI();
  124. }
  125. // -------------------------------------------------------------------
  126. // AbstractMidiPlayer calls
  127. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  128. {
  129. NativeMidiEvent midiEvent;
  130. midiEvent.port = port;
  131. midiEvent.time = uint32_t(timePosFrame/fTicksPerFrame);
  132. midiEvent.data[0] = event->data[0];
  133. midiEvent.data[1] = event->data[1];
  134. midiEvent.data[2] = event->data[2];
  135. midiEvent.data[3] = event->data[3];
  136. midiEvent.size = event->size;
  137. carla_stdout("Playing at %i :: %03X:%03i:%03i",
  138. midiEvent.time, midiEvent.data[0], midiEvent.data[1], midiEvent.data[2]);
  139. NativePluginAndUiClass::writeMidiEvent(&midiEvent);
  140. }
  141. // -------------------------------------------------------------------
  142. // Pipe Server calls
  143. bool msgReceived(const char* const msg) noexcept override
  144. {
  145. if (NativePluginAndUiClass::msgReceived(msg))
  146. return true;
  147. if (std::strcmp(msg, "midi-clear-all") == 0)
  148. {
  149. fMidiOut.clear();
  150. fNeedsAllNotesOff = true;
  151. return true;
  152. }
  153. if (std::strcmp(msg, "midievent-add") == 0)
  154. {
  155. uint64_t time;
  156. uint8_t size;
  157. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  158. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  159. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  160. uint8_t data[size], dvalue;
  161. for (uint8_t i=0; i<size; ++i)
  162. {
  163. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  164. data[i] = dvalue;
  165. }
  166. fMidiOut.addRaw(time, data, size);
  167. return true;
  168. }
  169. if (std::strcmp(msg, "midievent-remove") == 0)
  170. {
  171. uint64_t time;
  172. uint8_t size;
  173. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(time), true);
  174. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(size), true);
  175. CARLA_SAFE_ASSERT_RETURN(size > 0, true);
  176. uint8_t data[size], dvalue;
  177. for (uint8_t i=0; i<size; ++i)
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(dvalue), true);
  180. data[i] = dvalue;
  181. }
  182. fMidiOut.removeRaw(time, data, size);
  183. return true;
  184. }
  185. return false;
  186. }
  187. // -------------------------------------------------------------------
  188. private:
  189. bool fNeedsAllNotesOff;
  190. bool fWasPlayingBefore;
  191. double fTicksPerFrame;
  192. MidiPattern fMidiOut;
  193. NativeTimeInfo fTimeInfo;
  194. void _sendEventsToUI() const noexcept
  195. {
  196. char strBuf[0xff+1];
  197. strBuf[0xff] = '\0';
  198. const CarlaMutexLocker cml1(getPipeLock());
  199. const CarlaMutexLocker cml2(fMidiOut.getLock());
  200. writeMessage("midi-clear-all\n", 15);
  201. for (LinkedList<const RawMidiEvent*>::Itenerator it = fMidiOut.iteneratorBegin(); it.valid(); it.next())
  202. {
  203. const RawMidiEvent* const rawMidiEvent(it.getValue(nullptr));
  204. CARLA_SAFE_ASSERT_CONTINUE(rawMidiEvent != nullptr);
  205. writeMessage("midievent-add\n", 14);
  206. std::snprintf(strBuf, 0xff, P_INT64 "\n", rawMidiEvent->time);
  207. writeMessage(strBuf);
  208. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->size);
  209. writeMessage(strBuf);
  210. for (uint8_t i=0, size=rawMidiEvent->size; i<size; ++i)
  211. {
  212. std::snprintf(strBuf, 0xff, "%i\n", rawMidiEvent->data[i]);
  213. writeMessage(strBuf);
  214. }
  215. }
  216. }
  217. PluginClassEND(MidiPatternPlugin)
  218. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiPatternPlugin)
  219. };
  220. // -----------------------------------------------------------------------
  221. static const NativePluginDescriptor midipatternDesc = {
  222. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  223. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  224. |NATIVE_PLUGIN_HAS_UI
  225. |NATIVE_PLUGIN_USES_STATE
  226. |NATIVE_PLUGIN_USES_TIME),
  227. /* supports */ NATIVE_PLUGIN_SUPPORTS_EVERYTHING,
  228. /* audioIns */ 0,
  229. /* audioOuts */ 0,
  230. /* midiIns */ 0,
  231. /* midiOuts */ 1,
  232. /* paramIns */ MidiPatternPlugin::kParameterCount,
  233. /* paramOuts */ 0,
  234. /* name */ "MIDI Pattern",
  235. /* label */ "midipattern",
  236. /* maker */ "falkTX, tatch",
  237. /* copyright */ "GNU GPL v2+",
  238. PluginDescriptorFILL(MidiPatternPlugin)
  239. };
  240. // -----------------------------------------------------------------------
  241. CARLA_EXPORT
  242. void carla_register_native_plugin_midipattern();
  243. CARLA_EXPORT
  244. void carla_register_native_plugin_midipattern()
  245. {
  246. carla_register_native_plugin(&midipatternDesc);
  247. }
  248. // -----------------------------------------------------------------------