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.

243 lines
7.7KB

  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 = timePos->frame;
  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 = 2;
  63. for (int i=0; i < MAX_MIDI_CHANNELS; i++)
  64. {
  65. midiEvent.data[0] += i;
  66. PluginDescriptorClass::writeMidiEvent(&midiEvent);
  67. }
  68. }
  69. fWasPlayingBefore = timePos->playing;
  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. {
  79. uiCustomDataChanged("file", filename);
  80. }
  81. uiClosed();
  82. }
  83. // -------------------------------------------------------------------
  84. // AbstractMidiPlayer calls
  85. void writeMidiEvent(const uint32_t timePosFrame, const RawMidiEvent* const event) override
  86. {
  87. MidiEvent midiEvent;
  88. midiEvent.port = 0;
  89. midiEvent.time = event->time-timePosFrame;
  90. midiEvent.data[0] = event->data[0];
  91. midiEvent.data[1] = event->data[1];
  92. midiEvent.data[2] = event->data[2];
  93. midiEvent.data[3] = event->data[3];
  94. midiEvent.size = event->size;
  95. PluginDescriptorClass::writeMidiEvent(&midiEvent);
  96. }
  97. private:
  98. MidiPattern fMidiOut;
  99. bool fWasPlayingBefore;
  100. void _loadMidiFile(const char* const filename)
  101. {
  102. fMidiOut.clear();
  103. if (smf_t* const smf = smf_load(filename))
  104. {
  105. smf_event_t* event;
  106. const double sampleRate = getSampleRate();
  107. while ((event = smf_get_next_event(smf)) != nullptr)
  108. {
  109. if (smf_event_is_valid(event) == 0)
  110. continue;
  111. if (smf_event_is_metadata(event))
  112. continue;
  113. if (smf_event_is_system_realtime(event))
  114. continue;
  115. if (smf_event_is_system_common(event))
  116. continue;
  117. if (event->midi_buffer_length <= 0 || event->midi_buffer_length > MAX_EVENT_DATA_SIZE)
  118. continue;
  119. const uint32_t time = event->time_seconds*sampleRate;
  120. #if 1
  121. fMidiOut.addRaw(time, event->midi_buffer, event->midi_buffer_length);
  122. #else
  123. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(event->midi_buffer);
  124. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(event->midi_buffer);
  125. uint8_t nextBank[MAX_MIDI_CHANNELS] = { 0 };
  126. if (MIDI_IS_STATUS_NOTE_OFF(status))
  127. {
  128. const uint8_t note = event->midi_buffer[1];
  129. const uint8_t velo = event->midi_buffer[2];
  130. fMidiOut.addNoteOff(time, channel, note, velo);
  131. }
  132. else if (MIDI_IS_STATUS_NOTE_ON(status))
  133. {
  134. const uint8_t note = event->midi_buffer[1];
  135. const uint8_t velo = event->midi_buffer[2];
  136. fMidiOut.addNoteOn(time, channel, note, velo);
  137. }
  138. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  139. {
  140. const uint8_t note = event->midi_buffer[1];
  141. const uint8_t pressure = event->midi_buffer[2];
  142. fMidiOut.addNoteAftertouch(time, channel, note, pressure);
  143. }
  144. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  145. {
  146. const uint8_t control = event->midi_buffer[1];
  147. const uint8_t value = (event->midi_buffer_length > 2) ? event->midi_buffer[2] : 0;
  148. if (MIDI_IS_CONTROL_BANK_SELECT(control))
  149. nextBank[channel] = value;
  150. else
  151. fMidiOut.addControl(time, channel, control, value);
  152. }
  153. else if (MIDI_IS_STATUS_PROGRAM_CHANGE(status))
  154. {
  155. const uint8_t program = event->midi_buffer[1];
  156. fMidiOut.addProgram(time, channel, nextBank[channel], program);
  157. }
  158. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  159. {
  160. const uint8_t pressure = event->midi_buffer[1];
  161. fMidiOut.addChannelPressure(time, channel, pressure);
  162. }
  163. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  164. {
  165. const uint8_t lsb = event->midi_buffer[1];
  166. const uint8_t msb = event->midi_buffer[2];
  167. fMidiOut.addPitchbend(time, channel, lsb, msb);
  168. }
  169. #endif
  170. }
  171. smf_delete(smf);
  172. }
  173. }
  174. PluginDescriptorClassEND(MidiFilePlugin)
  175. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  176. };
  177. // -----------------------------------------------------------------------
  178. static const PluginDescriptor midifileDesc = {
  179. /* category */ PLUGIN_CATEGORY_UTILITY,
  180. /* hints */ static_cast<PluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_GUI),
  181. /* audioIns */ 0,
  182. /* audioOuts */ 0,
  183. /* midiIns */ 0,
  184. /* midiOuts */ 1,
  185. /* paramIns */ 0,
  186. /* paramOuts */ 0,
  187. /* name */ "MIDI File",
  188. /* label */ "midifile",
  189. /* maker */ "falkTX",
  190. /* copyright */ "GNU GPL v2+",
  191. PluginDescriptorFILL(MidiFilePlugin)
  192. };
  193. // -----------------------------------------------------------------------
  194. void carla_register_native_plugin_midifile()
  195. {
  196. carla_register_native_plugin(&midifileDesc);
  197. }
  198. // -----------------------------------------------------------------------