/
main.cpp
67 lines (57 loc) · 2.24 KB
/
main.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
#include "RtMidi.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
static char buf[1024];
int sample;
bool touch, touchOld = 0;
RtMidiOut *midiout = new RtMidiOut();
vector<unsigned char> message;
message.push_back(0); message.push_back(0); message.push_back(0); // TODO improve
// Check available ports.
unsigned int nPorts = midiout->getPortCount();
if (nPorts == 0) {
midiout->openVirtualPort("NoonMIDI");
cout << "No ports available, creating one: NoonMIDI" << endl;
} else {
midiout->openPort(0); // Open first available port.
cout << "Using port:" << midiout->getPortName(0) << endl;
}
for(;;) {
cin.getline(buf, 1024);
if (*buf) {
char *start=buf, *end;
touch = int(strtol(start, &end, 10));
start = end+1;
sample = int(strtol(start, &end, 10));
// cout << "T:" << touch << " Z:" << sample << endl;
///////////////////////////////////////////////////////////////////
if (touch) {
if (touchOld != touch) { // just started to touch
message[0] = 0x90 | 0x3; // on: channel 3
message[1] = 42; // random note
message[2] = sample; // velocity
midiout->sendMessage(&message);
cout << " ! touch rising edge, velocity: " << sample << endl;
}
// log when press
// cout << "Z:" << sample << endl;
} else { ///////////////////////////////////////////////////////////
if (touchOld != touch) { // just released the touch
cout << " ! touch falling edge !" << endl;
message[0] = 0x80 | 0x3; // off: channel 3
message[1] = 42; // random note
message[2] = 0; // velocity 0
midiout->sendMessage(&message);
}
// ...
}
touchOld = touch;
///////////////////////////////////////////////////////////////////
}
}
delete midiout;
return 0;
}