-
Notifications
You must be signed in to change notification settings - Fork 13
/
mfcmidi.h
450 lines (396 loc) · 18.1 KB
/
mfcmidi.h
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*****************************************************************************/
/* MFCMIDI.H : class definition for MIDI base classes */
/*****************************************************************************/
/******************************************************************************
Copyright (C) 2006 Hermann Seib
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef __INCLUDE_MFCMIDI_H__
#define __INCLUDE_MFCMIDI_H__
#if defined(_MSC_VER) && (_MSC_VER > 1000)
#pragma once
#endif // _MSC_VER > 1000
#ifndef __cplusplus
#error mfcmidi.h is for use with C++
#endif
/*****************************************************************************/
/* Necessary includes */
/*****************************************************************************/
/*****************************************************************************/
/* MIDI definitions */
/*****************************************************************************/
#define ISSTATUS(bData) ((bData) & 0x80)
#define FILTERCHANNEL(bStatus) ((BYTE)((bStatus) & 0xf0))
#define FILTERSTATUS(bStatus) ((BYTE)((bStatus) & 0x0f))
#define STATUS_NOTEOFF 0x80
#define STATUS_NOTEON 0x90
#define STATUS_POLYPHONICKEY 0xa0
#define STATUS_CONTROLCHANGE 0xb0
#define STATUS_PROGRAMCHANGE 0xc0
#define STATUS_CHANNELPRESSURE 0xd0
#define STATUS_PITCHBEND 0xe0
#define STATUS_SYS 0xf0
#define STATUS_SYSEX 0xf0
#define STATUS_QFRAME 0xf1
#define STATUS_SONGPOINTER 0xf2
#define STATUS_SONGSELECT 0xf3
#define STATUS_F4 0xf4
#define STATUS_F5 0xf5
#define STATUS_TUNEREQUEST 0xf6
#define STATUS_EOX 0xf7
#define STATUS_TIMINGCLOCK 0xf8
#define STATUS_F9 0xf9
#define STATUS_START 0xfa
#define STATUS_CONTINUE 0xfb
#define STATUS_STOP 0xfc
#define STATUS_FD 0xfd
#define STATUS_ACTIVESENSING 0xfe
#define STATUS_SYSTEMRESET 0xff
#define OPENDEFAULT ((DWORD)-1)
#define MAX_DETACHED 1024 /* max.# of detached MIDIHDRs */
/* size of the global ringbuffer */
#define MIDIIN_RINGSIZE (16 * 4096)
/*****************************************************************************/
/* CMidiMsg : a MIDI message */
/*****************************************************************************/
class CMidiMsg
{
public:
CMidiMsg(DWORD smsg = 0);
CMidiMsg(int b1, int b2, int b3=0);
CMidiMsg(LPMIDIHDR lphdr);
CMidiMsg(LPSTR data, int len);
CMidiMsg (CMidiMsg const & org)
{ Init(); DoCopy(org); }
// virtual ~CMidiMsg() /* virtual destructor not needed */
/* as there are no derived classes */
~CMidiMsg() /* destructor */
{ Empty(); }
/* assignment operator */
CMidiMsg & operator=(CMidiMsg const &org)
{ return DoCopy(org); }
CMidiMsg & operator=(DWORD smsg)
{ Set(smsg); return *this; }
CMidiMsg & operator=(LPMIDIHDR lphdr)
{ Set(lphdr); return *this; }
bool operator==(CMidiMsg &other)
{
return ((other.Length() == Length()) &&
(!memcmp((BYTE *)other, (BYTE *)*this, Length())));
}
/* set to MIDI message */
void Set(DWORD smsg = 0);
void Set(LPMIDIHDR lphdr);
void Set(LPSTR data, int len);
void Set(int b1, int b2, int b3);
void SetStamp(DWORD dwTS = 0) { dwStamp = dwTS; }
#ifdef _DEBUG
void SetMsg(UINT wMsg) { this->wMsg = wMsg; }
#endif
/* convert to other data */
operator char*();
operator BYTE*();
operator DWORD();
operator LPMIDIHDR();
/* get x-th byte of message */
BYTE & operator [](int idx) const
{ return ((CMidiMsg *)this)->GetAt(idx); }
BYTE & GetAt(int idx)
{
if (!lpHdr)
return ((BYTE *)&sMsg)[idx];
return ((BYTE *)lpHdr->lpData)[idx];
}
WORD Length(); /* get message length */
static BYTE Length(BYTE bStatus); /* get length of a short message */
/* get timestamp */
DWORD GetStamp() { return dwStamp; }
void Output(HMIDIOUT hOut) /* remember output device */
{ sMsg = (DWORD)hOut; }
static int CleanupDetached(); /* clean up detached messages */
#ifdef _DEBUG
UINT GetMsg() { return wMsg; } /* get message code */
LPCSTR GetMsgText()
{
static LPCSTR lpszMsgs[] =
{
"MIM_OPEN",
"MIM_CLOSE",
"MIM_DATA",
"MIM_LONGDATA",
"MIM_ERROR",
"MIM_LONGERROR",
"MOM_OPEN",
"MOM_CLOSE",
"MOM_DONE",
"MOM_POSITIONCB",
"MCISIGNAL",
"MIM_MOREDATA"
};
int nIdx = wMsg - MM_MIM_OPEN;
if ((nIdx < 0) || (nIdx >= (sizeof(lpszMsgs) / sizeof(lpszMsgs[0]))))
return "?";
return lpszMsgs[nIdx];
}
#endif
protected :
BOOL bAlloc; /* flag whether self-allocated buffer*/
DWORD sMsg; /* short message */
LPMIDIHDR lpHdr; /* long message header */
HGLOBAL hGlPtr; /* global pointer */
DWORD dwStamp; /* time stamp */
#ifdef _DEBUG
UINT wMsg; /* message code (MM_)MIM_... or 0 */
#endif
/* detached MIDI message handling */
static LPMIDIHDR lpDetached[MAX_DETACHED];
static HMIDIOUT hdDetached[MAX_DETACHED];
protected:
void Init(); /* initialize memory */
void Empty(); /* clear memory */
/* copy another MIDI message */
CMidiMsg & DoCopy(CMidiMsg const &org)
{
Empty(); /* remove any previous contents */
sMsg = org.sMsg; /* store short message */
if (org.lpHdr) /* if original has a buffer */
/* copy original's buffer */
Set(org.lpHdr->lpData, (int)org.lpHdr->dwBufferLength);
dwStamp = org.dwStamp;
#ifdef _DEBUG
wMsg = org.wMsg;
#endif
return *this; /* pass back pointer to ourselves */
}
void FreeHdr()
{
GlobalUnlock(hGlPtr);
GlobalFree(hGlPtr);
lpHdr = NULL;
hGlPtr = NULL;
}
void DetachHdr();
};
/*****************************************************************************/
/* CMidiDevice : MIDI Device base class */
/*****************************************************************************/
class CMidiDevice : public CObject
{
// Attributes
public:
CMidiDevice(); /* constructor */
~CMidiDevice(); /* destructor */
BOOL IsOpen() /* return whether device is open */
{ return bIsOpen; }
LPSTR DeviceName ( ) /* return opened device' name */
{ return bIsOpen ? szDevName : ""; }
// Operations
virtual BOOL Open(LPCSTR szName, /* open with device name */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_WINDOW);
virtual BOOL Open(int iID, /* open with device ID */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_WINDOW);
virtual void OnOpen() {}
virtual BOOL Reset() /* reset device */
{ return TRUE; }
virtual BOOL Close(); /* close device */
virtual void OnClose() {}
virtual DWORD Message(UINT msg, /* send special message */
DWORD dw1, DWORD dw2)
{ return 0; }
virtual LPCSTR GetErrorText(MMRESULT wError)
{ return ""; }
protected:
// Implementation
private :
BOOL bIsOpen; /* flag whether opened */
protected :
BOOL Create();
char szDevName[MAXPNAMELEN]; /* device name */
char szErrorText[256]; /* error text buffer */
};
/*****************************************************************************/
/* CMidiInDeviceList : MIDI Input device list */
/*****************************************************************************/
class CMidiInDeviceList : public CStringArray
{
typedef CStringArray inherited; /* base class */
public :
CMidiInDeviceList() { Load(); } /* constructor */
virtual ~CMidiInDeviceList();
LPMIDIINCAPS GetCaps(int nID)
{ return (LPMIDIINCAPS) paCaps.GetAt(nID); }
void Load(); /* (re-)load list */
protected:
void Unload();
CPtrArray paCaps;
};
/*****************************************************************************/
/* CMidiInDevice : MIDI Input device */
/*****************************************************************************/
class CMidiInDevice : public CMidiDevice
{
// Attributes
public:
BOOL IsRecording();
// Operations
public:
CMidiInDevice();
virtual BOOL Open(LPCSTR szName, /* open with device name */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_FUNCTION);
virtual BOOL Open(int iID, /* open with device ID */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_FUNCTION);
virtual BOOL Start(); /* start recording */
virtual BOOL Stop(); /* stop recording */
virtual BOOL Reset(); /* reset device */
virtual BOOL Close(); /* close device */
virtual DWORD Message(UINT msg, /* send special message */
DWORD dw1, DWORD dw2);
/* process incoming data */
virtual void Data(CMidiMsg &Msg);
virtual void OnMimOpen() { }
virtual void OnMimClose() { }
virtual void OnMimError(CMidiMsg &Msg) { Data(Msg); }
virtual void OnMimError(LPMIDIHDR lphdr, DWORD dwTS = 0);
virtual void OnMimLongData(LPMIDIHDR lphdr, DWORD dwTS);
virtual void OnMimMoreData(CMidiMsg &Msg) { }
virtual void NoteOn(int channel, int note, int vel) { }
virtual void NoteOff(int channel, int note, int vel = 64) { }
virtual void KeyAftertouch(int channel, int note, int vel) { }
virtual void ChannelAftertouch(int channel, int vel) { }
virtual void Controller(int channel, int ctrl, int val) { }
virtual void Patch(int channel, int patch) { }
virtual void PitchBend(int channel, int val) { }
virtual void SysEx(CMidiMsg &msg) { }
virtual void SystemReset() { };
virtual BOOL GetDevCaps(LPMIDIINCAPS pmCap);
virtual LPCSTR GetErrorText(MMRESULT wError);
static BOOL GetDevCaps(UINT iID, LPMIDIINCAPS pmCap);
DWORD GetStamp() { return ::timeGetTime() - dwStamp; }
// Implementation
public:
virtual ~CMidiInDevice();
protected:
HMIDIIN hDev; /* input device handle */
HGLOBAL hGlobal; /* global memory for SysEx buffers */
char * pGlobal; /* same as locked pointer */
LPMIDIHDR *lpGlHdr; /* MIDI input buffers */
volatile BOOL bRecording; /* flag whether currently recording */
DWORD dwStamp; /* internal start time stamp */
MIDIINCAPS mCap; /* input device capabilities */
volatile BOOL bClosing; /* flag whether currently closing */
static int nInDevs; /* # currently loaded CMidiInDevices */
static HGLOBAL hGlRing; /* handle for the ring buffer */
static LPBYTE pRing; /* ring buffer for all devices */
static volatile LPBYTE pRingRead; /* current read pointer */
static volatile LPBYTE pRingWrite; /* current write pointer */
static HANDLE hEvt; /* event to trigger */
static CWinThread * pEvtThread; /* event thread procedure */
static bool volatile bKillThread; /* thread kill flag */
static UINT EvtThreadProc(LPVOID pParam);
protected:
BOOL AddHeader(LPMIDIHDR lpHdr); /* add MIDI buffer to device */
BOOL CheckHeader(LPMIDIHDR lpHdr); /* check whether it's one of ours */
BOOL AllocBuf(); /* allocate buffer */
BOOL FreeBuf(); /* free allocated buffer */
BOOL PrepareBuf(); /* prepare buffer for usage */
BOOL UnprepareBuf(); /* unprepare buffer from usage */
int PreparedBuffers(); /* # currently prepared buffers */
static void CALLBACK MidiInProc(HMIDIIN hMidiIn, UINT wMsg,
DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2);
static void ProcessMessage(UINT wMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
bool StartThread(); /* start work thread */
bool StopThread(); /* stop work thread */
public:
// Overrides
protected:
};
/*****************************************************************************/
/* CMidiOutDeviceList : MIDI Output device list */
/*****************************************************************************/
class CMidiOutDeviceList : public CStringArray
{
typedef CStringArray inherited; /* base class */
public :
CMidiOutDeviceList() { Load(); } /* constructor */
virtual ~CMidiOutDeviceList();
LPMIDIOUTCAPS GetCaps(int nID)
{ return (LPMIDIOUTCAPS) paCaps.GetAt(nID); }
void Load(); /* (re-)load device list */
protected:
void Unload();
CPtrArray paCaps;
};
/*****************************************************************************/
/* CMidiOutDevice : MIDI Output device */
/*****************************************************************************/
class CMidiOutDevice : public CMidiDevice
{
// Attributes
public:
// Operations
public:
CMidiOutDevice();
virtual BOOL Open(LPCSTR szName, /* open with device name */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_FUNCTION);
virtual BOOL Open(int iID, /* open with device ID */
DWORD lp1 = OPENDEFAULT,
DWORD lp2 = OPENDEFAULT,
DWORD dwFlags = CALLBACK_FUNCTION);
virtual BOOL Reset();
virtual BOOL Close();
virtual void OnMomClose() {}
virtual void OnMomDone(LPMIDIHDR lphdr) { CMidiMsg::CleanupDetached(); }
virtual void OnMomOpen() {}
virtual DWORD Message(UINT msg, DWORD dw1, DWORD dw2);
virtual BOOL CachePatches(UINT uBank,
WORD FAR* lpwPatchArray, UINT uFlags);
virtual BOOL CacheDrumPatches(UINT uPatch,
WORD FAR* lpwKeyArray, UINT uFlags);
virtual BOOL Output(CMidiMsg &Msg, BOOL bSync = TRUE);
virtual BOOL NoteOn(int channel, int note, int vel);
virtual BOOL NoteOff(int channel, int note, int vel = 64);
virtual BOOL KeyAftertouch(int channel, int note, int vel);
virtual BOOL ChannelAftertouch(int channel, int vel);
virtual BOOL Controller(int channel, int ctrl, int val);
virtual BOOL Controller14(int channel, int ctrl, int val);
virtual BOOL Patch(int channel, int patch);
virtual BOOL PitchBend(int channel, int val);
virtual BOOL SysEx(CMidiMsg &msg) { return Output(msg); }
virtual BOOL SystemReset();
virtual BOOL Bank(int channel, int bank);
virtual BOOL Bank14(int channel, int bank);
virtual BOOL GetDevCaps(LPMIDIOUTCAPS pmCap);
virtual LPCSTR GetErrorText(MMRESULT wError);
static BOOL GetDevCaps(UINT iID, LPMIDIOUTCAPS pmCap);
// Implementation
public:
virtual ~CMidiOutDevice();
protected:
HMIDIOUT hDev; /* output device handle */
MIDIOUTCAPS mCap; /* output device capabilities */
BYTE RunningStatus; /* running status */
protected:
BOOL SendingDone(LPMIDIHDR lpHdr);
};
#endif /* __INCLUDE_MFCMIDI_H__ */