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.

251 lines
8.0KB

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