-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapcKey.cpp
216 lines (154 loc) · 6.11 KB
/
apcKey.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
#include "apcKey.h"
apcKey::apcKey() {
}
void apcKey::setup() {
state = false;
std::cout << std::endl << " === Control Midi === " << std::endl;
unsigned int nbPorts = 0;
std::string portName;
// RtMidiIn and Rt MidiOut constructor
try { midiin = new RtMidiIn(); }
catch (RtMidiError &error ) { printf("apcKey.constructor_er1\n"); error.printMessage(); exit( EXIT_FAILURE ); }
try { midiout = new RtMidiOut(); }
catch (RtMidiError &error ) { printf("apcKey.constructor_er2\n"); error.printMessage(); exit( EXIT_FAILURE ); }
std::string midiName = "APC Key 25";
std::cout << std::endl << " Searching for: " << midiName << std::endl;
// Check inputs.
nbPorts = midiin->getPortCount();
std::cout << nbPorts << " MIDI input sources available." << std::endl;
for (unsigned int i = 0; i<nbPorts; i++) {
try { portName = midiin->getPortName(i); }
catch (RtMidiError &error) { error.printMessage(); exit(EXIT_FAILURE); }
std::cout << "-Input Port #" << i << ": " << portName << std::endl;
if (portName.find(midiName) != std::string::npos) {
state = true;
midiin->openPort(i);
std::cout << "MIDI IN :) OK!" << std::endl;
}
}
// Check outputs.
nbPorts = midiout->getPortCount();
std::cout << nbPorts << " MIDI output ports available.\n";
for (unsigned int i = 0; i<nbPorts; i++) {
try { portName = midiout->getPortName(i); }
catch (RtMidiError &error) { error.printMessage(); exit(EXIT_FAILURE); }
std::cout << "-Output Port #" << i << ": " << portName << std::endl;
if (portName.find(midiName) != std::string::npos) {
state = true;
midiout->openPort(i);
std::cout << "MIDI OUT :) OK!" << std::endl;
}
}
if (state == false)
std::cout << "NO MIDI IN AND OUT :(" << std::endl;
// midiin->ignoreTypes(false, false, false);
// Initialisation
reset();
}
void apcKey::reset() {
for(int i = 0; i < 40; ++i) {
sendMidi(messApcKey(akObj::CLIP, i, 0));
clip[i] = 0;
clipPressed[i] = false;
}
for(int i = 0; i < 8; ++i) {
sendMidi(messApcKey(akObj::STOP, i, 0));
stop[i] = 0;
stopPressed[i] = false;
}
for(int i = 0; i < 5; ++i) {
sendMidi(messApcKey(akObj::SCENE, i, 0));
scene[i] = 0;
scenePressed[i] = false;
}
stopAll = sustain = play = rec = shift = false;
}
apcKey::~apcKey() {
for(int i = 0; i < 40; ++i)
sendMidi(messApcKey(akObj::CLIP, i, 0));
for(int i = 0; i < 8; ++i)
sendMidi(messApcKey(akObj::STOP, i, 0));
for(int i = 0; i < 5; ++i)
sendMidi(messApcKey(akObj::SCENE, i, 0));
delete midiin;
delete midiout;
}
void apcKey::pollMidi() {
std::vector<unsigned char> m;
m.resize(3);
while( midiin->getMessage( &m ) ) { //Hmmm, plantouille, car le premier message à toujours "td == 0"
//std::cout << "_______________" << std::endl;
//for(int i = 0; i < m.size(); ++i)
// std::cout << (int)m[i] << std::endl;
if(m[0] == 177 && m[1] == 64) {
// SUSTAIN, chelou...
listMess.push_back( messApcKey(akObj::SUSTAIN, 0, m[2] == 127 ? 1 : 0));
sustain = m[2] == 127;
} else if( 0 <= m[1] && m[1] <= 39 ) {
// CLIPS
listMess.push_back( messApcKey(akObj::CLIP, (4-m[1]/8)*8 + m[1]%8, m[0] == 144 ? 1 : 0));
clipPressed[ (4-m[1]/8)*8 + m[1]%8 ] = m[0] == 144;
} else if( 82 <= m[1] && m[1] <= 86 ) {
// SCENE
listMess.push_back( messApcKey(akObj::SCENE, m[1]-82, m[0] == 144 ? 1 : 0));
scene[ m[1]-82 ] = m[0] == 144;
} else if( 64 <= m[1] && m[1] <= 71 ) {
// STOP
listMess.push_back( messApcKey(akObj::STOP, m[1]-64, m[0] == 144 ? 1 : 0));
stop[ m[1]-64 ] = m[0] == 144;
} else if( 48 <= m[1] && m[1] <= 55 ) {
// KNOBS
listMess.push_back( messApcKey(akObj::KNOB, m[1]-48, m[2]));
knob[ m[1]-48 ] = m[2];
} else if( m[0] == 145 || m[0] == 129 ) {
// KEYS
listMess.push_back( messApcKey(akObj::KEY, m[1], m[0] == 145 ? m[2] : 0));
} else {
switch( m[1] ) {
case 81: // STOP ALL
listMess.push_back( messApcKey(akObj::STOP_ALL, 0, m[0] == 144 ? m[2] : 0));
stopAll = m[0] == 144;
break;
case 91: // PLAY
listMess.push_back( messApcKey(akObj::PLAY, 0, m[0] == 144 ? m[2] : 0));
play = m[0] == 144;
break;
case 92: // REC
listMess.push_back( messApcKey(akObj::REC, 0, m[0] == 144 ? m[2] : 0));
rec = m[0] == 144;
break;
case 98: // SHIFT
listMess.push_back( messApcKey(akObj::SHIFT, 0, m[0] == 144 ? m[2] : 0));
shift = m[0] == 144;
break;
}
}
}
}
void apcKey::sendMidi(messApcKey _mess) {
// (0, 0 -> 39, 0 -> 6 ) // Black, green, green blink, red, red blink, yellow, yellow blink.
if(!state) return;
std::vector<unsigned char> midiMess;
midiMess.resize(3);
switch(_mess.obj) {
case akObj::CLIP: // Blank, green, green blink, red, red blink, yellow, yellow blink.
midiMess[0] = 144;
midiMess[1] = (4-_mess.pos/8)*8 + _mess.pos%8;
midiMess[2] = _mess.val;
midiout->sendMessage( &midiMess );
break;
case akObj::STOP: // Blank, red, red blink
midiMess[0] = 144;
midiMess[1] = 64 + _mess.pos;
midiMess[2] = _mess.val;
midiout->sendMessage( &midiMess );
break;
case akObj::SCENE: // Blank, green, green blink
midiMess[0] = 144;
midiMess[1] = 82 + _mess.pos;
midiMess[2] = _mess.val;
midiout->sendMessage( &midiMess );
break;
}
//std::cout << "sending midi: " << midiMess[0] << ", " << midiMess[1] << ", " << midiMess[2] << std::endl;
}