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.3KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2014 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. {
  77. uiCustomDataChanged("file", filename);
  78. }
  79. uiClosed();
  80. }
  81. // -------------------------------------------------------------------
  82. // AbstractMidiPlayer calls
  83. void writeMidiEvent(const uint64_t timePosFrame, const RawMidiEvent* const event) override
  84. {
  85. NativeMidiEvent midiEvent;
  86. midiEvent.port = 0;
  87. midiEvent.time = uint32_t(event->time-timePosFrame);
  88. midiEvent.size = event->size;
  89. midiEvent.data[0] = event->data[0];
  90. midiEvent.data[1] = event->data[1];
  91. midiEvent.data[2] = event->data[2];
  92. midiEvent.data[3] = event->data[3];
  93. NativePluginClass::writeMidiEvent(&midiEvent);
  94. }
  95. private:
  96. MidiPattern fMidiOut;
  97. bool fWasPlayingBefore;
  98. void _loadMidiFile(const char* const filename)
  99. {
  100. fMidiOut.clear();
  101. using namespace juce;
  102. const String jfilename = String(CharPointer_UTF8(filename));
  103. File file(jfilename);
  104. if (! file.existsAsFile())
  105. return;
  106. FileInputStream fileStream(file);
  107. MidiFile midiFile;
  108. if (! midiFile.readFrom(fileStream))
  109. return;
  110. midiFile.convertTimestampTicksToSeconds();
  111. const double sampleRate(getSampleRate());
  112. for (int i=0, numTracks = midiFile.getNumTracks(); i<numTracks; ++i)
  113. {
  114. const MidiMessageSequence* const track(midiFile.getTrack(i));
  115. CARLA_SAFE_ASSERT_CONTINUE(track != nullptr);
  116. for (int j=0, numEvents = track->getNumEvents(); j<numEvents; ++j)
  117. {
  118. const MidiMessageSequence::MidiEventHolder* const midiEventHolder(track->getEventPointer(j));
  119. CARLA_SAFE_ASSERT_CONTINUE(midiEventHolder != nullptr);
  120. const MidiMessage& midiMessage(midiEventHolder->message);
  121. //const double time(track->getEventTime(i)*sampleRate);
  122. const int dataSize(midiMessage.getRawDataSize());
  123. if (dataSize <= 0 || dataSize > MAX_EVENT_DATA_SIZE)
  124. continue;
  125. if (midiMessage.isActiveSense())
  126. continue;
  127. if (midiMessage.isMetaEvent())
  128. continue;
  129. if (midiMessage.isMidiStart())
  130. continue;
  131. if (midiMessage.isMidiContinue())
  132. continue;
  133. if (midiMessage.isMidiStop())
  134. continue;
  135. if (midiMessage.isMidiClock())
  136. continue;
  137. if (midiMessage.isSongPositionPointer())
  138. continue;
  139. if (midiMessage.isQuarterFrame())
  140. continue;
  141. if (midiMessage.isFullFrame())
  142. continue;
  143. if (midiMessage.isMidiMachineControlMessage())
  144. continue;
  145. if (midiMessage.isSysEx())
  146. continue;
  147. const double time(midiMessage.getTimeStamp()*sampleRate);
  148. CARLA_SAFE_ASSERT_CONTINUE(time >= 0.0);
  149. fMidiOut.addRaw(static_cast<uint64_t>(time), midiMessage.getRawData(), static_cast<uint8_t>(dataSize));
  150. }
  151. }
  152. }
  153. PluginClassEND(MidiFilePlugin)
  154. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  155. };
  156. // -----------------------------------------------------------------------
  157. static const NativePluginDescriptor midifileDesc = {
  158. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  159. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  160. |NATIVE_PLUGIN_HAS_UI
  161. |NATIVE_PLUGIN_NEEDS_UI_OPEN_SAVE),
  162. /* supports */ static_cast<NativePluginSupports>(0x0),
  163. /* audioIns */ 0,
  164. /* audioOuts */ 0,
  165. /* midiIns */ 0,
  166. /* midiOuts */ 1,
  167. /* paramIns */ 0,
  168. /* paramOuts */ 0,
  169. /* name */ "MIDI File",
  170. /* label */ "midifile",
  171. /* maker */ "falkTX",
  172. /* copyright */ "GNU GPL v2+",
  173. PluginDescriptorFILL(MidiFilePlugin)
  174. };
  175. // -----------------------------------------------------------------------
  176. CARLA_EXPORT
  177. void carla_register_native_plugin_midifile();
  178. CARLA_EXPORT
  179. void carla_register_native_plugin_midifile()
  180. {
  181. carla_register_native_plugin(&midifileDesc);
  182. }
  183. // -----------------------------------------------------------------------