OpenTTD Source: src/sound/win32_s.cpp Source File

OpenTTD Source 15.0-beta1

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
28static HWAVEOUT _waveout;
29static WAVEHDR _wave_hdr[2];
30static int _bufsize;
31static HANDLE _thread;
32static DWORD _threadId;
33static HANDLE _event;
34
35static void PrepareHeader(WAVEHDR *hdr)
36{
37 hdr->dwBufferLength = _bufsize * 4;
38 hdr->dwFlags = 0;
39 hdr->lpData = MallocT<char>(_bufsize * 4);
40 if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) throw "waveOutPrepareHeader failed";
41}
42
43static DWORD WINAPI SoundThread(LPVOID)
44{
45 SetCurrentThreadName("ottd:win-sound");
46
47 do {
48 for (auto &hdr : _wave_hdr) {
49 if ((hdr.dwFlags & WHDR_INQUEUE) != 0) continue;
50 MxMixSamples(hdr.lpData, hdr.dwBufferLength / 4);
51 if (waveOutWrite(_waveout, &hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
52 MessageBox(nullptr, L"Sounds are disabled until restart.", L"waveOutWrite failed", MB_ICONINFORMATION);
53 return 0;
54 }
55 }
56 WaitForSingleObject(_event, INFINITE);
57 } while (_waveout != nullptr);
58
59 return 0;
60}
61
62std::optional<std::string_view> SoundDriver_Win32::Start(const StringList &parm)
63{
64 WAVEFORMATEX wfex;
65 wfex.wFormatTag = WAVE_FORMAT_PCM;
66 wfex.nChannels = 2;
67 wfex.wBitsPerSample = 16;
68 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
69 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
70 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
71
72 /* Limit buffer size to prevent overflows. */
73 _bufsize = GetDriverParamInt(parm, "samples", 1024);
74 _bufsize = std::min<int>(_bufsize, UINT16_MAX);
75
76 try {
77 if (nullptr == (_event = CreateEvent(nullptr, FALSE, FALSE, nullptr))) throw "Failed to create event";
78
79 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
80
81 MxInitialize(wfex.nSamplesPerSec);
82
83 PrepareHeader(&_wave_hdr[0]);
84 PrepareHeader(&_wave_hdr[1]);
85
86 if (nullptr == (_thread = CreateThread(nullptr, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
87 } catch (const char *error) {
88 this->Stop();
89 return error;
90 }
91
92 return std::nullopt;
93}
94
95 void SoundDriver_Win32::Stop()
96{
97 HWAVEOUT waveout = _waveout;
98
99 /* Stop the sound thread. */
100 _waveout = nullptr;
101 SignalObjectAndWait(_event, _thread, INFINITE, FALSE);
102
103 /* Close the sound device. */
104 waveOutReset(waveout);
105 waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
106 waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
107 waveOutClose(waveout);
108
109 CloseHandle(_thread);
110 CloseHandle(_event);
111}
FSoundDriver_Win32
Factory for the sound driver for Windows.
Definition win32_s.h:25
SoundDriver_Win32::Stop
void Stop() override
Stop this driver.
Definition win32_s.cpp:95
SoundDriver_Win32::Start
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_s.cpp:62
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:76
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
SetCurrentThreadName
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
Definition unix.cpp:245
win32_s.h
Base for Windows sound handling.