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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: Olivier Lapicque */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
#include "openmpt/all/BuildSettings.hpp"
#include "SoundDeviceWaveout.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/parse/parse.hpp"
#include "mpt/string/buffer.hpp"
#include "mpt/string/types.hpp"
#include "mpt/string_transcode/transcode.hpp"
#include "openmpt/base/Types.hpp"
#include "openmpt/logging/Logger.hpp"
#include "openmpt/soundbase/SampleFormat.hpp"
#include <algorithm>
#include <array>
#include <set>
#include <vector>
#include <cstddef>
#if MPT_OS_WINDOWS
#include <windows.h>
#endif // MPT_OS_WINDOWS
OPENMPT_NAMESPACE_BEGIN
namespace SoundDevice
{
#if MPT_OS_WINDOWS
static constexpr std::size_t WAVEOUT_MINBUFFERS = 3;
static constexpr std::size_t WAVEOUT_MAXBUFFERS = 4096;
static constexpr std::size_t WAVEOUT_MINBUFFERFRAMECOUNT = 8;
static constexpr std::size_t WAVEOUT_MAXBUFFERSIZE = 16384; // fits in int16
static inline LONG *interlocked_access(DWORD *p)
{
static_assert(sizeof(LONG) == sizeof(DWORD));
return reinterpret_cast<LONG *>(p);
}
CWaveDevice::CWaveDevice(ILogger &logger, SoundDevice::Info info, SoundDevice::SysInfo sysInfo)
: CSoundDeviceWithThread(logger, info, sysInfo)
, m_DriverBugs(0)
{
MPT_SOUNDDEV_TRACE_SCOPE();
m_ThreadWakeupEvent = NULL;
m_Failed = false;
m_hWaveOut = NULL;
m_nWaveBufferSize = 0;
m_JustStarted = false;
m_nPreparedHeaders = 0;
m_nWriteBuffer = 0;
m_nDoneBuffer = 0;
m_nBuffersPending = 0;
m_PositionLast = {};
m_PositionWrappedCount = 0;
}
CWaveDevice::~CWaveDevice()
{
MPT_SOUNDDEV_TRACE_SCOPE();
Close();
}
int CWaveDevice::GetDeviceIndex() const
{
return mpt::ConvertStringTo<int>(GetDeviceInternalID());
}
SoundDevice::Caps CWaveDevice::InternalGetDeviceCaps()
{
MPT_SOUNDDEV_TRACE_SCOPE();
SoundDevice::Caps caps;
caps.Available = true;
caps.CanUpdateInterval = true;
caps.CanSampleFormat = true;
caps.CanExclusiveMode = (GetDeviceIndex() > 0); // no direct mode for WAVE_MAPPER, makes no sense there
caps.CanBoostThreadPriority = true;
caps.CanKeepDeviceRunning = false;
caps.CanUseHardwareTiming = false;
caps.CanChannelMapping = false;
caps.CanInput = false;
caps.HasNamedInputSources = false;
caps.CanDriverPanel = false;
caps.HasInternalDither = false;
caps.ExclusiveModeDescription = MPT_USTRING("Use direct mode");
if(GetSysInfo().IsWine)
{
caps.DefaultSettings.sampleFormat = SampleFormat::Int16;
} else if(GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista))
{
caps.DefaultSettings.sampleFormat = SampleFormat::Float32;
} else
{
caps.DefaultSettings.sampleFormat = SampleFormat::Int16;
}
return caps;
}
SoundDevice::DynamicCaps CWaveDevice::GetDeviceDynamicCaps(const std::vector<uint32> &baseSampleRates)
{
MPT_SOUNDDEV_TRACE_SCOPE();
SoundDevice::DynamicCaps caps;
if(GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista))
{ // emulated on WASAPI
caps.supportedSampleFormats = {SampleFormat::Float32};
caps.supportedExclusiveModeSampleFormats = {SampleFormat::Float32};
} else
{ // native WDM/VDX, or Wine
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(GetDeviceIndex() > 0)
{ // direct mode
if((GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista)) || !GetSysInfo().IsOriginal())
{ // emulated on WASAPI, or Wine
WAVEOUTCAPS woc = {};
caps.supportedExclusiveModeSampleFormats.clear();
if(waveOutGetDevCaps(GetDeviceIndex() - 1, &woc, sizeof(woc)) == MMSYSERR_NOERROR)
{
if(woc.dwFormats & (WAVE_FORMAT_96M08 | WAVE_FORMAT_96M16 | WAVE_FORMAT_96S08 | WAVE_FORMAT_96S16))
{
caps.supportedExclusiveSampleRates.push_back(96000);
}
if(woc.dwFormats & (WAVE_FORMAT_48M08 | WAVE_FORMAT_48M16 | WAVE_FORMAT_48S08 | WAVE_FORMAT_48S16))
{
caps.supportedExclusiveSampleRates.push_back(48000);
}
if(woc.dwFormats & (WAVE_FORMAT_4M08 | WAVE_FORMAT_4M16 | WAVE_FORMAT_4S08 | WAVE_FORMAT_4S16))
{
caps.supportedExclusiveSampleRates.push_back(44100);
}
if(woc.dwFormats & (WAVE_FORMAT_2M08 | WAVE_FORMAT_2M16 | WAVE_FORMAT_2S08 | WAVE_FORMAT_2S16))
{
caps.supportedExclusiveSampleRates.push_back(22050);
}
if(woc.dwFormats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_1M16 | WAVE_FORMAT_1S08 | WAVE_FORMAT_1S16))
{
caps.supportedExclusiveSampleRates.push_back(11025);
}
if(woc.dwFormats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 | WAVE_FORMAT_4M08 | WAVE_FORMAT_48M08 | WAVE_FORMAT_96M08 | WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 | WAVE_FORMAT_48S08 | WAVE_FORMAT_96S08))
{
caps.supportedExclusiveModeSampleFormats.push_back(SampleFormat::Unsigned8);
}
if(woc.dwFormats & (WAVE_FORMAT_1M16 | WAVE_FORMAT_2M16 | WAVE_FORMAT_4M16 | WAVE_FORMAT_48M16 | WAVE_FORMAT_96M16 | WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 | WAVE_FORMAT_48S16 | WAVE_FORMAT_96S16))
{
caps.supportedExclusiveModeSampleFormats.push_back(SampleFormat::Int16);
}
}
} else
{ // native WDM/VDX
caps.supportedExclusiveSampleRates.clear();
caps.supportedExclusiveModeSampleFormats.clear();
std::set<uint32> supportedSampleRates;
std::set<SampleFormat> supportedSampleFormats;
std::array<SampleFormat, 5> baseSampleFormats = {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Unsigned8};
for(const uint32 sampleRate : baseSampleRates)
{
for(const SampleFormat sampleFormat : baseSampleFormats)
{
WAVEFORMATEXTENSIBLE wfex = {};
Settings settings;
settings.Samplerate = sampleRate;
settings.Channels = 2;
settings.sampleFormat = sampleFormat;
if(FillWaveFormatExtensible(wfex, settings))
{
if(waveOutOpen(NULL, GetDeviceIndex() - 1, &wfex.Format, NULL, NULL, CALLBACK_NULL | WAVE_FORMAT_DIRECT | WAVE_FORMAT_QUERY) == MMSYSERR_NOERROR)
{
supportedSampleRates.insert(sampleRate);
supportedSampleFormats.insert(sampleFormat);
}
}
}
}
for(const uint32 sampleRate : baseSampleRates)
{
if(supportedSampleRates.count(sampleRate) > 0)
{
caps.supportedExclusiveSampleRates.push_back(sampleRate);
}
}
for(const SampleFormat sampleFormat : baseSampleFormats)
{
if(supportedSampleFormats.count(sampleFormat) > 0)
{
caps.supportedExclusiveModeSampleFormats.push_back(sampleFormat);
}
}
}
}
return caps;
}
bool CWaveDevice::InternalOpen()
{
MPT_SOUNDDEV_TRACE_SCOPE();
if(m_Settings.InputChannels > 0)
{
return false;
}
WAVEFORMATEXTENSIBLE wfext;
if(!FillWaveFormatExtensible(wfext, m_Settings))
{
return false;
}
WAVEFORMATEX *pwfx = &wfext.Format;
UINT nWaveDev = GetDeviceIndex();
nWaveDev = (nWaveDev > 0) ? nWaveDev - 1 : WAVE_MAPPER;
m_ThreadWakeupEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(m_ThreadWakeupEvent == INVALID_HANDLE_VALUE)
{
InternalClose();
return false;
}
m_Failed = false;
m_DriverBugs = 0;
m_hWaveOut = NULL;
if(waveOutOpen(&m_hWaveOut, nWaveDev, pwfx, (DWORD_PTR)WaveOutCallBack, (DWORD_PTR)this, CALLBACK_FUNCTION | (m_Settings.ExclusiveMode ? WAVE_FORMAT_DIRECT : 0)) != MMSYSERR_NOERROR)
{
InternalClose();
return false;
}
if(waveOutPause(m_hWaveOut) != MMSYSERR_NOERROR)
{
InternalClose();
return false;
}
m_nWaveBufferSize = mpt::saturate_round<int32>(m_Settings.UpdateInterval * pwfx->nAvgBytesPerSec);
m_nWaveBufferSize = mpt::align_up<uint32>(m_nWaveBufferSize, pwfx->nBlockAlign);
m_nWaveBufferSize = std::clamp(m_nWaveBufferSize, static_cast<uint32>(WAVEOUT_MINBUFFERFRAMECOUNT * pwfx->nBlockAlign), static_cast<uint32>(mpt::align_down<uint32>(WAVEOUT_MAXBUFFERSIZE, pwfx->nBlockAlign)));
std::size_t numBuffers = mpt::saturate_round<int32>(m_Settings.Latency * pwfx->nAvgBytesPerSec / m_nWaveBufferSize);
numBuffers = std::clamp(numBuffers, WAVEOUT_MINBUFFERS, WAVEOUT_MAXBUFFERS);
m_nPreparedHeaders = 0;
m_WaveBuffers.resize(numBuffers);
m_WaveBuffersData.resize(numBuffers);
for(std::size_t buf = 0; buf < numBuffers; ++buf)
{
m_WaveBuffers[buf] = {};
m_WaveBuffersData[buf].resize(m_nWaveBufferSize);
m_WaveBuffers[buf].dwFlags = 0;
m_WaveBuffers[buf].lpData = &m_WaveBuffersData[buf][0];
m_WaveBuffers[buf].dwBufferLength = m_nWaveBufferSize;
if(waveOutPrepareHeader(m_hWaveOut, &m_WaveBuffers[buf], sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
{
break;
}
m_WaveBuffers[buf].dwFlags |= WHDR_DONE;
m_nPreparedHeaders++;
}
if(!m_nPreparedHeaders)
{
InternalClose();
return false;
}
if(m_Settings.sampleFormat == SampleFormat::Int8)
{
m_Settings.sampleFormat = SampleFormat::Unsigned8;
}
m_nBuffersPending = 0;
m_nWriteBuffer = 0;
m_nDoneBuffer = 0;
{
mpt::lock_guard<mpt::mutex> guard(m_PositionWraparoundMutex);
m_PositionLast = {};
m_PositionWrappedCount = 0;
}
SetWakeupEvent(m_ThreadWakeupEvent);
SetWakeupInterval(m_nWaveBufferSize * 1.0 / m_Settings.GetBytesPerSecond());
m_Flags.WantsClippedOutput = (GetSysInfo().IsOriginal() && GetSysInfo().WindowsVersion.IsAtLeast(mpt::osinfo::windows::Version::WinVista));
return true;
}
bool CWaveDevice::InternalClose()
{
MPT_SOUNDDEV_TRACE_SCOPE();
if(m_hWaveOut)
{
waveOutReset(m_hWaveOut);
m_JustStarted = false;
InterlockedExchange(&m_nBuffersPending, 0);
m_nWriteBuffer = 0;
m_nDoneBuffer = 0;
while(m_nPreparedHeaders > 0)
{
m_nPreparedHeaders--;
waveOutUnprepareHeader(m_hWaveOut, &m_WaveBuffers[m_nPreparedHeaders], sizeof(WAVEHDR));
}
waveOutClose(m_hWaveOut);
m_hWaveOut = NULL;
}
#ifdef MPT_BUILD_DEBUG
if(m_DriverBugs.load())
{
SendDeviceMessage(LogError, MPT_USTRING("Errors were detected while playing sound:\n") + GetStatistics().text);
}
#endif
m_DriverBugs = 0;
m_Failed = false;
if(m_ThreadWakeupEvent)
{
CloseHandle(m_ThreadWakeupEvent);
m_ThreadWakeupEvent = NULL;
}
{
mpt::lock_guard<mpt::mutex> guard(m_PositionWraparoundMutex);
m_PositionLast = {};
m_PositionWrappedCount = 0;
}
return true;
}
void CWaveDevice::StartFromSoundThread()
{
MPT_SOUNDDEV_TRACE_SCOPE();
if(m_hWaveOut)
{
{
mpt::lock_guard<mpt::mutex> guard(m_PositionWraparoundMutex);
m_PositionLast = {};
m_PositionWrappedCount = 0;
}
m_JustStarted = true;
// Actual starting is done in InternalFillAudioBuffer to avoid crackling with tiny buffers.
}
}
void CWaveDevice::StopFromSoundThread()
{
MPT_SOUNDDEV_TRACE_SCOPE();
if(m_hWaveOut)
{
CheckResult(waveOutPause(m_hWaveOut));
m_JustStarted = false;
{
mpt::lock_guard<mpt::mutex> guard(m_PositionWraparoundMutex);
m_PositionLast = {};
m_PositionWrappedCount = 0;
}
}
}
bool CWaveDevice::CheckResult(MMRESULT result)
{
if(result == MMSYSERR_NOERROR)
{
return true;
}
if(!m_Failed)
{ // only show the first error
m_Failed = true;
TCHAR errortext[MAXERRORLENGTH + 1] = {};
waveOutGetErrorText(result, errortext, MAXERRORLENGTH);
SendDeviceMessage(LogError, MPT_UFORMAT_MESSAGE("WaveOut error: 0x{}: {}")(mpt::format<mpt::ustring>::hex0<8>(result), mpt::transcode<mpt::ustring>(static_cast<mpt::winstring>(mpt::ReadWinBuf(errortext)))));
}
RequestClose();
return false;
}
bool CWaveDevice::CheckResult(MMRESULT result, DWORD param)
{
if(result == MMSYSERR_NOERROR)
{
return true;
}
if(!m_Failed)
{ // only show the first error
m_Failed = true;
TCHAR errortext[MAXERRORLENGTH + 1] = {};
waveOutGetErrorText(result, errortext, MAXERRORLENGTH);
SendDeviceMessage(LogError, MPT_UFORMAT_MESSAGE("WaveOut error: 0x{} (param 0x{}): {}")(mpt::format<mpt::ustring>::hex0<8>(result), mpt::format<mpt::ustring>::hex0<8>(param), mpt::transcode<mpt::ustring>(static_cast<mpt::winstring>(mpt::ReadWinBuf(errortext)))));
}
RequestClose();
return false;
}
void CWaveDevice::InternalFillAudioBuffer()
{
MPT_SOUNDDEV_TRACE_SCOPE();
if(!m_hWaveOut)
{
return;
}
const std::size_t bytesPerFrame = m_Settings.GetBytesPerFrame();
ULONG oldBuffersPending = InterlockedExchangeAdd(&m_nBuffersPending, 0); // read
ULONG nLatency = oldBuffersPending * m_nWaveBufferSize;
ULONG nBytesWritten = 0;
while((oldBuffersPending < m_nPreparedHeaders) && !m_Failed)
{
#if(_WIN32_WINNT >= 0x0600)
DWORD oldFlags = InterlockedOr(interlocked_access(&m_WaveBuffers[m_nWriteBuffer].dwFlags), 0);
#else
DWORD oldFlags = _InterlockedOr(interlocked_access(&m_WaveBuffers[m_nWriteBuffer].dwFlags), 0);
#endif
uint32 driverBugs = 0;
if(oldFlags & WHDR_INQUEUE)
{
driverBugs |= DriverBugBufferFillAndHeaderInQueue;
}
if(!(oldFlags & WHDR_DONE))
{
driverBugs |= DriverBugBufferFillAndHeaderNotDone;
}
driverBugs |= m_DriverBugs.fetch_or(driverBugs);
if(oldFlags & WHDR_INQUEUE)
{
if(driverBugs & DriverBugDoneNotificationOutOfOrder)
{
// Some drivers/setups can return WaveHeader notifications out of
// order. WaveHeaders which have not yet been notified to be ready stay
// in the INQUEUE and !DONE state internally and cannot be reused yet
// even though they causally should be able to. waveOutWrite fails for
// them.
// In this case we skip filling the buffers until we actually see the
// next expected buffer to be ready for refilling.
// This problem has been spotted on Wine 1.7.46 (non-official packages)
// running on Debian 8 Jessie 32bit. It may also be related to WaveOut
// playback being too fast and crackling which had benn reported on
// Wine 1.6 + WinePulse on UbuntuStudio 12.04 32bit (this has not been
// verified yet because the problem is not always reproducable on the
// system in question).
return;
}
}
nLatency += m_nWaveBufferSize;
CallbackLockedAudioReadPrepare(m_nWaveBufferSize / bytesPerFrame, nLatency / bytesPerFrame);
CallbackLockedAudioProcessVoid(m_WaveBuffers[m_nWriteBuffer].lpData, nullptr, m_nWaveBufferSize / bytesPerFrame);
nBytesWritten += m_nWaveBufferSize;
#if(_WIN32_WINNT >= 0x0600)
InterlockedAnd(interlocked_access(&m_WaveBuffers[m_nWriteBuffer].dwFlags), ~static_cast<DWORD>(WHDR_INQUEUE | WHDR_DONE));
#else
_InterlockedAnd(interlocked_access(&m_WaveBuffers[m_nWriteBuffer].dwFlags), ~static_cast<DWORD>(WHDR_INQUEUE | WHDR_DONE));
#endif
InterlockedExchange(interlocked_access(&m_WaveBuffers[m_nWriteBuffer].dwBufferLength), m_nWaveBufferSize);
InterlockedIncrement(&m_nBuffersPending);
oldBuffersPending++; // increment separately to avoid looping without leaving at all when rendering takes more than 100% CPU
CheckResult(waveOutWrite(m_hWaveOut, &m_WaveBuffers[m_nWriteBuffer], sizeof(WAVEHDR)), oldFlags);
m_nWriteBuffer++;
m_nWriteBuffer %= m_nPreparedHeaders;
CallbackLockedAudioProcessDone();
}
if(m_JustStarted && !m_Failed)
{
// Fill the buffers completely before starting the stream.
// This avoids buffer underruns which result in audible crackling with small buffers.
m_JustStarted = false;
CheckResult(waveOutRestart(m_hWaveOut));
}
}
int64 CWaveDevice::InternalGetStreamPositionFrames() const
{
// Apparently, at least with Windows XP, TIME_SAMPLES wraps aroud at 0x7FFFFFF (see
// http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.mmedia/2005-02/0070.html
// ).
// We may also, additionally, default to TIME_BYTES which would wraparound the earliest.
// We could thereby try to avoid any potential wraparound inside the driver on older
// Windows versions, which would be, once converted into other units, really
// difficult to detect or handle.
static constexpr UINT timeType = TIME_SAMPLES; // should work for sane systems
//static constexpr std::size_t valid_bits = 32; // should work for sane systems
//static constexpr UINT timeType = TIME_BYTES; // safest
static constexpr std::size_t valid_bits = 27; // safe for WinXP TIME_SAMPLES
static constexpr uint32 valid_mask = static_cast<uint32>((uint64(1) << valid_bits) - 1u);
static constexpr uint32 valid_watermark = static_cast<uint32>(uint64(1) << (valid_bits - 1u)); // half the valid range in order to be able to catch backwards fluctuations
MMTIME mmtime = {};
mmtime.wType = timeType;
if(waveOutGetPosition(m_hWaveOut, &mmtime, sizeof(mmtime)) != MMSYSERR_NOERROR)
{
return 0;
}
if(mmtime.wType != TIME_MS && mmtime.wType != TIME_BYTES && mmtime.wType != TIME_SAMPLES)
{ // unsupported time format
return 0;
}
int64 offset = 0;
{
// handle wraparound
mpt::lock_guard<mpt::mutex> guard(m_PositionWraparoundMutex);
if(!m_PositionLast.wType)
{
// first call
m_PositionWrappedCount = 0;
} else if(mmtime.wType != m_PositionLast.wType)
{
// what? value type changed, do not try handling that for now.
m_PositionWrappedCount = 0;
} else
{
DWORD oldval = 0;
DWORD curval = 0;
switch(mmtime.wType)
{
case TIME_MS:
oldval = m_PositionLast.u.ms;
curval = mmtime.u.ms;
break;
case TIME_BYTES:
oldval = m_PositionLast.u.cb;
curval = mmtime.u.cb;
break;
case TIME_SAMPLES:
oldval = m_PositionLast.u.sample;
curval = mmtime.u.sample;
break;
}
oldval &= valid_mask;
curval &= valid_mask;
if(((curval - oldval) & valid_mask) >= valid_watermark) // guard against driver problems resulting in time jumping backwards for short periods of time. BEWARE of integer wraparound when refactoring
{
curval = oldval;
}
switch(mmtime.wType)
{
case TIME_MS: mmtime.u.ms = curval; break;
case TIME_BYTES: mmtime.u.cb = curval; break;
case TIME_SAMPLES: mmtime.u.sample = curval; break;
}
if((curval ^ oldval) & valid_watermark) // MSB flipped
{
if(!(curval & valid_watermark)) // actually wrapped
{
m_PositionWrappedCount += 1;
}
}
}
m_PositionLast = mmtime;
offset = (static_cast<uint64>(m_PositionWrappedCount) << valid_bits);
}
int64 result = 0;
switch(mmtime.wType)
{
case TIME_MS: result += (static_cast<int64>(mmtime.u.ms & valid_mask) + offset) * m_Settings.GetBytesPerSecond() / (1000 * m_Settings.GetBytesPerFrame()); break;
case TIME_BYTES: result += (static_cast<int64>(mmtime.u.cb & valid_mask) + offset) / m_Settings.GetBytesPerFrame(); break;
case TIME_SAMPLES: result += (static_cast<int64>(mmtime.u.sample & valid_mask) + offset); break;
}
return result;
}
void CWaveDevice::HandleWaveoutDone(WAVEHDR *hdr)
{
MPT_SOUNDDEV_TRACE_SCOPE();
#if(_WIN32_WINNT >= 0x0600)
DWORD flags = InterlockedOr(interlocked_access(&hdr->dwFlags), 0);
#else
DWORD flags = _InterlockedOr(interlocked_access(&hdr->dwFlags), 0);
#endif
std::size_t hdrIndex = hdr - &(m_WaveBuffers[0]);
uint32 driverBugs = 0;
if(hdrIndex != m_nDoneBuffer)
{
driverBugs |= DriverBugDoneNotificationOutOfOrder;
}
if(!(flags & WHDR_DONE))
{
driverBugs |= DriverBugDoneNotificationAndHeaderNotDone;
}
if(flags & WHDR_INQUEUE)
{
driverBugs |= DriverBugDoneNotificationAndHeaderInQueue;
}
if(driverBugs)
{
m_DriverBugs.fetch_or(driverBugs);
}
m_nDoneBuffer += 1;
m_nDoneBuffer %= m_nPreparedHeaders;
InterlockedDecrement(&m_nBuffersPending);
SetEvent(m_ThreadWakeupEvent);
}
void CWaveDevice::WaveOutCallBack(HWAVEOUT, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR param1, DWORD_PTR /* param2 */)
{
MPT_SOUNDDEV_TRACE_SCOPE();
if((uMsg == WOM_DONE) && (dwUser))
{
CWaveDevice *that = (CWaveDevice *)dwUser;
that->HandleWaveoutDone((WAVEHDR *)param1);
}
}
SoundDevice::BufferAttributes CWaveDevice::InternalGetEffectiveBufferAttributes() const
{
SoundDevice::BufferAttributes bufferAttributes;
bufferAttributes.Latency = m_nWaveBufferSize * m_nPreparedHeaders * 1.0 / m_Settings.GetBytesPerSecond();
bufferAttributes.UpdateInterval = m_nWaveBufferSize * 1.0 / m_Settings.GetBytesPerSecond();
bufferAttributes.NumBuffers = m_nPreparedHeaders;
return bufferAttributes;
}
SoundDevice::Statistics CWaveDevice::GetStatistics() const
{
MPT_SOUNDDEV_TRACE_SCOPE();
SoundDevice::Statistics result;
result.InstantaneousLatency = InterlockedExchangeAdd(&m_nBuffersPending, 0) * m_nWaveBufferSize * 1.0 / m_Settings.GetBytesPerSecond();
result.LastUpdateInterval = 1.0 * m_nWaveBufferSize / m_Settings.GetBytesPerSecond();
uint32 bugs = m_DriverBugs.load();
if(bugs != 0)
{
result.text = MPT_UFORMAT_MESSAGE("Problematic driver detected! Error flags: {}")(mpt::format<mpt::ustring>::hex0<8>(bugs));
} else
{
result.text = MPT_UFORMAT_MESSAGE("Driver working as expected.")();
}
return result;
}
std::vector<SoundDevice::Info> CWaveDevice::EnumerateDevices(ILogger &logger, SoundDevice::SysInfo sysInfo)
{
auto GetLogger = [&]() -> ILogger &
{
return logger;
};
MPT_SOUNDDEV_TRACE_SCOPE();
std::vector<SoundDevice::Info> devices;
UINT numDevs = waveOutGetNumDevs();
for(UINT index = 0; index <= numDevs; ++index)
{
SoundDevice::Info info;
info.type = TypeWAVEOUT;
info.internalID = mpt::format<mpt::ustring>::dec(index);
info.apiName = MPT_USTRING("MME");
info.useNameAsIdentifier = true;
WAVEOUTCAPS woc = {};
if(waveOutGetDevCaps((index == 0) ? WAVE_MAPPER : (index - 1), &woc, sizeof(woc)) == MMSYSERR_NOERROR)
{
info.name = mpt::transcode<mpt::ustring>(static_cast<mpt::winstring>(mpt::ReadWinBuf(woc.szPname)));
info.extraData[MPT_USTRING("DriverID")] = MPT_UFORMAT_MESSAGE("{}:{}")(mpt::format<mpt::ustring>::hex0<4>(woc.wMid), mpt::format<mpt::ustring>::hex0<4>(woc.wPid));
info.extraData[MPT_USTRING("DriverVersion")] = MPT_UFORMAT_MESSAGE("{}.{}")(mpt::format<mpt::ustring>::dec((static_cast<uint32>(woc.vDriverVersion) >> 24) & 0xff), mpt::format<mpt::ustring>::dec((static_cast<uint32>(woc.vDriverVersion) >> 0) & 0xff));
}
if(info.name.empty())
{
if(index == 0)
{
info.name = MPT_UFORMAT_MESSAGE("Auto (Wave Mapper)")();
} else
{
info.name = MPT_UFORMAT_MESSAGE("Device {}")(index - 1);
}
}
info.default_ = ((index == 0) ? Info::Default::Managed : Info::Default::None);
// 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::Legacy : Info::Usability::NotAvailable,
Info::Level::Primary,
sysInfo.SystemClass == mpt::osinfo::osclass::Windows && sysInfo.IsWindowsOriginal() ? 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 devices;
}
#endif // MPT_OS_WINDOWS
} // namespace SoundDevice
OPENMPT_NAMESPACE_END
|