ACMX 2.139.0
Dual-Backend Real-Time GPU Video Synthesis
Loading...
Searching...
No Matches
midi_window.cpp
Go to the documentation of this file.
1#include "midi_window.hpp"
2#include <QFileDialog>
3#include <QFont>
4#include <QGroupBox>
5#include <QHBoxLayout>
6#include <QHeaderView>
7#include <QMessageBox>
8#include <QVBoxLayout>
9#include <algorithm>
10#include <fstream>
11#include <sstream>
12
13MidiMapWindow::MidiMapWindow(QWidget *parent) : QMainWindow(parent), midiIn(nullptr) {
14 setupUi();
19
20 pollTimer = new QTimer(this);
21 connect(pollTimer, &QTimer::timeout, this, &MidiMapWindow::pollMidi);
22
23 try {
24 midiIn = new RtMidiIn();
25 midiIn->ignoreTypes(false, false, false);
27 setStatus("MIDI initialized — select a device");
28 } catch (RtMidiError &e) {
29 setStatus(QString("MIDI init failed: %1").arg(e.getMessage().c_str()));
30 }
31}
32
34 if (pollTimer->isActive())
35 pollTimer->stop();
36 delete midiIn;
37}
38
40 setWindowTitle("ACMX2 MIDI Map Configuration");
41 setMinimumSize(820, 640);
42
43 auto *central = new QWidget(this);
44 auto *mainLayout = new QVBoxLayout(central);
45 mainLayout->setContentsMargins(12, 12, 12, 12);
46 mainLayout->setSpacing(8);
47
48 // --- Target application ---
49 auto *target_group = new QGroupBox("Target Application", this);
50 auto *target_layout = new QHBoxLayout(target_group);
51 target_combo = new QComboBox(this);
52 target_combo->addItem("ACMX2");
53 target_combo->addItem("ACMXVK");
54 target_combo->setToolTip("Selects target-specific action names while preserving the shared "
55 ".midi_cfg file format.");
56 target_layout->addWidget(target_combo);
57 mainLayout->addWidget(target_group);
58
59 connect(target_combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MidiMapWindow::change_target);
60
61 // --- Device section ---
62 auto *deviceGroup = new QGroupBox("MIDI Device", this);
63 auto *deviceLayout = new QHBoxLayout(deviceGroup);
64 deviceCombo = new QComboBox(this);
65 deviceCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
66 refreshButton = new QPushButton("Refresh", this);
67 deviceLayout->addWidget(deviceCombo);
68 deviceLayout->addWidget(refreshButton);
69 mainLayout->addWidget(deviceGroup);
70
71 connect(refreshButton, &QPushButton::clicked, this, &MidiMapWindow::refreshDevices);
72 connect(deviceCombo, QOverload<int>::of(&QComboBox::activated), this, &MidiMapWindow::openDevice);
73
74 // --- MIDI monitor ---
75 midiMonitorLabel = new QLabel("Last MIDI: —", this);
76 midiMonitorLabel->setObjectName("midiMonitor");
77 mainLayout->addWidget(midiMonitorLabel);
78
79 // --- Mapping table ---
80 auto *tableGroup = new QGroupBox("Action Mappings", this);
81 auto *tableLayout = new QVBoxLayout(tableGroup);
82 table = new QTableWidget(this);
83 table->setColumnCount(5);
84 table->setHorizontalHeaderLabels({"Action", "Description", "Key Codes", "MIDI Bytes", "Status"});
85 table->horizontalHeader()->setStretchLastSection(true);
86 table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
87 table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
88 table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
89 table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
90 table->setSelectionBehavior(QAbstractItemView::SelectRows);
91 table->setSelectionMode(QAbstractItemView::SingleSelection);
92 table->setEditTriggers(QAbstractItemView::NoEditTriggers);
93 table->verticalHeader()->setVisible(false);
94 tableLayout->addWidget(table);
95 mainLayout->addWidget(tableGroup, 1);
96
97 // --- Buttons ---
98 auto *btnLayout = new QHBoxLayout();
99 captureButton = new QPushButton("Capture Selected", this);
100 clearButton = new QPushButton("Clear Selected", this);
101 btnLayout->addWidget(captureButton);
102 btnLayout->addWidget(clearButton);
103 btnLayout->addStretch();
104 mainLayout->addLayout(btnLayout);
105
106 connect(captureButton, &QPushButton::clicked, this, &MidiMapWindow::captureMapping);
107 connect(clearButton, &QPushButton::clicked, this, &MidiMapWindow::clearMapping);
108
109 // --- File section ---
110 auto *fileGroup = new QGroupBox("Configuration File", this);
111 auto *fileLayout = new QHBoxLayout(fileGroup);
112 fileEdit = new QLineEdit("midi.midi_cfg", this);
113 saveButton = new QPushButton("Save", this);
114 loadButton = new QPushButton("Load", this);
115 fileLayout->addWidget(fileEdit, 1);
116 fileLayout->addWidget(loadButton);
117 fileLayout->addWidget(saveButton);
118 mainLayout->addWidget(fileGroup);
119
120 connect(saveButton, &QPushButton::clicked, this, &MidiMapWindow::saveConfig);
121 connect(loadButton, &QPushButton::clicked, this, &MidiMapWindow::loadConfig);
122
123 setCentralWidget(central);
124 statusBar()->showMessage("Ready");
125}
126
128 setStyleSheet("QMainWindow { background-color: rgb(0, 0, 0); }"
129 "* { color: white; font-family: 'Courier New', Courier, monospace; font-size: 13px; }"
130 "QGroupBox { border: 1px solid #444444; border-radius: 4px; margin-top: 8px; padding-top: 14px; font-weight: bold; color: white; }"
131 "QGroupBox::title { subcontrol-origin: margin; left: 10px; padding: 0 4px; color: white; }"
132 "QPushButton { border: 1px solid #555555; background-color: #1a1a1a; padding: 6px 14px; border-radius: 3px; font-weight: bold; color: white; }"
133 "QPushButton:hover { background-color: #333333; color: white; }"
134 "QPushButton:pressed { background-color: #555555; color: white; }"
135 "QPushButton:disabled { border: 1px solid #2a2a2a; color: #444444; background-color: #0d0d0d; }"
136 "QComboBox { border: 1px solid #555555; background-color: #1a1a1a; padding: 4px; color: white; selection-background-color: #444444; }"
137 "QComboBox::drop-down { border-left: 1px solid #555555; }"
138 "QComboBox QAbstractItemView { background-color: #1a1a1a; border: 1px solid #555555; selection-background-color: #444444; color: white; }"
139 "QLineEdit { border: 1px solid #555555; background-color: #1a1a1a; padding: 4px; color: white; selection-background-color: #444444; }"
140 "QTableWidget { background-color: black; color: white; font-size: 13px; gridline-color: #333333; border: 1px solid #444444; }"
141 "QTableWidget::item { padding: 4px; }"
142 "QTableWidget::item:selected { background-color: #444444; color: white; }"
143 "QHeaderView::section { background-color: #1a1a1a; border: 1px solid #444444; padding: 4px; font-weight: bold; color: white; }"
144 "QStatusBar { background-color: black; border-top: 1px solid #444444; color: lime; font-size: 13px; }"
145 "QLabel#midiMonitor { background-color: black; border: 1px solid #444444; padding: 6px; font-size: 14px; color: lime; }"
146 "QScrollBar:vertical { background: #0d0d0d; width: 12px; border: 1px solid #333333; }"
147 "QScrollBar::handle:vertical { background: #555555; min-height: 20px; border-radius: 3px; }"
148 "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: 0; }"
149 "QMessageBox { background-color: #1a1a1a; color: white; }"
150 "QMessageBox QLabel { color: white; }"
151 "QMessageBox QPushButton { min-width: 70px; }");
152}
153
155 mappings.clear();
157 // Knob mappings supported by ACMXVK.
158 mappings.push_back({"Left/Right Knob", "GPU filter index left/right", 262, 263, false, 0, 0, 0});
159 mappings.push_back({"Up/Down Knob", "Shader index or playlist node prev/next", 264, 265, false, 0, 0, 0});
160 mappings.push_back({"Time Fwd/Back Knob", "Shader time step forward/backward", 500, 501, false, 0, 0, 0});
161 mappings.push_back({"TimeSpeed Knob", "Shader time speed increase/decrease", 504, 505, false, 0, 0, 0});
162 mappings.push_back({"Rotate X Axis Knob", "Rotate the 3D model forward/backward on X", 506, 507, false, 0, 0, 0});
163 mappings.push_back({"Rotate Y Axis Knob", "Rotate the 3D model forward/backward on Y", 508, 509, false, 0, 0, 0});
164 mappings.push_back({"Rotate Z Axis Knob", "Rotate the 3D model forward/backward on Z", 512, 513, false, 0, 0, 0});
165 mappings.push_back({"Model Scale Knob", "Increase/decrease 3D model scale", 514, 515, false, 0, 0, 0});
166 mappings.push_back({"Slider 1 Knob", "Shader uniform slider1", 600, 601, false, 0, 0, 0});
167 mappings.push_back({"Slider 2 Knob", "Shader uniform slider2", 602, 603, false, 0, 0, 0});
168 mappings.push_back({"Slider 3 Knob", "Shader uniform slider3", 604, 605, false, 0, 0, 0});
169 mappings.push_back({"Slider 4 Knob", "Shader uniform slider4", 606, 607, false, 0, 0, 0});
170
171 // ACMXVK key/action mappings.
172 mappings.push_back({"Left", "Previous CUDA filter", 263, 0, false, 0, 0, 0});
173 mappings.push_back({"Right", "Next CUDA filter", 262, 0, false, 0, 0, 0});
174 mappings.push_back({"Up", "Previous shader or playlist node", 265, 0, false, 0, 0, 0});
175 mappings.push_back({"Down", "Next shader or playlist node", 264, 0, false, 0, 0, 0});
176 mappings.push_back({"Space", "Toggle shader bypass", 32, 0, false, 0, 0, 0});
177 mappings.push_back({"Time Forward", "Step shader time forward (U)", 500, 0, false, 0, 0, 0});
178 mappings.push_back({"Time Backward", "Step shader time backward (I)", 501, 0, false, 0, 0, 0});
179 mappings.push_back({"Page Up", "Increase shader time speed", 266, 0, false, 0, 0, 0});
180 mappings.push_back({"Page Down", "Decrease shader time speed", 267, 0, false, 0, 0, 0});
181 mappings.push_back({"P", "Toggle playlist mode or input pause", 80, 0, false, 0, 0, 0});
182 mappings.push_back({"L", "Toggle rendering freeze", 76, 0, false, 0, 0, 0});
183 mappings.push_back({"M", "Toggle configured multipass chain", 77, 0, false, 0, 0, 0});
184 mappings.push_back({"J", "Toggle random autopilot", 74, 0, false, 0, 0, 0});
185 mappings.push_back({"Y", "Toggle sequential autopilot", 89, 0, false, 0, 0, 0});
186 mappings.push_back({"N", "Toggle random autopilot XFade", 78, 0, false, 0, 0, 0});
187 mappings.push_back({"K", "Toggle shader lock", 75, 0, false, 0, 0, 0});
188 mappings.push_back({"T", "Toggle normal shader time", 84, 0, false, 0, 0, 0});
189 mappings.push_back({"Q", "Toggle audio-reactive shader time", 81, 0, false, 0, 0, 0});
190 mappings.push_back({"Home", "Toggle audio delta-time scaling", 268, 0, false, 0, 0, 0});
191 mappings.push_back({"End", "Toggle spectrum sensitivity scaling", 269, 0, false, 0, 0, 0});
192 mappings.push_back({"Insert", "Increase audio sensitivity", 260, 0, false, 0, 0, 0});
193 mappings.push_back({"Delete", "Decrease audio sensitivity", 261, 0, false, 0, 0, 0});
194 mappings.push_back({"E", "Toggle configured watermark", 69, 0, false, 0, 0, 0});
195 mappings.push_back({"F", "Toggle fullscreen", 70, 0, false, 0, 0, 0});
196 mappings.push_back({"F9", "Toggle preview-only runtime HUD", 298, 0, false, 0, 0, 0});
197 mappings.push_back({"Z", "Take PNG snapshot", 90, 0, false, 0, 0, 0});
198 mappings.push_back({"4", "Take optional TIFF snapshot", 52, 0, false, 0, 0, 0});
199 mappings.push_back({"5", "Take optional WebP snapshot", 53, 0, false, 0, 0, 0});
200 mappings.push_back({"6", "Take raw RGBA snapshot", 54, 0, false, 0, 0, 0});
201 mappings.push_back({"3", "Toggle 2D/3D rendering", 51, 0, false, 0, 0, 0});
202 mappings.push_back({"C", "Toggle 3D wave effect", 67, 0, false, 0, 0, 0});
203 mappings.push_back({"O", "Toggle 3D scale oscillation", 79, 0, false, 0, 0, 0});
204 mappings.push_back({"V", "Toggle automatic 3D view rotation", 86, 0, false, 0, 0, 0});
205 mappings.push_back({"X", "Reset 3D model view", 88, 0, false, 0, 0, 0});
206 mappings.push_back({"Comma", "Decrease 3D rotation speed", 44, 0, false, 0, 0, 0});
207 mappings.push_back({"Period", "Increase 3D rotation speed", 46, 0, false, 0, 0, 0});
208 mappings.push_back({"Scale Down", "Decrease 3D model scale", 91, 0, false, 0, 0, 0});
209 mappings.push_back({"Scale Up", "Increase 3D model scale", 93, 0, false, 0, 0, 0});
210 mappings.push_back({"RotSpeed Up", "Increase manual 3D rotation speed", 510, 0, false, 0, 0, 0});
211 mappings.push_back({"RotSpeed Down", "Decrease manual 3D rotation speed", 511, 0, false, 0, 0, 0});
212 return;
213 }
214
215 // Knob mappings (paired key codes)
216 mappings.push_back({"Left/Right Knob", "GPU filter index left/right", 262, 263, false, 0, 0, 0});
217 mappings.push_back({"Up/Down Knob", "Shader index prev/next (or playlist node)", 264, 265, false, 0, 0, 0});
218 mappings.push_back({"Time Fwd/Back Knob", "Time step forward/backward", 500, 501, false, 0, 0, 0});
219 mappings.push_back({"TimeSpeed Knob", "Time speed increase/decrease", 504, 505, false, 0, 0, 0});
220 mappings.push_back({"Rotate X Axis W/S Knob", "Camera pitch up/down (3D)", 506, 507, false, 0, 0, 0});
221 mappings.push_back({"Rotate Y Axis A/D Knob", "Camera yaw right/left (3D)", 508, 509, false, 0, 0, 0});
222 mappings.push_back({"Rotate Z Axis Knob", "Camera roll right/left (3D)", 512, 513, false, 0, 0, 0});
223 mappings.push_back({"Model Scale Knob", "Model scale increase/decrease (3D)", 514, 515, false, 0, 0, 0});
224 mappings.push_back({"Slider 1 Knob", "Shader uniform slider1 (0.0-1.0)", 600, 601, false, 0, 0, 0});
225 mappings.push_back({"Slider 2 Knob", "Shader uniform slider2 (0.0-1.0)", 602, 603, false, 0, 0, 0});
226 mappings.push_back({"Slider 3 Knob", "Shader uniform slider3 (0.0-1.0)", 604, 605, false, 0, 0, 0});
227 mappings.push_back({"Slider 4 Knob", "Shader uniform slider4 (0.0-1.0)", 606, 607, false, 0, 0, 0});
228 mappings.push_back({"RotSpeed Up", "Camera rotation speed increase", 510, 0, false, 0, 0, 0});
229 mappings.push_back({"RotSpeed Down", "Camera rotation speed decrease", 511, 0, false, 0, 0, 0});
230
231 // Key mappings (single key, key2 = 0)
232 mappings.push_back({"Left", "GPU filter index left", 263, 0, false, 0, 0, 0});
233 mappings.push_back({"Right", "GPU filter index right", 262, 0, false, 0, 0, 0});
234 mappings.push_back({"Up", "Previous shader (or playlist node)", 265, 0, false, 0, 0, 0});
235 mappings.push_back({"Down", "Next shader (or playlist node)", 264, 0, false, 0, 0, 0});
236 mappings.push_back({"Space", "Toggle shader bypass", 32, 0, false, 0, 0, 0});
237 mappings.push_back({"Time Forward", "Time step forward (U)", 500, 0, false, 0, 0, 0});
238 mappings.push_back({"Time Backward", "Time step backward (I)", 501, 0, false, 0, 0, 0});
239 mappings.push_back({"P", "Toggle playlist enable/disable", 80, 0, false, 0, 0, 0});
240 mappings.push_back({"L", "Freeze frame (video/graphic mode)", 76, 0, false, 0, 0, 0});
241 mappings.push_back({"M", "Toggle multi-pass shader", 77, 0, false, 0, 0, 0});
242 mappings.push_back({"Z", "Take PNG snapshot", 90, 0, false, 0, 0, 0});
243 mappings.push_back({"E", "Toggle watermark", 69, 0, false, 0, 0, 0});
244 mappings.push_back({"V", "Toggle view rotation (3D)", 86, 0, false, 0, 0, 0});
245 mappings.push_back({"O", "Toggle scale oscillation (3D)", 79, 0, false, 0, 0, 0});
246 mappings.push_back({"C", "Toggle wave effect (3D)", 67, 0, false, 0, 0, 0});
247 mappings.push_back({"X", "Reset camera distance (3D)", 88, 0, false, 0, 0, 0});
248 mappings.push_back({"3", "Toggle 2D/3D mode", 51, 0, false, 0, 0, 0});
249 mappings.push_back({"Plus/Equal", "Camera distance increase (3D)", 61, 0, false, 0, 0, 0});
250 mappings.push_back({"Minus", "Camera distance decrease (3D)", 45, 0, false, 0, 0, 0});
251 mappings.push_back({"N", "Toggle random autopilot crossfade", 78, 0, false, 0, 0, 0});
252 mappings.push_back({"Page Up", "Time speed increase", 266, 0, false, 0, 0, 0});
253 mappings.push_back({"Page Down", "Time speed decrease", 267, 0, false, 0, 0, 0});
254 mappings.push_back({"W", "Camera pitch up (3D)", 87, 0, false, 0, 0, 0});
255 mappings.push_back({"S", "Camera pitch down (3D)", 83, 0, false, 0, 0, 0});
256 mappings.push_back({"A", "Camera yaw left (3D)", 65, 0, false, 0, 0, 0});
257 mappings.push_back({"D", "Camera yaw right (3D)", 68, 0, false, 0, 0, 0});
258 mappings.push_back({"1", "Camera movement speed increase (3D)", 49, 0, false, 0, 0, 0});
259 mappings.push_back({"2", "Camera movement speed decrease (3D)", 50, 0, false, 0, 0, 0});
260 mappings.push_back({"B", "(3D) unused / available", 66, 0, false, 0, 0, 0});
261 mappings.push_back({"K", "Toggle shader lock", 75, 0, false, 0, 0, 0});
262 mappings.push_back({"R", "Toggle random multipass mode", 82, 0, false, 0, 0, 0});
263 mappings.push_back({"G", "Generate new random shader chain", 71, 0, false, 0, 0, 0});
264 mappings.push_back({"H", "Generate long random chain (up to 10)", 72, 0, false, 0, 0, 0});
265 mappings.push_back({"F", "Generate short random pair (2 shaders)", 70, 0, false, 0, 0, 0});
266 mappings.push_back({"End", "Toggle spectrum sensitivity scaling", 269, 0, false, 0, 0, 0});
267 mappings.push_back({"J", "Toggle autopilot (random)", 74, 0, false, 0, 0, 0});
268 mappings.push_back({"Y", "Toggle sequential autopilot", 89, 0, false, 0, 0, 0});
269 mappings.push_back({"4", "Take TIFF snapshot", 52, 0, false, 0, 0, 0});
270 mappings.push_back({"5", "Take HDR snapshot", 53, 0, false, 0, 0, 0});
271 mappings.push_back({"6", "Take RAW snapshot", 54, 0, false, 0, 0, 0});
272 mappings.push_back({"T", "Toggle active time", 84, 0, false, 0, 0, 0});
273 mappings.push_back({"Q", "Toggle audio time", 81, 0, false, 0, 0, 0});
274 mappings.push_back({"Home", "Toggle audio delta", 268, 0, false, 0, 0, 0});
275 mappings.push_back({"Insert", "Audio sensitivity increase", 260, 0, false, 0, 0, 0});
276 mappings.push_back({"Delete", "Audio sensitivity decrease", 261, 0, false, 0, 0, 0});
277 mappings.push_back({"F9", "Toggle HUD overlay visibility", 298, 0, false, 0, 0, 0});
278 mappings.push_back({"Left Bracket", "Crossfade shader previous", 91, 0, false, 0, 0, 0});
279 mappings.push_back({"Right Bracket", "Crossfade shader next", 93, 0, false, 0, 0, 0});
280}
281
283 if (index < 0 || index > 1) {
284 return;
285 }
286
287 const MidiTargetProfile next_profile = index == 0 ? MidiTargetProfile::Acmx2 : MidiTargetProfile::Acmxvk;
288 if (next_profile == target_profile) {
289 return;
290 }
291
292 if (capturing) {
293 capturing = false;
294 captureRow = -1;
295 captureButton->setText("Capture Selected");
296 captureButton->setEnabled(deviceOpen);
297 }
298
299 const std::size_t previous_profile_index = target_profile == MidiTargetProfile::Acmx2 ? 0U : 1U;
300 const std::size_t next_profile_index = next_profile == MidiTargetProfile::Acmx2 ? 0U : 1U;
301 profile_mappings[previous_profile_index] = mappings;
302 const std::vector<MidiMapping> previous_mappings = mappings;
303 target_profile = next_profile;
304
305 if (profile_mappings[next_profile_index].empty()) {
307 for (MidiMapping &mapping : mappings) {
308 const bool target_specific_meaning = mapping.key2 == 0 && (mapping.key1 == 53 || mapping.key1 == 70 || mapping.key1 == 91 || mapping.key1 == 93);
309 if (target_specific_meaning) {
310 continue;
311 }
312 const auto previous = std::find_if(previous_mappings.begin(), previous_mappings.end(), [&](const MidiMapping &candidate) { return candidate.captured && candidate.key1 == mapping.key1 && candidate.key2 == mapping.key2; });
313 if (previous == previous_mappings.end()) {
314 continue;
315 }
316 mapping.captured = true;
317 mapping.byte0 = previous->byte0;
318 mapping.byte1 = previous->byte1;
319 mapping.byte2 = previous->byte2;
320 }
321 profile_mappings[next_profile_index] = mappings;
322 } else {
323 mappings = profile_mappings[next_profile_index];
324 }
325
326 const QString target_name = target_profile == MidiTargetProfile::Acmx2 ? "ACMX2" : "ACMXVK";
327 setWindowTitle(target_name + " MIDI Map Configuration");
328 updateTable();
329 setStatus(QString("%1 profile selected — %2 actions available; shared "
330 "captured mappings preserved")
331 .arg(target_name)
332 .arg(mappings.size()));
333}
334
336 table->setRowCount(static_cast<int>(mappings.size()));
337 for (int i = 0; i < static_cast<int>(mappings.size()); ++i) {
338 const auto &m = mappings[i];
339 table->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(m.actionName)));
340 table->setItem(i, 1, new QTableWidgetItem(QString::fromStdString(m.description)));
341
342 QString keys = (m.key2 != 0) ? QString("%1:%2").arg(m.key1).arg(m.key2) : QString("%1:0").arg(m.key1);
343 table->setItem(i, 2, new QTableWidgetItem(keys));
344
345 if (m.captured) {
346 if (m.key2 != 0) {
347 // Knob: only byte0:byte1 matter for matching
348 table->setItem(i, 3, new QTableWidgetItem(QString("CC %1 %2 (knob)").arg(m.byte0).arg(m.byte1)));
349 auto *statusItem = new QTableWidgetItem("Knob mapped");
350 statusItem->setForeground(QBrush(QColor("#00ff00")));
351 table->setItem(i, 4, statusItem);
352 } else {
353 table->setItem(i, 3, new QTableWidgetItem(QString("%1 %2 %3").arg(m.byte0).arg(m.byte1).arg(m.byte2)));
354 auto *statusItem = new QTableWidgetItem("Mapped");
355 statusItem->setForeground(QBrush(QColor("#00ff00")));
356 table->setItem(i, 4, statusItem);
357 }
358 } else {
359 table->setItem(i, 3, new QTableWidgetItem("—"));
360 auto *statusItem = new QTableWidgetItem("Not mapped");
361 statusItem->setForeground(QBrush(QColor("#666666")));
362 table->setItem(i, 4, statusItem);
363 }
364 }
365}
366
368 deviceCombo->clear();
369 if (!midiIn)
370 return;
371
372 unsigned int ports = midiIn->getPortCount();
373 if (ports == 0) {
374 deviceCombo->addItem("No MIDI devices found");
375 captureButton->setEnabled(false);
376 setStatus("No MIDI devices detected");
377 return;
378 }
379
380 for (unsigned int i = 0; i < ports; ++i) {
381 deviceCombo->addItem(QString::fromStdString(midiIn->getPortName(i)));
382 }
383 captureButton->setEnabled(true);
384 setStatus(QString("%1 MIDI device(s) found").arg(ports));
385}
386
388 if (!midiIn || index < 0)
389 return;
390
391 if (deviceOpen) {
392 midiIn->closePort();
393 pollTimer->stop();
394 deviceOpen = false;
395 }
396
397 try {
398 midiIn->openPort(static_cast<unsigned int>(index));
399 deviceOpen = true;
400 pollTimer->start(10);
401 setStatus(QString("Opened: %1").arg(deviceCombo->currentText()));
402 } catch (RtMidiError &e) {
403 QMessageBox::critical(this, "Error", QString("Could not open port: %1").arg(e.getMessage().c_str()));
404 setStatus("Failed to open device");
405 }
406}
407
409 if (!deviceOpen) {
410 QMessageBox::warning(this,
411 "No Device",
412 "Please select and open a MIDI device first.\n"
413 "Click a device in the dropdown to connect.");
414 return;
415 }
416
417 int row = table->currentRow();
418 if (row < 0 || row >= static_cast<int>(mappings.size())) {
419 QMessageBox::information(this,
420 "Select Action",
421 "Select a row in the table, then click Capture.\n"
422 "Move a knob or press a button on your MIDI controller.");
423 return;
424 }
425
426 capturing = true;
427 captureRow = row;
428 setStatus(QString("Waiting for MIDI input for: %1 — move a knob or press a button...").arg(QString::fromStdString(mappings[row].actionName)));
429 captureButton->setText("Listening...");
430 captureButton->setEnabled(false);
431}
432
434 int row = table->currentRow();
435 if (row < 0 || row >= static_cast<int>(mappings.size())) {
436 QMessageBox::information(this, "Select Action", "Select a row in the table to clear its mapping.");
437 return;
438 }
439
440 mappings[row].captured = false;
441 mappings[row].byte0 = 0;
442 mappings[row].byte1 = 0;
443 mappings[row].byte2 = 0;
444 updateTable();
445 table->selectRow(row);
446 setStatus(QString("Cleared mapping for: %1").arg(QString::fromStdString(mappings[row].actionName)));
447}
448
450 if (!midiIn || !deviceOpen)
451 return;
452
453 std::vector<unsigned char> message;
454 midiIn->getMessage(&message);
455
456 if (message.size() < 3)
457 return;
458
459 midiMonitorLabel->setText(QString("Last MIDI: [%1 %2 %3] (ch %4)").arg(message[0]).arg(message[1]).arg(message[2]).arg((message[0] & 0x0F) + 1));
460
461 if (capturing && captureRow >= 0 && captureRow < static_cast<int>(mappings.size())) {
462 mappings[captureRow].byte0 = message[0];
463 mappings[captureRow].byte1 = message[1];
464 // For knob actions (key2 != 0), byte2 is ignored at runtime —
465 // acmx2 uses the live value to determine direction (>64 = key1, <=64 = key2).
466 // Store 0 to make it clear only byte0:byte1 matter for matching.
467 mappings[captureRow].byte2 = (mappings[captureRow].key2 != 0) ? 0 : message[2];
468 mappings[captureRow].captured = true;
469
470 updateTable();
471 table->selectRow(captureRow);
472
473 QString statusMsg;
474 if (mappings[captureRow].key2 != 0) {
475 statusMsg = QString("Captured knob CC [%1 %2] for: %3 — value >64 = %4, <=64 = %5").arg(message[0]).arg(message[1]).arg(QString::fromStdString(mappings[captureRow].actionName)).arg(QString::fromStdString(mappings[captureRow].actionName).split('/').first().trimmed()).arg(QString::fromStdString(mappings[captureRow].actionName).split('/').last().trimmed());
476 } else {
477 statusMsg = QString("Captured [%1 %2 %3] for: %4").arg(message[0]).arg(message[1]).arg(message[2]).arg(QString::fromStdString(mappings[captureRow].actionName));
478 }
479 setStatus(statusMsg);
480
481 capturing = false;
482 captureRow = -1;
483 captureButton->setText("Capture Selected");
484 captureButton->setEnabled(true);
485 }
486}
487
489 QString fileName = QFileDialog::getSaveFileName(this, "Save MIDI Config", fileEdit->text(), "MIDI Config (*.midi_cfg);;All Files (*)");
490 if (fileName.isEmpty())
491 return;
492
493 std::ofstream file(fileName.toStdString());
494 if (!file.is_open()) {
495 QMessageBox::critical(this, "Error", "Could not open file for writing.");
496 return;
497 }
498
499 int count = 0;
500 for (const auto &m : mappings) {
501 if (!m.captured)
502 continue;
503 file << m.key1 << ":" << m.key2 << " {" << static_cast<int>(m.byte0) << " " << static_cast<int>(m.byte1) << " " << static_cast<int>(m.byte2) << "}\n";
504 ++count;
505 }
506 file.close();
507 fileEdit->setText(fileName);
508 setStatus(QString("Saved %1 mapping(s) to %2").arg(count).arg(fileName));
509}
510
512 QString fileName = QFileDialog::getOpenFileName(this, "Load MIDI Config", fileEdit->text(), "MIDI Config (*.midi_cfg);;All Files (*)");
513 if (fileName.isEmpty())
514 return;
515
516 std::ifstream file(fileName.toStdString());
517 if (!file.is_open()) {
518 QMessageBox::critical(this, "Error", "Could not open file for reading.");
519 return;
520 }
521
522 // Clear existing captures
523 for (auto &m : mappings) {
524 m.captured = false;
525 m.byte0 = 0;
526 m.byte1 = 0;
527 m.byte2 = 0;
528 }
529
530 std::string line;
531 int loaded = 0;
532 while (std::getline(file, line)) {
533 std::istringstream iss(line);
534 std::string keyPair;
535 if (!(iss >> keyPair))
536 continue;
537
538 auto colonPos = keyPair.find(':');
539 if (colonPos == std::string::npos)
540 continue;
541
542 int k1 = std::stoi(keyPair.substr(0, colonPos));
543 int k2 = std::stoi(keyPair.substr(colonPos + 1));
544
545 char brace;
546 if (!(iss >> brace) || brace != '{')
547 continue;
548
549 int b0, b1, b2;
550 if (!(iss >> b0 >> b1 >> b2))
551 continue;
552
553 for (auto &m : mappings) {
554 if (m.key1 == k1 && m.key2 == k2) {
555 m.byte0 = static_cast<unsigned char>(b0);
556 m.byte1 = static_cast<unsigned char>(b1);
557 m.byte2 = static_cast<unsigned char>(b2);
558 m.captured = true;
559 ++loaded;
560 break;
561 }
562 }
563 }
564 file.close();
565
566 updateTable();
567 fileEdit->setText(fileName);
568 setStatus(QString("Loaded %1 mapping(s) from %2").arg(loaded).arg(fileName));
569}
570
571void MidiMapWindow::setStatus(const QString &msg) { statusBar()->showMessage(msg); }
QPushButton * captureButton
std::vector< MidiMapping > mappings
std::array< std::vector< MidiMapping >, 2 > profile_mappings
QPushButton * refreshButton
MidiMapWindow(QWidget *parent=nullptr)
QLabel * midiMonitorLabel
void loadConfig()
Load mappings from a config file.
void change_target(int index)
Switch between ACMX2 and ACMXVK action definitions.
QPushButton * loadButton
void captureMapping()
Start capture mode for the currently selected mapping row.
QPushButton * clearButton
void pollMidi()
Poll RtMidi input and apply capture logic.
void saveConfig()
Save mappings to a config file.
MidiTargetProfile target_profile
void clearMapping()
Clear mapping bytes for the selected row.
QComboBox * target_combo
void openDevice(int index)
Open selected MIDI input device.
~MidiMapWindow() override
QPushButton * saveButton
void refreshDevices()
Refresh available MIDI input devices.
QLineEdit * fileEdit
RtMidiIn * midiIn
void setStatus(const QString &msg)
void applyStyleSheet()
QTableWidget * table
QComboBox * deviceCombo
QTimer * pollTimer
void populateActions()
MIDI mapping editor window for action-to-message binding.
MidiTargetProfile
Single MIDI action binding entry.