Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../openttd.h"
00014 #include "../driver.h"
00015 #include "../mixer.h"
00016 #include "../core/alloc_func.hpp"
00017 #include "../core/bitmath_func.hpp"
00018 #include "../core/math_func.hpp"
00019 #include "win32_s.h"
00020 #include <windows.h>
00021 #include <mmsystem.h>
00022
00023 static FSoundDriver_Win32 iFSoundDriver_Win32;
00024
00025 static HWAVEOUT _waveout;
00026 static WAVEHDR _wave_hdr[2];
00027 static int _bufsize;
00028 static HANDLE _thread;
00029 static DWORD _threadId;
00030 static HANDLE _event;
00031
00032 static void PrepareHeader(WAVEHDR *hdr)
00033 {
00034 hdr->dwBufferLength = _bufsize * 4;
00035 hdr->dwFlags = 0;
00036 hdr->lpData = MallocT<char>(_bufsize * 4);
00037 if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) throw "waveOutPrepareHeader failed";
00038 }
00039
00040 static DWORD WINAPI SoundThread(LPVOID arg)
00041 {
00042 do {
00043 for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
00044 if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
00045 MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
00046 if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) {
00047 MessageBox(NULL, _T("Sounds are disabled until restart."), _T("waveOutWrite failed"), MB_ICONINFORMATION);
00048 return 0;
00049 }
00050 }
00051 WaitForSingleObject(_event, INFINITE);
00052 } while (_waveout != NULL);
00053
00054 return 0;
00055 }
00056
00057 const char *SoundDriver_Win32::Start(const char * const *parm)
00058 {
00059 WAVEFORMATEX wfex;
00060 wfex.wFormatTag = WAVE_FORMAT_PCM;
00061 wfex.nChannels = 2;
00062 wfex.wBitsPerSample = 16;
00063 wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
00064 wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
00065 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
00066
00067
00068 _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
00069 _bufsize = min(_bufsize, UINT16_MAX);
00070
00071 try {
00072 if (NULL == (_event = CreateEvent(NULL, FALSE, FALSE, NULL))) throw "Failed to create event";
00073
00074 if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) throw "waveOutOpen failed";
00075
00076 MxInitialize(wfex.nSamplesPerSec);
00077
00078 PrepareHeader(&_wave_hdr[0]);
00079 PrepareHeader(&_wave_hdr[1]);
00080
00081 if (NULL == (_thread = CreateThread(NULL, 8192, SoundThread, 0, 0, &_threadId))) throw "Failed to create thread";
00082 } catch (char *error) {
00083 this->Stop();
00084 return error;
00085 }
00086
00087 return NULL;
00088 }
00089
00090 void SoundDriver_Win32::Stop()
00091 {
00092 HWAVEOUT waveout = _waveout;
00093
00094
00095 _waveout = NULL;
00096 SetEvent(_event);
00097 WaitForSingleObject(_thread, INFINITE);
00098
00099
00100 waveOutReset(waveout);
00101 waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
00102 waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
00103 waveOutClose(waveout);
00104
00105 CloseHandle(_thread);
00106 CloseHandle(_event);
00107 }