Debian Codegraph @

win32/plugins/dfsound/winsrc/dsound.c

raw
1 /***************************************************************************
2 dsound.c - description
3 -------------------
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : [email protected]
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
16 * *
17 ***************************************************************************/
18
19 //*************************************************************************//
20 // History of changes:
21 //
22 // 2003/01/12 - Pete
23 // - added recording funcs
24 //
25 // 2002/05/15 - Pete
26 // - generic cleanup for the Peops release
27 //
28 //*************************************************************************//
29
30 #include "stdafx.h"
31
32 #define _IN_DSOUND
33
34 #include "externals.h"
35
36 #ifdef _WINDOWS
37
38 #include <dsound.h>
39
40 #include "record.h"
41
42 ////////////////////////////////////////////////////////////////////////
43 // dsound globals
44 ////////////////////////////////////////////////////////////////////////
45
46 LPDIRECTSOUND lpDS;
47 LPDIRECTSOUNDBUFFER lpDSBP = NULL;
48 LPDIRECTSOUNDBUFFER lpDSB = NULL;
49 DSBUFFERDESC dsbd;
50 DSBUFFERDESC dsbdesc;
51 DSCAPS dscaps;
52 DSBCAPS dsbcaps;
53
54 unsigned long LastWrite=0xffffffff;
55 unsigned long LastPlay=0;
56
57 ////////////////////////////////////////////////////////////////////////
58 // SETUP SOUND
59 ////////////////////////////////////////////////////////////////////////
60
61 void SetupSound(void)
62 {
63 HRESULT dsval;WAVEFORMATEX pcmwf;
64
65 dsval = DirectSoundCreate(NULL,&lpDS,NULL);
66 if(dsval!=DS_OK)
67 {
68 MessageBox(hWMain,"DirectSoundCreate!","Error",MB_OK);
69 return;
70 }
71
72 if(DS_OK!=IDirectSound_SetCooperativeLevel(lpDS,hWMain, DSSCL_PRIORITY))
73 {
74 if(DS_OK!=IDirectSound_SetCooperativeLevel(lpDS,hWMain, DSSCL_NORMAL))
75 {
76 MessageBox(hWMain,"SetCooperativeLevel!","Error",MB_OK);
77 return;
78 }
79 }
80
81 memset(&dsbd,0,sizeof(DSBUFFERDESC));
82 dsbd.dwSize = 20; // NT4 hack! sizeof(dsbd);
83 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
84 dsbd.dwBufferBytes = 0;
85 dsbd.lpwfxFormat = NULL;
86
87 dsval=IDirectSound_CreateSoundBuffer(lpDS,&dsbd,&lpDSBP,NULL);
88 if(dsval!=DS_OK)
89 {
90 MessageBox(hWMain, "CreateSoundBuffer (Primary)", "Error",MB_OK);
91 return;
92 }
93
94 memset(&pcmwf, 0, sizeof(WAVEFORMATEX));
95 pcmwf.wFormatTag = WAVE_FORMAT_PCM;
96
97 if(iDisStereo) {pcmwf.nChannels = 1; pcmwf.nBlockAlign = 2;}
98 else {pcmwf.nChannels = 2; pcmwf.nBlockAlign = 4;}
99
100 pcmwf.nSamplesPerSec = 44100;
101
102 pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
103 pcmwf.wBitsPerSample = 16;
104
105 dsval=IDirectSoundBuffer_SetFormat(lpDSBP,&pcmwf);
106 if(dsval!=DS_OK)
107 {
108 MessageBox(hWMain, "SetFormat!", "Error",MB_OK);
109 return;
110 }
111
112 dscaps.dwSize = sizeof(DSCAPS);
113 dsbcaps.dwSize = sizeof(DSBCAPS);
114 IDirectSound_GetCaps(lpDS,&dscaps);
115 IDirectSoundBuffer_GetCaps(lpDSBP,&dsbcaps);
116
117 memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
118 dsbdesc.dwSize = 20; // NT4 hack! sizeof(DSBUFFERDESC);
119 dsbdesc.dwFlags = DSBCAPS_LOCSOFTWARE | DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
120 dsbdesc.dwBufferBytes = SOUNDSIZE;
121 dsbdesc.lpwfxFormat = (LPWAVEFORMATEX)&pcmwf;
122
123 dsval=IDirectSound_CreateSoundBuffer(lpDS,&dsbdesc,&lpDSB,NULL);
124 if(dsval!=DS_OK)
125 {
126 MessageBox(hWMain,"CreateSoundBuffer (Secondary)", "Error",MB_OK);
127 return;
128 }
129
130 dsval=IDirectSoundBuffer_Play(lpDSBP,0,0,DSBPLAY_LOOPING);
131 if(dsval!=DS_OK)
132 {
133 MessageBox(hWMain,"Play (Primary)","Error",MB_OK);
134 return;
135 }
136
137 dsval=IDirectSoundBuffer_Play(lpDSB,0,0,DSBPLAY_LOOPING);
138 if(dsval!=DS_OK)
139 {
140 MessageBox(hWMain,"Play (Secondary)","Error",MB_OK);
141 return;
142 }
143 }
144
145 ////////////////////////////////////////////////////////////////////////
146 // REMOVE SOUND
147 ////////////////////////////////////////////////////////////////////////
148
149 void RemoveSound(void)
150 {
151 int iRes;
152
153 if(iDoRecord) RecordStop();
154
155 if(lpDSB!=NULL)
156 {
157 IDirectSoundBuffer_Stop(lpDSB);
158 iRes=IDirectSoundBuffer_Release(lpDSB);
159 // FF says such a loop is bad... Demo says it's good... Pete doesn't care
160 while(iRes!=0) iRes=IDirectSoundBuffer_Release(lpDSB);
161 lpDSB=NULL;
162 }
163
164 if(lpDSBP!=NULL)
165 {
166 IDirectSoundBuffer_Stop(lpDSBP);
167 iRes=IDirectSoundBuffer_Release(lpDSBP);
168 // FF says such a loop is bad... Demo says it's good... Pete doesn't care
169 while(iRes!=0) iRes=IDirectSoundBuffer_Release(lpDSBP);
170 lpDSBP=NULL;
171 }
172
173 if(lpDS!=NULL)
174 {
175 iRes=IDirectSound_Release(lpDS);
176 // FF says such a loop is bad... Demo says it's good... Pete doesn't care
177 while(iRes!=0) iRes=IDirectSound_Release(lpDS);
178 lpDS=NULL;
179 }
180
181 }
182
183 ////////////////////////////////////////////////////////////////////////
184 // GET BYTES BUFFERED
185 ////////////////////////////////////////////////////////////////////////
186
187 unsigned long SoundGetBytesBuffered(void)
188 {
189 unsigned long cplay,cwrite;
190
191 if(LastWrite==0xffffffff) return 0;
192
193 IDirectSoundBuffer_GetCurrentPosition(lpDSB,&cplay,&cwrite);
194
195 if(cplay>SOUNDSIZE) return SOUNDSIZE;
196
197 if(cplay<LastWrite) return LastWrite-cplay;
198 return (SOUNDSIZE-cplay)+LastWrite;
199 }
200
201 ////////////////////////////////////////////////////////////////////////
202 // FEED SOUND DATA
203 ////////////////////////////////////////////////////////////////////////
204
205 void SoundFeedStreamData(unsigned char* pSound,long lBytes)
206 {
207 LPVOID lpvPtr1, lpvPtr2;
208 unsigned long dwBytes1,dwBytes2;
209 unsigned long *lpSS, *lpSD;
210 unsigned long dw,cplay,cwrite;
211 HRESULT hr;
212 unsigned long status;
213
214 if (lpDSB == NULL)
215 return;
216
217 if(iDoRecord) RecordBuffer(pSound,lBytes);
218
219 IDirectSoundBuffer_GetStatus(lpDSB,&status);
220 if(status&DSBSTATUS_BUFFERLOST)
221 {
222 if(IDirectSoundBuffer_Restore(lpDSB)!=DS_OK) return;
223 IDirectSoundBuffer_Play(lpDSB,0,0,DSBPLAY_LOOPING);
224 }
225
226 IDirectSoundBuffer_GetCurrentPosition(lpDSB,&cplay,&cwrite);
227
228 if(LastWrite==0xffffffff) LastWrite=cwrite;
229
230 /*
231 // mmm... security... not needed, I think
232 if(LastWrite<cplay)
233 {
234 if((cplay-LastWrite)<=(unsigned long)lBytes)
235 {
236 LastWrite=0xffffffff;
237 return;
238 }
239 }
240 else
241 {
242 if(LastWrite<cwrite)
243 {
244 LastWrite=0xffffffff;
245 return;
246 }
247 }
248 */
249
250 hr=IDirectSoundBuffer_Lock(lpDSB,LastWrite,lBytes,
251 &lpvPtr1, &dwBytes1,
252 &lpvPtr2, &dwBytes2,
253 0);
254
255 if(hr!=DS_OK) {LastWrite=0xffffffff;return;}
256
257 lpSD=(unsigned long *)lpvPtr1;
258 dw=dwBytes1>>2;
259
260 lpSS=(unsigned long *)pSound;
261 while(dw) {*lpSD++=*lpSS++;dw--;}
262
263 if(lpvPtr2)
264 {
265 lpSD=(unsigned long *)lpvPtr2;
266 dw=dwBytes2>>2;
267 while(dw) {*lpSD++=*lpSS++;dw--;}
268 }
269
270 IDirectSoundBuffer_Unlock(lpDSB,lpvPtr1,dwBytes1,lpvPtr2,dwBytes2);
271
272 LastWrite+=lBytes;
273 if(LastWrite>=SOUNDSIZE) LastWrite-=SOUNDSIZE;
274 LastPlay=cplay;
275 }
276
277 #endif
278
279
280