forked from Riateche/piano-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Midi_reader.cpp
60 lines (55 loc) · 1.55 KB
/
Midi_reader.cpp
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
#include "Midi_reader.h"
#include <QStringList>
#include <QFile>
#include <QSettings>
Midi_reader::Midi_reader(PortMidiStream *p_stream):
stream(p_stream),
display(XOpenDisplay(0))
{
read_layout();
connect(&parser, SIGNAL(key_event(Note_set,bool)), this, SLOT(new_event(Note_set,bool)));
connect(&read_timer, SIGNAL(timeout()), this, SLOT(read()));
read_timer.start(10);
}
void Midi_reader::emulate_key(QString key, bool pressed) {
KeySym sym = XStringToKeysym(key.toAscii());
if (sym == NoSymbol) {
qWarning() << "Failed to emulate key: " << key;
return;
}
XTestFakeKeyEvent(display, XKeysymToKeycode(display, sym), pressed, 0);
XFlush(display);
}
void Midi_reader::read() {
PmEvent event;
int c = Pm_Read(stream, &event, 1);
if (c != 0 && Pm_MessageStatus(event.message) == 144) {
parser.add(
Pm_MessageData1 (event.message),
Pm_MessageData2 (event.message)
);
}
}
void Midi_reader::new_event(Note_set set, bool pressed) {
QString s = set.to_string();
qDebug() << s;
if (layout.contains(s)) {
qDebug() << "Matched. Sending key " << layout[s];
emulate_key(layout[s], pressed);
}
}
void Midi_reader::read_layout() {
QSettings s("layout.ini", QSettings::IniFormat);
foreach(QString key, s.allKeys()) {
QString note = s.value(key).toString();
if (note.isEmpty()) {
qWarning() << "Note missing for key: " << key;
} else {
if (layout.contains(note)) {
qWarning() << "Layout duplicate: " << note;
} else {
layout.insert(note, key);
}
}
}
}