# win32_s.cpp

Go to the documentation of this file.

    00001 /* $Id: win32_s.cpp 18809 2010-01-15 16:41:15Z 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
    00028 static void PrepareHeader(WAVEHDR *hdr)
    00029 {
    00030   hdr->dwBufferLength = _bufsize * 4;
    00031   hdr->dwFlags = 0;
    00032   hdr->lpData = MallocT<char>(_bufsize * 4);
    00033   if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00034     usererror("waveOutPrepareHeader failed");
    00035 }
    00036
    00037 static void FillHeaders()
    00038 {
    00039   WAVEHDR *hdr;
    00040
    00041   for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
    00042     if (!(hdr->dwFlags & WHDR_INQUEUE)) {
    00043       MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
    00044       if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00045         usererror("waveOutWrite failed");
    00046     }
    00047   }
    00048 }
    00049
    00050 static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
    00051   DWORD dwParam1, DWORD dwParam2)
    00052 {
    00053   switch (uMsg) {
    00054     case WOM_DONE:
    00055       if (_waveout != NULL) FillHeaders();
    00056       break;
    00057     default: break;
    00058   }
    00059 }
    00060
    00061 const char *SoundDriver_Win32::Start(const char * const *parm)
    00062 {
    00063   WAVEFORMATEX wfex;
    00064   wfex.wFormatTag = WAVE_FORMAT_PCM;
    00065   wfex.nChannels = 2;
    00066   wfex.wBitsPerSample = 16;
    00067   wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
    00068   wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
    00069   wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
    00070
    00071   _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
    00072
    00073   if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
    00074     return "waveOutOpen failed";
    00075
    00076   MxInitialize(wfex.nSamplesPerSec);
    00077
    00078   PrepareHeader(&_wave_hdr[0]);
    00079   PrepareHeader(&_wave_hdr[1]);
    00080   FillHeaders();
    00081   return NULL;
    00082 }
    00083
    00084 void SoundDriver_Win32::Stop()
    00085 {
    00086   HWAVEOUT waveout = _waveout;
    00087
    00088   _waveout = NULL;
    00089   waveOutReset(waveout);
    00090   waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
    00091   waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
    00092   waveOutClose(waveout);
    00093 }