-
Notifications
You must be signed in to change notification settings - Fork 16
/
dxsound.cpp
346 lines (279 loc) · 8.49 KB
/
dxsound.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
// MFC
#include "windows.h"
#include "dxsound.h"
#include "main.h"
#include "pcejin.h"
#pragma comment(lib,"dxguid.lib")
// DirectSound8
#include <dsound.h>
//bool soundBufferLow;
class DirectSound : public SoundDriver
{
private:
LPDIRECTSOUND8 pDirectSound; // DirectSound interface
LPDIRECTSOUNDBUFFER dsbPrimary; // Primary DirectSound buffer
LPDIRECTSOUNDBUFFER dsbSecondary; // Secondary DirectSound buffer
LPDIRECTSOUNDNOTIFY8 dsbNotify;
HANDLE dsbEvent;
WAVEFORMATEX wfx; // Primary buffer wave format
int soundBufferLen;
int soundBufferTotalLen;
unsigned int soundNextPosition;
public:
DirectSound();
virtual ~DirectSound();
bool init(long sampleRate); // initialize the primary and secondary sound buffer
void pause(); // pause the secondary sound buffer
void reset(); // stop and reset the secondary sound buffer
void resume(); // resume the secondary sound buffer
void write(u16 * finalWave, int length); // write the emulated sound to the secondary sound buffer
void mute();
void unMute();
void doUserMute();
};
DirectSound::DirectSound()
{
pDirectSound = NULL;
dsbPrimary = NULL;
dsbSecondary = NULL;
dsbNotify = NULL;
dsbEvent = NULL;
soundBufferTotalLen = 14700;
soundNextPosition = 0;
}
DirectSound::~DirectSound()
{
if(dsbNotify) {
dsbNotify->Release();
dsbNotify = NULL;
}
if(dsbEvent) {
CloseHandle(dsbEvent);
dsbEvent = NULL;
}
if(pDirectSound) {
if(dsbPrimary) {
dsbPrimary->Release();
dsbPrimary = NULL;
}
if(dsbSecondary) {
dsbSecondary->Release();
dsbSecondary = NULL;
}
pDirectSound->Release();
pDirectSound = NULL;
}
}
bool DirectSound::init(long sampleRate)
{
HRESULT hr;
DWORD freq;
DSBUFFERDESC dsbdesc;
int i;
hr = CoCreateInstance( CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound8, (LPVOID *)&pDirectSound );
if( hr != S_OK ) {
printf("IDS_CANNOT_CREATE_DIRECTSOUND");
// systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr );
return false;
}
pDirectSound->Initialize( &DSDEVID_DefaultPlayback );
if( hr != DS_OK ) {
printf("IDS_CANNOT_CREATE_DIRECTSOUND");
// systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr );
return false;
}
if( FAILED( hr = pDirectSound->SetCooperativeLevel( g_hWnd, DSSCL_EXCLUSIVE ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage( IDS_CANNOT_SETCOOPERATIVELEVEL, _T("Cannot SetCooperativeLevel %08x"), hr );
return false;
}
// Create primary sound buffer
ZeroMemory( &dsbdesc, sizeof(DSBUFFERDESC) );
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
// if( theApp.dsoundDisableHardwareAcceleration ) {
dsbdesc.dwFlags |= DSBCAPS_LOCSOFTWARE;
// }
if( FAILED( hr = pDirectSound->CreateSoundBuffer( &dsbdesc, &dsbPrimary, NULL ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage(IDS_CANNOT_CREATESOUNDBUFFER, _T("Cannot CreateSoundBuffer %08x"), hr);
return false;
}
freq = sampleRate;
// calculate the number of samples per frame first
// then multiply it with the size of a sample frame (16 bit * stereo)
// soundBufferLen previous value: 3628-120.
// Recalc: ((freq / (20000000 / (259 * 384 * 4))) * 4) & 0xFFFFFC = 3508.
// & FFFFFC ~= (3508.8 -%= 4) = 3508. 3509 won't work, modulus by 4.
soundBufferLen = 3508;//( freq / 60 ) * 4;
soundBufferTotalLen = soundBufferLen * 10;
soundNextPosition = 0;
ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = freq;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
if( FAILED( hr = dsbPrimary->SetFormat( &wfx ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage( IDS_CANNOT_SETFORMAT_PRIMARY, _T("CreateSoundBuffer(primary) failed %08x"), hr );
return false;
}
// Create secondary sound buffer
ZeroMemory( &dsbdesc, sizeof(DSBUFFERDESC) );
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME;
// if( theApp.dsoundDisableHardwareAcceleration ) {
dsbdesc.dwFlags |= DSBCAPS_LOCSOFTWARE;
// }
dsbdesc.dwBufferBytes = soundBufferTotalLen;
dsbdesc.lpwfxFormat = &wfx;
if( FAILED( hr = pDirectSound->CreateSoundBuffer( &dsbdesc, &dsbSecondary, NULL ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage( IDS_CANNOT_CREATESOUNDBUFFER, _T("CreateSoundBuffer(secondary) failed %08x"), hr );
return false;
}
if( FAILED( hr = dsbSecondary->SetCurrentPosition( 0 ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage( 0, _T("dsbSecondary->SetCurrentPosition failed %08x"), hr );
return false;
}
if( SUCCEEDED( hr = dsbSecondary->QueryInterface( IID_IDirectSoundNotify8, (LPVOID*)&dsbNotify ) ) ) {
dsbEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
DSBPOSITIONNOTIFY notify[10];
for( i = 0; i < 10; i++ ) {
notify[i].dwOffset = i * soundBufferLen;
notify[i].hEventNotify = dsbEvent;
}
if( FAILED( dsbNotify->SetNotificationPositions( 10, notify ) ) ) {
dsbNotify->Release();
dsbNotify = NULL;
CloseHandle(dsbEvent);
dsbEvent = NULL;
}
}
// Play primary buffer
if( FAILED( hr = dsbPrimary->Play( 0, 0, DSBPLAY_LOOPING ) ) ) {
printf("IDS_CANNOT_SETCOOPERATIVELEVEL");
// systemMessage( IDS_CANNOT_PLAY_PRIMARY, _T("Cannot Play primary %08x"), hr );
return false;
}
return true;
}
void DirectSound::pause()
{
if( dsbSecondary == NULL ) return;
DWORD status;
dsbSecondary->GetStatus( &status );
if( status & DSBSTATUS_PLAYING ) dsbSecondary->Stop();
}
void DirectSound::reset()
{
if( dsbSecondary == NULL ) return;
dsbSecondary->Stop();
dsbSecondary->SetCurrentPosition( 0 );
soundNextPosition = 0;
}
void DirectSound::resume()
{
if( dsbSecondary == NULL ) return;
dsbSecondary->Play( 0, 0, DSBPLAY_LOOPING );
}
bool soundPaused = false;
void DirectSound::write(u16 * finalWave, int length)
{
if(!pDirectSound) return;
HRESULT hr;
DWORD status = 0;
DWORD play = 0;
LPVOID lpvPtr1;
DWORD dwBytes1 = 0;
LPVOID lpvPtr2;
DWORD dwBytes2 = 0;
if( !pcejin.fastForward ) {//!speedup && synchronize && !theApp.throttle
hr = dsbSecondary->GetStatus(&status);
if( status & DSBSTATUS_PLAYING ) {
if( !soundPaused ) {
while( true ) {
dsbSecondary->GetCurrentPosition(&play, NULL);
int BufferLeft = ((soundNextPosition <= play) ?
play - soundNextPosition :
soundBufferTotalLen - soundNextPosition + play);
//If this was threaded, it likely wouldn't cause any trouble to make it
//wait a little bit longer than 10 milliseconds
//This coding causes the game to skip a little, depending on the
//soundBufferTotalLen and WaitForSingleObject time.
if((BufferLeft < soundBufferLen * 9) || soundDriver->userMute)
{
break;
} else {
if(dsbEvent) {
WaitForSingleObject(dsbEvent, 10);
}
}
}
}
}/* else {
// TODO: remove?
setsoundPaused(true);
}*/
}
// Obtain memory address of write block.
// This will be in two parts if the block wraps around.
if( DSERR_BUFFERLOST == ( hr = dsbSecondary->Lock(
soundNextPosition,
soundBufferLen,
&lpvPtr1,
&dwBytes1,
&lpvPtr2,
&dwBytes2,
0 ) ) ) {
// If DSERR_BUFFERLOST is returned, restore and retry lock.
dsbSecondary->Restore();
hr = dsbSecondary->Lock(
soundNextPosition,
soundBufferLen,
&lpvPtr1,
&dwBytes1,
&lpvPtr2,
&dwBytes2,
0 );
}
soundNextPosition += soundBufferLen;
soundNextPosition = soundNextPosition % soundBufferTotalLen;
if( SUCCEEDED( hr ) ) {
// Write to pointers.
CopyMemory( lpvPtr1, finalWave, dwBytes1 );
if ( lpvPtr2 ) {
CopyMemory( lpvPtr2, finalWave + dwBytes1, dwBytes2 );
}
// Release the data back to DirectSound.
hr = dsbSecondary->Unlock( lpvPtr1, dwBytes1, lpvPtr2, dwBytes2 );
} else {
printf("dsbSecondary->Lock() failed:");
// systemMessage( 0, _T("dsbSecondary->Lock() failed: %08x"), hr );
return;
}
}
SoundDriver *newDirectSound()
{
return new DirectSound();
}
void DirectSound::mute()
{
IDirectSoundBuffer8_SetVolume (dsbSecondary, DSBVOLUME_MIN);
}
void DirectSound::unMute() {
IDirectSoundBuffer8_SetVolume (dsbSecondary, DSBVOLUME_MAX);
}
void DirectSound::doUserMute() {
if(soundDriver->userMute) {
soundDriver->unMute();
soundDriver->userMute = false;
}
else {
soundDriver->mute();
soundDriver->userMute = true;
}
}