-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidiInput.cpp
More file actions
102 lines (85 loc) · 2.44 KB
/
midiInput.cpp
File metadata and controls
102 lines (85 loc) · 2.44 KB
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
#include "midiInput.h"
#include <iostream>
void callback(double deltatime, std::vector< unsigned char> *message, void *userData)
{
unsigned int nBytes = message->size();
if (nBytes < 3)
{
return;
}
unsigned char statusByte = message->at(0);
unsigned char byte1 = message->at(1);
unsigned char byte2 = message->at(2);
if (statusByte = 0b10010000)
{
// note on message
notes[(int)byte1] = 1.0f;
}
else if (statusByte = 0b10000000)
{
// note off message
notes[(int)byte1] = 0.0f;
}
for ( unsigned int i=0; i<nBytes; i++ )
std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
if ( nBytes > 0 )
std::cout << "stamp = " << deltatime << std::endl;
}
MidiInput::MidiInput()
{
midiin = 0;
try {
// RtMidiIn constructor
midiin = new RtMidiIn();
// Call function to select port.
if ( chooseMidiPort( midiin ) == false )
{
if (midiin)
{
delete midiin;
}
return;
};
// Set our callback function. This should be done immediately after
// opening the port to avoid having incoming messages written to the
// queue instead of sent to the callback function.
midiin->setCallback( callback, this );
// Don't ignore sysex, timing, or active sensing messages.
midiin->ignoreTypes( false, false, false );
} catch ( RtMidiError &error ) {
error.printMessage();
}
}
MidiInput::~MidiInput()
{
delete midiin;
}
bool MidiInput::chooseMidiPort( RtMidiIn *rtmidi )
{
std::string keyHit;
std::string portName;
unsigned int i = 0, nPorts = rtmidi->getPortCount();
if ( nPorts == 0 ) {
std::cout << "No input ports available!" << std::endl;
return false;
}
if ( nPorts == 1 ) {
std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
}
else {
// for ( i=0; i<nPorts; i++ ) {
// portName = rtmidi->getPortName(i);
// std::cout << " Input port #" << i << ": " << portName << '\n';
// }
// do {
// std::cout << "\nChoose a port number: ";
// std::cin >> i;
// } while ( i >= nPorts );
// std::getline( std::cin, keyHit ); // used to clear out stdin
// On my machine, the Behringer DeepMind 12 connects to port 1.
// Just default to that.
i = 1;
}
rtmidi->openPort( i );
return true;
}