OpenTTD
win32_s.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../openttd.h"
14 #include "../driver.h"
15 #include "../mixer.h"
16 #include "../core/alloc_func.hpp"
17 #include "../core/bitmath_func.hpp"
18 #include "../core/math_func.hpp"
19 #include "win32_s.h"
20 #include <windows.h>
21 #include <mmsystem.h>
22 #include "../os/windows/win32.h"
23 #include "../thread.h"
24 
25 #include "../safeguards.h"
26 
27 static FSoundDriver_Win32 iFSoundDriver_Win32;
28 
29 static HWAVEOUT _waveout;
30 static WAVEHDR _wave_hdr[2];
31 static int _bufsize;
32 static HANDLE _thread;
33 static DWORD _threadId;
34 static HANDLE _event;
35 
36 static void PrepareHeader(WAVEHDR *hdr)
37 {
38  hdr->dwBufferLength = _bufsize * 4;
39  hdr->dwFlags = 0;
40  hdr->lpData = MallocT<char>(_bufsize * 4);
41  if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) throw "waveOutPrepareHeader failed";
42 }
43 
44 static DWORD WINAPI SoundThread(LPVOID arg)
45 {
46  SetCurrentThreadName("ottd:win-sound");
47 
48  do {
49  for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
50  if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
51  MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
52  if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
53  MessageBox(nullptr, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION);
54  return 0;
55  }
56  }
57  WaitForSingleObject(_event, INFINITE);
58  } while (_waveout != nullptr);
59 
60  return 0;
61 }
62 
63 const char *SoundDriver_Win32::Start(const char * const *parm)
64 {
65  WAVEFORMATEX wfex;
66  wfex.wFormatTag = WAVE_FORMAT_PCM;
67  wfex.nChannels = 2;
68  wfex.wBitsPerSample = 16;
69  wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
70  wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
71  wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
72 
73  /* Limit buffer size to prevent overflows. */
74  _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
75  _bufsize = min(_bufsize, UINT16_MAX);
76 
77  try {
78  if (nullptr == (_event = CreateEvent(nullptr, FALSE, FALSE, nullptr))) throw "Failed to create event";
79 
80  if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
81 
82  MxInitialize(wfex.nSamplesPerSec);
83 
84  PrepareHeader(&_wave_hdr[0]);
85  PrepareHeader(&_wave_hdr[1]);
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 nullptr;
94 }
95 
97 {
98  HWAVEOUT waveout = _waveout;
99 
100  /* Stop the sound thread. */
101  _waveout = nullptr;
102  SetEvent(_event);
103  WaitForSingleObject(_thread, INFINITE);
104 
105  /* Close the sound device. */
106  waveOutReset(waveout);
107  waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
108  waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
109  waveOutClose(waveout);
110 
111  CloseHandle(_thread);
112  CloseHandle(_event);
113 }
const char * Start(const char *const *param) override
Start this driver.
Definition: win32_s.cpp:63
void SetCurrentThreadName(const char *)
Name the thread this function is called on for the debugger.
Definition: os2.cpp:217
Factory for the sound driver for Windows.
Definition: win32_s.h:27
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Base for Windows sound handling.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:114
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
void Stop() override
Stop this driver.
Definition: win32_s.cpp:96
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:75