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/>.
10 /** @file win32_s.cpp Handling of sound for Windows. */
12 #include "../stdafx.h"
13 #include "../openttd.h"
14 #include "../driver.h"
16 #include "../core/alloc_func.hpp"
17 #include "../core/bitmath_func.hpp"
18 #include "../core/math_func.hpp"
23 static FSoundDriver_Win32 iFSoundDriver_Win32
;
25 static HWAVEOUT _waveout
;
26 static WAVEHDR _wave_hdr
[2];
28 static HANDLE _thread
;
29 static DWORD _threadId
;
32 static void PrepareHeader(WAVEHDR
*hdr
)
34 hdr
->dwBufferLength
= _bufsize
* 4;
36 hdr
->lpData
= MallocT
<char>(_bufsize
* 4);
37 if (waveOutPrepareHeader(_waveout
, hdr
, sizeof(WAVEHDR
)) != MMSYSERR_NOERROR
) throw "waveOutPrepareHeader failed";
40 static DWORD WINAPI
SoundThread(LPVOID arg
)
43 for (WAVEHDR
*hdr
= _wave_hdr
; hdr
!= endof(_wave_hdr
); hdr
++) {
44 if ((hdr
->dwFlags
& WHDR_INQUEUE
) != 0) continue;
45 MxMixSamples(hdr
->lpData
, hdr
->dwBufferLength
/ 4);
46 if (waveOutWrite(_waveout
, hdr
, sizeof(WAVEHDR
)) != MMSYSERR_NOERROR
) {
47 MessageBox(NULL
, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION
);
51 WaitForSingleObject(_event
, INFINITE
);
52 } while (_waveout
!= NULL
);
57 const char *SoundDriver_Win32::Start(const char * const *parm
)
60 wfex
.wFormatTag
= WAVE_FORMAT_PCM
;
62 wfex
.wBitsPerSample
= 16;
63 wfex
.nSamplesPerSec
= GetDriverParamInt(parm
, "hz", 44100);
64 wfex
.nBlockAlign
= (wfex
.nChannels
* wfex
.wBitsPerSample
) / 8;
65 wfex
.nAvgBytesPerSec
= wfex
.nSamplesPerSec
* wfex
.nBlockAlign
;
67 /* Limit buffer size to prevent overflows. */
68 _bufsize
= GetDriverParamInt(parm
, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
69 _bufsize
= min(_bufsize
, UINT16_MAX
);
72 if (NULL
== (_event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
))) throw "Failed to create event";
74 if (waveOutOpen(&_waveout
, WAVE_MAPPER
, &wfex
, (DWORD_PTR
)_event
, 0, CALLBACK_EVENT
) != MMSYSERR_NOERROR
) throw "waveOutOpen failed";
76 MxInitialize(wfex
.nSamplesPerSec
);
78 PrepareHeader(&_wave_hdr
[0]);
79 PrepareHeader(&_wave_hdr
[1]);
81 if (NULL
== (_thread
= CreateThread(NULL
, 8192, SoundThread
, 0, 0, &_threadId
))) throw "Failed to create thread";
82 } catch (char *error
) {
90 void SoundDriver_Win32::Stop()
92 HWAVEOUT waveout
= _waveout
;
94 /* Stop the sound thread. */
97 WaitForSingleObject(_thread
, INFINITE
);
99 /* Close the sound device. */
100 waveOutReset(waveout
);
101 waveOutUnprepareHeader(waveout
, &_wave_hdr
[0], sizeof(WAVEHDR
));
102 waveOutUnprepareHeader(waveout
, &_wave_hdr
[1], sizeof(WAVEHDR
));
103 waveOutClose(waveout
);
105 CloseHandle(_thread
);