# win32_s.cpp

    00001 /* $Id: win32_s.cpp 12869 2008-04-24 12:36:18Z rubidium $ */
    00002
    00003 #include "../stdafx.h"
    00004 #include "../openttd.h"
    00005 #include "../driver.h"
    00006 #include "../mixer.h"
    00007 #include "../core/alloc_func.hpp"
    00008 #include "win32_s.h"
    00009 #include <windows.h>
    00010 #include <mmsystem.h>
    00011
    00012 static FSoundDriver_Win32 iFSoundDriver_Win32;
    00013
    00014 static HWAVEOUT _waveout;
    00015 static WAVEHDR _wave_hdr[2];
    00016 static int _bufsize;
    00017
    00018 static void PrepareHeader(WAVEHDR *hdr)
    00019 {
    00020   hdr->dwBufferLength = _bufsize * 4;
    00021   hdr->dwFlags = 0;
    00022   hdr->lpData = MallocT<char>(_bufsize * 4);
    00023   if (hdr->lpData == NULL ||
    00024       waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00025     error("waveOutPrepareHeader failed");
    00026 }
    00027
    00028 static void FillHeaders()
    00029 {
    00030   WAVEHDR *hdr;
    00031
    00032   for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
    00033     if (!(hdr->dwFlags & WHDR_INQUEUE)) {
    00034       MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
    00035       if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00036         error("waveOutWrite failed");
    00037     }
    00038   }
    00039 }
    00040
    00041 static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
    00042   DWORD dwParam1, DWORD dwParam2)
    00043 {
    00044   switch (uMsg) {
    00045     case WOM_DONE:
    00046       if (_waveout != NULL) FillHeaders();
    00047       break;
    00048     default: break;
    00049   }
    00050 }
    00051
    00052 const char *SoundDriver_Win32::Start(const char* const* parm)
    00053 {
    00054   WAVEFORMATEX wfex;
    00055   wfex.wFormatTag = WAVE_FORMAT_PCM;
    00056   wfex.nChannels = 2;
    00057   wfex.wBitsPerSample = 16;
    00058   wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 11025);
    00059   wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
    00060   wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
    00061
    00062   _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 2048 : 1024);
    00063
    00064   if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
    00065     return "waveOutOpen failed";
    00066
    00067   PrepareHeader(&_wave_hdr[0]);
    00068   PrepareHeader(&_wave_hdr[1]);
    00069   FillHeaders();
    00070   return NULL;
    00071 }
    00072
    00073 void SoundDriver_Win32::Stop()
    00074 {
    00075   HWAVEOUT waveout = _waveout;
    00076
    00077   _waveout = NULL;
    00078   waveOutReset(waveout);
    00079   waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
    00080   waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
    00081   waveOutClose(waveout);
    00082 }