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.

226 lines
7.1KB

  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. protected:
  30. // -------------------------------------------------------------------
  31. // Plugin state calls
  32. void setCustomData(const char* const key, const char* const value) override
  33. {
  34. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  35. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  36. if (std::strcmp(key, "file") != 0)
  37. return;
  38. _loadMidiFile(value);
  39. }
  40. // -------------------------------------------------------------------
  41. // Plugin process calls
  42. void process(float**, float**, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  43. {
  44. const NativeTimeInfo* const timePos(getTimeInfo());
  45. if (timePos->playing)
  46. {
  47. fMidiOut.play(timePos->frame, frames);
  48. }
  49. else if (fWasPlayingBefore)
  50. {
  51. NativeMidiEvent midiEvent;
  52. midiEvent.port = 0;
  53. midiEvent.time = 0;
  54. midiEvent.data[0] = MIDI_STATUS_CONTROL_CHANGE;
  55. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  56. midiEvent.data[2] = 0;
  57. midiEvent.data[3] = 0;
  58. midiEvent.size = 3;
  59. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  60. {
  61. midiEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE+i);
  62. NativePluginClass::writeMidiEvent(&midiEvent);
  63. }
  64. carla_stdout("WAS PLAYING BEFORE, NOW STOPPED");
  65. }
  66. fWasPlayingBefore = timePos->playing;
  67. }
  68. // -------------------------------------------------------------------
  69. // Plugin UI calls
  70. void uiShow(const bool show) override
  71. {
  72. if (! show)
  73. return;
  74. if (const char* const filename = uiOpenFile(false, "Open Audio File", "MIDI Files *.mid;*.midi;;"))
  75. {
  76. uiCustomDataChanged("file", filename);
  77. }
  78. uiClosed();
  79. }
  80. // -------------------------------------------------------------------
  81. // AbstractMidiPlayer calls
  82. void writeMidiEvent(const uint64_t timePosFrame, const RawMidiEvent* const event) override
  83. {
  84. NativeMidiEvent midiEvent;
  85. midiEvent.port = 0;
  86. midiEvent.time = uint32_t(event->time-timePosFrame);
  87. midiEvent.size = event->size;
  88. midiEvent.data[0] = event->data[0];
  89. midiEvent.data[1] = event->data[1];
  90. midiEvent.data[2] = event->data[2];
  91. midiEvent.data[3] = event->data[3];
  92. NativePluginClass::writeMidiEvent(&midiEvent);
  93. }
  94. private:
  95. MidiPattern fMidiOut;
  96. bool fWasPlayingBefore;
  97. void _loadMidiFile(const char* const filename)
  98. {
  99. fMidiOut.clear();
  100. using juce::File;
  101. using juce::FileInputStream;
  102. using juce::MidiFile;
  103. using juce::MidiMessage;
  104. using juce::MidiMessageSequence;
  105. using juce::ScopedPointer;
  106. File file(filename);
  107. if (! file.existsAsFile())
  108. return;
  109. FileInputStream fileStream(file);
  110. MidiFile midiFile;
  111. if (! midiFile.readFrom(fileStream))
  112. return;
  113. midiFile.convertTimestampTicksToSeconds();
  114. const double sampleRate(getSampleRate());
  115. for (int i=0, numTracks = midiFile.getNumTracks(); i<numTracks; ++i)
  116. {
  117. const MidiMessageSequence* const track(midiFile.getTrack(i));
  118. CARLA_SAFE_ASSERT_CONTINUE(track != nullptr);
  119. for (int j=0, numEvents = track->getNumEvents(); j<numEvents; ++j)
  120. {
  121. const MidiMessageSequence::MidiEventHolder* const midiEventHolder(track->getEventPointer(j));
  122. CARLA_SAFE_ASSERT_CONTINUE(midiEventHolder != nullptr);
  123. const MidiMessage& midiMessage(midiEventHolder->message);
  124. //const double time(track->getEventTime(i)*sampleRate);
  125. const int dataSize(midiMessage.getRawDataSize());
  126. if (dataSize <= 0 || dataSize > MAX_EVENT_DATA_SIZE)
  127. continue;
  128. if (midiMessage.isActiveSense())
  129. continue;
  130. if (midiMessage.isMetaEvent())
  131. continue;
  132. if (midiMessage.isMidiStart())
  133. continue;
  134. if (midiMessage.isMidiContinue())
  135. continue;
  136. if (midiMessage.isMidiStop())
  137. continue;
  138. if (midiMessage.isMidiClock())
  139. continue;
  140. if (midiMessage.isSongPositionPointer())
  141. continue;
  142. if (midiMessage.isQuarterFrame())
  143. continue;
  144. if (midiMessage.isFullFrame())
  145. continue;
  146. if (midiMessage.isMidiMachineControlMessage())
  147. continue;
  148. if (midiMessage.isSysEx())
  149. continue;
  150. const double time(midiMessage.getTimeStamp()*sampleRate);
  151. fMidiOut.addRaw(time, midiMessage.getRawData(), static_cast<uint8_t>(dataSize));
  152. }
  153. }
  154. }
  155. PluginClassEND(MidiFilePlugin)
  156. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  157. };
  158. // -----------------------------------------------------------------------
  159. static const NativePluginDescriptor midifileDesc = {
  160. /* category */ PLUGIN_CATEGORY_UTILITY,
  161. /* hints */ static_cast<NativePluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_UI|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. {
  179. carla_register_native_plugin(&midifileDesc);
  180. }
  181. // -----------------------------------------------------------------------