-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmfcmidi.cpp
1858 lines (1607 loc) · 59.3 KB
/
mfcmidi.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/* MFCMIDI.CPP : class implementation 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
******************************************************************************/
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
#include "stdafx.h" /* MFC include */
#include "mfcmidi.h" /* private definitions */
#ifdef __WATCOMC__
#pragma library("MMSystem")
#endif
#ifdef _DEBUG
#ifdef WIN32
#define new DEBUG_NEW
#endif
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
#define MIDIINHDRS 64 /* # MIDI SysEx Input headers */
#define MIDIINBUFSIZE 256L /* length of MIDI input buffer */
/*****************************************************************************/
/* Global Data */
/*****************************************************************************/
/*===========================================================================*/
/* Class CMidiMsg */
/*===========================================================================*/
LPMIDIHDR CMidiMsg::lpDetached[MAX_DETACHED] = {0};
HMIDIOUT CMidiMsg::hdDetached[MAX_DETACHED] = {0};
/*****************************************************************************/
/* CMidiMsg : constructor */
/*****************************************************************************/
CMidiMsg::CMidiMsg
(
DWORD smsg
)
{
Init();
Set(smsg);
}
CMidiMsg::CMidiMsg
(
int b1,
int b2,
int b3
)
{
DWORD smsg = 0;
Init();
Set(b1, b2, b3);
}
CMidiMsg::CMidiMsg
(
LPMIDIHDR lphdr
)
{
Init();
Set(lphdr);
}
CMidiMsg::CMidiMsg
(
LPSTR data,
int len
)
{
Init();
Set(data,len);
}
/*****************************************************************************/
/* Init : initializes CMidiMsg storage */
/*****************************************************************************/
void CMidiMsg::Init ( )
{
bAlloc = FALSE;
sMsg = 0;
lpHdr = NULL;
hGlPtr = NULL;
dwStamp = 0;
#ifdef _DEBUG
wMsg = 0;
#endif
}
/*****************************************************************************/
/* Empty : empties preinitialized CMidiMsg storage */
/*****************************************************************************/
void CMidiMsg::Empty ( )
{
if (bAlloc)
{
if (lpHdr->dwFlags & MHDR_PREPARED) /* if not finished yet, */
DetachHdr();
else
FreeHdr();
}
Init();
}
/*****************************************************************************/
/* CleanupDetached : clean up dateached headers, return 1st free pos */
/*****************************************************************************/
int CMidiMsg::CleanupDetached()
{
int i, j;
/* remove all finished ones */
for (i = 0; i < MAX_DETACHED; i++)
{
if (!lpDetached[i]) /* stop upon first free one */
break;
if (lpDetached[i]->dwFlags & MHDR_DONE)
{
midiOutUnprepareHeader(hdDetached[i], lpDetached[i], sizeof(MIDIHDR));
HGLOBAL hGl = GlobalHandle(lpDetached[i]);
if (hGl)
{
GlobalUnlock(hGl);
GlobalFree(hGl);
}
for (j = i; j < MAX_DETACHED - 1; j++)
{
lpDetached[j] = lpDetached[j + 1];
hdDetached[j] = hdDetached[j + 1];
}
lpDetached[j] = NULL;
hdDetached[j] = 0;
i--;
continue;
}
}
TRACE1("CMidiMsg::CleanupDetached(): %d detached messages waiting\n", i);
return i; /* pass back 1st free position */
}
/*****************************************************************************/
/* DetachHdr : detaches the LPMIDIHDR from the object */
/*****************************************************************************/
void CMidiMsg::DetachHdr()
{
if (lpHdr->dwFlags & MHDR_DONE) /* if this msg has been output, */
FreeHdr(); /* just free it */
else /* otherwise */
{
int i = CleanupDetached(); /* clean up list of detached msgs */
if (i < MAX_DETACHED) /* if space for 1 more, */
{
lpDetached[i] = lpHdr; /* insert this header */
hdDetached[i] = (HMIDIOUT)sMsg; /* and remember the device (tricky..)*/
}
TRACE1("CMidiMsg::DetachHdr(): %d detached messages waiting\n", i + 1);
}
}
/*****************************************************************************/
/* Set : sets to MIDI message of various types */
/*****************************************************************************/
void CMidiMsg::Set
(
int b1,
int b2,
int b3
)
{
DWORD smsg =
(DWORD)(b1 & 0xffL) +
(DWORD)((b2 & 0xffL) << 8L) +
(DWORD)((b3 & 0xffL) << 16L);
Set(smsg);
}
void CMidiMsg::Set
(
DWORD smsg
)
{
Empty();
sMsg = smsg;
if (((sMsg & 0xf0) == 0x90) && /* if NOTE ON that's really NOTE OFF */
(!(sMsg & 0xff0000L)))
/* make it a NOTE OFF with vel.64 */
sMsg = (sMsg & (0x00ff0fL)) | 0x400080L;
}
void CMidiMsg::Set
(
LPMIDIHDR lphdr
)
{
Empty();
lpHdr = lphdr;
sMsg = (DWORD)-3;
}
void CMidiMsg::Set
(
LPSTR data,
int len
)
{
if (len <= 3)
{
Set(*((DWORD *)data));
return;
}
Empty();
hGlPtr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
sizeof(MIDIHDR) + len);
if (!hGlPtr)
return;
lpHdr = (LPMIDIHDR)GlobalLock(hGlPtr);
if (lpHdr)
{
bAlloc = TRUE;
memset((LPVOID)lpHdr, 0, sizeof(MIDIHDR));
lpHdr->lpData = (LPSTR)(lpHdr+1);
lpHdr->dwBufferLength = (DWORD)len;
lpHdr->dwBytesRecorded = (DWORD)len;
memcpy(lpHdr->lpData, data, len);
sMsg = (DWORD)-4;
}
}
/*****************************************************************************/
/* operator char * : returns the message as a character pointer */
/*****************************************************************************/
CMidiMsg::operator char * ( )
{
if (!lpHdr) /* if no header */
return (char *)&sMsg; /* pass back short MIDI msg address */
return (char *)lpHdr->lpData; /* otherwise pass back data pointer */
}
/*****************************************************************************/
/* operator BYTE * : returns the message as a character pointer */
/*****************************************************************************/
CMidiMsg::operator BYTE * ( )
{
if (!lpHdr) /* if no header */
return (BYTE *)&sMsg; /* pass back short MIDI msg address */
return (BYTE *)lpHdr->lpData; /* otherwise pass back data pointer */
}
/*****************************************************************************/
/* operator DWORD : returns short message as DWORD */
/*****************************************************************************/
CMidiMsg::operator DWORD ( )
{
if (!lpHdr) /* if no header */
return sMsg; /* pass back short MIDI msg */
return *((DWORD *)lpHdr->lpData); /* otherwise pass back data pointer */
}
/*****************************************************************************/
/* operator LPMIDIHDR : returns the message as a MIDIHDR pointer */
/*****************************************************************************/
CMidiMsg::operator LPMIDIHDR ( )
{
return lpHdr; /* pass back MIDI header */
}
/*****************************************************************************/
/* Length : returns the length of the passed message */
/*****************************************************************************/
BYTE CMidiMsg::Length(BYTE bStatus)
{
switch (bStatus & 0xf0) /* filter all known lengths */
{
case STATUS_PROGRAMCHANGE : /* 2-byte messages */
case STATUS_CHANNELPRESSURE :
return 2;
case STATUS_NOTEOFF : /* 3-byte messages */
case STATUS_NOTEON :
case STATUS_POLYPHONICKEY :
case STATUS_CONTROLCHANGE :
case STATUS_PITCHBEND :
return 3;
}
switch (bStatus) /* now process the others */
{
case STATUS_TUNEREQUEST : /* 1-byte codes */
case STATUS_TIMINGCLOCK :
case STATUS_START :
case STATUS_CONTINUE :
case STATUS_STOP :
case STATUS_ACTIVESENSING :
case STATUS_SYSTEMRESET :
return 1;
case STATUS_SONGSELECT : /* 2-byte codes */
return 2;
case STATUS_QFRAME : /* 3-byte codes */
case STATUS_SONGPOINTER :
return 3;
}
return 0; /* return invalid */
}
WORD CMidiMsg::Length ( )
{
if (!lpHdr) /* if a short message */
return Length((BYTE)(sMsg & 0xFF)); /* return its length */
return (WORD) /* otherwise return length of buffer */
(lpHdr->dwBytesRecorded ?
lpHdr->dwBytesRecorded :
lpHdr->dwBufferLength);
}
/*===========================================================================*/
/* Class CMidiDevice */
/*===========================================================================*/
/*****************************************************************************/
/* CMidiDevice : constructor */
/*****************************************************************************/
CMidiDevice::CMidiDevice ( )
{
szDevName[0] = 0; /* reset device name */
bIsOpen = FALSE; /* reset opened flag */
}
/*****************************************************************************/
/* ~CMidiDevice : destructor */
/*****************************************************************************/
CMidiDevice::~CMidiDevice ( )
{
}
/*****************************************************************************/
/* Create : creates necessary notification window */
/*****************************************************************************/
BOOL CMidiDevice::Create ( )
{
return TRUE;
}
/*****************************************************************************/
/* Open : opens the device */
/*****************************************************************************/
BOOL CMidiDevice::Open
(
int iID,
DWORD lp1,
DWORD lp2,
DWORD dwFlags
)
{
bIsOpen = TRUE;
OnOpen();
return TRUE;
}
BOOL CMidiDevice::Open
(
LPCSTR szName,
DWORD lp1,
DWORD lp2,
DWORD dwFlags
)
{
strcpy(szDevName, szName);
return TRUE;
}
/*****************************************************************************/
/* Close : closes the device */
/*****************************************************************************/
BOOL CMidiDevice::Close ( )
{
*szDevName = 0;
bIsOpen = FALSE;
OnClose();
return TRUE;
}
/*===========================================================================*/
/* Class CMidiInDeviceList */
/*===========================================================================*/
/*****************************************************************************/
/* ~CMidiInDeviceList : destructor */
/*****************************************************************************/
CMidiInDeviceList::~CMidiInDeviceList()
{
Unload();
}
/*****************************************************************************/
/* Load : (re-)loads the device list */
/*****************************************************************************/
void CMidiInDeviceList::Load()
{
WORD wNumDevices;
int i;
LPMIDIINCAPS pCaps;
Unload();
wNumDevices = midiInGetNumDevs(); /* get # MIDI input devices */
if (wNumDevices)
{
for (i=0; i < (int) wNumDevices; i++)
{
pCaps = new MIDIINCAPS;
if (pCaps)
{
CMidiInDevice::GetDevCaps(i, pCaps);
Add(pCaps->szPname);
paCaps.Add(pCaps);
}
}
}
}
/*****************************************************************************/
/* Unload : removes list from memory */
/*****************************************************************************/
void CMidiInDeviceList::Unload()
{
for (int i = paCaps.GetUpperBound(); i >= 0; i--)
{
LPMIDIINCAPS pCaps = GetCaps(i);
if (pCaps)
delete pCaps;
}
paCaps.RemoveAll();
RemoveAll();
}
/*===========================================================================*/
/* Class CMidiInDevice */
/*===========================================================================*/
int CMidiInDevice::nInDevs = 0; /* # loaded in devices */
HGLOBAL CMidiInDevice::hGlRing = 0; /* one handle, */
LPBYTE CMidiInDevice::pRing = NULL; /* one ring to chain them all :-) */
volatile LPBYTE CMidiInDevice::pRingRead = NULL;
volatile LPBYTE CMidiInDevice::pRingWrite = NULL;
HANDLE CMidiInDevice::hEvt = NULL; /* event handle & thread */
CWinThread *CMidiInDevice::pEvtThread = NULL;
bool volatile CMidiInDevice::bKillThread = false;
/*****************************************************************************/
/* CMidiInDevice : constructor */
/*****************************************************************************/
CMidiInDevice::CMidiInDevice ( )
{
hDev = NULL; /* reset device handle */
hGlobal = NULL; /* MIDI In buffers */
pGlobal = NULL;
lpGlHdr = new LPMIDIHDR[MIDIINHDRS];
if (lpGlHdr)
for (int i = 0; i < MIDIINHDRS; i++)
lpGlHdr[i] = NULL;
bRecording = FALSE; /* reset recording flag */
dwStamp = 0; /* set internal time stamp */
memset(&mCap, 0, sizeof(mCap)); /* init capabilities */
bClosing = FALSE;
if (!hGlRing) /* if ring needs to be setup */
{
hGlRing = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, MIDIIN_RINGSIZE);
if (hGlRing)
pRingRead = pRingWrite = pRing = (LPBYTE)GlobalLock(hGlRing);
}
nInDevs++;
}
/*****************************************************************************/
/* ~CMidiInDevice : destructor */
/*****************************************************************************/
CMidiInDevice::~CMidiInDevice ( )
{
if (IsOpen())
Close();
FreeBuf();
delete[] lpGlHdr;
if (--nInDevs <= 0)
{
nInDevs = 0;
if (pRing)
{
GlobalUnlock(hGlRing);
pRingRead = pRingWrite = pRing = NULL;
}
if (hGlRing)
{
GlobalFree(hGlRing);
hGlRing = 0;
}
StopThread(); /* make sure the work thread dies */
}
}
/*****************************************************************************/
/* MidiInProc : callback procedure */
/*****************************************************************************/
void CALLBACK CMidiInDevice::MidiInProc
(
HMIDIIN hMidiIn,
UINT wMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
)
{
#if 0
/* direct processing */
ProcessMessage(wMsg, dwInstance, dwParam1, dwParam2);
#else
/* processing in separate thread */
int nRest = (pRingRead - pRingWrite);
if (nRest <= 0)
nRest += MIDIIN_RINGSIZE;
if (nRest >= 16)
{
((LPDWORD)pRingWrite)[0] = wMsg;
((LPDWORD)pRingWrite)[1] = dwInstance;
((LPDWORD)pRingWrite)[2] = dwParam1;
((LPDWORD)pRingWrite)[3] = dwParam2;
pRingWrite += 16;
if (pRingWrite >= (pRing + MIDIIN_RINGSIZE))
pRingWrite = pRing;
SetEvent(hEvt);
}
#endif
}
/*****************************************************************************/
/* EvtThreadProc : called when MIDI messages come in */
/*****************************************************************************/
UINT CMidiInDevice::EvtThreadProc(LPVOID pParam)
{
UINT result;
UINT wMsg;
DWORD dwInstance, dwParam1, dwParam2;
while (!bKillThread) /* while looping */
{ /* wait for event to be signaled */
result = WaitForSingleObject(hEvt,INFINITE);
if ((result == WAIT_OBJECT_0) && /* if so, */
(!bKillThread))
{
while (pRingRead != pRingWrite)
{
wMsg = ((LPDWORD)pRingRead)[0];
dwInstance = ((LPDWORD)pRingRead)[1];
dwParam1 = ((LPDWORD)pRingRead)[2];
dwParam2 = ((LPDWORD)pRingRead)[3];
pRingRead += 16;
if (pRingRead >= (pRing + MIDIIN_RINGSIZE))
pRingRead = pRing;
ProcessMessage(wMsg, dwInstance, dwParam1, dwParam2);
}
}
else /* if terminating, */
break; /* stop the loop */
}
bKillThread = false; /* reset kill flag */
return 0; /* and end the thread */
}
/*****************************************************************************/
/* ProcessMessage : processes a MIDI message */
/*****************************************************************************/
void CMidiInDevice::ProcessMessage
(
UINT wMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
)
{
CMidiInDevice * pInDev = (CMidiInDevice *)dwInstance;
CMidiMsg msg;
#ifdef _DEBUG
DWORD dwDelay = 0;
DWORD dwNow = timeGetTime() - pInDev->dwStamp;
#endif
switch (wMsg)
{
case MIM_CLOSE :
pInDev->OnMimClose();
break;
case MIM_OPEN :
pInDev->OnMimOpen();
break;
case MIM_ERROR :
msg.Set(dwParam1);
msg.SetStamp(dwParam2);
#ifdef _DEBUG
msg.SetMsg(wMsg);
dwDelay = dwNow - dwParam2;
#endif
pInDev->OnMimError(msg);
break;
case MIM_LONGERROR :
pInDev->OnMimError((LPMIDIHDR)dwParam1, dwParam2);
break;
case MIM_MOREDATA :
msg.Set(dwParam1);
msg.SetStamp(dwParam2);
#ifdef _DEBUG
msg.SetMsg(wMsg);
dwDelay = dwNow - dwParam2;
#endif
pInDev->OnMimMoreData(msg);
break;
case MIM_DATA :
msg.Set(dwParam1);
msg.SetStamp(dwParam2);
#ifdef _DEBUG
msg.SetMsg(wMsg);
dwDelay = dwNow - dwParam2;
#endif
pInDev->Data(msg);
break;
case MIM_LONGDATA :
pInDev->OnMimLongData((LPMIDIHDR)dwParam1, dwParam2);
break;
}
#ifdef _DEBUG
if ((dwDelay >= 10) && pInDev->dwStamp)
TRACE1("MIDI In Thread Processing Delay: %dms\n", dwDelay);
#endif
}
/*****************************************************************************/
/* StartThread : assures that the work thread is there */
/*****************************************************************************/
bool CMidiInDevice::StartThread()
{
if (!hEvt) /* if not yet done, */
{
/* then create a new event handle */
hEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!hEvt) /* upon error */
return false; /* return error */
}
if (!pEvtThread) /* then create the thread */
{
pEvtThread = AfxBeginThread(EvtThreadProc, this,
THREAD_PRIORITY_TIME_CRITICAL, 0,
CREATE_SUSPENDED, NULL);
if (!pEvtThread) /* upon error */
{
StopThread(); /* kill the event handle */
return false; /* and return error */
}
pEvtThread->m_bAutoDelete = TRUE; /* let it go! */
pEvtThread->ResumeThread();
}
return true; /* pass back OK */
}
/*****************************************************************************/
/* StopThread: kills an eventually running event thread & event handle */
/*****************************************************************************/
bool CMidiInDevice::StopThread()
{
if (hEvt) /* if event handle allocated */
{
TRACE0("Killing MIDI In work thread\n");
if (pEvtThread) /* if thread still running */
{
bKillThread = true; /* set cancel flag */
SetEvent(hEvt); /* tell thread to die */
int nLoop = 0;
while (bKillThread) /* wait until it does. */
{
TRACE0("Waiting for MIDI In work thread to stop\n");
Sleep(50);
if (++nLoop > 20) /* if more than a second passed, */
{
TRACE0("Given up waiting for MIDI In work thread to stop\n");
break;
}
}
pEvtThread = 0; /* and thread handle */
}
CloseHandle(hEvt); /* and close the event handle */
hEvt = 0; /* and reset it... */
}
return true;
}
/*****************************************************************************/
/* GetDevCaps : retrieves a specific device' capabilities */
/*****************************************************************************/
BOOL CMidiInDevice::GetDevCaps(UINT iID, LPMIDIINCAPS pmCap)
{
MMRESULT res = midiInGetDevCaps(iID, /* get device capabilities */
pmCap,
sizeof(*pmCap));
return (res == MMSYSERR_NOERROR);
}
BOOL CMidiInDevice::GetDevCaps(LPMIDIINCAPS pmCap)
{
memcpy(pmCap, &mCap, sizeof(mCap)); /* return known capabilities */
return TRUE;
}
/*****************************************************************************/
/* Open : opens a MIDI In Device with its ID */
/*****************************************************************************/
BOOL CMidiInDevice::Open
(
int iID,
DWORD lp1,
DWORD lp2,
DWORD dwFlags
)
{
if (IsOpen()) /* if already open */
Close(); /* close the opened device */
if ((dwFlags & CALLBACK_TYPEMASK) == /* if function callback */
CALLBACK_FUNCTION)
{
if (lp1 == OPENDEFAULT) /* if default value */
lp1 = (DWORD)MidiInProc; /* set LP1 to procedure */
if (lp2 == OPENDEFAULT) /* if default value */
lp2 = (DWORD)this; /* set LP2 to object */
#ifdef WIN32
StartThread(); /* make sure the thread is there */
#endif
}
#ifdef WIN32
/* if thread callback */
else if ((dwFlags & CALLBACK_TYPEMASK) ==
CALLBACK_THREAD)
{
if (lp1 == OPENDEFAULT) /* if default value */
lp1 = (DWORD)GetCurrentThreadId(); /* set LP1 to thread ID */
if (lp2 == OPENDEFAULT) /* if default value */
lp2 = (UINT)AfxGetInstanceHandle(); /* set LP2 to instance */
}
#endif
else /* window callback not allowed here */
return FALSE;
WORD wRtn = 1;
try
{
wRtn = midiInOpen(&hDev, /* open the device */
iID, /* with the passed ID */
lp1, lp2, dwFlags);
}
catch(...)
{
}
if (wRtn) /* if open error */
{
TRACE1("CMidiInDevice::Open:midiInOpen(): %s\n", GetErrorText(wRtn));
hDev = NULL; /* make sure that handle is reset */
return FALSE;
}
if (!AllocBuf()) /* allocate MIDI Input buffers */
{
Close(); /* upon error close device */
return(FALSE); /* and */
}
CMidiDevice::Open(iID, lp1, lp2, dwFlags);/* call ancestor */
GetDevCaps(iID, &mCap); /* get device capabilities */
return Start(); /* start recording */
}
/*****************************************************************************/
/* Open : opens a device with its name */
/*****************************************************************************/
BOOL CMidiInDevice::Open
(
LPCSTR szName,
DWORD lp1,
DWORD lp2,
DWORD dwFlags
)
{
WORD wNumDevices;
int i;
WORD wRtn;
MIDIINCAPS mcap = {0};
wNumDevices = midiInGetNumDevs(); /* get # MIDI input devices */
if (wNumDevices)
{
for (i=-1; i < (int) wNumDevices; i++)
{
wRtn = GetDevCaps((i == -1) ? MIDI_MAPPER : i, &mcap);
if (!lstrcmp(szName, mcap.szPname))
{
if (!Open(i, lp1, lp2, dwFlags))
return(FALSE); /* error - return NOT OK */
return CMidiDevice::Open(szName, /* opened - return OK */
lp1, lp2, dwFlags);
}
}
}
return(FALSE); /* return NOT OK */
}
/*****************************************************************************/
/* Start : start recording from device */
/*****************************************************************************/
BOOL CMidiInDevice::Start ( )
{
if (!IsOpen() || IsRecording()) /* if no device open or recording */
return FALSE; /* return error */
MMRESULT res;
if ((res = midiInStart(hDev))) /* start recording from device */
{
TRACE1("CMidiInDevice::Start:midiInStart(): %s\n", GetErrorText(res));
return FALSE; /* upon error return error */
}
dwStamp = ::timeGetTime(); /* set internal time stamp */
bRecording = TRUE; /* set recording flag */
return TRUE;
}
/*****************************************************************************/
/* Stop : stop recording from device */
/*****************************************************************************/
BOOL CMidiInDevice::Stop ( )
{
if (!IsOpen() || !IsRecording()) /* if no device open or recording */
return FALSE; /* return error */
bRecording = FALSE; /* reset recording flag */
MMRESULT res = midiInStop(hDev); /* stop recording */
#if _DEBUG
if (res)
TRACE1("CMidiInDevice::Stop:midiInStop(): %s\n", GetErrorText(res));
#endif
dwStamp = 0; /* set internal time stamp */
return TRUE;
}
/*****************************************************************************/
/* Reset : resets the attached MIDI In device */
/*****************************************************************************/
BOOL CMidiInDevice::Reset ( )
{
if (!IsOpen()) /* if no device open */
return FALSE; /* return error */
MMRESULT res = midiInReset(hDev); /* reset the attached device */
#ifdef _DEBUG
if (res != MMSYSERR_NOERROR)
TRACE1("CMidiInDevice::Reset:midiInReset(): %s\n", GetErrorText(res));
#endif
bRecording = FALSE; /* reset recording flag */
return TRUE; /* and pass back OK */
}
/*****************************************************************************/
/* Close : closes the opened MIDI In Device */
/*****************************************************************************/
BOOL CMidiInDevice::Close ( )
{
if (!IsOpen()) /* if no device open */
return FALSE; /* return error */
bClosing = TRUE; /* set flag that we're closing now */
Stop(); /* stop recording from device */
Reset(); /* reset the attached device */
UnprepareBuf(); /* unprepare allocated data */
MMRESULT res = midiInClose(hDev); /* close the attached device */
#ifdef _DEBUG
if (res != MMSYSERR_NOERROR)
TRACE1("CMidiInDevice::Close: midiInClose()=%d\n", res);
#endif
hDev = NULL; /* and reset the handle */
bClosing = FALSE;
return CMidiDevice::Close(); /* and pass back OK */
}
/*****************************************************************************/
/* Message : sends a special message to the device */
/*****************************************************************************/
DWORD CMidiInDevice::Message
(
UINT msg,
DWORD dw1,
DWORD dw2
)
{
DWORD rc = 0;
if (hDev)
rc = midiInMessage(hDev, msg, dw1, dw2);
return rc;
}
/*****************************************************************************/
/* IsRecording : returns whether currently recording */
/*****************************************************************************/
BOOL CMidiInDevice::IsRecording ( )
{
return bRecording;
}
/*****************************************************************************/
/* AddHeader : adds a MIDI Input buffer to the device */
/*****************************************************************************/
BOOL CMidiInDevice::AddHeader
(
LPMIDIHDR lpHdr