10 #include "../stdafx.h" 11 #include "../openttd.h" 12 #include "../driver.h" 14 #include "../core/alloc_func.hpp" 15 #include "../core/bitmath_func.hpp" 16 #include "../core/math_func.hpp" 20 #include "../os/windows/win32.h" 21 #include "../thread.h" 23 #include "../safeguards.h" 27 static HWAVEOUT _waveout;
28 static WAVEHDR _wave_hdr[2];
30 static HANDLE _thread;
31 static DWORD _threadId;
34 static void PrepareHeader(WAVEHDR *hdr)
36 hdr->dwBufferLength = _bufsize * 4;
38 hdr->lpData = MallocT<char>(_bufsize * 4);
39 if (waveOutPrepareHeader(_waveout, hdr,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
throw "waveOutPrepareHeader failed";
42 static DWORD WINAPI SoundThread(LPVOID arg)
47 for (WAVEHDR *hdr = _wave_hdr; hdr !=
endof(_wave_hdr); hdr++) {
48 if ((hdr->dwFlags & WHDR_INQUEUE) != 0)
continue;
49 MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
50 if (waveOutWrite(_waveout, hdr,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
51 MessageBox(
nullptr, _T(
"Sounds are disabled until restart."), _T(
"waveOutWrite failed"), MB_ICONINFORMATION);
55 WaitForSingleObject(_event, INFINITE);
56 }
while (_waveout !=
nullptr);
64 wfex.wFormatTag = WAVE_FORMAT_PCM;
66 wfex.wBitsPerSample = 16;
68 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
69 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
73 _bufsize =
min(_bufsize, UINT16_MAX);
76 if (
nullptr == (_event = CreateEvent(
nullptr, FALSE, FALSE,
nullptr)))
throw "Failed to create event";
78 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR)
throw "waveOutOpen failed";
80 MxInitialize(wfex.nSamplesPerSec);
82 PrepareHeader(&_wave_hdr[0]);
83 PrepareHeader(&_wave_hdr[1]);
85 if (
nullptr == (_thread = CreateThread(
nullptr, 8192, SoundThread, 0, 0, &_threadId)))
throw "Failed to create thread";
86 }
catch (
const char *
error) {
96 HWAVEOUT waveout = _waveout;
101 WaitForSingleObject(_thread, INFINITE);
104 waveOutReset(waveout);
105 waveOutUnprepareHeader(waveout, &_wave_hdr[0],
sizeof(WAVEHDR));
106 waveOutUnprepareHeader(waveout, &_wave_hdr[1],
sizeof(WAVEHDR));
107 waveOutClose(waveout);
109 CloseHandle(_thread);
const char * Start(const char *const *param) override
Start this driver.
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
Factory for the sound driver for Windows.
static T min(const T a, const T b)
Returns the minimum of two values.
Base for Windows sound handling.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
#define endof(x)
Get the end element of an fixed size array.
void Stop() override
Stop this driver.
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.