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.

midi-file.cpp 7.9KB

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