-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidifilter.cpp
128 lines (112 loc) · 3.33 KB
/
midifilter.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
#include "RtMidi.h"
#include <string.h>
bool isVerbose = true;
void mycallback(double deltatime, std::vector<unsigned char> *message, void *userData)
{
RtMidiOut *midiout = static_cast<RtMidiOut *>(userData);
// MIDI message format
// byte 0 = status byte
// byte 1 data
// byte 2 data
unsigned int messageSize = message->size();
if (messageSize < 1)
{
// Not sure if emtpy messages can actually get here, but let's ignore
return;
}
unsigned char statusByte = message->at(0);
// Is it a status byte?
if ((statusByte && 0xF0) > 0)
{
// Note on
if (statusByte == 0x90)
{
// Ensure we have the 3rd value (should always be true)
if (messageSize >= 2)
{
unsigned char velocity = message->at(2);
if (velocity == 0)
{
if (isVerbose)
{
std::cout << "Removing 0 velocity note off\n";
}
return;
}
}
}
}
if (isVerbose)
{
std::cout << "Sending ";
for (unsigned i=0; i < messageSize; i++)
{
std::cout << "byte " << i << "=" << (int)message->at(i) << " ";
}
std::cout << "\n";
}
midiout->sendMessage(message);
}
int main()
{
RtMidiIn *midiin = 0;
RtMidiOut *midiout = 0;
try
{
midiin = new RtMidiIn(RtMidi::Api::LINUX_ALSA, "midifilter input");
midiout = new RtMidiOut(RtMidi::Api::LINUX_ALSA, "midifilter output");
}
catch (RtMidiError &error)
{
error.printMessage();
exit(EXIT_FAILURE);
}
unsigned int ports = midiin->getPortCount();
if (ports <= 0)
{
// TODO: change this to a loop style behaviour - detect ports, subscribe
std::cout << "No MIDI input ports detected. Exiting...\n";
exit(0);
}
std::string portName;
for (unsigned int i = 0; i < ports; i++)
{
try
{
portName = midiin->getPortName(i);
// TODO: Use command line as input instead (or subscribe to all?)
std::size_t found = portName.find("VMPK");
if (found == std::string::npos)
{
found = portName.find("mio");
}
if (found != std::string::npos)
{
std::cout << "Connecting to " << portName << " on port " << i << '\n';
midiout->openVirtualPort("midifilter output port");
midiin->openPort(i);
midiin->setCallback(&mycallback, midiout);
// Disable ignoring of sysex, timing and active sense message (we want it all)
midiin->ignoreTypes(false, false, false);
std::cout << "Filter enabled. Press <Ctrl+c> to quit.\n";
std::string str;
while (true)
{
std::getline(std::cin, str);
}
midiin->closePort();
midiout->closePort();
}
}
catch (RtMidiError &error)
{
error.printMessage();
delete midiin;
delete midiout;
exit(EXIT_FAILURE);
}
}
delete midiin;
delete midiout;
return 0;
}