-
Notifications
You must be signed in to change notification settings - Fork 10
/
MidiClient.cpp
258 lines (209 loc) · 8.35 KB
/
MidiClient.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
#include "stdafx.h"
#include "WinRTMidi.h"
#include "WindowsVersionHelper.h"
#include <iostream>
#include <string>
#include <conio.h>
#define USING_APP_MANIFEST
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
using namespace std;
using namespace WinRT;
CRITICAL_SECTION gCriticalSection;
// WinRTMidi DLL function pointers
WinRTWatcherPortCountFunc gWatcherPortCountFunc = nullptr;
WinRTWatcherPortNameFunc gWatcherPortNameFunc = nullptr;
WinRTWatcherPortTypeFunc gWatcherPortTypeFunc = nullptr;
WinRTMidiInitializeFunc gMidiInitFunc = nullptr;
WinRTMidiFreeFunc gMidiFreeFunc = nullptr;
WinRTMidiGetPortWatcherFunc gMidiGetPortWatcher = nullptr;
WinRTMidiInPortOpenFunc gMidiInPortOpenFunc = nullptr;
WinRTMidiInPortFreeFunc gMidiInPortFreeFunc = nullptr;
WinRTMidiOutPortOpenFunc gMidiOutPortOpenFunc = nullptr;
WinRTMidiOutPortFreeFunc gMidiOutPortFreeFunc = nullptr;
WinRTMidiOutPortSendFunc gMidiOutPortSendFunc = nullptr;
// Midi Out port
WinRTMidiOutPortPtr gMidiOutPort = nullptr;
void printPortNames(const WinRTMidiPortWatcherPtr watcher)
{
if(watcher == nullptr)
{
return;
}
WinRTMidiPortType type = gWatcherPortTypeFunc(watcher);
if(type == In)
{
cout << "MIDI In Ports" << endl;
}
else
{
cout << "MIDI Out Ports" << endl;
}
int nPorts = gWatcherPortCountFunc(watcher);
for (int i = 0; i < nPorts; i++)
{
const char* name = gWatcherPortNameFunc(watcher, i);
cout << i << ": " << gWatcherPortNameFunc(watcher, i) << endl;
}
cout << endl;
}
void midiPortChangedCallback(const WinRTMidiPortWatcherPtr portWatcher, WinRTMidiPortUpdateType update)
{
EnterCriticalSection(&gCriticalSection);
string portName = gWatcherPortTypeFunc(portWatcher) == In ? "In" : "Out";
switch (update)
{
case PortAdded:
cout << "***MIDI " << portName << " port added***" << endl;
break;
case PortRemoved:
cout << "***MIDI " << portName << " port removed***" << endl;
break;
case EnumerationComplete:
cout << "***MIDI " << portName << " port enumeration complete***" << endl;
break;
}
printPortNames(portWatcher);
LeaveCriticalSection(&gCriticalSection);
}
void midiInCallback(const WinRTMidiInPortPtr port, double timeStamp, const unsigned char* message, unsigned int nBytes)
{
if(gMidiOutPort != nullptr)
{
gMidiOutPortSendFunc(gMidiOutPort, message, nBytes);
}
EnterCriticalSection(&gCriticalSection);
for (unsigned int i = 0; i < nBytes; i++)
{
cout << "Byte " << i << " = " << (int)message[i] << ", ";
}
if(nBytes > 0)
{
cout << "timestamp = " << timeStamp << endl;
}
LeaveCriticalSection(&gCriticalSection);
}
int main()
{
HINSTANCE dllHandle = NULL;
WinRTMidiPtr midiPtr = nullptr;
WinRTMidiInPortPtr midiInPort = nullptr;
//Load the WinRTMidi dll
#ifdef USING_APP_MANIFEST
if(windows10orGreaterWithManifest())
{
dllHandle = LoadLibrary(L"WinRTMidi.dll");
}
#else
if (windows10orGreater())
{
dllHandle = LoadLibrary(L"WinRTMidi.dll");
}
#endif
if(NULL == dllHandle)
{
cout << "Unable to load WinRTMidi.dll" << endl;
goto cleanup;
}
InitializeCriticalSection(&gCriticalSection);
// GetWinRTMidi DLL function pointers. Error checking needs to be added!
//Get pointer to the WinRTMidiInitializeFunc function using GetProcAddress:
gMidiInitFunc = reinterpret_cast<WinRTMidiInitializeFunc>(::GetProcAddress(dllHandle, "winrt_initialize_midi"));
//Get pointer to the WinRTMidiFreeFunc function using GetProcAddress:
gMidiFreeFunc = reinterpret_cast<WinRTMidiFreeFunc>(::GetProcAddress(dllHandle, "winrt_free_midi"));
//Get pointer to the WinRTMidiGetPortWatcherFunc function using GetProcAddress:
gMidiGetPortWatcher = reinterpret_cast<WinRTMidiGetPortWatcherFunc>(::GetProcAddress(dllHandle, "winrt_get_portwatcher"));
//Get pointer to the WinRTMidiInPortOpenFunc function using GetProcAddress:
gMidiInPortOpenFunc = reinterpret_cast<WinRTMidiInPortOpenFunc>(::GetProcAddress(dllHandle, "winrt_open_midi_in_port"));
//Get pointer to the WinRTMidiInPortFreeFunc function using GetProcAddress:
gMidiInPortFreeFunc = reinterpret_cast<WinRTMidiInPortFreeFunc>(::GetProcAddress(dllHandle, "winrt_free_midi_in_port"));
//Get pointer to the WinRTMidiOutPortOpenFunc function using GetProcAddress:
gMidiOutPortOpenFunc = reinterpret_cast<WinRTMidiOutPortOpenFunc>(::GetProcAddress(dllHandle, "winrt_open_midi_out_port"));
//Get pointer to the WinRTMidiOutPortFreeFunc function using GetProcAddress:
gMidiOutPortFreeFunc = reinterpret_cast<WinRTMidiOutPortFreeFunc>(::GetProcAddress(dllHandle, "winrt_free_midi_out_port"));
//Get pointer to the WinRTMidiOutPortSendFunc function using GetProcAddress:
gMidiOutPortSendFunc = reinterpret_cast<WinRTMidiOutPortSendFunc>(::GetProcAddress(dllHandle, "winrt_midi_out_port_send"));
//Get pointer to the WinRTWatcherPortCountFunc function using GetProcAddress:
gWatcherPortCountFunc = reinterpret_cast<WinRTWatcherPortCountFunc>(::GetProcAddress(dllHandle, "winrt_watcher_get_port_count"));
//Get pointer to the WinRTWatcherPortCountFunc function using GetProcAddress:
gWatcherPortNameFunc = reinterpret_cast<WinRTWatcherPortNameFunc>(::GetProcAddress(dllHandle, "winrt_watcher_get_port_name"));
//Get pointer to the WinRTWatcherPortCountFunc function using GetProcAddress:
gWatcherPortTypeFunc = reinterpret_cast<WinRTWatcherPortTypeFunc>(::GetProcAddress(dllHandle, "winrt_watcher_get_port_type"));
// initialize Midi interface
WinRTMidiErrorType result = gMidiInitFunc(midiPortChangedCallback, &midiPtr);
if(result != WINRT_NO_ERROR)
{
cout << "Unable to initialize WinRTMidi" << endl;
goto cleanup;
}
// open midi in port 0
result = gMidiInPortOpenFunc(midiPtr, 0, midiInCallback, &midiInPort);
if(result != WINRT_NO_ERROR)
{
cout << "Unable to create Midi In port" << endl;
goto cleanup;
}
// open midi out port 0
result = gMidiOutPortOpenFunc(midiPtr, 0, &gMidiOutPort);
if(result != WINRT_NO_ERROR)
{
cout << "Unable to create Midi Out port" << endl;
goto cleanup;
}
// send a note on message to the midi out port
unsigned char buffer[3] = { 144, 60 , 127 };
cout << "Sending Note On to midi output port 0" << endl;
gMidiOutPortSendFunc(gMidiOutPort, buffer, 3);
Sleep(500);
// send a note off message to the midi out port
cout << "Sending Note Off to midi output port 0" << endl;
buffer[0] = 128;
gMidiOutPortSendFunc(gMidiOutPort, buffer, 3);
// example on how get midi port info
const WinRTMidiPortWatcherPtr watcher = gMidiGetPortWatcher(midiPtr, In);
if(watcher != nullptr)
{
unsigned int numPorts = gWatcherPortCountFunc(watcher);
if(numPorts > 0)
{
const char* name = gWatcherPortNameFunc(watcher, 0);
}
}
cout << endl << "Sending midi in data to midi out port..." << endl;
cleanup:
// process midi until user presses key on keyboard
cout << "Press any key to exit..." << endl;
char c = _getch();
// clean up midi objects
// okay to call with nullptrs
if(gMidiOutPortFreeFunc)
{
gMidiOutPortFreeFunc(gMidiOutPort);
}
if(gMidiInPortFreeFunc)
{
gMidiInPortFreeFunc(midiInPort);
}
if(gMidiFreeFunc)
{
gMidiFreeFunc(midiPtr);
}
//Free the library:
if(dllHandle)
{
FreeLibrary(dllHandle);
}
DeleteCriticalSection(&gCriticalSection);
return 0;
}