Debian Codegraph @

src/engine.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * midisnoop - MIDI monitor and prober
3 * Copyright (C) 2012 Devin Anderson
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675 Mass
17 * Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <cassert>
21
22 #include <QtCore/QDebug>
23
24 #include "engine.h"
25 #include "error.h"
26
27 // RtError was renamed to RtMidiError in rtmidi 2.1
•28 typedef RtMidiError RtError;
29
30 #if QT_VERSION >= 0x040700
31 #include <QtCore/QDateTime>
32 #else
33 #include <cerrno>
34 #include <cstring>
35 #include <sys/time.h>
36 #endif
37
38 // Static functions
39
40 void
41 Engine::handleMidiInput(double timeStamp, std::vector<unsigned char> *message,
42 void *engine)
43 {
44 static_cast<Engine *>(engine)->handleMidiInput(timeStamp, *message);
45 }
46
47 // Class definition
48
49 Engine::Engine(QObject *parent):
50 QObject(parent)
51 {
52 // Get available MIDI drivers.
53 std::vector<RtMidi::Api> apis;
54 RtMidi::getCompiledApi(apis);
55 int apiCount = apis.size();
56
57 for (int i = 0; i < apiCount; i++) {
58 RtMidi::Api api = apis[i];
59 switch (api) {
60 case RtMidi::LINUX_ALSA:
61 driverNames.append(tr("ALSA Sequencer"));
62 break;
63 case RtMidi::MACOSX_CORE:
64 driverNames.append(tr("CoreMidi"));
65 break;
66 case RtMidi::UNIX_JACK:
67 driverNames.append(tr("JACK Audio Connection Kit"));
68 break;
69 case RtMidi::WINDOWS_MM:
70 driverNames.append(tr("Windows Multimedia MIDI"));
71 break;
72 default:
73 qWarning() << tr("Unexpected MIDI API constant: %1").
74 arg(static_cast<int>(api));
75 // Fallthrough on purpose
76 case RtMidi::RTMIDI_DUMMY:
77 case RtMidi::UNSPECIFIED:
78 continue;
79 }
80 driverAPIs.append(api);
81 }
82
83 driver = -1;
84 ignoreActiveSensingEvents = true;
85 ignoreSystemExclusiveEvents = true;
86 ignoreTimeEvents = true;
87 inputPort = -1;
88 outputPort = -1;
89 }
90
91 Engine::~Engine()
92 {
93 setDriver(-1);
94 }
95
96 int
97 Engine::getDriver() const
98 {
99 return driver;
100 }
101
102 int
103 Engine::getDriverCount() const
104 {
105 return driverAPIs.count();
106 }
107
108 QString
109 Engine::getDriverName(int index) const
110 {
111 assert((index >= 0) && (index < driverAPIs.count()));
112 return driverNames[index];
113 }
114
115 bool
116 Engine::getIgnoreActiveSensingEvents() const
117 {
118 return ignoreActiveSensingEvents;
119 }
120
121 bool
122 Engine::getIgnoreSystemExclusiveEvents() const
123 {
124 return ignoreSystemExclusiveEvents;
125 }
126
127 bool
128 Engine::getIgnoreTimeEvents() const
129 {
130 return ignoreTimeEvents;
131 }
132
133 int
134 Engine::getInputPort() const
135 {
136 return inputPort;
137 }
138
139 int
140 Engine::getInputPortCount() const
141 {
142 return inputPortNames.count();
143 }
144
145 QString
146 Engine::getInputPortName(int index) const
147 {
148 assert((index >= 0) && (index < inputPortNames.count()));
149 return inputPortNames[index];
150 }
151
152 int
153 Engine::getOutputPort() const
154 {
155 return outputPort;
156 }
157
158 int
159 Engine::getOutputPortCount() const
160 {
161 return outputPortNames.count();
162 }
163
164 QString
165 Engine::getOutputPortName(int index) const
166 {
167 assert((index >= 0) && (index < outputPortNames.count()));
168 return outputPortNames[index];
169 }
170
171 quint64
172 Engine::getCurrentTimestamp() const
173 {
174
175 #if QT_VERSION >= 0x040700
176 return QDateTime::currentDateTime().toMSecsSinceEpoch();
177 #else
178 struct timeval time;
179 if (gettimeofday(&time, 0) == -1) {
180 throw Error(tr("failed to get time of day: %1").arg(strerror(errno)));
181 }
182 return (static_cast<quint64>(time.tv_sec) * 1000) +
183 (static_cast<quint64>(time.tv_usec) / 1000);
184 #endif
185
186 }
187
188 void
189 Engine::handleMidiInput(double /*timeStamp*/,
190 const std::vector<unsigned char> &message)
191 {
192 switch (message[0]) {
193 case 0xf0:
194 if (ignoreSystemExclusiveEvents) {
195 qWarning() << "RtMidi did not filter system exclusive event";
196 return;
197 }
198 break;
199 case 0xf1:
200 case 0xf8:
201 case 0xf9:
202 if (ignoreTimeEvents) {
203 qWarning() << "RtMidi did not filter time event";
204 return;
205 }
206 break;
207 case 0xfe:
208 if (ignoreActiveSensingEvents) {
209 qWarning() << "RtMidi did not filter active sensing event";
210 return;
211 }
212 }
213 quint64 timeStamp = getCurrentTimestamp();
214 QByteArray msg;
215 int size = static_cast<int>(message.size());
216 for (int i = 0; i < size; i++) {
217 msg.append(message[i]);
218 }
219 emit messageReceived(timeStamp, msg);
220 }
221
222 void
223 Engine::removePorts()
224 {
225 setInputPort(-1);
226 setOutputPort(-1);
227 for (int i = inputPortNames.count() - 1; i >= 0; i--) {
228 inputPortNames.removeAt(i);
229 emit inputPortRemoved(i);
230 }
231 for (int i = outputPortNames.count() - 1; i >= 0; i--) {
232 outputPortNames.removeAt(i);
233 emit outputPortRemoved(i);
234 }
235 }
236
237 quint64
238 Engine::sendMessage(const QByteArray &message)
239 {
240 assert(outputPort != -1);
241 std::vector<unsigned char> msg;
242 for (int i = 0; i < message.count(); i++) {
243 msg.push_back(static_cast<unsigned char>(message[i]));
244 }
245 try {
246 output->sendMessage(&msg);
247 } catch (RtError &e) {
248 throw Error(e.what());
249 }
250 return getCurrentTimestamp();
251 }
252
253 void
254 Engine::setDriver(int index)
255 {
256 assert((index >= -1) && (index < driverAPIs.count()));
257 if (driver != index) {
258
259 // Close the currently open MIDI driver.
260 if (driver != -1) {
261 removePorts();
262 delete input;
263 delete output;
264 driver = -1;
265 emit driverChanged(-1);
266 }
267
268 // Open the new driver.
269 if (index != -1) {
270 RtMidi::Api api = driverAPIs[index];
271 try {
272 input = new RtMidiIn(api, "midisnoop");
273 QScopedPointer<RtMidiIn> inputPtr(input);
274 output = new RtMidiOut(api, "midisnoop");
275 QScopedPointer<RtMidiOut> outputPtr(output);
276 input->setCallback(handleMidiInput, this);
277
278 // Add ports.
279 try {
280 unsigned int count = input->getPortCount();
281 QString name;
282 for (unsigned int i = 0; i < count; i++) {
283 name = QString::fromStdString(input->getPortName(i));
284 inputPortNames.append(name);
285 emit inputPortAdded(i, name);
286 }
287 count = output->getPortCount();
288 for (unsigned int i = 0; i < count; i++) {
289 name = QString::fromStdString(output->getPortName(i));
290 outputPortNames.append(name);
291 emit outputPortAdded(i, name);
292 }
293
294 // Add a virtual port to drivers that support virtual ports.
295 switch (api) {
296 case RtMidi::LINUX_ALSA:
297 case RtMidi::MACOSX_CORE:
298 case RtMidi::UNIX_JACK:
299 name = tr("[virtual input]");
300 inputPortNames.append(name);
301 emit inputPortAdded(inputPortNames.count() - 1, name);
302 name = tr("[virtual output]");
303 outputPortNames.append(name);
304 emit outputPortAdded(outputPortNames.count() - 1, name);
305 virtualPortsAdded = true;
306 break;
307 default:
308 virtualPortsAdded = false;
309 }
310 } catch (...) {
311 removePorts();
312 throw;
313 }
314 inputPtr.take();
315 outputPtr.take();
316 } catch (RtError &e) {
317 throw Error(e.what());
318 }
319 driver = index;
320 emit driverChanged(index);
321 }
322 }
323 }
324
325 void
326 Engine::setIgnoreActiveSensingEvents(bool ignore)
327 {
328 if (ignoreActiveSensingEvents != ignore) {
329 ignoreActiveSensingEvents = ignore;
330 updateEventFilter();
331 emit ignoreActiveSensingEventsChanged(ignore);
332 }
333 }
334
335 void
336 Engine::setIgnoreSystemExclusiveEvents(bool ignore)
337 {
338 if (ignoreSystemExclusiveEvents != ignore) {
339 ignoreSystemExclusiveEvents = ignore;
340 updateEventFilter();
341 emit ignoreSystemExclusiveEventsChanged(ignore);
342 }
343 }
344
345 void
346 Engine::setIgnoreTimeEvents(bool ignore)
347 {
348 if (ignoreTimeEvents != ignore) {
349 ignoreTimeEvents = ignore;
350 updateEventFilter();
351 emit ignoreTimeEventsChanged(ignore);
352 }
353 }
354
355 void
356 Engine::setInputPort(int index)
357 {
358 assert((index >= -1) && (index < inputPortNames.count()));
359 if (inputPort != index) {
360
361 // Close the currently open input port.
362 if (inputPort != -1) {
363 try {
364 input->closePort();
365 } catch (RtError &e) {
366 qWarning() << e.what();
367 }
368 inputPort = -1;
369 emit inputPortChanged(-1);
370 }
371
372 // Open the new input port.
373 if (index != -1) {
374 try {
375 if (virtualPortsAdded &&
376 (index == (inputPortNames.count() - 1))) {
377 input->openVirtualPort("MIDI Input");
378 } else {
379 input->openPort(index, "MIDI Input");
380 }
381 } catch (RtError &e) {
382 throw Error(e.what());
383 }
384 inputPort = index;
385 updateEventFilter();
386 emit inputPortChanged(index);
387 }
388 }
389 }
390
391 void
392 Engine::setOutputPort(int index)
393 {
394 assert((index >= -1) && (index < outputPortNames.count()));
395 if (outputPort != index) {
396
397 // Close the currently open output port.
398 if (outputPort != -1) {
399 try {
400 output->closePort();
401 } catch (RtError &e) {
402 qWarning() << e.what();
403 }
404 outputPort = -1;
405 emit outputPortChanged(-1);
406 }
407
408 // Open the new output port.
409 if (index != -1) {
410 try {
411 if (virtualPortsAdded &&
412 (index == (outputPortNames.count() - 1))) {
413 output->openVirtualPort("MIDI Output");
414 } else {
415 output->openPort(index, "MIDI Output");
416 }
417 } catch (RtError &e) {
418 throw Error(e.what());
419 }
420 outputPort = index;
421 emit outputPortChanged(index);
422 }
423 }
424 }
425
426 void
427 Engine::updateEventFilter()
428 {
429 if (inputPort != -1) {
430 input->ignoreTypes(ignoreSystemExclusiveEvents, ignoreTimeEvents,
431 ignoreActiveSensingEvents);
432 }
433 }