forked from Manuel-K/shruthi-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_midiout.cpp
231 lines (192 loc) · 6.54 KB
/
lib_midiout.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Shruthi-Editor: An unofficial Editor for the Shruthi hardware synthesizer. For
// informations about the Shruthi, see <http://www.mutable-instruments.net/shruthi1>.
//
// Copyright (C) 2011 Manuel Krönig
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "lib_midiout.h"
#include <QDebug>
// ******************************************
MidiOut::MidiOut() {
// ******************************************
#ifdef DEBUG
qDebug() << "MidiOut::MidiOut()";
#endif
opened=false;
output=-1;
try {
midiout = new RtMidiOut();
}
catch ( RtError &error ) {
error.printMessage();
qWarning() << "MidiOut::MidiOut(): could not initilize midi port for writing.";
// exit( EXIT_FAILURE );
}
}
// ******************************************
MidiOut::~MidiOut() {
// ******************************************
#ifdef DEBUG
qDebug() << "MidiOut::~MidiOut()";
#endif
delete midiout;
}
// ******************************************
bool MidiOut::open(unsigned int port) {
// ******************************************
#ifdef DEBUG
qDebug() << "MidiOut::open("<<port<<")";
#endif
if (output==port && opened)
return true;
if (opened)
midiout->closePort();
if (port >= midiout->getPortCount() ) {
qWarning() << "MidiOut::open(): trying to open midi port for writing which doesn't exist.";
opened = false;
return false;
}
try {
midiout->openPort( port );
opened = true;
}
catch ( RtError &error ) {
#ifdef DEBUG
qDebug() << "MidiOut::open("<<port<<"): RtError on openPort().";
#endif
opened = false;
}
if (opened)
output = port;
else
qWarning() << "MidiOut::open(): could not open midi port for writing.";
return opened;
}
// ******************************************
bool MidiOut::write(unsigned char sysex[]) {
// ******************************************
if (!opened) {
qDebug() << "MidiOut::write(unsigned char[]): could not send. Port not opened.";
return false;
}
std::vector<unsigned char> message;
int i;
for (i=0; sysex[i]!=247; i++)
message.push_back(sysex[i]);
message.push_back(sysex[i]);
return write(message);
}
// ******************************************
bool MidiOut::write(std::vector<unsigned char> message) {
// ******************************************
if (!opened) {
qDebug() << "MidiOut::write(std::vector<unsigned char>): could not send. Port not opened.";
return false;
}
try {
midiout->sendMessage (&message);
return true;
}
catch ( RtError &error ) {
qDebug() << "MidiOut::write(std::vector<unsigned char>): could not send. Error on sending.";
error.printMessage();
}
return false;
}
// ******************************************
bool MidiOut::write(unsigned char c1,unsigned char c2,unsigned char c3) {
// ******************************************
std::vector<unsigned char> message;
message.push_back( c1 );
message.push_back( c2 );
message.push_back( c3 );
return write( message );
}
// ******************************************
bool MidiOut::write(unsigned char c1,unsigned char c2) {
// ******************************************
std::vector<unsigned char> message;
message.push_back( c1 );
message.push_back( c2 );
return write( message );
}
// ******************************************
// ******************************************
// Wrappers:
// ******************************************
// ******************************************
// ******************************************
bool MidiOut::nrpn(int nrpn, int value) {
// ******************************************
if (!opened) {
qDebug() << "MidiOut::nrpn(): could not send. Port not opened.";
return false;
}
int32_t nrpn_msb=nrpn>>7;
int32_t nrpn_lsb=nrpn%128;
int32_t value_msb;
int32_t value_lsb;
if (value<0) {
value_msb=1;
value_lsb=(value+128)%128;
} else {
value_msb=value>>7;
value_lsb=value%128;
}
return (write(176, 99, nrpn_msb) &&
write(176, 98, nrpn_lsb) &&
write(176, 6, value_msb) &&
write(176, 38, value_lsb));
}
// ******************************************
bool MidiOut::noteOn(unsigned char channel, unsigned char note, unsigned char velocity) {
// ******************************************
return write((144|channel),note,velocity);
}
// ******************************************
bool MidiOut::noteOff(unsigned char channel, unsigned char note) {
// ******************************************
return write((128|channel),note,0);
}
// ******************************************
bool MidiOut::allNotesOff(unsigned char channel) {
// ******************************************
return controlChange(channel,123,0);
}
// ******************************************
bool MidiOut::programChange(unsigned char channel, unsigned char program) {
// ******************************************
if (program>127) {
return controlChange(channel,32,1) && write((192|channel),(program-128));
} else {
return controlChange(channel,32,0) && write((192|channel),program);
}
}
// ******************************************
bool MidiOut::controlChange(unsigned char channel, unsigned char controller, unsigned char value) {
// ******************************************
return write ((176|channel),controller,value);
}
// ******************************************
bool MidiOut::patchTransferRequest() {
// ******************************************
unsigned char patchTransferRequest[]={240,0,32,119,0,2,17,0,0,0,247};
return write (patchTransferRequest);
}
// // ******************************************
// bool MidiOut::patchWriteRequest() {
// // ******************************************
// unsigned char patchTransferRequest[]={240,0,32,119,0,2,17,0,0,0,247};
// return write (patchWriteRequest);
// }