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.

244 lines
7.8KB

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