|
1 | 1 | #include "RtMidiInput.hpp" |
2 | 2 | #include "Logger.hpp" |
3 | 3 | #include <chrono> |
| 4 | +#include <cstdlib> |
4 | 5 | #include <map> |
5 | 6 | #include <rtmidi/RtMidi.h> |
6 | 7 | #include <thread> |
@@ -72,24 +73,86 @@ bool RtMidiInput::initialize() { |
72 | 73 | Logger::log("[RtMidiInput] Nombre de ports MIDI trouvés: {}", |
73 | 74 | std::to_string(nPorts)); |
74 | 75 |
|
| 76 | + unsigned int hardwarePortIndex = 0; |
75 | 77 | for (unsigned int i = 0; i < nPorts; i++) { |
76 | 78 | std::string portName = midiIn->getPortName(i); |
77 | 79 | Logger::log("[RtMidiInput] Port {}: {}", std::to_string(i), |
78 | 80 | portName); |
79 | 81 | // On cherche un clavier (ex: "reface CP", "USB MIDI", etc.) |
80 | | - // On évite de se connecter à soi-même ou à "Midi Through" |
| 82 | + // On évite de se connecter à soi-même, "Midi Through", ou |
| 83 | + // "FluidSynth" |
81 | 84 | if (portName.find("SmartPianoEngine") == std::string::npos && |
82 | | - portName.find("Through") == std::string::npos) { |
| 85 | + portName.find("Through") == std::string::npos && |
| 86 | + portName.find("FluidSynth") == std::string::npos && |
| 87 | + portName.find("fluidsynth") == std::string::npos && |
| 88 | + portName.find("FLUID Synth") == std::string::npos) { |
83 | 89 | Logger::log("[RtMidiInput] Tentative d'ouverture du port: {}", |
84 | 90 | portName); |
85 | 91 | midiIn->openPort(i); |
86 | 92 | hardwareFound = true; |
| 93 | + hardwarePortIndex = i; |
87 | 94 | Logger::log("[RtMidiInput] Connecté au port matériel: {}", |
88 | 95 | portName); |
89 | 96 | break; |
90 | 97 | } |
91 | 98 | } |
92 | 99 |
|
| 100 | + if (hardwareFound) { |
| 101 | + // Check if there is a running instance of FluidSynth |
| 102 | + unsigned int nOutPorts = midiOut->getPortCount(); |
| 103 | + bool fluidSynthFound = false; |
| 104 | + std::string fluidSynthPortName; |
| 105 | + for (unsigned int j = 0; j < nOutPorts; j++) { |
| 106 | + std::string outPortName = midiOut->getPortName(j); |
| 107 | + if (outPortName.find("FluidSynth") != std::string::npos || |
| 108 | + outPortName.find("fluidsynth") != std::string::npos || |
| 109 | + outPortName.find("FLUID Synth") != std::string::npos) { |
| 110 | + fluidSynthFound = true; |
| 111 | + fluidSynthPortName = outPortName; |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + if (fluidSynthFound) { |
| 116 | + std::string keyboardPortName = |
| 117 | + midiIn->getPortName(hardwarePortIndex); |
| 118 | + Logger::log("[RtMidiInput] FluidSynth détecté sur le port: {}", |
| 119 | + fluidSynthPortName); |
| 120 | + |
| 121 | + auto getClientPort = [](const std::string& name) { |
| 122 | + size_t lastSpace = name.find_last_of(' '); |
| 123 | + if (lastSpace != std::string::npos) { |
| 124 | + std::string lastToken = name.substr(lastSpace + 1); |
| 125 | + if (lastToken.find(':') != std::string::npos) { |
| 126 | + return lastToken; |
| 127 | + } |
| 128 | + } |
| 129 | + return name; |
| 130 | + }; |
| 131 | + |
| 132 | + std::string src = getClientPort(keyboardPortName); |
| 133 | + std::string dest = getClientPort(fluidSynthPortName); |
| 134 | + Logger::log("[RtMidiInput] Tentative de connexion via " |
| 135 | + "aconnect: {} -> {}", |
| 136 | + src, dest); |
| 137 | + |
| 138 | + std::string command = |
| 139 | + "aconnect \"" + src + "\" \"" + dest + "\""; |
| 140 | + int status = std::system(command.c_str()); |
| 141 | + if (status == 0) { |
| 142 | + Logger::log("[RtMidiInput] Connexion aconnect réussie de " |
| 143 | + "{} vers {}", |
| 144 | + src, dest); |
| 145 | + } else { |
| 146 | + Logger::err( |
| 147 | + "[RtMidiInput] Échec de la commande aconnect: {}", |
| 148 | + std::to_string(status)); |
| 149 | + } |
| 150 | + } else { |
| 151 | + Logger::log( |
| 152 | + "[RtMidiInput] Aucun synthétiseur FluidSynth détecté"); |
| 153 | + } |
| 154 | + } |
| 155 | + |
93 | 156 | if (!hardwareFound) { |
94 | 157 | Logger::log("[RtMidiInput] Aucun clavier détecté, " |
95 | 158 | "ouverture d'un port virtuel"); |
|
0 commit comments