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-file.cpp 8.8KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2019 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 "CarlaNativePrograms.hpp"
  18. #include "midi-base.hpp"
  19. #include "water/files/FileInputStream.h"
  20. #include "water/midi/MidiFile.h"
  21. #ifndef HAVE_PYQT
  22. # define process2 process
  23. #endif
  24. // -----------------------------------------------------------------------
  25. #ifdef HAVE_PYQT
  26. class MidiFilePlugin : public NativePluginWithMidiPrograms<FileMIDI>,
  27. #else
  28. class MidiFilePlugin : public NativePluginClass,
  29. #endif
  30. public AbstractMidiPlayer
  31. {
  32. public:
  33. MidiFilePlugin(const NativeHostDescriptor* const host)
  34. #ifdef HAVE_PYQT
  35. : NativePluginWithMidiPrograms<FileMIDI>(host, fPrograms, 0),
  36. #else
  37. : NativePluginClass(host),
  38. #endif
  39. fMidiOut(this),
  40. fNeedsAllNotesOff(false),
  41. fWasPlayingBefore(false)
  42. #ifdef HAVE_PYQT
  43. , fPrograms(hostGetFilePath("midi"), "*.mid;*.midi")
  44. #endif
  45. {
  46. }
  47. protected:
  48. // -------------------------------------------------------------------
  49. // Plugin state calls
  50. void setCustomData(const char* const key, const char* const value) override
  51. {
  52. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  53. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  54. if (std::strcmp(key, "file") != 0)
  55. return;
  56. #ifdef HAVE_PYQT
  57. invalidateNextFilename();
  58. #endif
  59. _loadMidiFile(value);
  60. }
  61. // -------------------------------------------------------------------
  62. // Plugin process calls
  63. void process2(const float* const*, float**, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  64. {
  65. const NativeTimeInfo* const timePos(getTimeInfo());
  66. if (timePos == nullptr)
  67. return;
  68. if (fWasPlayingBefore != timePos->playing)
  69. {
  70. fNeedsAllNotesOff = true;
  71. fWasPlayingBefore = timePos->playing;
  72. }
  73. if (fNeedsAllNotesOff)
  74. {
  75. NativeMidiEvent midiEvent;
  76. midiEvent.port = 0;
  77. midiEvent.time = 0;
  78. midiEvent.data[0] = 0;
  79. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  80. midiEvent.data[2] = 0;
  81. midiEvent.data[3] = 0;
  82. midiEvent.size = 3;
  83. for (int channel=MAX_MIDI_CHANNELS; --channel >= 0;)
  84. {
  85. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (channel & MIDI_CHANNEL_BIT));
  86. NativePluginClass::writeMidiEvent(&midiEvent);
  87. }
  88. fNeedsAllNotesOff = false;
  89. }
  90. if (fWasPlayingBefore)
  91. if (! fMidiOut.play(timePos->frame, frames))
  92. fNeedsAllNotesOff = true;
  93. }
  94. // -------------------------------------------------------------------
  95. // Plugin UI calls
  96. void uiShow(const bool show) override
  97. {
  98. if (! show)
  99. return;
  100. if (const char* const filename = uiOpenFile(false, "Open MIDI File", "MIDI Files (*.mid *.midi);;"))
  101. uiCustomDataChanged("file", filename);
  102. uiClosed();
  103. }
  104. // -------------------------------------------------------------------
  105. // Plugin state calls
  106. char* getState() const override
  107. {
  108. return fMidiOut.getState();
  109. }
  110. void setState(const char* const data) override
  111. {
  112. fMidiOut.setState(data);
  113. }
  114. #ifdef HAVE_PYQT
  115. void setStateFromFile(const char* const filename) override
  116. {
  117. _loadMidiFile(filename);
  118. }
  119. #endif
  120. // -------------------------------------------------------------------
  121. // AbstractMidiPlayer calls
  122. void writeMidiEvent(const uint8_t port, const long double timePosFrame, const RawMidiEvent* const event) override
  123. {
  124. NativeMidiEvent midiEvent;
  125. midiEvent.port = port;
  126. midiEvent.time = uint32_t(timePosFrame);
  127. midiEvent.size = event->size;
  128. midiEvent.data[0] = event->data[0];
  129. midiEvent.data[1] = event->data[1];
  130. midiEvent.data[2] = event->data[2];
  131. midiEvent.data[3] = event->data[3];
  132. NativePluginClass::writeMidiEvent(&midiEvent);
  133. }
  134. // -------------------------------------------------------------------
  135. private:
  136. MidiPattern fMidiOut;
  137. bool fNeedsAllNotesOff;
  138. bool fWasPlayingBefore;
  139. #ifdef HAVE_PYQT
  140. NativeMidiPrograms fPrograms;
  141. #endif
  142. void _loadMidiFile(const char* const filename)
  143. {
  144. fMidiOut.clear();
  145. using namespace water;
  146. const String jfilename = String(CharPointer_UTF8(filename));
  147. File file(jfilename);
  148. if (! file.existsAsFile())
  149. return;
  150. FileInputStream fileStream(file);
  151. MidiFile midiFile;
  152. if (! midiFile.readFrom(fileStream))
  153. return;
  154. midiFile.convertTimestampTicksToSeconds();
  155. const double sampleRate(getSampleRate());
  156. for (size_t i=0, numTracks = midiFile.getNumTracks(); i<numTracks; ++i)
  157. {
  158. const MidiMessageSequence* const track(midiFile.getTrack(i));
  159. CARLA_SAFE_ASSERT_CONTINUE(track != nullptr);
  160. for (int j=0, numEvents = track->getNumEvents(); j<numEvents; ++j)
  161. {
  162. const MidiMessageSequence::MidiEventHolder* const midiEventHolder(track->getEventPointer(j));
  163. CARLA_SAFE_ASSERT_CONTINUE(midiEventHolder != nullptr);
  164. const MidiMessage& midiMessage(midiEventHolder->message);
  165. //const double time(track->getEventTime(i)*sampleRate);
  166. const int dataSize(midiMessage.getRawDataSize());
  167. if (dataSize <= 0 || dataSize > MAX_EVENT_DATA_SIZE)
  168. continue;
  169. if (midiMessage.isActiveSense())
  170. continue;
  171. if (midiMessage.isMetaEvent())
  172. continue;
  173. if (midiMessage.isMidiStart())
  174. continue;
  175. if (midiMessage.isMidiContinue())
  176. continue;
  177. if (midiMessage.isMidiStop())
  178. continue;
  179. if (midiMessage.isMidiClock())
  180. continue;
  181. if (midiMessage.isSongPositionPointer())
  182. continue;
  183. if (midiMessage.isQuarterFrame())
  184. continue;
  185. if (midiMessage.isFullFrame())
  186. continue;
  187. if (midiMessage.isMidiMachineControlMessage())
  188. continue;
  189. if (midiMessage.isSysEx())
  190. continue;
  191. const double time(midiMessage.getTimeStamp()*sampleRate);
  192. CARLA_SAFE_ASSERT_CONTINUE(time >= 0.0);
  193. fMidiOut.addRaw(static_cast<uint64_t>(time), midiMessage.getRawData(), static_cast<uint8_t>(dataSize));
  194. }
  195. }
  196. fNeedsAllNotesOff = true;
  197. }
  198. PluginClassEND(MidiFilePlugin)
  199. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  200. };
  201. // -----------------------------------------------------------------------
  202. static const NativePluginDescriptor midifileDesc = {
  203. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  204. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  205. |NATIVE_PLUGIN_HAS_UI
  206. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  207. #ifdef HAVE_PYQT
  208. |NATIVE_PLUGIN_REQUESTS_IDLE
  209. #endif
  210. |NATIVE_PLUGIN_USES_STATE
  211. |NATIVE_PLUGIN_USES_TIME),
  212. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  213. /* audioIns */ 0,
  214. /* audioOuts */ 0,
  215. /* midiIns */ 0,
  216. /* midiOuts */ 1,
  217. /* paramIns */ 0,
  218. /* paramOuts */ 0,
  219. /* name */ "MIDI File",
  220. /* label */ "midifile",
  221. /* maker */ "falkTX",
  222. /* copyright */ "GNU GPL v2+",
  223. PluginDescriptorFILL(MidiFilePlugin)
  224. };
  225. // -----------------------------------------------------------------------
  226. CARLA_EXPORT
  227. void carla_register_native_plugin_midifile();
  228. CARLA_EXPORT
  229. void carla_register_native_plugin_midifile()
  230. {
  231. carla_register_native_plugin(&midifileDesc);
  232. }
  233. // -----------------------------------------------------------------------
  234. #ifndef HAVE_PYQT
  235. # undef process2
  236. #endif