Guest User

minuet midi output patch

a guest
Jul 28th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.10 KB | None | 0 0
  1. diff --git a/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.cpp b/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.cpp
  2. index cbe6127..f9c5a36 100644
  3. --- a/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.cpp
  4. +++ b/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.cpp
  5. @@ -112,6 +112,10 @@ void FluidSynthSoundController::setVolume(quint8 volume)
  6.      }
  7.      m_volume = volume;
  8.      fluid_synth_cc(m_synth, 1, 7, m_volume * 127 / 200);
  9. +    snd_seq_event_t event;
  10. +   snd_seq_ev_clear(&event);
  11. +   snd_seq_ev_set_controller(&event, 1, 7, m_volume * 127 / 200);
  12. +   snd_seq_event_output_direct(alsa_seq_handle, &event);
  13.  }
  14.  
  15.  void FluidSynthSoundController::setTempo(quint8 tempo)
  16. @@ -122,7 +126,9 @@ void FluidSynthSoundController::setTempo(quint8 tempo)
  17.  void FluidSynthSoundController::prepareFromExerciseOptions(QJsonArray selectedExerciseOptions)
  18.  {
  19.      auto *song = new QList<fluid_event_t *>;
  20. +    auto *song_alsa = new QList<snd_seq_event_t>;
  21.      m_song.reset(song);
  22. +   m_song_alsa.reset(song_alsa);
  23.  
  24.      if (m_playMode == QLatin1String("rhythm")) {
  25.          for (int i = 0; i < 4; ++i) {
  26. @@ -162,7 +168,16 @@ void FluidSynthSoundController::prepareFromExerciseOptions(QJsonArray selectedEx
  27.      fluid_event_t *event = new_fluid_event();
  28.      fluid_event_set_source(event, -1);
  29.      fluid_event_all_notes_off(event, 1);
  30. +   alsaAllNotesOff();
  31.      m_song->append(event);
  32. +    m_song->append(event);
  33. +}
  34. +
  35. +void FluidSynthSoundController::alsaAllNotesOff(){
  36. +    snd_seq_event_t event;
  37. +   snd_seq_ev_clear(&event);
  38. +   snd_seq_ev_set_controller(&event, 1, 123, 0);
  39. +   snd_seq_event_output_direct(alsa_seq_handle, &event);
  40.  }
  41.  
  42.  void FluidSynthSoundController::prepareFromMidiFile(const QString &fileName)
  43. @@ -190,7 +205,28 @@ void FluidSynthSoundController::play()
  44.                     : (m_playMode == QLatin1String("scale")) ? 1000 * (60.0 / m_tempo)
  45.                                                              : 0;
  46.          }
  47. +
  48. +       snd_seq_start_queue(alsa_seq_handle, alsa_seq_queue_id, NULL);
  49. +       /*snd_seq_queue_status_t *mqueu;*/
  50. +       /*snd_seq_queue_status_malloc(&mqueu);*/
  51. +       /*snd_seq_get_queue_status(alsa_seq_handle, alsa_seq_queue_id, mqueu);*/
  52. +       unsigned int now_alsa = 0;
  53. +       /*snd_seq_queue_status_free(mqueu);*/
  54. +
  55. +        foreach (snd_seq_event_t event2, *m_song_alsa.data()) {
  56. +               snd_seq_ev_schedule_tick(&event2, alsa_seq_queue_id, SND_SEQ_TIME_MODE_REL, now_alsa);
  57. +               snd_seq_ev_set_source(&event2, alsa_port_out_id);
  58. +               snd_seq_ev_set_subs(&event2);
  59. +               snd_seq_event_output(alsa_seq_handle, &event2);
  60. +               snd_seq_drain_output(alsa_seq_handle);
  61. +            now_alsa += (m_playMode == QLatin1String("rhythm"))  ? event2.data.note.duration
  62. +                   : (m_playMode == QLatin1String("scale")) ? 24
  63. +                                                            : 0;
  64. +        }
  65. +
  66. +       snd_seq_drain_output(alsa_seq_handle);
  67.          setState(PlayingState);
  68. +
  69.      }
  70.  }
  71.  
  72. @@ -204,6 +240,9 @@ void FluidSynthSoundController::stop()
  73.          fluid_event_all_notes_off(event, 1);
  74.          fluid_event_set_dest(event, m_synthSeqID);
  75.          fluid_sequencer_send_now(m_sequencer, event);
  76. +       snd_seq_stop_queue(alsa_seq_handle, alsa_seq_queue_id, NULL);
  77. +       snd_seq_drain_output(alsa_seq_handle);
  78. +       alsaAllNotesOff();
  79.          resetEngine();
  80.      }
  81.  }
  82. @@ -212,6 +251,7 @@ void FluidSynthSoundController::reset()
  83.  {
  84.      stop();
  85.      m_song.reset(nullptr);
  86. +   m_song_alsa.reset(nullptr);
  87.  }
  88.  
  89.  void FluidSynthSoundController::appendEvent(int channel, short key, short velocity,
  90. @@ -221,6 +261,13 @@ void FluidSynthSoundController::appendEvent(int channel, short key, short veloci
  91.      fluid_event_set_source(event, -1);
  92.      fluid_event_note(event, channel, key, velocity, duration);
  93.      m_song->append(event);
  94. +
  95. +   snd_seq_event_t ev;
  96. +   unsigned int d2;
  97. +   snd_seq_ev_clear(&ev);
  98. +   d2 = duration/(m_tempo);
  99. +   snd_seq_ev_set_note(&ev, channel, key, velocity, d2);
  100. +   m_song_alsa->append(ev);
  101.  }
  102.  
  103.  void FluidSynthSoundController::sequencerCallback(unsigned int time, fluid_event_t *event,
  104. @@ -282,7 +329,30 @@ void FluidSynthSoundController::resetEngine()
  105.          = fluid_sequencer_register_client(m_sequencer, "Minuet Fluidsynth Sound Controller",
  106.                                            &FluidSynthSoundController::sequencerCallback, this);
  107.  
  108. +   if (snd_seq_open(&alsa_seq_handle, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK) < 0) {
  109. +        qCritical() << "Error opening ALSA sequencer\n";
  110. +   }
  111. +
  112. +   if ((alsa_port_out_id = snd_seq_create_simple_port(alsa_seq_handle, "output",
  113. +           SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ,
  114. +           SND_SEQ_PORT_TYPE_APPLICATION | SND_SEQ_PORT_TYPE_MIDI_GENERIC )) < 0) {
  115. +       qCritical() << "Error creating output port\n";
  116. +       snd_seq_close(alsa_seq_handle);
  117. +   }
  118.      m_initialTime = 0;
  119. +   snd_seq_set_client_name(alsa_seq_handle, "Minuet MIDI Port");
  120. +   alsa_seq_queue_id = snd_seq_alloc_queue(alsa_seq_handle);
  121. +
  122. +   snd_seq_queue_tempo_t *queue_tempo;
  123. +   int truetempo = (int) ((6e7 * 4) / (60 * 4));
  124. +
  125. +   snd_seq_queue_tempo_alloca(&queue_tempo);
  126. +   snd_seq_queue_tempo_set_tempo(queue_tempo, truetempo);
  127. +   snd_seq_queue_tempo_set_ppq(queue_tempo, 24);
  128. +   snd_seq_set_queue_tempo(alsa_seq_handle, alsa_seq_queue_id, queue_tempo);
  129. +
  130. +
  131. +
  132.      setPlaybackLabel(QStringLiteral("00:00.00"));
  133.      setState(StoppedState);
  134.  }
  135. @@ -298,6 +368,15 @@ void FluidSynthSoundController::deleteEngine()
  136.          fluid_sequencer_send_now(m_sequencer, m_unregisteringEvent);
  137.  #endif
  138.          delete_fluid_sequencer(m_sequencer);
  139. +
  140. +       snd_seq_remove_events_t *remove_ev;
  141. +
  142. +       snd_seq_remove_events_alloca(&remove_ev);
  143. +       snd_seq_remove_events_set_queue(remove_ev, alsa_seq_queue_id);
  144. +       snd_seq_remove_events_set_condition(remove_ev,
  145. +                           SND_SEQ_REMOVE_OUTPUT |
  146. +                           SND_SEQ_REMOVE_IGNORE_OFF);
  147. +       snd_seq_close(alsa_seq_handle);
  148.      }
  149.      if (m_audioDriver) {
  150.          delete_fluid_audio_driver(m_audioDriver);
  151. diff --git a/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.h b/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.h
  152. index 3035786..86547c1 100644
  153. --- a/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.h
  154. +++ b/src/plugins/fluidsynthsoundcontroller/fluidsynthsoundcontroller.h
  155. @@ -26,6 +26,7 @@
  156.  #include <interfaces/isoundcontroller.h>
  157.  
  158.  #include <fluidsynth.h>
  159. +#include <alsa/asoundlib.h>
  160.  
  161.  class FluidSynthSoundController : public Minuet::ISoundController
  162.  {
  163. @@ -58,6 +59,7 @@ private:
  164.                                    void *data);
  165.      void resetEngine();
  166.      void deleteEngine();
  167. +   void alsaAllNotesOff();
  168.  
  169.  private:
  170.      fluid_settings_t *m_settings;
  171. @@ -65,12 +67,16 @@ private:
  172.      fluid_sequencer_t *m_sequencer;
  173.      fluid_synth_t *m_synth;
  174.      fluid_event_t *m_unregisteringEvent;
  175. +   snd_seq_t *alsa_seq_handle;
  176. +   int alsa_port_out_id;
  177. +   int alsa_seq_queue_id;
  178.  
  179.      short m_synthSeqID;
  180.      short m_callbackSeqID;
  181.      static unsigned int m_initialTime;
  182.  
  183.      QScopedPointer<QList<fluid_event_t *>> m_song;
  184. +    QScopedPointer<QList<snd_seq_event_t>> m_song_alsa;
  185.  };
  186.  
  187.  #endif
  188. --
  189. 2.43.0
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment