This repository was archived by the owner on Feb 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (96 loc) · 3.2 KB
/
main.cpp
File metadata and controls
112 lines (96 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <thread>
#include <limits>
#include "rtmidi/RtMidi.h"
#ifdef _WIN32
#include <windows.h>
#endif
class MidiRouter {
private:
RtMidiIn midiIn;
RtMidiOut midiOut;
static constexpr size_t SLEEP_MS = 1000;
static void midiCallback(double deltaTime, std::vector<unsigned char>* message, void* userData) noexcept {
if (!message || message->empty() || !userData) return;
try {
auto* midiOut = static_cast<RtMidiOut*>(userData);
midiOut->sendMessage(message);
} catch (...) {
}
}
template<typename T>
static void listDevices(T& midi, const char* type) noexcept {
const unsigned int nPorts = midi.getPortCount();
std::cout << "Available MIDI " << type << " ports:\n";
for (unsigned int i = 0; i < nPorts; ++i) {
try {
std::cout << i << ": " << midi.getPortName(i) << '\n';
} catch (...) {
std::cout << i << ": <error reading port name>\n";
}
}
std::cout.flush();
}
template<typename T>
static unsigned int selectDevice(T& midi, const char* type) {
while (true) {
listDevices(midi, type);
const unsigned int portCount = midi.getPortCount();
if (portCount == 0) {
throw std::runtime_error("No MIDI devices available");
}
std::cout << "Select " << type << " port (0 to " << (portCount - 1) << "): ";
unsigned int deviceIndex;
if (std::cin >> deviceIndex && deviceIndex < portCount) {
return deviceIndex;
}
std::cin.clear();
std::cin.ignore((std::numeric_limits<std::streamsize>::max)(), '\n');
std::cerr << "Invalid port number! Please try again.\n";
}
}
public:
MidiRouter() = default;
void initialize() {
const unsigned int inIdx = selectDevice(midiIn, "input");
const unsigned int outIdx = selectDevice(midiOut, "output");
midiIn.openPort(inIdx);
midiOut.openPort(outIdx);
std::cout << "\n[ACTIVE] Routing: " << midiIn.getPortName(inIdx)
<< " -> " << midiOut.getPortName(outIdx) << "\n";
std::cout << "Process Priority: HIGH | Logic: Callback-based\n";
std::cout << "Press Ctrl+C to stop.\n" << std::flush;
midiIn.setCallback(&midiCallback, &midiOut);
}
void run() {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_MS));
}
}
};
int main() {
#ifdef _WIN32
if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS)) {
std::cerr << "Warning: Could not set high priority class.\n";
}
#endif
try {
MidiRouter router;
router.initialize();
router.run();
}
catch (const RtMidiError& error) {
std::cerr << "RtMidi error: " << error.getMessage() << '\n';
return 1;
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
return 1;
}
catch (...) {
std::cerr << "Unknown error occurred.\n";
return 1;
}
return 0;
}