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.

262 lines
8.3KB

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