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

OpenTTD Source 1.11.0-RC1

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 "../os/windows/win32.h"
21 #include "../thread.h"
22
23 #include "../safeguards.h"
24
25 static FSoundDriver_Win32 iFSoundDriver_Win32;
26
27 static HWAVEOUT _waveout;
28 static WAVEHDR _wave_hdr[2];
29 static int _bufsize;
30 static HANDLE _thread;
31 static DWORD _threadId;
32 static HANDLE _event;
33
34 static void PrepareHeader(WAVEHDR *hdr)
35 {
36 hdr->dwBufferLength = _bufsize * 4;
37 hdr->dwFlags = 0;
38 hdr->lpData = MallocT<char>(_bufsize * 4);
39 if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) throw "waveOutPrepareHeader failed";
40 }
41
42 static DWORD WINAPI SoundThread(LPVOID arg)
43 {
44 SetCurrentThreadName("ottd:win-sound");
45
46 do {
47 for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
48 if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
49 MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
50 if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
51 MessageBox(nullptr, L"Sounds are disabled until restart.", L"waveOutWrite failed", MB_ICONINFORMATION);
52 return 0;
53 }
54 }
55 WaitForSingleObject(_event, INFINITE);
56 } while (_waveout != nullptr);
57
58 return 0;
59 }
60
61 const char *SoundDriver_Win32::Start(const StringList &parm)
62 {
63 WAVEFORMATEX wfex;
64 wfex.wFormatTag = WAVE_FORMAT_PCM;
65 wfex.nChannels = 2;
66 wfex.wBitsPerSample = 16;
67 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
68 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
69 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
70
71 /* Limit buffer size to prevent overflows. */
72 _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
73 _bufsize = std::min<int>(_bufsize, UINT16_MAX);
74
75 try {
76 if (nullptr == (_event = CreateEvent(nullptr, FALSE, FALSE, nullptr))) throw "Failed to create event";
77
78 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
79
80 MxInitialize(wfex.nSamplesPerSec);
81
82 PrepareHeader(&_wave_hdr[0]);
83 PrepareHeader(&_wave_hdr[1]);
84
85 if (nullptr == (_thread = CreateThread(nullptr, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
86 } catch (const char *error) {
87 this->Stop();
88 return error;
89 }
90
91 return nullptr;
92 }
93
94 void SoundDriver_Win32::Stop()
95 {
96 HWAVEOUT waveout = _waveout;
97
98 /* Stop the sound thread. */
99 _waveout = nullptr;
100 SetEvent(_event);
101 WaitForSingleObject(_thread, INFINITE);
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 }
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
FSoundDriver_Win32
Factory for the sound driver for Windows.
Definition: win32_s.h:25
SetCurrentThreadName
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
Definition: os2.cpp:215
win32_s.h
SoundDriver_Win32::Stop
void Stop() override
Stop this driver.
Definition: win32_s.cpp:94
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:73
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:375
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:131
SoundDriver_Win32::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: win32_s.cpp:61