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

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