11#include "../openttd.h"
14#include "../core/alloc_func.hpp"
15#include "../core/bitmath_func.hpp"
16#include "../core/math_func.hpp"
20#include <versionhelpers.h>
21#include "../os/windows/win32.h"
24#include "../safeguards.h"
28using HeaderDataPair = std::pair<WAVEHDR, std::unique_ptr<CHAR[]>>;
30static HWAVEOUT _waveout;
31static HeaderDataPair _wave_hdr[2];
34static DWORD _threadId;
37static void PrepareHeader(HeaderDataPair &hdr)
39 hdr.second = std::make_unique<CHAR[]>(_bufsize * 4);
40 hdr.first.dwBufferLength = _bufsize * 4;
41 hdr.first.dwFlags = 0;
42 hdr.first.lpData = hdr.second.get();
43 if (waveOutPrepareHeader(_waveout, &hdr.first,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
throw "waveOutPrepareHeader failed";
46static DWORD WINAPI SoundThread(LPVOID)
51 for (
auto &hdr : _wave_hdr) {
52 if ((hdr.first.dwFlags & WHDR_INQUEUE) != 0)
continue;
53 MxMixSamples(hdr.first.lpData, hdr.first.dwBufferLength / 4);
54 if (waveOutWrite(_waveout, &hdr.first,
sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
55 MessageBox(
nullptr, L
"Sounds are disabled until restart.", L
"waveOutWrite failed", MB_ICONINFORMATION);
59 WaitForSingleObject(_event, INFINITE);
60 }
while (_waveout !=
nullptr);
68 wfex.wFormatTag = WAVE_FORMAT_PCM;
70 wfex.wBitsPerSample = 16;
72 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
73 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
77 _bufsize = std::min<int>(_bufsize, UINT16_MAX);
80 if (
nullptr == (_event = CreateEvent(
nullptr, FALSE, FALSE,
nullptr)))
throw "Failed to create event";
82 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR)
throw "waveOutOpen failed";
84 MxInitialize(wfex.nSamplesPerSec);
86 for (
auto &hdr : _wave_hdr) PrepareHeader(hdr);
88 if (
nullptr == (_thread = CreateThread(
nullptr, 8192, SoundThread, 0, 0, &_threadId)))
throw "Failed to create thread";
89 }
catch (
const char *error) {
99 HWAVEOUT waveout = _waveout;
103 SignalObjectAndWait(_event, _thread, INFINITE, FALSE);
106 waveOutReset(waveout);
107 for (
auto &hdr : _wave_hdr) {
108 waveOutUnprepareHeader(waveout, &hdr.first,
sizeof(WAVEHDR));
111 waveOutClose(waveout);
113 CloseHandle(_thread);
Factory for the sound driver for Windows.
void Stop() override
Stop this driver.
std::optional< std::string_view > Start(const StringList ¶m) override
Start this driver.
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
std::vector< std::string > StringList
Type for a list of strings.
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
Base for Windows sound handling.