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.

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