OpenTTD Source 20250205-master-gfd85ab1e2c
win32_s.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "../driver.h"
13#include "../mixer.h"
14#include "../core/alloc_func.hpp"
15#include "../core/bitmath_func.hpp"
16#include "../core/math_func.hpp"
17#include "win32_s.h"
18#include <windows.h>
19#include <mmsystem.h>
20#include <versionhelpers.h>
21#include "../os/windows/win32.h"
22#include "../thread.h"
23
24#include "../safeguards.h"
25
26static FSoundDriver_Win32 iFSoundDriver_Win32;
27
28using HeaderDataPair = std::pair<WAVEHDR, std::unique_ptr<CHAR[]>>;
29
30static HWAVEOUT _waveout;
31static HeaderDataPair _wave_hdr[2];
32static int _bufsize;
33static HANDLE _thread;
34static DWORD _threadId;
35static HANDLE _event;
36
37static void PrepareHeader(HeaderDataPair &hdr)
38{
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";
44}
45
46static DWORD WINAPI SoundThread(LPVOID)
47{
48 SetCurrentThreadName("ottd:win-sound");
49
50 do {
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);
56 return 0;
57 }
58 }
59 WaitForSingleObject(_event, INFINITE);
60 } while (_waveout != nullptr);
61
62 return 0;
63}
64
65std::optional<std::string_view> SoundDriver_Win32::Start(const StringList &parm)
66{
67 WAVEFORMATEX wfex;
68 wfex.wFormatTag = WAVE_FORMAT_PCM;
69 wfex.nChannels = 2;
70 wfex.wBitsPerSample = 16;
71 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
72 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
73 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
74
75 /* Limit buffer size to prevent overflows. */
76 _bufsize = GetDriverParamInt(parm, "samples", 1024);
77 _bufsize = std::min<int>(_bufsize, UINT16_MAX);
78
79 try {
80 if (nullptr == (_event = CreateEvent(nullptr, FALSE, FALSE, nullptr))) throw "Failed to create event";
81
82 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
83
84 MxInitialize(wfex.nSamplesPerSec);
85
86 for (auto &hdr : _wave_hdr) PrepareHeader(hdr);
87
88 if (nullptr == (_thread = CreateThread(nullptr, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
89 } catch (const char *error) {
90 this->Stop();
91 return error;
92 }
93
94 return std::nullopt;
95}
96
98{
99 HWAVEOUT waveout = _waveout;
100
101 /* Stop the sound thread. */
102 _waveout = nullptr;
103 SignalObjectAndWait(_event, _thread, INFINITE, FALSE);
104
105 /* Close the sound device. */
106 waveOutReset(waveout);
107 for (auto &hdr : _wave_hdr) {
108 waveOutUnprepareHeader(waveout, &hdr.first, sizeof(WAVEHDR));
109 hdr.second.reset();
110 }
111 waveOutClose(waveout);
112
113 CloseHandle(_thread);
114 CloseHandle(_event);
115}
Factory for the sound driver for Windows.
Definition win32_s.h:25
void Stop() override
Stop this driver.
Definition win32_s.cpp:97
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_s.cpp:65
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:76
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
Definition unix.cpp:245
Base for Windows sound handling.