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.

274 lines
8.1KB

  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 int dataSize = midiMessage.getRawDataSize();
  166. if (dataSize <= 0 || dataSize > MAX_EVENT_DATA_SIZE)
  167. continue;
  168. const uint8_t* const data = midiMessage.getRawData();
  169. if (! MIDI_IS_CHANNEL_MESSAGE(data[0]))
  170. continue;
  171. const double time = midiMessage.getTimeStamp() * sampleRate;
  172. // const double time = track->getEventTime(i) * sampleRate;
  173. CARLA_SAFE_ASSERT_CONTINUE(time >= 0.0);
  174. fMidiOut.addRaw(static_cast<uint64_t>(time), midiMessage.getRawData(), static_cast<uint8_t>(dataSize));
  175. }
  176. }
  177. fNeedsAllNotesOff = true;
  178. }
  179. PluginClassEND(MidiFilePlugin)
  180. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  181. };
  182. // -----------------------------------------------------------------------
  183. static const NativePluginDescriptor midifileDesc = {
  184. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  185. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  186. |NATIVE_PLUGIN_HAS_UI
  187. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE
  188. #ifdef HAVE_PYQT
  189. |NATIVE_PLUGIN_REQUESTS_IDLE
  190. #endif
  191. |NATIVE_PLUGIN_USES_STATE
  192. |NATIVE_PLUGIN_USES_TIME),
  193. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  194. /* audioIns */ 0,
  195. /* audioOuts */ 0,
  196. /* midiIns */ 0,
  197. /* midiOuts */ 1,
  198. /* paramIns */ 0,
  199. /* paramOuts */ 0,
  200. /* name */ "MIDI File",
  201. /* label */ "midifile",
  202. /* maker */ "falkTX",
  203. /* copyright */ "GNU GPL v2+",
  204. PluginDescriptorFILL(MidiFilePlugin)
  205. };
  206. // -----------------------------------------------------------------------
  207. CARLA_EXPORT
  208. void carla_register_native_plugin_midifile();
  209. CARLA_EXPORT
  210. void carla_register_native_plugin_midifile()
  211. {
  212. carla_register_native_plugin(&midifileDesc);
  213. }
  214. // -----------------------------------------------------------------------
  215. #ifndef HAVE_PYQT
  216. # undef process2
  217. #endif