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.

254 lines
7.8KB

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