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
|
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: Olivier Lapicque */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
#include "openmpt/all/BuildSettings.hpp"
#include "SoundDeviceDirectSound.hpp"
#include "SoundDevice.hpp"
#include "SoundDeviceUtilities.hpp"
#include "mpt/base/detect.hpp"
#include "mpt/base/numeric.hpp"
#include "mpt/base/saturate_round.hpp"
#include "mpt/format/message_macros.hpp"
#include "mpt/format/simple.hpp"
#include "mpt/string/types.hpp"
#include "mpt/string_transcode/transcode.hpp"
#include "mpt/uuid/guid.hpp"
#include "mpt/uuid/uuid.hpp"
#include "openmpt/base/Types.hpp"
#include "openmpt/logging/Logger.hpp"
#include "openmpt/soundbase/SampleFormat.hpp"
#include <algorithm>
#include <vector>
#if MPT_OS_WINDOWS
#include <windows.h>
#endif // MPT_OS_WINDOWS
OPENMPT_NAMESPACE_BEGIN
namespace SoundDevice
{
#if defined(MPT_WITH_DIRECTSOUND)
namespace
{
struct DevicesAndLoggerAndSysInfo
{
std::vector<SoundDevice::Info> devices;
ILogger *logger;
SoundDevice::SysInfo sysInfo;
};
} // namespace
static BOOL WINAPI DSEnumCallback(GUID *lpGuid, LPCTSTR lpstrDescription, LPCTSTR lpstrDriver, LPVOID lpContext)
{
DevicesAndLoggerAndSysInfo &devicesAndLoggerAndSysInfo = *(DevicesAndLoggerAndSysInfo *)lpContext;
std::vector<SoundDevice::Info> &devices = devicesAndLoggerAndSysInfo.devices;
ILogger &logger = *devicesAndLoggerAndSysInfo.logger;
SoundDevice::SysInfo &sysInfo = devicesAndLoggerAndSysInfo.sysInfo;
auto GetLogger = [&]() -> ILogger &
{
return logger;
};
if(!lpstrDescription)
{
return TRUE;
}
GUID guid = (lpGuid ? *lpGuid : GUID());
SoundDevice::Info info;
info.type = TypeDSOUND;
info.default_ = (!lpGuid ? Info::Default::Managed : Info::Default::None);
info.internalID = mpt::transcode<mpt::ustring>(mpt::GUIDToString(guid));
info.name = mpt::transcode<mpt::ustring>(mpt::winstring(lpstrDescription));
if(lpstrDriver)
{
info.extraData[MPT_USTRING("DriverName")] = mpt::transcode<mpt::ustring>(mpt::winstring(lpstrDriver));
}
if(lpGuid)
{
info.extraData[MPT_USTRING("UUID")] = mpt::format<mpt::ustring>::val(mpt::UUID(guid));
}
info.apiName = MPT_USTRING("DirectSound");
info.useNameAsIdentifier = false;
// clang-format off
info.flags = {
sysInfo.SystemClass == mpt::osinfo::osclass::Windows ? sysInfo.IsWindowsOriginal() && sysInfo.WindowsVersion.IsBefore(mpt::osinfo::windows::Version::Win7) ? Info::Usability::Usable : Info::Usability::Deprecated : Info::Usability::NotAvailable,
Info::Level::Primary,
sysInfo.SystemClass == mpt::osinfo::osclass::Windows && sysInfo.IsWindowsWine() ? Info::Compatible::Yes : Info::Compatible::No,
sysInfo.SystemClass == mpt::osinfo::osclass::Windows ? sysInfo.IsWindowsWine() ? Info::Api::Emulated : sysInfo.WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista) ? Info::Api::Emulated : Info::Api::Native : Info::Api::Emulated,
Info::Io::OutputOnly,
Info::Mixing::Software,
Info::Implementor::OpenMPT
};
// clang-format on
devices.push_back(info);
return TRUE;
}
std::vector<SoundDevice::Info> CDSoundDevice::EnumerateDevices(ILogger &logger, SoundDevice::SysInfo sysInfo)
{
DevicesAndLoggerAndSysInfo devicesAndLoggerAndSysInfo = {std::vector<SoundDevice::Info>(), &logger, sysInfo};
DirectSoundEnumerate(DSEnumCallback, &devicesAndLoggerAndSysInfo);
return devicesAndLoggerAndSysInfo.devices;
}
CDSoundDevice::CDSoundDevice(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo)
: CSoundDeviceWithThread(logger, info, sysInfo)
, m_piDS(NULL)
, m_pPrimary(NULL)
, m_pMixBuffer(NULL)
, m_nDSoundBufferSize(0)
, m_bMixRunning(FALSE)
, m_dwWritePos(0)
, m_StatisticLatencyFrames(0)
, m_StatisticPeriodFrames(0)
{
return;
}
CDSoundDevice::~CDSoundDevice()
{
Close();
}
SoundDevice::Caps CDSoundDevice::InternalGetDeviceCaps()
{
SoundDevice::Caps caps;
caps.Available = true;
caps.CanUpdateInterval = true;
caps.CanSampleFormat = true;
caps.CanExclusiveMode = false;
caps.CanBoostThreadPriority = true;
caps.CanUseHardwareTiming = false;
caps.CanChannelMapping = false;
caps.CanInput = false;
caps.HasNamedInputSources = false;
caps.CanDriverPanel = false;
caps.ExclusiveModeDescription = MPT_USTRING("Use primary buffer");
caps.DefaultSettings.sampleFormat = (GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista)) ? SampleFormat::Float32 : SampleFormat::Int16;
IDirectSound *dummy = nullptr;
IDirectSound *ds = nullptr;
if(m_piDS)
{
ds = m_piDS;
} else
{
GUID guid = mpt::StringToGUID(mpt::transcode<mpt::winstring>(GetDeviceInternalID()));
if(DirectSoundCreate(mpt::IsValid(guid) ? &guid : NULL, &dummy, NULL) != DS_OK)
{
return caps;
}
if(!dummy)
{
return caps;
}
ds = dummy;
}
DSCAPS dscaps = {};
dscaps.dwSize = sizeof(dscaps);
if(DS_OK == ds->GetCaps(&dscaps))
{
if(!(dscaps.dwFlags & DSCAPS_EMULDRIVER))
{
caps.CanExclusiveMode = true;
}
}
if(dummy)
{
dummy->Release();
dummy = nullptr;
}
ds = nullptr;
return caps;
}
SoundDevice::DynamicCaps CDSoundDevice::GetDeviceDynamicCaps(const std::vector<uint32> &baseSampleRates)
{
SoundDevice::DynamicCaps caps;
IDirectSound *dummy = nullptr;
IDirectSound *ds = nullptr;
if(m_piDS)
{
ds = m_piDS;
} else
{
GUID guid = mpt::StringToGUID(mpt::transcode<mpt::winstring>(GetDeviceInternalID()));
if(DirectSoundCreate(mpt::IsValid(guid) ? &guid : NULL, &dummy, NULL) != DS_OK)
{
return caps;
}
if(!dummy)
{
return caps;
}
ds = dummy;
}
DSCAPS dscaps = {};
dscaps.dwSize = sizeof(dscaps);
if(DS_OK == ds->GetCaps(&dscaps))
{
if(dscaps.dwMaxSecondarySampleRate == 0)
{
// nothing known about supported sample rates
} else
{
for(const auto &rate : baseSampleRates)
{
if(dscaps.dwMinSecondarySampleRate <= rate && rate <= dscaps.dwMaxSecondarySampleRate)
{
caps.supportedSampleRates.push_back(rate);
caps.supportedExclusiveSampleRates.push_back(rate);
}
}
}
if(GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista))
{
// Vista
caps.supportedSampleFormats = {SampleFormat::Float32};
caps.supportedExclusiveModeSampleFormats = {SampleFormat::Float32};
} else if(!(dscaps.dwFlags & DSCAPS_EMULDRIVER))
{
// XP wdm
caps.supportedSampleFormats = {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Unsigned8};
caps.supportedExclusiveModeSampleFormats.clear();
if(dscaps.dwFlags & DSCAPS_PRIMARY8BIT)
{
caps.supportedExclusiveModeSampleFormats.push_back(SampleFormat::Unsigned8);
}
if(dscaps.dwFlags & DSCAPS_PRIMARY16BIT)
{
caps.supportedExclusiveModeSampleFormats.push_back(SampleFormat::Int16);
}
if(caps.supportedExclusiveModeSampleFormats.empty())
{
caps.supportedExclusiveModeSampleFormats = {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Unsigned8};
}
} else
{
// XP vdx
// nothing, announce all, fail later
caps.supportedSampleFormats = {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Unsigned8};
caps.supportedExclusiveModeSampleFormats = {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Unsigned8};
}
}
if(dummy)
{
dummy->Release();
dummy = nullptr;
}
ds = nullptr;
return caps;
}
bool CDSoundDevice::InternalOpen()
{
if(m_Settings.InputChannels > 0) return false;
WAVEFORMATEXTENSIBLE wfext;
if(!FillWaveFormatExtensible(wfext, m_Settings)) return false;
WAVEFORMATEX *pwfx = &wfext.Format;
const uint32 bytesPerFrame = static_cast<uint32>(m_Settings.GetBytesPerFrame());
DSBUFFERDESC dsbd;
DSBCAPS dsc;
if(m_piDS) return true;
GUID guid = mpt::StringToGUID(mpt::transcode<mpt::winstring>(GetDeviceInternalID()));
if(DirectSoundCreate(mpt::IsValid(guid) ? &guid : NULL, &m_piDS, NULL) != DS_OK) return false;
if(!m_piDS) return false;
if(m_piDS->SetCooperativeLevel(m_AppInfo.GetHWND(), m_Settings.ExclusiveMode ? DSSCL_WRITEPRIMARY : DSSCL_PRIORITY) != DS_OK)
{
Close();
return false;
}
m_bMixRunning = FALSE;
m_nDSoundBufferSize = mpt::saturate_round<int32>(m_Settings.Latency * pwfx->nAvgBytesPerSec);
m_nDSoundBufferSize = mpt::align_up<uint32>(m_nDSoundBufferSize, bytesPerFrame);
m_nDSoundBufferSize = std::clamp(m_nDSoundBufferSize, mpt::align_up<uint32>(DSBSIZE_MIN, bytesPerFrame), mpt::align_down<uint32>(DSBSIZE_MAX, bytesPerFrame));
if(!m_Settings.ExclusiveMode)
{
// Set the format of the primary buffer
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0;
dsbd.dwReserved = 0;
dsbd.lpwfxFormat = NULL;
if(m_piDS->CreateSoundBuffer(&dsbd, &m_pPrimary, NULL) != DS_OK)
{
Close();
return false;
}
if(m_pPrimary->SetFormat(pwfx) != DS_OK)
{
Close();
return false;
}
// Create the secondary buffer
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2;
dsbd.dwBufferBytes = m_nDSoundBufferSize;
dsbd.dwReserved = 0;
dsbd.lpwfxFormat = pwfx;
if(m_piDS->CreateSoundBuffer(&dsbd, &m_pMixBuffer, NULL) != DS_OK)
{
Close();
return false;
}
} else
{
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
dsbd.dwBufferBytes = 0;
dsbd.dwReserved = 0;
dsbd.lpwfxFormat = NULL;
if(m_piDS->CreateSoundBuffer(&dsbd, &m_pPrimary, NULL) != DS_OK)
{
Close();
return false;
}
if(m_pPrimary->SetFormat(pwfx) != DS_OK)
{
Close();
return false;
}
dsc.dwSize = sizeof(dsc);
if(m_pPrimary->GetCaps(&dsc) != DS_OK)
{
Close();
return false;
}
m_nDSoundBufferSize = dsc.dwBufferBytes;
m_pMixBuffer = m_pPrimary;
m_pMixBuffer->AddRef();
}
if(m_Settings.sampleFormat == SampleFormat::Int8)
{
m_Settings.sampleFormat = SampleFormat::Unsigned8;
}
LPVOID lpBuf1, lpBuf2;
DWORD dwSize1, dwSize2;
if(m_pMixBuffer->Lock(0, m_nDSoundBufferSize, &lpBuf1, &dwSize1, &lpBuf2, &dwSize2, 0) == DS_OK)
{
UINT zero = (pwfx->wBitsPerSample == 8) ? 0x80 : 0x00;
if((lpBuf1) && (dwSize1)) memset(lpBuf1, zero, dwSize1);
if((lpBuf2) && (dwSize2)) memset(lpBuf2, zero, dwSize2);
m_pMixBuffer->Unlock(lpBuf1, dwSize1, lpBuf2, dwSize2);
} else
{
DWORD dwStat = 0;
m_pMixBuffer->GetStatus(&dwStat);
if(dwStat & DSBSTATUS_BUFFERLOST) m_pMixBuffer->Restore();
}
m_dwWritePos = 0xFFFFFFFF;
SetWakeupInterval(std::min(m_Settings.UpdateInterval, m_nDSoundBufferSize / (2.0 * m_Settings.GetBytesPerSecond())));
m_Flags.WantsClippedOutput = (GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista));
return true;
}
bool CDSoundDevice::InternalClose()
{
if(m_pMixBuffer)
{
m_pMixBuffer->Release();
m_pMixBuffer = NULL;
}
if(m_pPrimary)
{
m_pPrimary->Release();
m_pPrimary = NULL;
}
if(m_piDS)
{
m_piDS->Release();
m_piDS = NULL;
}
m_bMixRunning = FALSE;
return true;
}
void CDSoundDevice::StartFromSoundThread()
{
// done in InternalFillAudioBuffer
}
void CDSoundDevice::StopFromSoundThread()
{
if(m_pMixBuffer)
{
m_pMixBuffer->Stop();
}
m_bMixRunning = FALSE;
}
void CDSoundDevice::InternalFillAudioBuffer()
{
if(!m_pMixBuffer)
{
RequestClose();
return;
}
if(m_nDSoundBufferSize == 0)
{
RequestClose();
return;
}
DWORD dwLatency = 0;
for(int refillCount = 0; refillCount < 2; ++refillCount)
{
// Refill the buffer at most twice so we actually sleep some time when CPU is overloaded.
const uint32 bytesPerFrame = static_cast<uint32>(m_Settings.GetBytesPerFrame());
DWORD dwPlay = 0;
DWORD dwWrite = 0;
if(m_pMixBuffer->GetCurrentPosition(&dwPlay, &dwWrite) != DS_OK)
{
RequestClose();
return;
}
uint32 dwBytes = m_nDSoundBufferSize / 2;
if(!m_bMixRunning)
{
// startup
m_dwWritePos = dwWrite;
dwLatency = 0;
} else
{
// running
dwLatency = (m_dwWritePos - dwPlay + m_nDSoundBufferSize) % m_nDSoundBufferSize;
dwLatency = (dwLatency + m_nDSoundBufferSize - 1) % m_nDSoundBufferSize + 1;
dwBytes = (dwPlay - m_dwWritePos + m_nDSoundBufferSize) % m_nDSoundBufferSize;
dwBytes = std::clamp(dwBytes, uint32(0), m_nDSoundBufferSize / 2); // limit refill amount to half the buffer size
}
dwBytes = dwBytes / bytesPerFrame * bytesPerFrame; // truncate to full frame
if(dwBytes < bytesPerFrame)
{
// ok, nothing to do
return;
}
void *buf1 = nullptr;
void *buf2 = nullptr;
DWORD dwSize1 = 0;
DWORD dwSize2 = 0;
HRESULT hr = m_pMixBuffer->Lock(m_dwWritePos, dwBytes, &buf1, &dwSize1, &buf2, &dwSize2, 0);
if(hr == DSERR_BUFFERLOST)
{
// buffer lost, restore buffer and try again, fail if it fails again
if(m_pMixBuffer->Restore() != DS_OK)
{
RequestClose();
return;
}
if(m_pMixBuffer->Lock(m_dwWritePos, dwBytes, &buf1, &dwSize1, &buf2, &dwSize2, 0) != DS_OK)
{
RequestClose();
return;
}
} else if(hr != DS_OK)
{
RequestClose();
return;
}
CallbackLockedAudioReadPrepare(dwSize1 / bytesPerFrame + dwSize2 / bytesPerFrame, dwLatency / bytesPerFrame);
CallbackLockedAudioProcessVoid(buf1, nullptr, dwSize1 / bytesPerFrame);
CallbackLockedAudioProcessVoid(buf2, nullptr, dwSize2 / bytesPerFrame);
m_StatisticLatencyFrames.store(dwLatency / bytesPerFrame);
m_StatisticPeriodFrames.store(dwSize1 / bytesPerFrame + dwSize2 / bytesPerFrame);
if(m_pMixBuffer->Unlock(buf1, dwSize1, buf2, dwSize2) != DS_OK)
{
CallbackLockedAudioProcessDone();
RequestClose();
return;
}
m_dwWritePos += dwSize1 + dwSize2;
m_dwWritePos %= m_nDSoundBufferSize;
CallbackLockedAudioProcessDone();
DWORD dwStatus = 0;
m_pMixBuffer->GetStatus(&dwStatus);
if(!m_bMixRunning || !(dwStatus & DSBSTATUS_PLAYING))
{
if(!(dwStatus & DSBSTATUS_BUFFERLOST))
{
// start playing
hr = m_pMixBuffer->Play(0, 0, DSBPLAY_LOOPING);
} else
{
// buffer lost flag is set, do not try start playing, we know it will fail with DSERR_BUFFERLOST.
hr = DSERR_BUFFERLOST;
}
if(hr == DSERR_BUFFERLOST)
{
// buffer lost, restore buffer and try again, fail if it fails again
if(m_pMixBuffer->Restore() != DS_OK)
{
RequestClose();
return;
}
if(m_pMixBuffer->Play(0, 0, DSBPLAY_LOOPING) != DS_OK)
{
RequestClose();
return;
}
} else if(hr != DS_OK)
{
RequestClose();
return;
}
m_bMixRunning = TRUE;
}
if(dwBytes < m_nDSoundBufferSize / 2)
{
// Sleep again if we did fill less than half the buffer size.
// Otherwise it's a better idea to refill again right away.
break;
}
}
}
SoundDevice::BufferAttributes CDSoundDevice::InternalGetEffectiveBufferAttributes() const
{
SoundDevice::BufferAttributes bufferAttributes;
bufferAttributes.Latency = m_nDSoundBufferSize * 1.0 / m_Settings.GetBytesPerSecond();
bufferAttributes.UpdateInterval = std::min(m_Settings.UpdateInterval, m_nDSoundBufferSize / (2.0 * m_Settings.GetBytesPerSecond()));
bufferAttributes.NumBuffers = 1;
return bufferAttributes;
}
SoundDevice::Statistics CDSoundDevice::GetStatistics() const
{
MPT_SOUNDDEV_TRACE_SCOPE();
SoundDevice::Statistics result;
result.InstantaneousLatency = 1.0 * m_StatisticLatencyFrames.load() / m_Settings.Samplerate;
result.LastUpdateInterval = 1.0 * m_StatisticPeriodFrames.load() / m_Settings.Samplerate;
result.text = mpt::ustring();
return result;
}
#endif // MPT_WITH_DIRECTSOUND
} // namespace SoundDevice
OPENMPT_NAMESPACE_END
|