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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 doc/GPL.txt file.
  16. */
  17. #include "CarlaNative.hpp"
  18. #include "midi-base.hpp"
  19. #include <smf.h>
  20. class MidiFilePlugin : public NativePluginClass,
  21. public AbstractMidiPlayer
  22. {
  23. public:
  24. MidiFilePlugin(const NativeHostDescriptor* const host)
  25. : NativePluginClass(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 NativeMidiEvent* const, const uint32_t) override
  47. {
  48. const NativeTimeInfo* const timePos(getTimeInfo());
  49. if (timePos->playing)
  50. {
  51. fMidiOut.play(timePos->frame, frames);
  52. }
  53. else if (fWasPlayingBefore)
  54. {
  55. NativeMidiEvent 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] = uint8_t(MIDI_STATUS_CONTROL_CHANGE+i);
  66. NativePluginClass::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 uint64_t timePosFrame, const RawMidiEvent* const event) override
  87. {
  88. NativeMidiEvent midiEvent;
  89. midiEvent.port = 0;
  90. midiEvent.time = uint32_t(event->time-timePosFrame);
  91. midiEvent.size = event->size;
  92. midiEvent.data[0] = event->data[0];
  93. midiEvent.data[1] = event->data[1];
  94. midiEvent.data[2] = event->data[2];
  95. midiEvent.data[3] = event->data[3];
  96. NativePluginClass::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 uint64_t time(uint64_t(event->time_seconds*sampleRate));
  121. #if 1
  122. fMidiOut.addRaw(time, event->midi_buffer, uint8_t(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. PluginClassEND(MidiFilePlugin)
  176. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MidiFilePlugin)
  177. };
  178. // -----------------------------------------------------------------------
  179. static const NativePluginDescriptor midifileDesc = {
  180. /* category */ PLUGIN_CATEGORY_UTILITY,
  181. /* hints */ static_cast<NativePluginHints>(PLUGIN_IS_RTSAFE|PLUGIN_HAS_UI|PLUGIN_NEEDS_UI_OPEN_SAVE),
  182. /* supports */ static_cast<NativePluginSupports>(0x0),
  183. /* audioIns */ 0,
  184. /* audioOuts */ 0,
  185. /* midiIns */ 0,
  186. /* midiOuts */ 1,
  187. /* paramIns */ 0,
  188. /* paramOuts */ 0,
  189. /* name */ "MIDI File",
  190. /* label */ "midifile",
  191. /* maker */ "falkTX",
  192. /* copyright */ "GNU GPL v2+",
  193. PluginDescriptorFILL(MidiFilePlugin)
  194. };
  195. // -----------------------------------------------------------------------
  196. CARLA_EXPORT
  197. void carla_register_native_plugin_midifile()
  198. {
  199. carla_register_native_plugin(&midifileDesc);
  200. }
  201. // -----------------------------------------------------------------------