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.

231 lines
7.3KB

  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 juce::File;
  102. using juce::FileInputStream;
  103. using juce::MidiFile;
  104. using juce::MidiMessage;
  105. using juce::MidiMessageSequence;
  106. using juce::ScopedPointer;
  107. File file(filename);
  108. if (! file.existsAsFile())
  109. return;
  110. FileInputStream fileStream(file);
  111. MidiFile midiFile;
  112. if (! midiFile.readFrom(fileStream))
  113. return;
  114. midiFile.convertTimestampTicksToSeconds();
  115. const double sampleRate(getSampleRate());
  116. for (int i=0, numTracks = midiFile.getNumTracks(); i<numTracks; ++i)
  117. {
  118. const MidiMessageSequence* const track(midiFile.getTrack(i));
  119. CARLA_SAFE_ASSERT_CONTINUE(track != nullptr);
  120. for (int j=0, numEvents = track->getNumEvents(); j<numEvents; ++j)
  121. {
  122. const MidiMessageSequence::MidiEventHolder* const midiEventHolder(track->getEventPointer(j));
  123. CARLA_SAFE_ASSERT_CONTINUE(midiEventHolder != nullptr);
  124. const MidiMessage& midiMessage(midiEventHolder->message);
  125. //const double time(track->getEventTime(i)*sampleRate);
  126. const int dataSize(midiMessage.getRawDataSize());
  127. if (dataSize <= 0 || dataSize > MAX_EVENT_DATA_SIZE)
  128. continue;
  129. if (midiMessage.isActiveSense())
  130. continue;
  131. if (midiMessage.isMetaEvent())
  132. continue;
  133. if (midiMessage.isMidiStart())
  134. continue;
  135. if (midiMessage.isMidiContinue())
  136. continue;
  137. if (midiMessage.isMidiStop())
  138. continue;
  139. if (midiMessage.isMidiClock())
  140. continue;
  141. if (midiMessage.isSongPositionPointer())
  142. continue;
  143. if (midiMessage.isQuarterFrame())
  144. continue;
  145. if (midiMessage.isFullFrame())
  146. continue;
  147. if (midiMessage.isMidiMachineControlMessage())
  148. continue;
  149. if (midiMessage.isSysEx())
  150. continue;
  151. const double time(midiMessage.getTimeStamp()*sampleRate);
  152. CARLA_SAFE_ASSERT_CONTINUE(time >= 0.0);
  153. fMidiOut.addRaw(static_cast<uint64_t>(time), midiMessage.getRawData(), static_cast<uint8_t>(dataSize));
  154. }
  155. }
  156. }
  157. PluginClassEND(MidiFilePlugin)
  158. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  159. };
  160. // -----------------------------------------------------------------------
  161. static const NativePluginDescriptor midifileDesc = {
  162. /* category */ PLUGIN_CATEGORY_UTILITY,
  163. /* hints */ static_cast<NativePluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_UI|PLUGIN_NEEDS_UI_OPEN_SAVE),
  164. /* supports */ static_cast<NativePluginSupports>(0x0),
  165. /* audioIns */ 0,
  166. /* audioOuts */ 0,
  167. /* midiIns */ 0,
  168. /* midiOuts */ 1,
  169. /* paramIns */ 0,
  170. /* paramOuts */ 0,
  171. /* name */ "MIDI File",
  172. /* label */ "midifile",
  173. /* maker */ "falkTX",
  174. /* copyright */ "GNU GPL v2+",
  175. PluginDescriptorFILL(MidiFilePlugin)
  176. };
  177. // -----------------------------------------------------------------------
  178. CARLA_EXPORT
  179. void carla_register_native_plugin_midifile();
  180. CARLA_EXPORT
  181. void carla_register_native_plugin_midifile()
  182. {
  183. carla_register_native_plugin(&midifileDesc);
  184. }
  185. // -----------------------------------------------------------------------