12 #include "../stdafx.h" 13 #include "../openttd.h" 14 #include "../driver.h" 16 #include "../core/alloc_func.hpp" 17 #include "../core/bitmath_func.hpp" 18 #include "../core/math_func.hpp" 22 #include "../os/windows/win32.h" 23 #include "../thread.h" 25 #include "../safeguards.h" 29 static HWAVEOUT _waveout;
30 static WAVEHDR _wave_hdr[2];
32 static HANDLE _thread;
33 static DWORD _threadId;
36 static void PrepareHeader(WAVEHDR *hdr)
38 hdr->dwBufferLength = _bufsize * 4;
40 hdr->lpData = MallocT<char>(_bufsize * 4);
41 if (waveOutPrepareHeader(_waveout, hdr,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
throw "waveOutPrepareHeader failed";
44 static DWORD WINAPI SoundThread(LPVOID arg)
49 for (WAVEHDR *hdr = _wave_hdr; hdr !=
endof(_wave_hdr); hdr++) {
50 if ((hdr->dwFlags & WHDR_INQUEUE) != 0)
continue;
51 MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
52 if (waveOutWrite(_waveout, hdr,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
53 MessageBox(
nullptr, _T(
"Sounds are disabled until restart."), _T(
"waveOutWrite failed"), MB_ICONINFORMATION);
57 WaitForSingleObject(_event, INFINITE);
58 }
while (_waveout !=
nullptr);
66 wfex.wFormatTag = WAVE_FORMAT_PCM;
68 wfex.wBitsPerSample = 16;
70 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
71 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
75 _bufsize =
min(_bufsize, UINT16_MAX);
78 if (
nullptr == (_event = CreateEvent(
nullptr, FALSE, FALSE,
nullptr)))
throw "Failed to create event";
80 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR)
throw "waveOutOpen failed";
82 MxInitialize(wfex.nSamplesPerSec);
84 PrepareHeader(&_wave_hdr[0]);
85 PrepareHeader(&_wave_hdr[1]);
87 if (
nullptr == (_thread = CreateThread(
nullptr, 8192, SoundThread, 0, 0, &_threadId)))
throw "Failed to create thread";
88 }
catch (
const char *
error) {
98 HWAVEOUT waveout = _waveout;
103 WaitForSingleObject(_thread, INFINITE);
106 waveOutReset(waveout);
107 waveOutUnprepareHeader(waveout, &_wave_hdr[0],
sizeof(WAVEHDR));
108 waveOutUnprepareHeader(waveout, &_wave_hdr[1],
sizeof(WAVEHDR));
109 waveOutClose(waveout);
111 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.