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.

390 lines
12KB

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