-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyMidiIn.cpp
157 lines (121 loc) · 2.95 KB
/
MyMidiIn.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
#include <vector>
#include "DirectMidi/CDirectMidi.h"
#include "MyMidi.h"
#include "MyCriticalSection.h"
using namespace std;
using namespace directmidi;
// Maximum size for SysEx data in input port
#define SYSTEM_EXCLUSIVE_MEM 48000
extern CDirectMusic gDMusic;
extern COutputPort gOutPort;
MyCriticalSection gLock;
list<MidiMessage> gMessages;
class MyReceiver: public CReceiver
{
public:
int input;
virtual void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwBytesRead,BYTE *lpBuffer) {}
virtual void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwMsg) {
gLock.enter();
MidiMessage msg;
CInputPort::DecodeMidiMsg(dwMsg,&msg.command,&msg.channel,&msg.data1,&msg.data2);
msg.input = input;
gMessages.push_back(msg);
gLock.leave();
}
};
class MidiSource {
public:
CInputPort port;
MyReceiver reciver;
};
vector<MidiSource> gSources;
int MyMidi_StartReceiving (int redirect)
{
/*
HMIDIIN handle;
MMRESULT Result = ::midiInOpen(&handle, 0, NULL, NULL, CALLBACK_NULL);
if(Result == MMSYSERR_NOERROR)
{
midiInStart(handle);
midiInStop(handle);
midiInReset(handle);
midiInClose(handle);
}
*/
// Stop all old ports
MyMidi_StopReceiving ();
{
CInputPort temp;
temp.Initialize(gDMusic);
gSources.resize(temp.GetNumPorts());
}
for (DWORD i = 0; i < gSources.size(); ++i)
{
try
{
INFOPORT info;
gSources[i].port.Initialize(gDMusic);
gSources[i].port.GetPortInfo(i+1, &info);
printf ("MyMidi: Opened input port #%d: %s\n", i, info.szPortDescription);
gSources[i].port.ActivatePort(&info, SYSTEM_EXCLUSIVE_MEM);
gSources[i].reciver.input = i;
gSources[i].port.SetReceiver(gSources[i].reciver);
gSources[i].port.ActivateNotification();
if (redirect) gSources[i].port.SetThru(0,0,0, gOutPort);
}
catch (CDMusicException& e)
{
MyMidi_SetLastError(__FUNCTION__, e.GetErrorDescription());
}
}
return 1;
}
void MyMidi_StopReceiving ()
{
for (DWORD i = 0; i < gSources.size(); ++i)
{
try
{
gSources[i].port.BreakThru(0,0,0);
gSources[i].port.TerminateNotification();
gSources[i].port.ReleasePort();
}
catch (CDMusicException& e)
{
MyMidi_SetLastError(__FUNCTION__, e.GetErrorDescription());
}
}
gSources.clear();
}
int MyMidi_PopMessage (MidiMessage* message)
{
gLock.enter();
if (gMessages.empty())
{
gLock.leave();
return 0;
}
*message = gMessages.front();
gMessages.pop_front();
gLock.leave();
return 1;
}
const char* MyMidi_GetInputName (unsigned int input)
{
CInputPort temp;
temp.Initialize(gDMusic);
if (input >= temp.GetNumPorts())
return NULL;
INFOPORT info;
temp.GetPortInfo(input+1, &info);
static string name;
name = info.szPortDescription;
return name.c_str();
}
int MyMidi_GetDeviceCount ()
{
CInputPort temp;
temp.Initialize(gDMusic);
return temp.GetNumPorts();
}