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