forked from ClemensLode/Wars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectso.cpp
313 lines (255 loc) · 8.87 KB
/
directso.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
#include "engine.h"
#include "wave.h"
BOOL Engine_Sound::InitDirectSound(BOOL CreatePrimary3D,int channels,int freq,int bits)
{
if(!CreatePrimary3D)
{
DirectSoundCreate( NULL, &info.lpDS, NULL );
info.lpDS->SetCooperativeLevel( info.hwnd, DSSCL_NORMAL );
}
else
{
DirectSoundCreate( NULL, &info.lpDS, NULL );
info.lpDS->SetCooperativeLevel( info.hwnd, DSSCL_PRIORITY );
ZeroMemory( &dsbdesc, sizeof( DSBUFFERDESC ) );
dsbdesc.dwSize = sizeof( DSBUFFERDESC );
dsbdesc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;
if ( FAILED( info.lpDS->CreateSoundBuffer( &dsbdesc, &Primaer, NULL ) ) )
return FALSE;
memset( &wfm, 0, sizeof( WAVEFORMATEX ) );
wfm.wFormatTag = WAVE_FORMAT_PCM;
wfm.nChannels = channels;
wfm.nSamplesPerSec = freq;
wfm.wBitsPerSample = bits;
wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels;
wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign;
Primaer->SetFormat( &wfm );
if ( FAILED( Primaer->QueryInterface(
IID_IDirectSound3DListener,
( LPVOID * )&Listener ) ) )
{
Primaer->Release();
return FALSE;
}
Primaer->Release();
}
return TRUE;
}
void Engine_Sound::ReleaseObjects()
{
RELEASE(this->Listener);
RELEASE(info.lpDS);
}
void Engine_Sound::LoadStatic(LPDIRECTSOUNDBUFFER *buffer,char* filename)
{
HMMIO wavefile;
wavefile = mmioOpen(filename,0,MMIO_READ|MMIO_ALLOCBUF);
if(wavefile==NULL)
return ;
MMCKINFO parent;
memset(&parent,0,sizeof(MMCKINFO));
parent.fccType = mmioFOURCC('W','A','V','E');
mmioDescend(wavefile,&parent,0,MMIO_FINDRIFF);
MMCKINFO child;
memset(&child,0,sizeof(MMCKINFO));
child.fccType = mmioFOURCC('f','m','t',' ');
mmioDescend(wavefile,&child,&parent,0);
WAVEFORMATEX wavefmt;
mmioRead(wavefile,(char*)&wavefmt,sizeof(wavefmt));
if(wavefmt.wFormatTag != WAVE_FORMAT_PCM)
return ;
mmioAscend(wavefile,&child,0);
child.ckid = mmioFOURCC('d','a','t','a');
mmioDescend(wavefile,&child,&parent,MMIO_FINDCHUNK);
DSBUFFERDESC bufdesc;
memset(&bufdesc,0,sizeof(DSBUFFERDESC));
bufdesc.dwSize = sizeof(DSBUFFERDESC);
bufdesc.dwFlags = DSBCAPS_CTRLDEFAULT;
bufdesc.dwBufferBytes = child.cksize;
bufdesc.lpwfxFormat = &wavefmt;
if(DS_OK != (info.lpDS->CreateSoundBuffer(&bufdesc,&(*buffer),NULL)))
return ;
void *write1=0,*write2=0;
unsigned long length1,length2;
(*buffer)->Lock(0,child.cksize,&write1,&length1,&write2,&length2,0);
if(write1>0)
mmioRead(wavefile,(char*)write1,length1);
if(write2>0)
mmioRead(wavefile,(char*)write2,length2);
(*buffer)->Unlock(write1,length1,write2,length2);
mmioClose(wavefile,0);
return ;
}
void Engine_Sound::PlayStatic(IDirectSoundBuffer *snd,BOOL loop)
{
snd->SetCurrentPosition( 0 );
if(!loop)
snd->Play(0,0,0);
else
snd->Play(0,0,DSBPLAY_LOOPING);
}
void Engine_Sound::PanMove(IDirectSoundBuffer *buffer,int dir)
{
LONG pan;
buffer->GetPan(&pan);
if(dir==PANPLUS)
buffer->SetPan(pan+10);
else
buffer->SetPan(pan-10);
}
void Engine_Sound::VolumeMove(IDirectSoundBuffer *buffer,int dir)
{
LONG vol;
buffer->GetVolume(&vol);
if(dir==VOLPLUS)
buffer->SetVolume(vol+10);
else
buffer->SetVolume(vol-10);
}
void Engine_Sound::Stop(IDirectSoundBuffer *snd)
{
snd->Stop();
}
void Engine_Sound::FreqMove(IDirectSoundBuffer *buffer,int dir)
{
DWORD freq;
buffer->GetFrequency(&freq);
if(dir==FREQPLUS)
buffer->SetFrequency(freq+500);
else
buffer->SetFrequency(freq-500);
}
void Engine_Sound::TurnSound(IDirectSoundBuffer *buffer,int dir)
{
DWORD pos;
buffer->GetCurrentPosition(&pos,NULL);
if(dir==TURNPLUS)
buffer->SetCurrentPosition(pos+10);
else
buffer->SetCurrentPosition(pos-10);
}
BOOL Engine_Sound::LoadStreamBuffer(IDirectSoundBuffer *lpdsb,LPSTR lpzFileName )
{
DSBUFFERDESC dsbdesc;
HRESULT hr;
if ( lpdsb != NULL )
lpdsb->Stop();
WaveCloseReadFile( &hmmio, &pwfx );
if ( lpdsb != NULL )
{
lpdsb->Release();
lpdsb = NULL;
}
if ( WaveOpenFile( lpzFileName, &hmmio, &pwfx, &mmckinfoParent ) != 0 )
return FALSE;
if ( WaveStartDataRead( &hmmio, &mmckinfo, &mmckinfoParent ) != 0 )
return FALSE;
memset( &dsbdesc, 0, sizeof( DSBUFFERDESC ) );
dsbdesc.dwSize = sizeof( DSBUFFERDESC );
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2
| DSBCAPS_GLOBALFOCUS
| DSBCAPS_CTRLPAN;
dsbdesc.dwBufferBytes = pwfx->nAvgBytesPerSec * 2;
dsbdesc.lpwfxFormat = pwfx;
if ( FAILED( hr = info.lpDS->CreateSoundBuffer( &dsbdesc, &lpdsb, NULL ) ) )
{
WaveCloseReadFile( &hmmio, &pwfx );
return FALSE;
}
FillBufferWithSilence( lpdsb );
hr = lpdsb->Play( 0, 0, DSBPLAY_LOOPING );
dwMidBuffer = dsbdesc.dwBufferBytes / 2;
return TRUE;
}
BOOL Engine_Sound::PlayStreamBuffer(IDirectSoundBuffer *lpdsb)
{
HRESULT hr;
DWORD dwPlay;
DWORD dwStartOfs;
static DWORD dwLastPlayPos;
VOID *lpvData;
DWORD dwBytesLocked;
UINT cbBytesRead;
if ( lpdsb == NULL ) return FALSE;
if ( FAILED( lpdsb->GetCurrentPosition( &dwPlay, NULL ) ) )
return FALSE;
// Wenn der Abspielcursor gerade von der ersten in die zweite Hälfte
// des Puffers gewechselt hat oder umgekehrt, dann können wir die jeweils
// andere Hälfte des Puffers mit neuen Daten füllen
if ( ( ( dwPlay >= dwMidBuffer ) && ( dwLastPlayPos < dwMidBuffer ) )
|| ( dwPlay < dwLastPlayPos ) )
{
dwStartOfs = ( dwPlay >= dwMidBuffer ) ? 0 : dwMidBuffer;
hr = lpdsb->Lock( dwStartOfs, // Start = 0 oder Mitte des Puffers
dwMidBuffer, // Größe = halber Puffer
&lpvData, // Zeiger auf den Puffer, wird gesetzt
&dwBytesLocked, // Anzahl gesperrter Bytes, dito
NULL, // kein zweiter Bereichsstart
NULL, // keine zweite Bereichsgröße
0 ); // keine speziellen Flags
WaveReadFile( hmmio, // Handle der WAV-Datei
dwBytesLocked, // zu lesen = Größe d. Sperrbereichs
( BYTE * )lpvData, // Zieladresse
&mmckinfo, // data-Chunk
&cbBytesRead ); // Anzahl gelesener Bytes
if ( cbBytesRead < dwBytesLocked ) // Dateiende erreicht?
{
if ( WaveStartDataRead( &hmmio, &mmckinfo, &mmckinfoParent )
!= 0 )
{
OutputDebugString( "Fehler bei Repositionierung " \
"auf Dateianfang.\n" );
}
else
{ // die noch fehlenden Bytes einlesen
WaveReadFile( hmmio,
dwBytesLocked - cbBytesRead,
( BYTE * )lpvData + cbBytesRead,
&mmckinfo,
&cbBytesRead );
}
}
lpdsb->Unlock( lpvData, dwBytesLocked, NULL, 0 );
}
dwLastPlayPos = dwPlay;
return TRUE;
}
BOOL Engine_Sound::FillBufferWithSilence( IDirectSoundBuffer *lpDsb )
{
WAVEFORMATEX wfx;
DWORD dwSizeWritten;
PBYTE pb1;
DWORD cb1;
if ( FAILED( lpDsb->GetFormat( &wfx, sizeof( WAVEFORMATEX ), &dwSizeWritten ) ) )
return FALSE;
if ( SUCCEEDED( lpDsb->Lock( 0, 0,
( LPVOID * )&pb1, &cb1,
NULL, NULL, DSBLOCK_ENTIREBUFFER ) ) )
{
FillMemory( pb1, cb1, ( wfx.wBitsPerSample == 8 ) ? 128 : 0 );
lpDsb->Unlock( pb1, cb1, NULL, 0 );
return TRUE;
}
return FALSE;
}
void Engine_Sound::CheckHardware(DS_DRIVERINFO ds_dinf)
{
IDirectSound *dstemp;
DirectSoundCreate(NULL,&dstemp,NULL);
ds_dinf.HW_EMULDRIVER=FALSE;ds_dinf.HW_MSCERTIFIED=FALSE;ds_dinf.HW_PB16=FALSE;
ds_dinf.HW_PB8=FALSE;ds_dinf.HW_PMONO=FALSE;ds_dinf.HW_PSTEREO=FALSE;ds_dinf.HW_SB16=FALSE;
ds_dinf.HW_SB8=FALSE;ds_dinf.HW_SMONO=FALSE;ds_dinf.HW_SSTEREO=FALSE;
dscaps.dwSize = sizeof(dscaps);
dstemp->GetCaps(&dscaps);
if(dscaps.dwFlags & DSCAPS_CERTIFIED) ds_dinf.HW_MSCERTIFIED = TRUE;
if(dscaps.dwFlags & DSCAPS_EMULDRIVER) ds_dinf.HW_EMULDRIVER = TRUE;
if(dscaps.dwFlags & DSCAPS_PRIMARY16BIT) ds_dinf.HW_PB16 = TRUE;
if(dscaps.dwFlags & DSCAPS_PRIMARY8BIT) ds_dinf.HW_PB8 = TRUE;
if(dscaps.dwFlags & DSCAPS_PRIMARYMONO) ds_dinf.HW_PMONO = TRUE;
if(dscaps.dwFlags & DSCAPS_PRIMARYSTEREO) ds_dinf.HW_PSTEREO = TRUE;
if(dscaps.dwFlags & DSCAPS_SECONDARY16BIT) ds_dinf.HW_SB16 = TRUE;
if(dscaps.dwFlags & DSCAPS_SECONDARY8BIT) ds_dinf.HW_SB8 = TRUE;
if(dscaps.dwFlags & DSCAPS_SECONDARYMONO) ds_dinf.HW_SMONO = TRUE;
if(dscaps.dwFlags & DSCAPS_SECONDARYSTEREO) ds_dinf.HW_SSTEREO = TRUE;
dstemp->Release();
}