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 7.9KB

10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2014 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 "CarlaNative.hpp"
  18. #include "midi-base.hpp"
  19. class MidiSequencerPlugin : public NativePluginClass,
  20. public AbstractMidiPlayer
  21. {
  22. public:
  23. MidiSequencerPlugin(const NativeHostDescriptor* const host)
  24. : NativePluginClass(host),
  25. fWantInEvents(false),
  26. fMidiOut(this)
  27. {
  28. // TEST SONG (unsorted to test RtList API)
  29. uint32_t m = 44;
  30. fMidiOut.addControl(0*m, 0, 7, 99);
  31. fMidiOut.addControl(0*m, 0, 10, 63);
  32. fMidiOut.addProgram(0*m, 0, 0, 0);
  33. // 6912 On ch=1 n=60 v=90
  34. // 7237 Off ch=1 n=60 v=90
  35. // 7296 On ch=1 n=62 v=90
  36. // 7621 Off ch=1 n=62 v=90
  37. // 7680 On ch=1 n=64 v=90
  38. // 8005 Off ch=1 n=64 v=90
  39. fMidiOut.addNote(6912*m, 0, 60, 90, 325*m);
  40. fMidiOut.addNote(7680*m, 0, 64, 90, 325*m);
  41. fMidiOut.addNote(7296*m, 0, 62, 90, 325*m);
  42. // 1152 On ch=1 n=62 v=90
  43. // 1477 Off ch=1 n=62 v=90
  44. // 1536 On ch=1 n=64 v=90
  45. // 1861 Off ch=1 n=64 v=90
  46. // 1920 On ch=1 n=64 v=90
  47. // 2245 Off ch=1 n=64 v=90
  48. fMidiOut.addNote(1152*m, 0, 62, 90, 325*m);
  49. fMidiOut.addNote(1920*m, 0, 64, 90, 325*m);
  50. fMidiOut.addNote(1536*m, 0, 64, 90, 325*m);
  51. // 3840 On ch=1 n=62 v=90
  52. // 4491 Off ch=1 n=62 v=90
  53. // 4608 On ch=1 n=64 v=90
  54. // 4933 Off ch=1 n=64 v=90
  55. // 4992 On ch=1 n=67 v=90
  56. // 5317 Off ch=1 n=67 v=90
  57. fMidiOut.addNote(3840*m, 0, 62, 90, 650*m);
  58. fMidiOut.addNote(4992*m, 0, 67, 90, 325*m);
  59. fMidiOut.addNote(4608*m, 0, 64, 90, 325*m);
  60. // 0 On ch=1 n=64 v=90
  61. // 325 Off ch=1 n=64 v=90
  62. // 384 On ch=1 n=62 v=90
  63. // 709 Off ch=1 n=62 v=90
  64. // 768 On ch=1 n=60 v=90
  65. //1093 Off ch=1 n=60 v=90
  66. fMidiOut.addNote( 0*m, 0, 64, 90, 325*m);
  67. fMidiOut.addNote(768*m, 0, 60, 90, 325*m);
  68. fMidiOut.addNote(384*m, 0, 62, 90, 325*m);
  69. // 10752 On ch=1 n=60 v=90
  70. // 12056 Off ch=1 n=60 v=90
  71. fMidiOut.addNote(10752*m, 0, 60, 90, 650*m);
  72. // 5376 On ch=1 n=67 v=90
  73. // 6027 Off ch=1 n=67 v=90
  74. // 6144 On ch=1 n=64 v=90
  75. // 6469 Off ch=1 n=64 v=90
  76. // 6528 On ch=1 n=62 v=90
  77. // 6853 Off ch=1 n=62 v=90
  78. fMidiOut.addNote(5376*m, 0, 67, 90, 650*m);
  79. fMidiOut.addNote(6144*m, 0, 64, 90, 325*m);
  80. fMidiOut.addNote(6528*m, 0, 62, 90, 325*m);
  81. // 8064 On ch=1 n=64 v=90
  82. // 8389 Off ch=1 n=64 v=90
  83. // 8448 On ch=1 n=64 v=90
  84. // 9099 Off ch=1 n=64 v=90
  85. // 9216 On ch=1 n=62 v=90
  86. // 9541 Off ch=1 n=62 v=90
  87. fMidiOut.addNote(8064*m, 0, 64, 90, 325*m);
  88. fMidiOut.addNote(8448*m, 0, 64, 90, 650*m);
  89. fMidiOut.addNote(9216*m, 0, 62, 90, 325*m);
  90. // 9600 On ch=1 n=62 v=90
  91. // 9925 Off ch=1 n=62 v=90
  92. // 9984 On ch=1 n=64 v=90
  93. // 10309 Off ch=1 n=64 v=90
  94. // 10368 On ch=1 n=62 v=90
  95. // 10693 Off ch=1 n=62 v=90
  96. fMidiOut.addNote(9600*m, 0, 62, 90, 325*m);
  97. fMidiOut.addNote(9984*m, 0, 64, 90, 325*m);
  98. fMidiOut.addNote(10368*m, 0, 62, 90, 325*m);
  99. // 2304 On ch=1 n=64 v=90
  100. // 2955 Off ch=1 n=64 v=90
  101. // 3072 On ch=1 n=62 v=90
  102. // 3397 Off ch=1 n=62 v=90
  103. // 3456 On ch=1 n=62 v=90
  104. // 3781 Off ch=1 n=62 v=90
  105. fMidiOut.addNote(2304*m, 0, 64, 90, 650*m);
  106. fMidiOut.addNote(3072*m, 0, 62, 90, 325*m);
  107. fMidiOut.addNote(3456*m, 0, 62, 90, 325*m);
  108. }
  109. ~MidiSequencerPlugin() override
  110. {
  111. }
  112. protected:
  113. // -------------------------------------------------------------------
  114. // Plugin process calls
  115. void activate() override
  116. {
  117. }
  118. void deactivate() override
  119. {
  120. }
  121. void process(float**, float**, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  122. {
  123. const NativeTimeInfo* const timePos = getTimeInfo();
  124. if (fWantInEvents)
  125. {
  126. RawMidiEvent rawMidiEvent;
  127. for (uint32_t i=0; i < midiEventCount; ++i)
  128. {
  129. const NativeMidiEvent* const midiEvent = &midiEvents[i];
  130. rawMidiEvent.data[0] = midiEvent->data[0];
  131. rawMidiEvent.data[1] = midiEvent->data[1];
  132. rawMidiEvent.data[2] = midiEvent->data[2];
  133. rawMidiEvent.data[3] = midiEvent->data[3];
  134. rawMidiEvent.size = midiEvent->size;
  135. rawMidiEvent.time = timePos->frame + midiEvent->time;
  136. fInEvents.appendRT(rawMidiEvent);
  137. }
  138. if (fInEvents.mutex.tryLock())
  139. {
  140. fInEvents.splice();
  141. fInEvents.mutex.unlock();
  142. }
  143. }
  144. if (timePos->playing)
  145. fMidiOut.play(timePos->frame, frames);
  146. }
  147. // -------------------------------------------------------------------
  148. // Plugin process calls
  149. void writeMidiEvent(const uint32_t timePosFrame, const RawMidiEvent* const event) override
  150. {
  151. NativeMidiEvent midiEvent;
  152. midiEvent.port = 0;
  153. midiEvent.time = event->time-timePosFrame;
  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. NativePluginClass::writeMidiEvent(&midiEvent);
  160. }
  161. private:
  162. bool fWantInEvents;
  163. struct InRtEvents {
  164. CarlaMutex mutex;
  165. RtList<RawMidiEvent>::Pool dataPool;
  166. RtList<RawMidiEvent> data;
  167. RtList<RawMidiEvent> dataPendingRT;
  168. InRtEvents()
  169. : dataPool(MIN_PREALLOCATED_EVENT_COUNT, MAX_PREALLOCATED_EVENT_COUNT),
  170. data(dataPool),
  171. dataPendingRT(dataPool) {}
  172. ~InRtEvents()
  173. {
  174. clear();
  175. }
  176. void appendRT(const RawMidiEvent& event)
  177. {
  178. dataPendingRT.append(event);
  179. }
  180. void clear()
  181. {
  182. data.clear();
  183. dataPendingRT.clear();
  184. }
  185. void splice()
  186. {
  187. dataPendingRT.spliceAppend(data);
  188. }
  189. } fInEvents;
  190. MidiPattern fMidiOut;
  191. PluginClassEND(MidiSequencerPlugin)
  192. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiSequencerPlugin)
  193. };
  194. // -----------------------------------------------------------------------
  195. static const NativePluginDescriptor midisequencerDesc = {
  196. /* category */ PLUGIN_CATEGORY_UTILITY,
  197. /* hints */ PLUGIN_IS_RTSAFE/*|PLUGIN_HAS_GUI*/,
  198. /* supports */ static_cast<NativePluginSupports>(0x0),
  199. /* audioIns */ 0,
  200. /* audioOuts */ 0,
  201. /* midiIns */ 1,
  202. /* midiOuts */ 1,
  203. /* paramIns */ 0,
  204. /* paramOuts */ 0,
  205. /* name */ "MIDI Sequencer",
  206. /* label */ "midisequencer",
  207. /* maker */ "falkTX",
  208. /* copyright */ "GNU GPL v2+",
  209. PluginDescriptorFILL(MidiSequencerPlugin)
  210. };
  211. // -----------------------------------------------------------------------
  212. CARLA_EXPORT
  213. void carla_register_native_plugin_midisequencer();
  214. CARLA_EXPORT
  215. void carla_register_native_plugin_midisequencer()
  216. {
  217. carla_register_native_plugin(&midisequencerDesc);
  218. }
  219. // -----------------------------------------------------------------------