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.

245 lines
7.8KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2013 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 GPL.txt file
  16. */
  17. #include "CarlaNative.hpp"
  18. #include "midi-base.hpp"
  19. #include <smf.h>
  20. class MidiFilePlugin : public PluginDescriptorClass,
  21. public AbstractMidiPlayer
  22. {
  23. public:
  24. MidiFilePlugin(const HostDescriptor* const host)
  25. : PluginDescriptorClass(host),
  26. fMidiOut(this),
  27. fWasPlayingBefore(false)
  28. {
  29. }
  30. ~MidiFilePlugin() override
  31. {
  32. }
  33. protected:
  34. // -------------------------------------------------------------------
  35. // Plugin state calls
  36. void setCustomData(const char* const key, const char* const value) override
  37. {
  38. CARLA_ASSERT(key != nullptr);
  39. CARLA_ASSERT(value != nullptr);
  40. if (std::strcmp(key, "file") != 0)
  41. return;
  42. _loadMidiFile(value);
  43. }
  44. // -------------------------------------------------------------------
  45. // Plugin process calls
  46. void process(float**, float**, const uint32_t frames, const uint32_t, const MidiEvent* const) override
  47. {
  48. const TimeInfo* const timePos(getTimeInfo());
  49. if (timePos->playing)
  50. {
  51. fMidiOut.play(timePos->frame, frames);
  52. }
  53. else if (fWasPlayingBefore)
  54. {
  55. MidiEvent midiEvent;
  56. midiEvent.port = 0;
  57. midiEvent.time = 0;
  58. midiEvent.data[0] = MIDI_STATUS_CONTROL_CHANGE;
  59. midiEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  60. midiEvent.data[2] = 0;
  61. midiEvent.data[3] = 0;
  62. midiEvent.size = 3;
  63. for (int i=0; i < MAX_MIDI_CHANNELS; ++i)
  64. {
  65. midiEvent.data[0] = MIDI_STATUS_CONTROL_CHANGE+i;
  66. PluginDescriptorClass::writeMidiEvent(&midiEvent);
  67. }
  68. carla_stdout("WAS PLAYING BEFORE, NOW STOPPED");
  69. }
  70. fWasPlayingBefore = timePos->playing;
  71. }
  72. // -------------------------------------------------------------------
  73. // Plugin UI calls
  74. void uiShow(const bool show) override
  75. {
  76. if (! show)
  77. return;
  78. if (const char* const filename = uiOpenFile(false, "Open Audio File", "MIDI Files *.mid;*.midi;;"))
  79. {
  80. uiCustomDataChanged("file", filename);
  81. }
  82. uiClosed();
  83. }
  84. // -------------------------------------------------------------------
  85. // AbstractMidiPlayer calls
  86. void writeMidiEvent(const uint32_t timePosFrame, const RawMidiEvent* const event) override
  87. {
  88. MidiEvent midiEvent;
  89. midiEvent.port = 0;
  90. midiEvent.time = event->time-timePosFrame;
  91. midiEvent.data[0] = event->data[0];
  92. midiEvent.data[1] = event->data[1];
  93. midiEvent.data[2] = event->data[2];
  94. midiEvent.data[3] = event->data[3];
  95. midiEvent.size = event->size;
  96. PluginDescriptorClass::writeMidiEvent(&midiEvent);
  97. }
  98. private:
  99. MidiPattern fMidiOut;
  100. bool fWasPlayingBefore;
  101. void _loadMidiFile(const char* const filename)
  102. {
  103. fMidiOut.clear();
  104. if (smf_t* const smf = smf_load(filename))
  105. {
  106. smf_event_t* event;
  107. const double sampleRate(getSampleRate());
  108. while ((event = smf_get_next_event(smf)) != nullptr)
  109. {
  110. if (smf_event_is_valid(event) == 0)
  111. continue;
  112. if (smf_event_is_metadata(event))
  113. continue;
  114. if (smf_event_is_system_realtime(event))
  115. continue;
  116. if (smf_event_is_system_common(event))
  117. continue;
  118. if (event->midi_buffer_length <= 0 || event->midi_buffer_length > MAX_EVENT_DATA_SIZE)
  119. continue;
  120. const uint32_t time(event->time_seconds*sampleRate);
  121. #if 1
  122. fMidiOut.addRaw(time, event->midi_buffer, event->midi_buffer_length);
  123. #else
  124. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(event->midi_buffer);
  125. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(event->midi_buffer);
  126. uint8_t nextBank[MAX_MIDI_CHANNELS] = { 0 };
  127. if (MIDI_IS_STATUS_NOTE_OFF(status))
  128. {
  129. const uint8_t note = event->midi_buffer[1];
  130. const uint8_t velo = event->midi_buffer[2];
  131. fMidiOut.addNoteOff(time, channel, note, velo);
  132. }
  133. else if (MIDI_IS_STATUS_NOTE_ON(status))
  134. {
  135. const uint8_t note = event->midi_buffer[1];
  136. const uint8_t velo = event->midi_buffer[2];
  137. fMidiOut.addNoteOn(time, channel, note, velo);
  138. }
  139. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  140. {
  141. const uint8_t note = event->midi_buffer[1];
  142. const uint8_t pressure = event->midi_buffer[2];
  143. fMidiOut.addNoteAftertouch(time, channel, note, pressure);
  144. }
  145. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  146. {
  147. const uint8_t control = event->midi_buffer[1];
  148. const uint8_t value = (event->midi_buffer_length > 2) ? event->midi_buffer[2] : 0;
  149. if (MIDI_IS_CONTROL_BANK_SELECT(control))
  150. nextBank[channel] = value;
  151. else
  152. fMidiOut.addControl(time, channel, control, value);
  153. }
  154. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(status))
  155. {
  156. const uint8_t program = event->midi_buffer[1];
  157. fMidiOut.addProgram(time, channel, nextBank[channel], program);
  158. }
  159. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  160. {
  161. const uint8_t pressure = event->midi_buffer[1];
  162. fMidiOut.addChannelPressure(time, channel, pressure);
  163. }
  164. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  165. {
  166. const uint8_t lsb = event->midi_buffer[1];
  167. const uint8_t msb = event->midi_buffer[2];
  168. fMidiOut.addPitchbend(time, channel, lsb, msb);
  169. }
  170. #endif
  171. }
  172. smf_delete(smf);
  173. }
  174. }
  175. PluginDescriptorClassEND(MidiFilePlugin)
  176. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  177. };
  178. // -----------------------------------------------------------------------
  179. static const PluginDescriptor midifileDesc = {
  180. /* category */ PLUGIN_CATEGORY_UTILITY,
  181. /* hints */ static_cast<PluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_GUI|PLUGIN_USES_GUI_AS_FILE),
  182. /* audioIns */ 0,
  183. /* audioOuts */ 0,
  184. /* midiIns */ 0,
  185. /* midiOuts */ 1,
  186. /* paramIns */ 0,
  187. /* paramOuts */ 0,
  188. /* name */ "MIDI File",
  189. /* label */ "midifile",
  190. /* maker */ "falkTX",
  191. /* copyright */ "GNU GPL v2+",
  192. PluginDescriptorFILL(MidiFilePlugin)
  193. };
  194. // -----------------------------------------------------------------------
  195. void carla_register_native_plugin_midifile()
  196. {
  197. carla_register_native_plugin(&midifileDesc);
  198. }
  199. // -----------------------------------------------------------------------