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
|
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
//============================================================
//
// sound.c - Win32 implementation of MAME sound routines
//
//============================================================
#include "sound_module.h"
#include "modules/osdmodule.h"
#if defined(OSD_WINDOWS) || defined(SDLMAME_WIN32)
// standard windows headers
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
// undef WINNT for dsound.h to prevent duplicate definition
#undef WINNT
#include <dsound.h>
#undef interface
// MAME headers
#include "emu.h"
#include "osdepend.h"
#include "emuopts.h"
#ifdef SDLMAME_WIN32
#include "../../sdl/osdsdl.h"
#if (SDLMAME_SDL2)
#include <SDL2/SDL_syswm.h>
#else
#include <SDL/SDL_syswm.h>
#endif
#include "../../sdl/window.h"
#else
#include "winmain.h"
#include "window.h"
#endif
//============================================================
// DEBUGGING
//============================================================
#define LOG_SOUND 0
#define LOG(x) do { if (LOG_SOUND) logerror x; } while(0)
class sound_direct_sound : public osd_module, public sound_module
{
public:
sound_direct_sound()
: osd_module(OSD_SOUND_PROVIDER, "dsound"), sound_module()
{
}
virtual ~sound_direct_sound() { }
virtual int init();
virtual void exit();
// sound_module
virtual void update_audio_stream(bool is_throttled, const INT16 *buffer, int samples_this_frame);
virtual void set_mastervolume(int attenuation);
private:
HRESULT dsound_init();
void dsound_kill();
HRESULT dsound_create_buffers();
void dsound_destroy_buffers();
void copy_sample_data(const INT16 *data, int bytes_to_copy);
};
//============================================================
// LOCAL VARIABLES
//============================================================
// DirectSound objects
static LPDIRECTSOUND dsound;
static DSCAPS dsound_caps;
// sound buffers
static LPDIRECTSOUNDBUFFER primary_buffer;
static LPDIRECTSOUNDBUFFER stream_buffer;
static UINT32 stream_buffer_size;
static UINT32 stream_buffer_in;
// descriptors and formats
static DSBUFFERDESC primary_desc;
static DSBUFFERDESC stream_desc;
static WAVEFORMATEX primary_format;
static WAVEFORMATEX stream_format;
// buffer over/underflow counts
static int buffer_underflows;
static int buffer_overflows;
//============================================================
// PROTOTYPES
//============================================================
//-------------------------------------------------
// sound_direct_sound - constructor
//-------------------------------------------------
int sound_direct_sound::init()
{
// attempt to initialize directsound
// don't make it fatal if we can't -- we'll just run without sound
dsound_init();
return 0;
}
void sound_direct_sound::exit()
{
// kill the buffers and dsound
dsound_destroy_buffers();
dsound_kill();
// print out over/underflow stats
if (buffer_overflows || buffer_underflows)
osd_printf_verbose("Sound: buffer overflows=%d underflows=%d\n", buffer_overflows, buffer_underflows);
LOG(("Sound buffer: overflows=%d underflows=%d\n", buffer_overflows, buffer_underflows));
}
//============================================================
// copy_sample_data
//============================================================
void sound_direct_sound::copy_sample_data(const INT16 *data, int bytes_to_copy)
{
void *buffer1, *buffer2;
DWORD length1, length2;
HRESULT result;
int cur_bytes;
// attempt to lock the stream buffer
result = IDirectSoundBuffer_Lock(stream_buffer, stream_buffer_in, bytes_to_copy, &buffer1, &length1, &buffer2, &length2, 0);
// if we failed, assume it was an underflow (i.e.,
if (result != DS_OK)
{
buffer_underflows++;
return;
}
// adjust the input pointer
stream_buffer_in = (stream_buffer_in + bytes_to_copy) % stream_buffer_size;
// copy the first chunk
cur_bytes = (bytes_to_copy > length1) ? length1 : bytes_to_copy;
memcpy(buffer1, data, cur_bytes);
// adjust for the number of bytes
bytes_to_copy -= cur_bytes;
data = (INT16 *)((UINT8 *)data + cur_bytes);
// copy the second chunk
if (bytes_to_copy != 0)
{
cur_bytes = (bytes_to_copy > length2) ? length2 : bytes_to_copy;
memcpy(buffer2, data, cur_bytes);
}
// unlock
result = IDirectSoundBuffer_Unlock(stream_buffer, buffer1, length1, buffer2, length2);
}
//============================================================
// update_audio_stream
//============================================================
void sound_direct_sound::update_audio_stream(bool is_throttled, const INT16 *buffer, int samples_this_frame)
{
int bytes_this_frame = samples_this_frame * stream_format.nBlockAlign;
DWORD play_position, write_position;
HRESULT result;
// if no sound, there is no buffer
if (stream_buffer == NULL)
return;
// determine the current play position
result = IDirectSoundBuffer_GetCurrentPosition(stream_buffer, &play_position, &write_position);
if (result == DS_OK)
{
DWORD stream_in;
//DWORD orig_write = write_position;
// normalize the write position so it is always after the play position
if (write_position < play_position)
write_position += stream_buffer_size;
// normalize the stream in position so it is always after the write position
stream_in = stream_buffer_in;
if (stream_in < write_position)
stream_in += stream_buffer_size;
// now we should have, in order:
// <------pp---wp---si--------------->
// if we're between play and write positions, then bump forward, but only in full chunks
while (stream_in < write_position)
{
//printf("Underflow: PP=%d WP=%d(%d) SI=%d(%d) BTF=%d\n", (int)play_position, (int)write_position, (int)orig_write, (int)stream_in, (int)stream_buffer_in, (int)bytes_this_frame);
buffer_underflows++;
stream_in += bytes_this_frame;
}
// if we're going to overlap the play position, just skip this chunk
if (stream_in + bytes_this_frame > play_position + stream_buffer_size)
{
//printf("Overflow: PP=%d WP=%d(%d) SI=%d(%d) BTF=%d\n", (int)play_position, (int)write_position, (int)orig_write, (int)stream_in, (int)stream_buffer_in, (int)bytes_this_frame);
buffer_overflows++;
return;
}
// now we know where to copy; let's do it
stream_buffer_in = stream_in % stream_buffer_size;
copy_sample_data(buffer, bytes_this_frame);
}
}
//============================================================
// set_mastervolume
//============================================================
void sound_direct_sound::set_mastervolume(int attenuation)
{
// clamp the attenuation to 0-32 range
if (attenuation > 0)
attenuation = 0;
if (attenuation < -32)
attenuation = -32;
// set the master volume
if (stream_buffer != NULL)
IDirectSoundBuffer_SetVolume(stream_buffer, (attenuation == -32) ? DSBVOLUME_MIN : attenuation * 100);
}
//============================================================
// dsound_init
//============================================================
HRESULT sound_direct_sound::dsound_init()
{
HRESULT result;
// create the DirectSound object
result = DirectSoundCreate(NULL, &dsound, NULL);
if (result != DS_OK)
{
osd_printf_error("Error creating DirectSound: %08x\n", (UINT32)result);
goto error;
}
// get the capabilities
dsound_caps.dwSize = sizeof(dsound_caps);
result = IDirectSound_GetCaps(dsound, &dsound_caps);
if (result != DS_OK)
{
osd_printf_error("Error getting DirectSound capabilities: %08x\n", (UINT32)result);
goto error;
}
// set the cooperative level
#ifdef SDLMAME_WIN32
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);
#if (SDLMAME_SDL2)
SDL_GetWindowWMInfo(sdl_window_list->sdl_window, &wminfo);
result = IDirectSound_SetCooperativeLevel(dsound, wminfo.info.win.window, DSSCL_PRIORITY);
#else
SDL_GetWMInfo(&wminfo);
result = IDirectSound_SetCooperativeLevel(dsound, wminfo.window, DSSCL_PRIORITY);
#endif
#else
result = IDirectSound_SetCooperativeLevel(dsound, win_window_list->m_hwnd, DSSCL_PRIORITY);
#endif
if (result != DS_OK)
{
osd_printf_error("Error setting DirectSound cooperative level: %08x\n", (UINT32)result);
goto error;
}
// make a format description for what we want
stream_format.wBitsPerSample = 16;
stream_format.wFormatTag = WAVE_FORMAT_PCM;
stream_format.nChannels = 2;
stream_format.nSamplesPerSec = sample_rate();
stream_format.nBlockAlign = stream_format.wBitsPerSample * stream_format.nChannels / 8;
stream_format.nAvgBytesPerSec = stream_format.nSamplesPerSec * stream_format.nBlockAlign;
// compute the buffer size based on the output sample rate
int audio_latency;
audio_latency = m_audio_latency;
stream_buffer_size = stream_format.nSamplesPerSec * stream_format.nBlockAlign * audio_latency / 10;
stream_buffer_size = (stream_buffer_size / 1024) * 1024;
if (stream_buffer_size < 1024)
stream_buffer_size = 1024;
LOG(("stream_buffer_size = %d\n", stream_buffer_size));
// create the buffers
result = dsound_create_buffers();
if (result != DS_OK)
goto error;
// start playing
result = IDirectSoundBuffer_Play(stream_buffer, 0, 0, DSBPLAY_LOOPING);
if (result != DS_OK)
{
osd_printf_error("Error playing: %08x\n", (UINT32)result);
goto error;
}
return DS_OK;
// error handling
error:
dsound_destroy_buffers();
dsound_kill();
return result;
}
//============================================================
// dsound_kill
//============================================================
void sound_direct_sound::dsound_kill()
{
// release the object
if (dsound != NULL)
IDirectSound_Release(dsound);
dsound = NULL;
}
//============================================================
// dsound_create_buffers
//============================================================
HRESULT sound_direct_sound::dsound_create_buffers()
{
HRESULT result;
void *buffer;
DWORD locked;
// create a buffer desc for the primary buffer
memset(&primary_desc, 0, sizeof(primary_desc));
primary_desc.dwSize = sizeof(primary_desc);
primary_desc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_GETCURRENTPOSITION2;
primary_desc.lpwfxFormat = NULL;
// create the primary buffer
result = IDirectSound_CreateSoundBuffer(dsound, &primary_desc, &primary_buffer, NULL);
if (result != DS_OK)
{
osd_printf_error("Error creating primary DirectSound buffer: %08x\n", (UINT32)result);
goto error;
}
// attempt to set the primary format
result = IDirectSoundBuffer_SetFormat(primary_buffer, &stream_format);
if (result != DS_OK)
{
osd_printf_error("Error setting primary DirectSound buffer format: %08x\n", (UINT32)result);
goto error;
}
// get the primary format
result = IDirectSoundBuffer_GetFormat(primary_buffer, &primary_format, sizeof(primary_format), NULL);
if (result != DS_OK)
{
osd_printf_error("Error getting primary DirectSound buffer format: %08x\n", (UINT32)result);
goto error;
}
osd_printf_verbose("DirectSound: Primary buffer: %d Hz, %d bits, %d channels\n",
(int)primary_format.nSamplesPerSec, (int)primary_format.wBitsPerSample, (int)primary_format.nChannels);
// create a buffer desc for the stream buffer
memset(&stream_desc, 0, sizeof(stream_desc));
stream_desc.dwSize = sizeof(stream_desc);
stream_desc.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2;
stream_desc.dwBufferBytes = stream_buffer_size;
stream_desc.lpwfxFormat = &stream_format;
// create the stream buffer
result = IDirectSound_CreateSoundBuffer(dsound, &stream_desc, &stream_buffer, NULL);
if (result != DS_OK)
{
osd_printf_error("Error creating DirectSound stream buffer: %08x\n", (UINT32)result);
goto error;
}
// lock the buffer
result = IDirectSoundBuffer_Lock(stream_buffer, 0, stream_buffer_size, &buffer, &locked, NULL, NULL, 0);
if (result != DS_OK)
{
osd_printf_error("Error locking DirectSound stream buffer: %08x\n", (UINT32)result);
goto error;
}
// clear the buffer and unlock it
memset(buffer, 0, locked);
IDirectSoundBuffer_Unlock(stream_buffer, buffer, locked, NULL, 0);
return DS_OK;
// error handling
error:
dsound_destroy_buffers();
return result;
}
//============================================================
// dsound_destroy_buffers
//============================================================
void sound_direct_sound::dsound_destroy_buffers(void)
{
// stop any playback
if (stream_buffer != NULL)
IDirectSoundBuffer_Stop(stream_buffer);
// release the stream buffer
if (stream_buffer != NULL)
IDirectSoundBuffer_Release(stream_buffer);
stream_buffer = NULL;
// release the primary buffer
if (primary_buffer != NULL)
IDirectSoundBuffer_Release(primary_buffer);
primary_buffer = NULL;
}
#else /* SDLMAME_UNIX */
MODULE_NOT_SUPPORTED(sound_direct_sound, OSD_SOUND_PROVIDER, "dsound")
#endif
MODULE_DEFINITION(SOUND_DSOUND, sound_direct_sound)
|