# win32_s.cpp

Go to the documentation of this file.

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