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.

midi-sequencer.cpp 12KB

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