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

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