win32_s.cpp

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

Generated on Wed Dec 30 20:40:06 2009 for OpenTTD by  doxygen 1.5.6