OpenTTD Source 20250312-master-gcdcc6b491d
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/bitmath_func.hpp"
15#include "../core/math_func.hpp"
16#include "win32_s.h"
17#include <windows.h>
18#include <mmsystem.h>
19#include <versionhelpers.h>
20#include "../os/windows/win32.h"
21#include "../thread.h"
22
23#include "../safeguards.h"
24
25static FSoundDriver_Win32 iFSoundDriver_Win32;
26
27using HeaderDataPair = std::pair<WAVEHDR, std::unique_ptr<CHAR[]>>;
28
29static HWAVEOUT _waveout;
30static HeaderDataPair _wave_hdr[2];
31static int _bufsize;
32static HANDLE _thread;
33static DWORD _threadId;
34static HANDLE _event;
35
36static void PrepareHeader(HeaderDataPair &hdr)
37{
38 hdr.second = std::make_unique<CHAR[]>(_bufsize * 4);
39 hdr.first.dwBufferLength = _bufsize * 4;
40 hdr.first.dwFlags = 0;
41 hdr.first.lpData = hdr.second.get();
42 if (waveOutPrepareHeader(_waveout, &hdr.first, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) throw "waveOutPrepareHeader failed";
43}
44
45static DWORD WINAPI SoundThread(LPVOID)
46{
47 SetCurrentThreadName("ottd:win-sound");
48
49 do {
50 for (auto &hdr : _wave_hdr) {
51 if ((hdr.first.dwFlags & WHDR_INQUEUE) != 0) continue;
52 MxMixSamples(hdr.first.lpData, hdr.first.dwBufferLength / 4);
53 if (waveOutWrite(_waveout, &hdr.first, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
54 MessageBox(nullptr, L"Sounds are disabled until restart.", L"waveOutWrite failed", MB_ICONINFORMATION);
55 return 0;
56 }
57 }
58 WaitForSingleObject(_event, INFINITE);
59 } while (_waveout != nullptr);
60
61 return 0;
62}
63
64std::optional<std::string_view> SoundDriver_Win32::Start(const StringList &parm)
65{
66 WAVEFORMATEX wfex;
67 wfex.wFormatTag = WAVE_FORMAT_PCM;
68 wfex.nChannels = 2;
69 wfex.wBitsPerSample = 16;
70 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
71 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
72 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
73
74 /* Limit buffer size to prevent overflows. */
75 _bufsize = GetDriverParamInt(parm, "samples", 1024);
76 _bufsize = std::min<int>(_bufsize, UINT16_MAX);
77
78 try {
79 if (nullptr == (_event = CreateEvent(nullptr, FALSE, FALSE, nullptr))) throw "Failed to create event";
80
81 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
82
83 MxInitialize(wfex.nSamplesPerSec);
84
85 for (auto &hdr : _wave_hdr) PrepareHeader(hdr);
86
87 if (nullptr == (_thread = CreateThread(nullptr, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
88 } catch (const char *error) {
89 this->Stop();
90 return error;
91 }
92
93 return std::nullopt;
94}
95
97{
98 HWAVEOUT waveout = _waveout;
99
100 /* Stop the sound thread. */
101 _waveout = nullptr;
102 SignalObjectAndWait(_event, _thread, INFINITE, FALSE);
103
104 /* Close the sound device. */
105 waveOutReset(waveout);
106 for (auto &hdr : _wave_hdr) {
107 waveOutUnprepareHeader(waveout, &hdr.first, sizeof(WAVEHDR));
108 hdr.second.reset();
109 }
110 waveOutClose(waveout);
111
112 CloseHandle(_thread);
113 CloseHandle(_event);
114}
Factory for the sound driver for Windows.
Definition win32_s.h:25
void Stop() override
Stop this driver.
Definition win32_s.cpp:96
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_s.cpp:64
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:77
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.