# win32_s.cpp

Go to the documentation of this file.

    00001 /* $Id: win32_s.cpp 18934 2010-01-28 13:49:20Z glx $ */
    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) usererror("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) usererror("waveOutWrite failed");
    00046     }
    00047     WaitForSingleObject(_event, INFINITE);
    00048   } while (_waveout != NULL);
    00049
    00050   return 0;
    00051 }
    00052
    00053 const char *SoundDriver_Win32::Start(const char * const *parm)
    00054 {
    00055   WAVEFORMATEX wfex;
    00056   wfex.wFormatTag = WAVE_FORMAT_PCM;
    00057   wfex.nChannels = 2;
    00058   wfex.wBitsPerSample = 16;
    00059   wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
    00060   wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
    00061   wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
    00062
    00063   _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
    00064
    00065   if (NULL == (_event = CreateEvent(NULL, FALSE, FALSE, NULL))) return "Failed to create event";
    00066
    00067   if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_event, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) return "waveOutOpen failed";
    00068
    00069   MxInitialize(wfex.nSamplesPerSec);
    00070
    00071   PrepareHeader(&_wave_hdr[0]);
    00072   PrepareHeader(&_wave_hdr[1]);
    00073
    00074   if (NULL == (_thread = CreateThread(NULL, 8192, SoundThread, 0, 0, &_threadId))) return "Failed to create thread";
    00075
    00076   return NULL;
    00077 }
    00078
    00079 void SoundDriver_Win32::Stop()
    00080 {
    00081   HWAVEOUT waveout = _waveout;
    00082
    00083   /* Stop the sound thread. */
    00084   _waveout = NULL;
    00085   SetEvent(_event);
    00086   WaitForSingleObject(_thread, INFINITE);
    00087
    00088   /* Close the sound device. */
    00089   waveOutReset(waveout);
    00090   waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
    00091   waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
    00092   waveOutClose(waveout);
    00093
    00094   CloseHandle(_thread);
    00095   CloseHandle(_event);
    00096 }