-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMidi.cpp
281 lines (256 loc) · 6.57 KB
/
Midi.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
MINI VIRTUAL ANALOG SYNTHESIZER
Copyright 2014 Kenneth D. Miller III
MIDI Support
*/
#include "StdAfx.h"
#include "Debug.h"
#include "Midi.h"
#include "Voice.h"
#include "Control.h"
#include "Amplifier.h"
// midi messages
// http://www.midi.org/techspecs/midimessages.php
namespace Midi
{
// status types
enum Status
{
MIDI_NOTE_OFF,
MIDI_NOTE_ON,
MIDI_KEY_PRESSURE,
MIDI_CONTROL_CHANGE,
MIDI_PROGRAM_CHANGE,
MIDI_CHANNEL_PRESSURE,
MIDI_PITCH_WHEEL_CHANGE,
MIDI_SYSTEM,
MIDI_COUNT
};
// special channel modes
enum ChannelMode
{
MIDI_ALL_SOUND_OFF = 120,
MIDI_RESET_ALL_CONTROLLERS = 121,
MIDI_LOCAL_CONTROL = 122,
MIDI_ALL_NOTES_OFF = 123,
MIDI_OMNI_MODE_OFF = 124,
MIDI_OMNI_MODE_ON = 125,
MIDI_MONO_MODE_ON = 126,
MIDI_POLY_MODE_ON = 127,
};
namespace Input
{
HMIDIIN handle;
MIDIHDR header;
CHAR sysexbuffer[256];
bool sysexreceive;
bool stopping;
// default to listening on all channels
int listen_channels = ~0U;
void HandleData(DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
unsigned char channel = ((dwParam1)& 0xF) + 1;
unsigned char status = (dwParam1 >> 4) & 0x7;
unsigned char data1 = (dwParam1 >> 8) & 0xFF;
unsigned char data2 = (dwParam1 >> 16) & 0xFF;
unsigned long timestamp = dwParam2;
DebugPrint("%4d.%03ds ", timestamp / 1000, timestamp % 1000);
if (status != MIDI_SYSTEM)
{
DebugPrint("#%02d ", channel);
}
if (!(listen_channels & (1 << channel)))
{
DebugPrint("\n");
return;
}
switch (status)
{
case MIDI_NOTE_OFF:
DebugPrint("Note Off: note=%d velocity=%d\n", data1, data2);
NoteOff(data1, data2);
break;
case MIDI_NOTE_ON:
DebugPrint("Note On: note=%d velocity=%d\n", data1, data2);
if (data2)
NoteOn(data1, data2);
else
NoteOff(data1);
break;
case MIDI_KEY_PRESSURE:
DebugPrint("Key Pressure: note=%d pressure=%d\n", data1, data2);
break;
case MIDI_CONTROL_CHANGE:
switch (data1)
{
default:
DebugPrint("Control Change: control=%d value=%d\n", data1, data2);
break;
case MIDI_ALL_SOUND_OFF:
DebugPrint("All Sound Off\n");
for (int v = 0; v < VOICES; ++v)
{
NoteOff(voice_note[v], 0);
amp_env_state[v].amplitude = 0;
amp_env_state[v].state = EnvelopeState::OFF;
}
break;
case MIDI_RESET_ALL_CONTROLLERS:
DebugPrint("Reset All Controllers\n");
Control::ResetAll();
break;
case MIDI_LOCAL_CONTROL:
DebugPrint("Local Control %s\n", data2 ? "On" : "Off");
break;
case MIDI_ALL_NOTES_OFF:
DebugPrint("All Notes Off\n");
for (int v = 0; v < VOICES; ++v)
NoteOff(voice_note[v], 0);
break;
case MIDI_OMNI_MODE_OFF:
DebugPrint("Omni Mode Off\n");
break;
case MIDI_OMNI_MODE_ON:
DebugPrint("Omni Mode On\n");
break;
case MIDI_MONO_MODE_ON:
DebugPrint("Mono Mode On %d\n", data2);
break;
case MIDI_POLY_MODE_ON:
DebugPrint("Poly Mode On\n");
break;
}
break;
case MIDI_PROGRAM_CHANGE:
DebugPrint("Program Change: program=%d\n", data1);
break;
case MIDI_CHANNEL_PRESSURE:
DebugPrint("Channel Pressure: pressure=%d\n", data1);
break;
case MIDI_PITCH_WHEEL_CHANGE:
DebugPrint("Pitch Wheel Change: value=%d\n", (data2 << 7) + data1 - 0x2000);
Control::SetPitchWheel((data2 << 7) + data1 - 0x2000);
break;
case MIDI_SYSTEM:
DebugPrint("System %02x %02x %02x\n", data1, data2);
break;
}
}
void HandleLongData(DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
DebugPrint("MIDI Input Long Data: %08x %08x\n", dwParam1, dwParam2);
if (!stopping)
{
LPMIDIHDR header = LPMIDIHDR(dwParam1);
LPSTR data = header->lpData;
// if this is the first block of sysex data...
if (!sysexreceive)
{
DebugPrint("MIDI System Exclusive\n");
sysexreceive = true;
}
// if this is the last block of sysex data...
if (data[header->dwBytesRecorded - 1] == 0xF7)
{
// end of block
sysexreceive = false;
}
// queue the header for more input
midiInAddBuffer(handle, header, sizeof(MIDIHDR));
}
}
void CALLBACK Proc(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
switch (wMsg)
{
case MM_MIM_OPEN:
DebugPrint("MIDI Input Open\n");
break;
case MM_MIM_CLOSE:
DebugPrint("MIDI Input Close\n");
break;
case MM_MIM_DATA:
HandleData(dwInstance, dwParam1, dwParam2);
break;
case MM_MIM_LONGDATA:
HandleLongData(dwInstance, dwParam1, dwParam2);
break;
case MM_MIM_ERROR:
DebugPrint("MIDI Input Error: %08x %08x\n", dwParam1, dwParam2);
break;
case MM_MIM_LONGERROR:
DebugPrint("MIDI Input Long Error: %08x %08x\n", dwParam1, dwParam2);
break;
}
}
unsigned int GetNumDevices()
{
return midiInGetNumDevs();
}
MMRESULT GetDeviceCaps(unsigned int id, MIDIINCAPS &caps)
{
return midiInGetDevCaps(id, &caps, sizeof(MIDIINCAPS));
}
MMRESULT Open(unsigned int deviceId)
{
// open midi input
MMRESULT mmResult;
mmResult = midiInOpen(&handle, deviceId, DWORD_PTR(Proc), NULL, CALLBACK_FUNCTION);
if (mmResult)
{
char buf[256];
GetLastErrorMessage(buf, 256);
DebugPrint("Error opening MIDI input: %s", buf);
return mmResult;
}
// prepare input buffer
header.lpData = sysexbuffer;
header.dwBufferLength = sizeof(sysexbuffer);
header.dwFlags = 0;
mmResult = midiInPrepareHeader(handle, &header, sizeof(MIDIHDR));
if (mmResult)
{
char buf[256];
GetLastErrorMessage(buf, 256);
DebugPrint("Error preparing MIDI header: %s", buf);
return mmResult;
}
// queue input buffer
mmResult = midiInAddBuffer(handle, &header, sizeof(MIDIHDR));
if (mmResult)
{
char buf[256];
GetLastErrorMessage(buf, 256);
DebugPrint("Error adding MIDI input buffer: %s", buf);
return mmResult;
}
return mmResult;
}
void Start()
{
if (handle)
{
// start midi input
midiInStart(handle);
stopping = false;
}
}
void Stop()
{
if (handle)
{
// stop midi input
stopping = true;
midiInStop(handle);
}
}
void Close()
{
if (handle)
{
midiInUnprepareHeader(handle, &header, sizeof(MIDIHDR));
midiInClose(handle);
}
}
}
}