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/>.
8 /** @file win32_s.cpp Handling of sound for Windows. */
10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "../driver.h"
14 #include "../core/alloc_func.hpp"
15 #include "../core/bitmath_func.hpp"
16 #include "../core/math_func.hpp"
20 #include "../os/windows/win32.h"
21 #include "../thread.h"
23 #include "../safeguards.h"
25 static FSoundDriver_Win32 iFSoundDriver_Win32
;
27 static HWAVEOUT _waveout
;
28 static WAVEHDR _wave_hdr
[2];
30 static HANDLE _thread
;
31 static DWORD _threadId
;
34 static void PrepareHeader(WAVEHDR
*hdr
)
36 hdr
->dwBufferLength
= _bufsize
* 4;
38 hdr
->lpData
= MallocT
<char>(_bufsize
* 4);
39 if (waveOutPrepareHeader(_waveout
, hdr
, sizeof(WAVEHDR
)) != MMSYSERR_NOERROR
) throw "waveOutPrepareHeader failed";
42 static DWORD WINAPI
SoundThread(LPVOID arg
)
44 SetCurrentThreadName("ottd:win-sound");
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, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION
);
55 WaitForSingleObject(_event
, INFINITE
);
56 } while (_waveout
!= nullptr);
61 const char *SoundDriver_Win32::Start(const char * const *parm
)
64 wfex
.wFormatTag
= WAVE_FORMAT_PCM
;
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
;
71 /* Limit buffer size to prevent overflows. */
72 _bufsize
= GetDriverParamInt(parm
, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
73 _bufsize
= min(_bufsize
, UINT16_MAX
);
76 if (nullptr == (_event
= CreateEvent(nullptr, FALSE
, FALSE
, nullptr))) throw "Failed to create event";
78 if (waveOutOpen(&_waveout
, WAVE_MAPPER
, &wfex
, (DWORD_PTR
)_event
, 0, CALLBACK_EVENT
) != MMSYSERR_NOERROR
) throw "waveOutOpen failed";
80 MxInitialize(wfex
.nSamplesPerSec
);
82 PrepareHeader(&_wave_hdr
[0]);
83 PrepareHeader(&_wave_hdr
[1]);
85 if (nullptr == (_thread
= CreateThread(nullptr, 8192, SoundThread
, 0, 0, &_threadId
))) throw "Failed to create thread";
86 } catch (const char *error
) {
94 void SoundDriver_Win32::Stop()
96 HWAVEOUT waveout
= _waveout
;
98 /* Stop the sound thread. */
101 WaitForSingleObject(_thread
, INFINITE
);
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
);
109 CloseHandle(_thread
);