-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmidi.h
165 lines (141 loc) · 5.25 KB
/
tmidi.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef TMIDI_H
#define TMIDI_H
#include "RtMidi.h"
#include <QObject>
#include <QStringList>
#include <QDomDocument>
#include <QDomElement>
class tCommand
{
public:
tCommand() {}
tCommand(uint8_t cmd_, QString str_) {
cmd = cmd_;
cmdStr = str_;
}
uint8_t cmd;
QString cmdStr;
};
class tShortMessage
{
public:
typedef enum e_Command {
UNKNOWN = 0x00,
NOTE_OFF = 0x80, // 128
NOTE_ON = 0x90, // 144
POLY_PRESSURE = 0xA0, // 160
CONTROL_CHANGE = 0xB0, // 176
PROGRAM_CHANGE = 0xC0, // 192
CHANNEL_PRESSURE = 0xD0, // 208
PITCH_BEND = 0xE0 // 224
} Command;
typedef enum e_ColorAPCMini {
BLACK = 0,
GREEN = 1,
GREEN_BLINK = 2,
RED = 3,
RED_BLINK = 4,
ORANGE = 5,
ORANGE_BLINK = 6
} ColorAPCMini;
tShortMessage() { command=UNKNOWN; channel=0; number=0; value=0; valueMin=0, valueMax=0; }
tShortMessage(uint8_t command_, uint8_t channel_, uint8_t data1_, uint8_t data2_) { command=command_; channel=channel_; number=data1_; value=data2_; valueMin=0; valueMax=0; }
// Return Command in string mode
static QString getStringCommand(uint8_t command_) {
switch (command_) {
case Command::NOTE_ON : return kListCommand.at(0).cmdStr; break;
case Command::NOTE_OFF : return kListCommand.at(1).cmdStr; break;
case Command::POLY_PRESSURE : return kListCommand.at(2).cmdStr; break;
case Command::CONTROL_CHANGE : return kListCommand.at(3).cmdStr; break;
case Command::PROGRAM_CHANGE : return kListCommand.at(4).cmdStr; break;
case Command::CHANNEL_PRESSURE : return kListCommand.at(5).cmdStr; break;
case Command::PITCH_BEND : return kListCommand.at(6).cmdStr; break;
default: return QString("UNKNOWN_COMMAND"); break;
}
}
// Return Command from String
static uint8_t getCommandFromString(QString cmd_) {
tCommand cmd;
foreach (cmd, kListCommand) {
if (cmd.cmdStr == cmd_) {
return cmd.cmd;
}
}
return 0;
}
bool isEqual(tShortMessage *msg) {
if (command != msg->command) return false;
if (channel != msg->channel) return false;
if (number != msg->number) return false;
// if (value != msg->value) return false;
return true;
}
void clear() {
command=UNKNOWN;
channel=0;
number=0;
value=0;
valueMin=0;
valueMax=0;
deltaTime=0;
}
// Convert Data to Xml
void toXml(QDomElement &child) {
child.setAttribute("command", this->command);
child.setAttribute("channel", this->channel);
child.setAttribute("number", this->number);
child.setAttribute("value", this->value);
child.setAttribute("valueMin", this->valueMin);
child.setAttribute("valueMax", this->valueMax);
}
// Convert Xml to Data
void fromXml(QDomElement &node) {
this->command = node.attribute("command").toInt();
this->channel = node.attribute("channel").toInt();
this->number = node.attribute("number").toInt();
this->value = node.attribute("value").toInt();
this->valueMin = node.attribute("valueMin").toInt();
this->valueMax = node.attribute("valueMax").toInt();
}
uint8_t command, channel, number, value;
uint8_t valueMin, valueMax;
uint32_t deltaTime;
static const QList<tCommand> kListCommand;
};
class tMidi : public QObject
{
Q_OBJECT
public:
tMidi(QObject *parent = 0);
unsigned int getNbDeviceIn() { return RtMidiIn().getPortCount(); }
unsigned int getNbDeviceOut() { return RtMidiOut().getPortCount(); }
QStringList getDeviceIn();
QStringList getDeviceOut();
QStringList getDeviceInOpen() { return listStringMidiIn; }
QStringList getDeviceOutOpen() { return listStringMidiOut; }
int getIdxFromDeviceIn(QString device_);
int getIdxFromDeviceOut(QString device_);
int openDeviceIn(int idxDevice_);
int openDeviceOut(int idxDevice_);
bool isDeviceInIsOpen(int idxList_);
bool isDeviceOutIsOpen(int idxList_);
void closeDeviceIn(int idxList_);
void closeDeviceIn(QString device_);
void closeDeviceOut(int idxList_);
void closeDeviceOut(QString device_);
void closeAllDevice();
int sendMessage(int idxList_, tShortMessage &msg_);
int sendMessage(QString device_, tShortMessage &msg_);
int getMessage(int idxList_, tShortMessage *msg_);
void toXml(QDomDocument &parent, QDomElement &child);
void fromXml(QDomElement &node);
int setColorToAPCMini(const tShortMessage &msg_, tShortMessage::ColorAPCMini color_);
signals:
void onNewMessageReceive(tShortMessage *msg);
private:
QList<RtMidiIn *> listMidiIn;
QStringList listStringMidiIn;
QList<RtMidiOut *> listMidiOut;
QStringList listStringMidiOut;
};
#endif // TMIDI_H