OpenTTD
win32_s.cpp
Go to the documentation of this file.
1 /* $Id: win32_s.cpp 27673 2016-10-30 18:22:55Z michi_cc $ */
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 
24 #include "../safeguards.h"
25 
26 static FSoundDriver_Win32 iFSoundDriver_Win32;
27 
28 static HWAVEOUT _waveout;
29 static WAVEHDR _wave_hdr[2];
30 static int _bufsize;
31 static HANDLE _thread;
32 static DWORD _threadId;
33 static HANDLE _event;
34 
35 static 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 
43 static DWORD WINAPI SoundThread(LPVOID arg)
44 {
45  SetWin32ThreadName(-1, "ottd:win-sound");
46 
47  do {
48  for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); 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(NULL, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION);
53  return 0;
54  }
55  }
56  WaitForSingleObject(_event, INFINITE);
57  } while (_waveout != NULL);
58 
59  return 0;
60 }
61 
62 const char *SoundDriver_Win32::Start(const char * const *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, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
74  _bufsize = min(_bufsize, UINT16_MAX);
75 
76  try {
77  if (NULL == (_event = CreateEvent(NULL, FALSE, FALSE, NULL))) 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 (NULL == (_thread = CreateThread(NULL, 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 NULL;
93 }
94 
96 {
97  HWAVEOUT waveout = _waveout;
98 
99  /* Stop the sound thread. */
100  _waveout = NULL;
101  SetEvent(_event);
102  WaitForSingleObject(_thread, INFINITE);
103 
104  /* Close the sound device. */
105  waveOutReset(waveout);
106  waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
107  waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
108  waveOutClose(waveout);
109 
110  CloseHandle(_thread);
111  CloseHandle(_event);
112 }