OpenTTD: src/sound/win32_s.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

win32_s.cpp
Go to the documentation of this file.
1 /* $Id: win32_s.cpp 27383 2015-08-12 20:50:10Z rubidium $ */
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
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 do {
45 for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
46 if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
47 MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
48 if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
49 MessageBox(NULL, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION);
50 return 0;
51 }
52 }
53 WaitForSingleObject(_event, INFINITE);
54 } while (_waveout != NULL);
55
56 return 0;
57 }
58
59 const char *SoundDriver_Win32::Start(const char * const *parm)
60 {
61 WAVEFORMATEX wfex;
62 wfex.wFormatTag = WAVE_FORMAT_PCM;
63 wfex.nChannels = 2;
64 wfex.wBitsPerSample = 16;
65 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
66 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
67 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
68
69 /* Limit buffer size to prevent overflows. */
70 _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
71 _bufsize = min(_bufsize, UINT16_MAX);
72
73 try {
74 if (NULL == (_event = CreateEvent(NULL, FALSE, FALSE, NULL))) throw "Failed to create event";
75
76 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
77
78 MxInitialize(wfex.nSamplesPerSec);
79
80 PrepareHeader(&_wave_hdr[0]);
81 PrepareHeader(&_wave_hdr[1]);
82
83 if (NULL == (_thread = CreateThread(NULL, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
84 } catch (const char *error) {
85 this->Stop();
86 return error;
87 }
88
89 return NULL;
90 }
91
92 void SoundDriver_Win32::Stop()
93 {
94 HWAVEOUT waveout = _waveout;
95
96 /* Stop the sound thread. */
97 _waveout = NULL;
98 SetEvent(_event);
99 WaitForSingleObject(_thread, INFINITE);
100
101 /* Close the sound device. */
102 waveOutReset(waveout);
103 waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
104 waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
105 waveOutClose(waveout);
106
107 CloseHandle(_thread);
108 CloseHandle(_event);
109 }