-
Notifications
You must be signed in to change notification settings - Fork 13
/
DSoundDev.cpp
1143 lines (956 loc) · 41.9 KB
/
DSoundDev.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
/*****************************************************************************/
/* DSoundHost.cpp: DirectSound double-buffer 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
******************************************************************************/
#include "stdafx.h"
#include "DSoundDev.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/*****************************************************************************/
/* GUIDs (potentially) used herein */
/*****************************************************************************/
#include <initguid.h>
/* copied from dsound.h */
DEFINE_GUID(CLSID_DirectSound, 0x47d4d946, 0x62e8, 0x11cf, 0x93, 0xbc, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0);
DEFINE_GUID(CLSID_DirectSoundCapture, 0xb0210780, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
DEFINE_GUID(IID_IDirectSound, 0x279AFA83, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60);
DEFINE_GUID(IID_IDirectSoundCapture, 0xb0210781, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
DEFINE_GUID(IID_IDirectSoundBuffer, 0x279AFA85, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60);
DEFINE_GUID(IID_IDirectSound3DListener, 0x279AFA84, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60);
DEFINE_GUID(IID_IDirectSound3DBuffer, 0x279AFA86, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60);
DEFINE_GUID(IID_IDirectSoundCaptureBuffer, 0xb0210782, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf, 0x8, 0x0, 0xa0, 0xc9, 0x25, 0xcd, 0x16);
DEFINE_GUID(IID_IKsPropertySet, 0x31efac30, 0x515c, 0x11d0, 0xa9, 0xaa, 0x00, 0xaa, 0x00, 0x61, 0xbe, 0x93);
/*****************************************************************************/
/* CDSoundDll class declaration */
/*****************************************************************************/
/* This encapsulation is necessary since not all systems have the same set */
/* of functions in DSOUND.dll (or no DSOUND.dll at all!) */
/*****************************************************************************/
class CDSoundDll
{
public:
CDSoundDll();
virtual ~CDSoundDll();
bool IsLoaded() { return !!hDll; }
HRESULT CaptureCreate(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter);
HRESULT Create(LPGUID lpGuid, LPDIRECTSOUND * ppDS, IUnknown FAR * pUnkOuter);
HRESULT CaptureEnumerate(LPDSENUMCALLBACK lpDSEnumCallback, LPVOID lpContext);
HRESULT Enumerate(LPDSENUMCALLBACK lpDSEnumCallback, LPVOID lpContext);
protected:
enum
{
eFuncCaptureCreate,
eFuncCreate,
eFuncCaptureEnumerate,
eFuncEnumerate,
eFuncMax
};
bool LoadDll();
bool UnloadDll();
HMODULE hDll;
FARPROC proc[eFuncMax];
};
/*****************************************************************************/
/* CDSoundDll : constructor */
/*****************************************************************************/
CDSoundDll::CDSoundDll()
{
hDll = NULL;
LoadDll(); /* initialize DLL automatically */
}
/*****************************************************************************/
/* ~CDSoundDll : destructor */
/*****************************************************************************/
CDSoundDll::~CDSoundDll()
{
UnloadDll(); /* make sure to unload the DLL */
}
/*****************************************************************************/
/* LoadDll : loads the DirectSound DLL and resolves needed entry points */
/*****************************************************************************/
bool CDSoundDll::LoadDll()
{
UnloadDll(); /* start clean */
/* make sure there's no popup */
UINT oem = ::SetErrorMode(SEM_FAILCRITICALERRORS |
SEM_NOGPFAULTERRORBOX |
SEM_NOOPENFILEERRORBOX);
hDll = ::LoadLibrary("DSOUND.dll"); /* load the DLL */
::SetErrorMode(oem); /* restore previous error mode */
if (!hDll) /* if error loading the DLL */
return false; /* return with error */
static LPCSTR lpszFuncName[eFuncMax] =
{
"DirectSoundCaptureCreate",
"DirectSoundCreate",
"DirectSoundCaptureEnumerate",
"DirectSoundEnumerate",
};
for (int i = 0; i < eFuncMax; i++) /* now load all necessary functions */
{
CString sName = lpszFuncName[i];
proc[i] = GetProcAddress(hDll, sName);
if (!proc[i])
{
sName += "A";
proc[i] = GetProcAddress(hDll, sName);
}
}
return true; /* pass back OK */
}
/*****************************************************************************/
/* UnloadDll : removes the loaded DLL and all entry points */
/*****************************************************************************/
bool CDSoundDll::UnloadDll()
{
if (!hDll)
return true;
if (hDll)
FreeLibrary(hDll);
hDll = NULL;
for (int i = 0; i < eFuncMax; i++)
proc[i] = NULL;
return true;
}
/*****************************************************************************/
/* CaptureCreate : creates an input device */
/*****************************************************************************/
HRESULT CDSoundDll::CaptureCreate(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter)
{
if (!proc[eFuncCaptureCreate])
return DSERR_INVALIDPARAM;
return ((HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUNDCAPTURE *, LPUNKNOWN))
proc[eFuncCaptureCreate])(lpGUID, lplpDSC, pUnkOuter);
}
/*****************************************************************************/
/* Create : creates an output device */
/*****************************************************************************/
HRESULT CDSoundDll::Create(LPGUID lpGuid, LPDIRECTSOUND * ppDS, IUnknown FAR * pUnkOuter)
{
if (!proc[eFuncCreate])
return DSERR_INVALIDPARAM;
return ((HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND *, IUnknown FAR *))
proc[eFuncCreate])(lpGuid, ppDS, pUnkOuter);
}
/*****************************************************************************/
/* CaptureEnumerate : enumerates all capture devices if possible */
/*****************************************************************************/
HRESULT CDSoundDll::CaptureEnumerate(LPDSENUMCALLBACK lpDSEnumCallback, LPVOID lpContext)
{
if (!proc[eFuncCaptureEnumerate])
return DSERR_INVALIDPARAM;
return ((HRESULT (WINAPI *)(LPDSENUMCALLBACK,LPVOID))
proc[eFuncCaptureEnumerate])(lpDSEnumCallback, lpContext);
}
/*****************************************************************************/
/* Enumerate : enumerates all output devices if possible */
/*****************************************************************************/
HRESULT CDSoundDll::Enumerate(LPDSENUMCALLBACK lpDSEnumCallback, LPVOID lpContext)
{
if (!proc[eFuncEnumerate])
return DSERR_INVALIDPARAM;
return ((HRESULT (WINAPI *)(LPDSENUMCALLBACK,LPVOID))
proc[eFuncEnumerate])(lpDSEnumCallback, lpContext);
}
/*===========================================================================*/
/* Global Data needed in this module */
/*===========================================================================*/
static CDSoundDll dll; /* the one and only DirectSound DLL */
/*===========================================================================*/
/* CDSoundDeviceList class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundDeviceList : constructor */
/*****************************************************************************/
CDSoundDeviceList::CDSoundDeviceList()
{
}
/*****************************************************************************/
/* ~CDSoundDeviceList : destructor */
/*****************************************************************************/
CDSoundDeviceList::~CDSoundDeviceList()
{
for (int i = 0; i < Guids.GetSize(); i++)
if (Guids[i])
{
LPGUID pGuid = (LPGUID)Guids[i];
delete pGuid;
}
}
/*****************************************************************************/
/* EnumDevices : output enumeration */
/*****************************************************************************/
BOOL CALLBACK CDSoundDeviceList::EnumDevices
(
LPGUID lpGUID,
LPCTSTR lpszDesc,
LPCTSTR lpszDrvName,
LPVOID lpContext
)
{
CDSoundDeviceList *pList = (CDSoundDeviceList *)lpContext;
CString sOut;
LPGUID pcGuid = NULL;
if (lpGUID)
{
pcGuid = new GUID;
if (!pcGuid)
return FALSE;
*pcGuid = *lpGUID;
}
if (pList->Guids.Add(pcGuid) < 0)
{
if (pcGuid)
delete pcGuid;
return FALSE;
}
if (pList->Add(lpszDesc) < 0)
return FALSE;
if (pList->Names.Add(lpszDrvName))
return FALSE;
return TRUE;
}
/*****************************************************************************/
/* GetName : retrieves the driver name for a device */
/*****************************************************************************/
CString CDSoundDeviceList::GetName(int nDevNum)
{
if ((nDevNum < 0) || (nDevNum >= Guids.GetSize()))
return "";
return Names[nDevNum];
}
CString CDSoundDeviceList::GetName(LPCSTR lpszDesc)
{
for (int nDevNum = 0; nDevNum < GetSize(); nDevNum++)
if (GetAt(nDevNum) == lpszDesc)
return Names[nDevNum];
return "";
}
/*****************************************************************************/
/* GetGUID : returns the GUID for a device */
/*****************************************************************************/
LPGUID CDSoundDeviceList::GetGUID(int nDevNum)
{
if ((nDevNum < 0) || (nDevNum >= Guids.GetSize()))
return NULL;
return (LPGUID)Guids[nDevNum];
}
LPGUID CDSoundDeviceList::GetGUID(LPCSTR lpszDesc)
{
for (int nDevNum = 0; nDevNum < GetSize(); nDevNum++)
if (GetAt(nDevNum) == lpszDesc)
return (LPGUID)Guids[nDevNum];
return NULL;
}
/*===========================================================================*/
/* CDSoundInputList class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundInputList : constructor */
/*****************************************************************************/
CDSoundInputList::CDSoundInputList()
{
if (dll.CaptureEnumerate((LPDSENUMCALLBACK)EnumDevices, this) != DS_OK)
RemoveAll();
}
/*===========================================================================*/
/* CDSoundOutputList class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundOutputList : constructor */
/*****************************************************************************/
CDSoundOutputList::CDSoundOutputList()
{
if (dll.Enumerate((LPDSENUMCALLBACK)EnumDevices, this) != DS_OK)
RemoveAll();
}
/*===========================================================================*/
/* CDSoundDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundDevice : constructor */
/*****************************************************************************/
CDSoundDevice::CDSoundDevice()
{
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 */
SetupSBDesc(&sbd, 0, 0, &wf); /* no meaningful values yet */
sbpn[ePosStart].dwOffset = 0; /* fill predefined positions */
sbpn[ePosStop].dwOffset = DSBPN_OFFSETSTOP;
}
/*****************************************************************************/
/* ~CDSoundDevice : destructor */
/*****************************************************************************/
CDSoundDevice::~CDSoundDevice()
{
Close();
KillEvt(); /* kill event thread & handle */
}
/*****************************************************************************/
/* SetupWaveformat : initializes a wave format buffer */
/*****************************************************************************/
void CDSoundDevice::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;
}
/*****************************************************************************/
/* SetupSBDesc : set up a secondary buffer description */
/*****************************************************************************/
void CDSoundDevice::SetupSBDesc
(
LPDSCBUFFERDESC pbd, /* description to fill */
DWORD dwFlags, /* I/O specific flags */
DWORD dwSamples, /* buffer size in samples */
LPWAVEFORMATEX pwf /* buffer format */
)
{
pbd->dwSize = sizeof(DSCBUFFERDESC); /* set up size */
pbd->dwFlags = dwFlags; /* set up flags */
pbd->dwReserved = 0;
pbd->lpwfxFormat = (pwf) ? pwf : &wf; /* use external / internal format */
/* calculate buffer size in bytes */
DWORD dwCalcBuf = 2 * dwSamples; /* we use a double-buffering scheme */
dwCalcBuf *= pbd->lpwfxFormat->nBlockAlign;
pbd->dwBufferBytes = dwCalcBuf;
}
/*****************************************************************************/
/* Open : opens a specific device */
/*****************************************************************************/
BOOL CDSoundDevice::Open
(
LPCSTR lpszDesc,
DWORD dwFlags,
DWORD dwSamples, /* #samples in one buffer */
LPWAVEFORMATEX pwfx
)
{
LPGUID lpGuid = GetGuid(lpszDesc); /* try to get a GUID for that */
if (!lpGuid) /* if impossible */
return FALSE; /* return error */
/* otherwise setup buffer description*/
SetupSBDesc(&sbd, dwFlags, dwSamples, pwfx);
if (!Open(lpGuid)) /* and try to open the device */
return FALSE; /* upon error return error */
strcpy(szDevName, lpszDesc); /* remember the name */
OnOpen(); /* allow subclasses to react */
return TRUE; /* and pass back OK */
}
BOOL CDSoundDevice::Open(LPGUID lpGuid)
{
bIsOpen = TRUE; /* remember this has been opened */
return TRUE; /* pass back OK */
}
/*****************************************************************************/
/* ReadBuffer : reads one halfth of the double-buffer */
/*****************************************************************************/
BOOL CDSoundDevice::ReadBuffer(int nBuffer, LPVOID lpBuf, DWORD &rdwSamples)
{
if ((nBuffer < 0) || (nBuffer > 1) ||
(!IsOpen()) || (!sbd.lpwfxFormat))
return FALSE;
DWORD dwCopy = rdwSamples * sbd.lpwfxFormat->nBlockAlign;
if (dwCopy > sbpn[ePosMiddle].dwOffset)
dwCopy = sbpn[ePosMiddle].dwOffset;
rdwSamples = dwCopy / sbd.lpwfxFormat->nBlockAlign;
LPVOID lpBuf1, lpBuf2;
DWORD dwBytes1, dwBytes2;
if (!Lock(sbpn[nBuffer].dwOffset,
sbpn[ePosMiddle].dwOffset,
&lpBuf1, &dwBytes1,
&lpBuf2, &dwBytes2))
return FALSE;
memcpy(lpBuf, lpBuf1, dwCopy); /* CAN only be pointer 1 */
return Unlock(lpBuf1, dwBytes1, lpBuf2, dwBytes2);
}
/*****************************************************************************/
/* WriteBuffer : fills one halfth of the double-buffer */
/*****************************************************************************/
BOOL CDSoundDevice::WriteBuffer(int nBuffer, LPVOID lpBuf, DWORD &rdwSamples)
{
if ((nBuffer < 0) || (nBuffer > 1) ||
(!IsOpen()) || (!sbd.lpwfxFormat))
return FALSE;
DWORD dwCopy = rdwSamples * sbd.lpwfxFormat->nBlockAlign;
if (dwCopy > sbpn[ePosMiddle].dwOffset)
dwCopy = sbpn[ePosMiddle].dwOffset;
rdwSamples = dwCopy / sbd.lpwfxFormat->nBlockAlign;
LPVOID lpBuf1, lpBuf2;
DWORD dwBytes1, dwBytes2;
if (!Lock(sbpn[nBuffer].dwOffset,
sbpn[ePosMiddle].dwOffset,
&lpBuf1, &dwBytes1,
&lpBuf2, &dwBytes2))
return FALSE;
memcpy(lpBuf1, lpBuf, dwCopy); /* CAN only be pointer 1 */
return Unlock(lpBuf1, dwBytes1, lpBuf2, dwBytes2);
}
/*****************************************************************************/
/* Close : kill eventually running event thread */
/*****************************************************************************/
BOOL CDSoundDevice::Close()
{
Release(); /* release allocated objects */
KillEvt(); /* kill event thread & handle */
*szDevName = 0; /* reset device name */
bIsOpen = FALSE; /* remember we're closed */
return TRUE;
}
/*****************************************************************************/
/* EvtThreadProc : event thread procedure */
/*****************************************************************************/
UINT CDSoundDevice::EvtThreadProc(LPVOID pParam)
{
UINT result;
CDSoundDevice *pDev = (CDSoundDevice *)pParam;
while (!pDev->bEvtCancel)
{
result = WaitForSingleObject(pDev->hEvent, INFINITE);
if ((result == WAIT_OBJECT_0) &&
(!pDev->bEvtCancel))
{
if (pDev->IsStarted()) /* if driver is currently started */
{ /* find out where driver really is */
/* pass back the other buffer */
pDev->OnSwitch(1 - pDev->GetDriverBuf());
}
else /* otherwise */
pDev->OnStop(); /* this is a Stop event */
}
else
break;
}
pDev->bEvtCancel = FALSE;
return 0;
}
/*****************************************************************************/
/* StartEvt : creates an event handle & thread */
/*****************************************************************************/
BOOL CDSoundDevice::StartEvt()
{
int i;
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 */
/* put it into notification handler */
for (i = 0; i < (sizeof(sbpn)/sizeof(sbpn[0])); i++)
sbpn[i].hEventNotify = hEvent;
/* 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 & event handle */
/*****************************************************************************/
void CDSoundDevice::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 */
while (bEvtCancel) /* wait until it does. */
{
TRACE0("Waiting for thread to stop\n");
Sleep(50);
}
pEvtThread = 0; /* and thread handle */
}
CloseHandle(hEvent); /* and close the event handle */
hEvent = 0; /* and reset it... */
}
}
/*****************************************************************************/
/* BoostPriority : increases the thread's priority */
/*****************************************************************************/
BOOL CDSoundDevice::BoostPriority()
{
if (pEvtThread)
{
pEvtThread->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL);
return TRUE;
}
return FALSE;
}
/*===========================================================================*/
/* CDSoundInDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundInDevice : constructor */
/*****************************************************************************/
CDSoundInDevice::CDSoundInDevice()
{
lpDS = NULL;
lpDSB = NULL;
}
/*****************************************************************************/
/* ~CDSoundInDevice : destructor */
/*****************************************************************************/
CDSoundInDevice::~CDSoundInDevice()
{
}
/*****************************************************************************/
/* Open : opens the input device */
/*****************************************************************************/
BOOL CDSoundInDevice::Open(LPGUID lpGuid)
{
if (IsOpen()) /* if already open */
Close(); /* close the opened device */
/* create DirectSoundCapture instance*/
HRESULT hr = dll.CaptureCreate(lpGuid,
&lpDS,
NULL);
if (FAILED(hr)) /* if failed */
return FALSE;
/* create secondary buffer */
hr = lpDS->CreateCaptureBuffer((LPCDSCBUFFERDESC)&sbd, &lpDSB, NULL);
if (FAILED(hr)) /* upon error */
{
lpDSB = NULL; /* make sure pointer is correct */
Release(); /* release device */
return FALSE; /* and return error */
}
sbpn[ePosMiddle].dwOffset = /* set up notification positions */
sbd.dwBufferBytes >> 1;
#if 1
// got to find out whether this works... for now, just do as if it would...
/* set up notifications */
hr = lpDSB->QueryInterface(IID_IDirectSoundNotify, (LPVOID *)&lpDSN);
if (FAILED(hr)) /* upon error */
{
lpDSN = NULL; /* make sure the pointer is correct */
Release(); /* release device */
return FALSE; /* and return error */
}
hr = lpDSN->SetNotificationPositions(ePosMax, sbpn);
if (FAILED(hr)) /* if error */
{
Release(); /* release device */
return FALSE; /* and return with error */
}
#endif
return CDSoundDevice::Open(lpGuid); /* let base class do its job */
}
/*****************************************************************************/
/* IsStarted : returns whether the device is currently recording */
/*****************************************************************************/
BOOL CDSoundInDevice::IsStarted()
{
if (!lpDSB)
return FALSE;
DWORD dwStatus = 0;
lpDSB->GetStatus(&dwStatus);
return !!(dwStatus & DSCBSTATUS_CAPTURING);
}
/*****************************************************************************/
/* Start : start recording */
/*****************************************************************************/
BOOL CDSoundInDevice::Start(DWORD dwFlags)
{
if (!lpDSB)
return FALSE;
if (dwFlags == (DWORD)-1) /* if default flags */
dwFlags = DSCBSTART_LOOPING; /* start in loop mode */
return SUCCEEDED(lpDSB->Start(dwFlags));
}
/*****************************************************************************/
/* Stop : stops recording */
/*****************************************************************************/
BOOL CDSoundInDevice::Stop()
{
if (!lpDSB)
return FALSE;
return SUCCEEDED(lpDSB->Stop());
}
/*****************************************************************************/
/* Close : closes the current output device */
/*****************************************************************************/
BOOL CDSoundInDevice::Close()
{
Stop();
return CDSoundDevice::Close();
}
/*****************************************************************************/
/* GetDriverPos : get driver's current position */
/*****************************************************************************/
DWORD CDSoundInDevice::GetDriverPos()
{
if (!lpDSB)
return 0;
DWORD dwCapture, dwRead;
lpDSB->GetCurrentPosition(&dwCapture, &dwRead);
return dwCapture;
}
/*****************************************************************************/
/* GetBufferPos : get our own position */
/*****************************************************************************/
DWORD CDSoundInDevice::GetBufferPos()
{
if (!lpDSB)
return 0;
DWORD dwCapture, dwRead;
lpDSB->GetCurrentPosition(&dwCapture, &dwRead);
return dwRead;
}
/*****************************************************************************/
/* GetFormat : retrieves the currently configured format */
/*****************************************************************************/
BOOL CDSoundInDevice::GetFormat(LPWAVEFORMATEX lpWF)
{
if (!lpDSB)
return FALSE;
return SUCCEEDED(lpDSB->GetFormat(lpWF, sizeof(WAVEFORMATEX), NULL));
}
/*****************************************************************************/
/* GetGuid : returns the GUID for a device descriptor */
/*****************************************************************************/
LPGUID CDSoundInDevice::GetGuid(LPCSTR lpszDesc)
{
CDSoundInputList list;
LPGUID lpGuid = list.GetGUID(lpszDesc);
if (!lpGuid)
return NULL;
guid = *lpGuid;
return &guid;
}
/*****************************************************************************/
/* GetCaps : retrieves the device capabilities */
/*****************************************************************************/
BOOL CDSoundInDevice::GetCaps(LPDSCBCAPS lpDSCBCaps)
{
if (!lpDSB)
return FALSE;
lpDSCBCaps->dwSize = sizeof(DSCBCAPS);
return !FAILED(lpDSB->GetCaps(lpDSCBCaps));
}
/*****************************************************************************/
/* Lock : locks a range of bytes */
/*****************************************************************************/
BOOL CDSoundInDevice::Lock(DWORD dwCursor, DWORD dwBytes, LPVOID *lplpvPtr1, LPDWORD lpdwBytes1, LPVOID *lplpvPtr2, LPDWORD lpdwBytes2, DWORD dwFlags)
{
if (!lpDSB)
return FALSE;
return SUCCEEDED(lpDSB->Lock(dwCursor, dwBytes, lplpvPtr1, lpdwBytes1, lplpvPtr2, lpdwBytes2, dwFlags));
}
/*****************************************************************************/
/* Unlock : remnoves current lock */
/*****************************************************************************/
BOOL CDSoundInDevice::Unlock(LPVOID lpPtr1, DWORD dwBytes1, LPVOID lpPtr2, DWORD dwBytes2)
{
if (!lpDSB)
return FALSE;
return SUCCEEDED(lpDSB->Unlock(lpPtr1, dwBytes1, lpPtr2, dwBytes2));
}
/*****************************************************************************/
/* Release : releases the allocated */
/*****************************************************************************/
void CDSoundInDevice::Release()
{
if (lpDSN)
lpDSN->Release();
lpDSN = NULL;
if (lpDSB)
lpDSB->Release();
lpDSB = NULL;
if (lpDS)
lpDS->Release();
lpDS = NULL;
}
/*===========================================================================*/
/* CDSoundOutDevice class members */
/*===========================================================================*/
/*****************************************************************************/
/* CDSoundOutDevice : constructor */
/*****************************************************************************/
CDSoundOutDevice::CDSoundOutDevice()
{
lpDS = NULL;
lpDSB = NULL;
lpDSN = NULL;
}
/*****************************************************************************/
/* ~CDSoundOutDevice : destructor */
/*****************************************************************************/
CDSoundOutDevice::~CDSoundOutDevice()
{
}
/*****************************************************************************/
/* Open : opens the output device */
/*****************************************************************************/
BOOL CDSoundOutDevice::Open(LPGUID lpGuid)
{
if (IsOpen()) /* if already open */
Close(); /* close the opened device */
HRESULT hr = dll.Create(lpGuid, /* create DirectSound instance */
&lpDS,
NULL);
if (FAILED(hr)) /* upon error */
{
lpDS = NULL; /* reset DirectSound pointer */
return FALSE; /* and pass back error */
}
/* set cooperative level to normal */
hr = lpDS->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(),
DSSCL_NORMAL);
if (FAILED(hr)) /* upon error */
{
lpDSB = NULL; /* make sure pointer is correct */
Release(); /* release device */
return FALSE; /* and return error */
}
/* create secondary buffer */
hr = lpDS->CreateSoundBuffer((LPCDSBUFFERDESC)&sbd, &lpDSB, NULL);
if (FAILED(hr)) /* upon error */
{
lpDSB = NULL; /* make sure pointer is correct */
Release(); /* release device */
return FALSE; /* and return error */
}
sbpn[ePosMiddle].dwOffset = /* set up notification positions */
sbd.dwBufferBytes >> 1;
if (sbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY)
{
#if 0
// this simply doesn't work. No idea why.
// Ah well, set up a timer for that...
/* set up notifications */
hr = lpDSB->QueryInterface(IID_IDirectSoundNotify, (LPVOID *)&lpDSN);
if (FAILED(hr)) /* upon error */
{
lpDSN = NULL; /* make sure the pointer is correct */
Release(); /* release device */
return FALSE; /* and return error */
}
hr = lpDSN->SetNotificationPositions(ePosMax, sbpn);
if (FAILED(hr))
{
Release();
return FALSE;
}
#else
// set up timer-based double-buffer handling?
// nah, we let input handle that
#endif
}
return CDSoundDevice::Open(lpGuid); /* let base class do its job */
}
/*****************************************************************************/
/* GetDriverPos : get driver's current position */
/*****************************************************************************/
DWORD CDSoundOutDevice::GetDriverPos()
{
if (!lpDSB)
return 0;
DWORD dwPlay, dwWrite;
lpDSB->GetCurrentPosition(&dwPlay, &dwWrite);
TRACE2("DSoundOutDevice::GetCurrentPosition(%d,%d)\n", dwPlay, dwWrite);
return dwPlay;
}
/*****************************************************************************/
/* SetDriverPos : sets the driver's current position */
/*****************************************************************************/
BOOL CDSoundOutDevice::SetDriverPos(DWORD dwPos)
{
if (!lpDSB)
return FALSE;
return SUCCEEDED(lpDSB->SetCurrentPosition(dwPos));
}
/*****************************************************************************/
/* GetBufferPos : get our own position */
/*****************************************************************************/
DWORD CDSoundOutDevice::GetBufferPos()
{
if (!lpDSB)
return 0;
DWORD dwPlay, dwWrite;
lpDSB->GetCurrentPosition(&dwPlay, &dwWrite);
TRACE2("DSoundOutDevice::GetCurrentPosition(%d,%d)\n", dwPlay, dwWrite);
return dwWrite;
}
/*****************************************************************************/
/* IsStarted : returns whether the device is currently playing */
/*****************************************************************************/
BOOL CDSoundOutDevice::IsStarted()
{
if (!lpDSB)
return FALSE;
DWORD dwStatus = 0;
lpDSB->GetStatus(&dwStatus);
return !!(dwStatus & DSBSTATUS_PLAYING);
}
/*****************************************************************************/
/* Start : start playing */
/*****************************************************************************/
BOOL CDSoundOutDevice::Start(DWORD dwFlags)
{
if (!lpDSB)
return FALSE;
if (dwFlags == (DWORD)-1) /* if default flags */
dwFlags = DSBPLAY_LOOPING; /* start in loop mode */
return SUCCEEDED(lpDSB->Play(0, 0, dwFlags));
}