forked from Arakula/vsthost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfcwave.cpp
1620 lines (1377 loc) · 57 KB
/
mfcwave.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
/*****************************************************************************/
/* MFCWAVE.CPP : class implementation for wave 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"
#include "mfcwave.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*****************************************************************************/
/* Global Data */
/*****************************************************************************/
static char szWMapName[] = WMAP_NAME; /* Wave Mapper device name */
/*===========================================================================*/
/* CWaveBuffer class members */
/*===========================================================================*/
/*****************************************************************************/
/* CWaveBuffer : constructor (various styles) */
/*****************************************************************************/
CWaveBuffer::CWaveBuffer(DWORD dwLen)
{
Init();
Set(dwLen);
}
CWaveBuffer::CWaveBuffer(LPWAVEHDR lpHdr)
{
Init();
Set(lpHdr);
}
CWaveBuffer::CWaveBuffer(LPVOID lpBuf, DWORD dwLen)
{
Init();
Set(lpBuf, dwLen);
}
/*****************************************************************************/
/* Set : sets Wave buffer in various ways */
/*****************************************************************************/
void CWaveBuffer::Set(DWORD dwLen)
{
Set((LPVOID)NULL, dwLen);
}
void CWaveBuffer::Set(LPWAVEHDR lpHdr)
{
Empty();
this->lpHdr = lpHdr;
}
void CWaveBuffer::Set(LPVOID lpBuf, DWORD dwLen)
{
Empty();
hGlPtr = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
sizeof(WAVEHDR) + dwLen);
if (!hGlPtr)
return;
lpHdr = (LPWAVEHDR)GlobalLock(hGlPtr);
if (lpHdr)
{
bAlloc = TRUE;
memset((LPVOID)lpHdr, 0, sizeof(WAVEHDR));
lpHdr->lpData = (LPSTR)(lpHdr+1);
lpHdr->dwBufferLength = dwLen;
lpHdr->dwBytesRecorded = (lpBuf) ? dwLen : 0;
if (lpBuf)
memcpy(lpHdr->lpData, lpBuf, dwLen);
else
memset(lpHdr->lpData, 0, dwLen);
}
}
/*****************************************************************************/
/* LPWAVEHDR : returns an LPWAVEHDR */
/*****************************************************************************/
CWaveBuffer::operator LPWAVEHDR()
{
return lpHdr;
}
/*****************************************************************************/
/* Length : returns the buffer's data length */
/*****************************************************************************/
DWORD CWaveBuffer::Length()
{
if (!lpHdr) /* if empty */
return 0; /* return that fact */
if (lpHdr->dwBytesRecorded) /* if something recorded in there */
return lpHdr->dwBytesRecorded; /* return that */
else /* otherwise */
return lpHdr->dwBufferLength; /* return allocated buffer length */
}
/*****************************************************************************/
/* Init : initialize memory */
/*****************************************************************************/
void CWaveBuffer::Init()
{
bAlloc = FALSE;
lpHdr = NULL;
hGlPtr = NULL;
dwStamp = 0;
}
/*****************************************************************************/
/* Empty : empy memory */
/*****************************************************************************/
void CWaveBuffer::Empty()
{
if (bAlloc) /* if self-allocated */
{
GlobalUnlock(hGlPtr); /* free buffer */
GlobalFree(hGlPtr);
}
Init(); /* then re-initialize */
}
/*===========================================================================*/
/* CWaveDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CWaveDevice : constructor */
/*****************************************************************************/
CWaveDevice::CWaveDevice()
{
szDevName[0] = 0; /* reset device name */
hEvent = 0; /* no event handle allocated */
pEvtThread = 0; /* no event thread started */
bEvtCancel = FALSE; /* reset event thread cancel flag */
bIsOpen = FALSE; /* reset opened flag */
SetupWaveformat(&wf); /* default to 44.1kHz,16bit,stereo */
}
/*****************************************************************************/
/* SetupWaveformat : initializes a wave format buffer */
/*****************************************************************************/
void CWaveDevice::SetupWaveformat
(
LPWAVEFORMATEX pwf,
DWORD dwSamplesPerSec,
WORD wBitsPerSample,
WORD nChannels
)
{
pwf->wFormatTag = WAVE_FORMAT_PCM;
pwf->nChannels = nChannels;
pwf->nSamplesPerSec = dwSamplesPerSec;
pwf->nAvgBytesPerSec = dwSamplesPerSec * nChannels * wBitsPerSample / 8;
pwf->nBlockAlign = nChannels * wBitsPerSample / 8;
pwf->wBitsPerSample = wBitsPerSample;
pwf->cbSize = 0;
}
/*****************************************************************************/
/* ~CWaveDevice : destructor */
/*****************************************************************************/
CWaveDevice::~CWaveDevice()
{
KillEvt(); /* kill event thread & handle */
}
/*****************************************************************************/
/* CWaveDevice message map */
/*****************************************************************************/
BEGIN_MESSAGE_MAP(CWaveDevice, CWnd)
//{{AFX_MSG_MAP(CWaveDevice)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*****************************************************************************/
/* Create : creates necessary notification window */
/*****************************************************************************/
BOOL CWaveDevice::Create()
{
if (!CreateEx(0,
AfxRegisterWndClass(0),
"Wave Wnd",
WS_POPUP,
0,
0,
0,
0,
NULL,
NULL))
{
TRACE("Failed to create wave notification window");
return FALSE;
}
return TRUE;
}
/*****************************************************************************/
/* Open : */
/*****************************************************************************/
BOOL CWaveDevice::Open
(
int iID,
LPWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwCallbackInstance,
DWORD fdwOpen
)
{
if ((fdwOpen & CALLBACK_TYPEMASK) == /* if window callback */
CALLBACK_WINDOW)
AssureWnd(); /* make sure window is there */
bIsOpen = TRUE;
return TRUE;
}
BOOL CWaveDevice::Open
(
LPCSTR szName,
LPWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwCallbackInstance,
DWORD fdwOpen
)
{
strcpy(szDevName, szName);
return TRUE;
}
/*****************************************************************************/
/* Close : closes the device */
/*****************************************************************************/
BOOL CWaveDevice::Close ( )
{
KillEvt(); /* kill event thread & handle */
*szDevName = 0;
bIsOpen = FALSE;
return TRUE;
}
/*****************************************************************************/
/* GetSample : retrieves a sample from a buffer using open format */
/*****************************************************************************/
short CWaveDevice::GetSample(CWaveBuffer &buf, int nSample, int nChannel)
{
if (!IsOpen()) /* if not opened */
return 0; /* no sample, sonny... */
if (Channels() < 2) /* if mono */
nChannel = 0; /* reset requested channel # */
if ((nSample < 0) || /* sample must be in range! */
(nSample * BlockAlignment() > (int)buf.Length()))
return 0;
/* position on sample */
LPSTR lpPos = ((LPWAVEHDR)buf)->lpData +
(nSample * BlockAlignment()) +
(nChannel * Bits() / 8);
switch (Bits()) /* process according to sample bits */
{
case 8 : /* 8 bits ? */
return ((short)(*(BYTE *)lpPos) * 256) - (short)32768;
case 16 : /* 16 bits ? */
return *((short *)lpPos);
default : /* anything else ? */
return 0; /* we don't deal with pro values here*/
}
}
/*****************************************************************************/
/* SetSample : puts a sample into a buffer using open format */
/*****************************************************************************/
void CWaveDevice::SetSample
(
CWaveBuffer &buf,
int nSample,
int nChannel,
short sValue
)
{
if ((!IsOpen()) || /* if not opened */
(nChannel >= Channels()) || /* or beyond possible channels */
(nSample < 0) || /* sample must be in range! */
(nSample * BlockAlignment() > (int)buf.Length()))
return; /* no sample, sonny... */
/* position on sample */
LPSTR lpPos = ((LPWAVEHDR)buf)->lpData +
(nSample * BlockAlignment()) +
(nChannel * Bits() / 8);
switch (Bits()) /* process according to sample bits */
{
case 8 : /* 8 bits ? */
*((BYTE *)lpPos) =
(BYTE)((sValue / 256) + 128);
break;
case 16 : /* 16 bits ? */
*((short *)lpPos) = sValue;
break;
default : /* anything else ? */
return; /* we don't deal with pro values here*/
}
}
/*****************************************************************************/
/* EvtThreadProc : event thread procedure */
/*****************************************************************************/
UINT CWaveDevice::EvtThreadProc(LPVOID pParam)
{
UINT result;
CWaveDevice *pDev = (CWaveDevice *)pParam;
while (!pDev->bEvtCancel)
{
result = WaitForSingleObject(pDev->hEvent,INFINITE);
if ((result == WAIT_OBJECT_0) &&
(!pDev->bEvtCancel))
pDev->OnEvent();
else
break;
}
pDev->bEvtCancel = FALSE;
return 0;
}
/*****************************************************************************/
/* StartEvt : creates an event handle & thread */
/*****************************************************************************/
BOOL CWaveDevice::StartEvt()
{
KillEvt(); /* make sure none's there */
/* then create a new event handle */
hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
if (!hEvent) /* upon error */
return FALSE; /* return error */
/* then create the thread */
pEvtThread = AfxBeginThread(EvtThreadProc, this,
THREAD_PRIORITY_NORMAL, 0,
CREATE_SUSPENDED, NULL);
if (!pEvtThread) /* upon error */
{
KillEvt(); /* kill event handle */
return FALSE; /* and return error */
}
pEvtThread->m_bAutoDelete = TRUE; /* let it go! */
pEvtThread->ResumeThread();
return TRUE;
}
/*****************************************************************************/
/* KillEvt : kills an eventually running event thread or window */
/*****************************************************************************/
void CWaveDevice::KillEvt()
{
if (hEvent) /* if event handle allocated */
{
TRACE0("Killing event thread\n");
if (pEvtThread) /* if thread still running */
{
bEvtCancel = TRUE; /* set cancel flag */
SetEvent(hEvent); /* tell thread to die */
int nLoop = 0;
while (bEvtCancel) /* wait until it does. */
{
TRACE0("Waiting for thread to stop\n");
Sleep(50);
if (++nLoop > 40) /* if 2 seconds have passed, */
{
TRACE0("Timed out waiting for thread to stop!\n");
break;
}
}
pEvtThread = 0; /* and thread handle */
}
CloseHandle(hEvent); /* and close the event handle */
hEvent = 0; /* and reset it... */
}
if (m_hWnd) /* if window handle there, */
DestroyWindow(); /* destroy the window */
}
/*****************************************************************************/
/* BoostPriority : increases the thread's priority */
/*****************************************************************************/
BOOL CWaveDevice::BoostPriority()
{
if (pEvtThread)
{
pEvtThread->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
return TRUE;
}
return FALSE;
}
/*===========================================================================*/
/* Class CWaveInDeviceList */
/*===========================================================================*/
/*****************************************************************************/
/* CWaveInDeviceList : constructor */
/*****************************************************************************/
CWaveInDeviceList::CWaveInDeviceList ( )
{
WORD wNumDevices;
int i;
WAVEINCAPS wcap;
wNumDevices = waveInGetNumDevs(); /* get # Wave input devices */
if (wNumDevices)
{
for (i=0; i < (int) wNumDevices; i++)
{
waveInGetDevCaps(i, &wcap, sizeof(wcap));
Add(wcap.szPname);
}
}
}
/*===========================================================================*/
/* Class CWaveInDevice */
/*===========================================================================*/
/*****************************************************************************/
/* CWaveInDevice message map */
/*****************************************************************************/
BEGIN_MESSAGE_MAP(CWaveInDevice, CWaveDevice)
//{{AFX_MSG_MAP(CWaveInDevice)
ON_MESSAGE(MM_WIM_OPEN, OnWimOpen)
ON_MESSAGE(MM_WIM_DATA, OnWimData)
ON_MESSAGE(MM_WIM_CLOSE, OnWimClose)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*****************************************************************************/
/* CWaveInDevice : constructor */
/*****************************************************************************/
CWaveInDevice::CWaveInDevice ( )
{
hDev = NULL; /* reset device handle */
hGlobal = NULL; /* Wave In buffers */
pGlobal = NULL;
lpGlHdr = NULL;
nAllocated = 0;
nAllocCount = WAVEINHDRS;
nAllocSize = WAVEINBUFSIZE;
bRecording = FALSE; /* reset recording flag */
dwStamp = 0; /* set internal time stamp */
bClosing = FALSE;
}
/*****************************************************************************/
/* ~CWaveInDevice : destructor */
/*****************************************************************************/
CWaveInDevice::~CWaveInDevice ( )
{
if (IsOpen())
Close();
if (lpGlHdr)
delete[] lpGlHdr;
}
/*****************************************************************************/
/* WaveInProc : callback procedure */
/*****************************************************************************/
static void CALLBACK WaveInProc
(
HWAVEIN hMidiIn,
UINT wMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
)
{
CWaveInDevice * pInDev = (CWaveInDevice *)dwInstance;
switch (wMsg)
{
case WIM_CLOSE :
pInDev->OnWimClose();
break;
case WIM_OPEN :
pInDev->OnWimOpen();
break;
case WIM_DATA :
#if 0
pInDev->OnWimData((LPWAVEHDR)dwParam1);
#else
/* postpone processing since it could*/
/* lead to a deadlock */
pInDev->PostMessage(MM_WIM_DATA, 0, dwParam1);
#endif
break;
}
}
/*****************************************************************************/
/* Open : opens a Wave In Device with its ID */
/*****************************************************************************/
BOOL CWaveInDevice::Open
(
int iID,
LPWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwCallbackInstance,
DWORD fdwOpen
)
{
if (IsOpen()) /* if already open */
Close(); /* close the opened device */
if ((fdwOpen & CALLBACK_TYPEMASK) == /* if window callback */
CALLBACK_WINDOW)
AssureWnd(); /* make sure window is there */
waveInGetDevCaps(iID, /* get device capabilities */
&wCap, sizeof(wCap));
if (!pwfx) /* if no wave format defined */
{
SetupWaveformat(&wf); /* default to 16bit stereo 44.1kHz */
pwfx = &wf;
}
if ((fdwOpen & CALLBACK_TYPEMASK) == /* if function callback */
CALLBACK_FUNCTION)
{
if (dwCallback == WAVEOPENDEFAULT) /* if default value */
dwCallback = (DWORD)WaveInProc; /* set LP1 to procedure */
if (dwCallbackInstance == /* if default value */
WAVEOPENDEFAULT)
dwCallbackInstance = (DWORD)this; /* set LP2 to object */
}
else if ((fdwOpen&CALLBACK_TYPEMASK)== /* if event callback */
CALLBACK_EVENT)
{
if (dwCallback == WAVEOPENDEFAULT) /* if default value */
{
if (!StartEvt()) /* start event thread */
return FALSE; /* upon error terminate */
dwCallback = (DWORD)hEvent; /* set LP1 to event handle */
}
if (dwCallbackInstance == /* if default value */
WAVEOPENDEFAULT)
dwCallbackInstance = /* set LP2 to instance */
(UINT)AfxGetInstanceHandle();
}
/* if thread callback */
else if ((fdwOpen & CALLBACK_TYPEMASK) ==
CALLBACK_THREAD)
{
if (dwCallback == WAVEOPENDEFAULT) /* if default value */
dwCallback = (DWORD) /* set LP1 to current thread */
GetCurrentThread();
if (dwCallbackInstance == /* if default value */
WAVEOPENDEFAULT)
dwCallbackInstance = /* set LP2 to instance */
(UINT)AfxGetInstanceHandle();
}
else
{
if (dwCallback == WAVEOPENDEFAULT) /* if default value */
dwCallback = (UINT)m_hWnd; /* set LP1 to window */
if (dwCallbackInstance == /* if default value */
WAVEOPENDEFAULT)
dwCallbackInstance = /* set LP2 to instance */
(UINT)AfxGetInstanceHandle();
}
WORD wRtn = waveInOpen(&hDev, /* open the device */
iID, /* with the passed ID */
pwfx, dwCallback, dwCallbackInstance, fdwOpen);
if (wRtn) /* if open error */
{
#if defined(_DEBUG) || defined(_DEBUGFILE)
char szTxt[256];
waveInGetErrorText(wRtn, szTxt, sizeof(szTxt));
TRACE2("CWaveInDevice::Open(%d): %s\n", wRtn, szTxt);
#endif
hDev = NULL; /* make sure that handle is reset */
return FALSE;
}
if ((!AllocBuf(nAllocCount, /* allocate Wave Input buffers */
nAllocSize * pwfx->nBlockAlign))
#if 0
|| (!PrepareBuf()) /* and prepare them for usage */
#endif
)
{
Close(); /* upon error close device */
return FALSE; /* and return error */
}
/* call base class */
CWaveDevice::Open(iID, pwfx, dwCallback, dwCallbackInstance, fdwOpen);
if (pwfx != &wf) /* remember wave format called with */
wf = *pwfx;
#if 0
// if you want to have this computing-intensive task running all the time,
// use this method
return Start(); /* start recording */
#else
// if you want to start/stop recording manually to save processing power
// for more important tasks when not recording, use this method
return TRUE;
#endif
}
/*****************************************************************************/
/* Open : opens a device with its name */
/*****************************************************************************/
BOOL CWaveInDevice::Open
(
LPCSTR szName,
LPWAVEFORMATEX pwfx,
DWORD dwCallback,
DWORD dwCallbackInstance,
DWORD fdwOpen
)
{
WORD wNumDevices;
int i;
WORD wRtn;
WAVEINCAPS wcap;
if (IsOpen()) /* if already open */
Close(); /* close the opened device */
if (!szName) /* if no name passed */
szName = szWMapName; /* assume wave mapper */
wNumDevices = waveInGetNumDevs(); /* get # Wave input devices */
if (wNumDevices)
{
for (i=-1; i < (int) wNumDevices; i++)
{
if (i == -1)
lstrcpy(wcap.szPname, szWMapName);
else
wRtn = waveInGetDevCaps(i, &wcap, sizeof(wcap));
if (!lstrcmp(szName, wcap.szPname))
{
if (!Open(i, pwfx, dwCallback, dwCallbackInstance, fdwOpen))
return(FALSE); /* error - return NOT OK */
return CWaveDevice::Open(szName, /* opened - return OK */
pwfx, dwCallback, dwCallbackInstance, fdwOpen);
}
}
}
return(FALSE); /* return NOT OK */
}
/*****************************************************************************/
/* Start : start recording from device */
/*****************************************************************************/
BOOL CWaveInDevice::Start ( )
{
if (!IsOpen() || IsRecording()) /* if no device open or recording */
return FALSE; /* return error */
if ((!PrepareBuf()) || /* start recording from device */
(waveInStart(hDev)))
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 CWaveInDevice::Stop ( )
{
#if 1
if (!IsOpen() || !IsRecording()) /* if no device open or recording */
return FALSE; /* return error */
#else
if (!IsOpen()) /* if no device open */
return FALSE; /* return error */
if (!IsRecording()) /* if not recording */
{
if (PreparedBuffers()) /* but buffers prepared */
return UnprepareBuf(); /* unprepare buffers */
return FALSE; /* otherwise just return error */
}
#endif
bRecording = FALSE; /* reset recording flag */
Sleep(10); /* allow others to find that out */
waveInStop(hDev); /* stop recording */
Sleep(10); /* allow others to find that out */
dwStamp = 0; /* set internal time stamp */
return TRUE;
}
/*****************************************************************************/
/* PrepareHeader : prepares a header for use in mmsystem */
/*****************************************************************************/
BOOL CWaveInDevice::PrepareHeader(LPWAVEHDR lpHdr)
{
if (!hDev)
return FALSE;
return !waveInPrepareHeader(hDev, lpHdr, sizeof(WAVEHDR));
}
/*****************************************************************************/
/* UnprepareHeader : unprepares a used header */
/*****************************************************************************/
BOOL CWaveInDevice::UnprepareHeader(LPWAVEHDR lpHdr)
{
if (!hDev)
return FALSE;
return !waveInUnprepareHeader(hDev, lpHdr, sizeof(WAVEHDR));
}
/*****************************************************************************/
/* Reset : resets the attached Wave In device */
/*****************************************************************************/
BOOL CWaveInDevice::Reset ( )
{
if (!IsOpen()) /* if no device open */
return FALSE; /* return error */
Stop(); /* make sure recording is stopped */
waveInReset(hDev); /* reset the attached device */
Sleep(10); /* allow others to find out */
int nSleeps = 0; /* simple fuse... */
while (PreparedBuffers()) /* wait until all buffers unprepared */
{
TRACE1("CWaveInDevice::Reset(): waiting for %d buffers to be unprepared\n",
PreparedBuffers());
Sleep(50);
if (++nSleeps > 20) /* wait up to a second */
{
TRACE1("CWaveInDevice::Reset(): ABORTED (%d buffers to be unprepared)\n",
PreparedBuffers());
return FALSE; /* return with error */
}
}
return TRUE; /* and pass back OK */
}
/*****************************************************************************/
/* Close : closes the opened Wave In Device */
/*****************************************************************************/
BOOL CWaveInDevice::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 */
waveInClose(hDev); /* close the attached device */
hDev = NULL; /* and reset the handle */
bClosing = FALSE;
return CWaveDevice::Close(); /* and pass back OK */
}
/*****************************************************************************/
/* GetPosition : returns the current Wave In position */
/*****************************************************************************/
BOOL CWaveInDevice::GetPosition
(
LPMMTIME pmmt,
UINT cbmmt
)
{
return !waveInGetPosition(hDev, pmmt, cbmmt);
}
/*****************************************************************************/
/* Message : sends a special message to the device */
/*****************************************************************************/
DWORD CWaveInDevice::Message
(
UINT msg,
DWORD dw1,
DWORD dw2
)
{
DWORD rc = 0;
if (hDev)
rc = waveInMessage(hDev, msg, dw1, dw2);
return rc;
}
/*****************************************************************************/
/* AddHeader : adds a Wave Input buffer to the device */
/*****************************************************************************/
BOOL CWaveInDevice::AddHeader
(
LPWAVEHDR lpHdr
)
{
if (lpHdr->dwFlags & WHDR_PREPARED)
#if 1
return TRUE;
#else
UnprepareHeader(lpHdr);
#endif
lpHdr->dwFlags = 0;
//Hdr->dwBufferLength = wf.nAvgBytesPerSec / (2 * nAllocated);
lpHdr->dwBytesRecorded = 0;
if (!PrepareHeader(lpHdr))
return FALSE;
if (waveInAddBuffer(hDev, lpHdr, sizeof(WAVEHDR)))
return FALSE;
return TRUE;
}
/*****************************************************************************/
/* CheckHeader : checks the passed Wave header for validity */
/*****************************************************************************/
BOOL CWaveInDevice::CheckHeader
(
LPWAVEHDR lpHdr
)
{
int i;
if ((IsBadReadPtr(lpHdr, sizeof(WAVEHDR))) ||
(!lpGlHdr))
goto Ouch;
for (i = 0; i < nAllocated; i++) /* if it's one we allocated */
if (lpHdr == lpGlHdr[i])
return TRUE; /* pass back OK */
Ouch:
TRACE0("CWaveInDevice::CheckHeader: invalid header encountered!\n");
return FALSE;
}
/*****************************************************************************/
/* AllocBuf : allocates Wave In buffers */
/*****************************************************************************/
BOOL CWaveInDevice::AllocBuf (int nCount, int nSize)
{
int i; /* loop counter */
FreeBuf(); /* free eventually allocated buffers */
hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
nCount * (sizeof(WAVEHDR) + nSize));
if (hGlobal == NULL)
return FALSE;
pGlobal = (char *)GlobalLock(hGlobal); /* lock allocated memory */
if (pGlobal == NULL)
{
GlobalFree(hGlobal);
hGlobal = NULL;
return FALSE;
}
lpGlHdr = new LPWAVEHDR[nCount];
if (!lpGlHdr)
return FALSE;
for (i = 0; i < nCount; i++) /* now generate the wave buffers */
{
lpGlHdr[i] = (LPWAVEHDR)
(pGlobal + i * (sizeof(WAVEHDR) + nSize));
memset(lpGlHdr[i], 0, sizeof(WAVEHDR) + nSize);
lpGlHdr[i]->dwBufferLength = nSize;
lpGlHdr[i]->lpData = (char *)(lpGlHdr[i]+1);
}
nAllocated = nCount;
return TRUE;
}
/*****************************************************************************/
/* FreeBuf : frees allocated Wave In buffers */
/*****************************************************************************/
BOOL CWaveInDevice::FreeBuf ( )
{
int i;
if (!hGlobal) /* if nothing allocated */
return(TRUE); /* pass back OK */
UnprepareBuf(); /* make sure everything's unprepared */
for (i = 0; i < nAllocated; i++) /* otherwise remove the Wave buffers */
lpGlHdr[i] = NULL; /* reset buffer pointer */
pGlobal = NULL; /* reset global buffer pointer */
GlobalUnlock(hGlobal); /* unlock the global buffer */
GlobalFree(hGlobal); /* free it */
hGlobal = NULL; /* and reset the handle */
if (lpGlHdr)
delete[] lpGlHdr;
lpGlHdr = NULL;
nAllocated = 0;
return(TRUE);
}
/*****************************************************************************/
/* PrepareBuf : prepares Wave In buffers */
/*****************************************************************************/
BOOL CWaveInDevice::PrepareBuf ( )
{
int i; /* loop counter */
if ((!hGlobal) || (!pGlobal)) /* if not already allocated */
return FALSE; /* pass back NOT OK */
for (i = 0; i < nAllocated; i++) /* now prepare the Wave buffers */
if (!AddHeader(lpGlHdr[i]))
return FALSE;
return TRUE;
}