# win32_s.cpp

Go to the documentation of this file.

    00001 /* $Id: win32_s.cpp 16804 2009-07-13 10:16:50Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../openttd.h"
    00007 #include "../driver.h"
    00008 #include "../mixer.h"
    00009 #include "../core/alloc_func.hpp"
    00010 #include "win32_s.h"
    00011 #include <windows.h>
    00012 #include <mmsystem.h>
    00013
    00014 static FSoundDriver_Win32 iFSoundDriver_Win32;
    00015
    00016 static HWAVEOUT _waveout;
    00017 static WAVEHDR _wave_hdr[2];
    00018 static int _bufsize;
    00019
    00020 static void PrepareHeader(WAVEHDR *hdr)
    00021 {
    00022   hdr->dwBufferLength = _bufsize * 4;
    00023   hdr->dwFlags = 0;
    00024   hdr->lpData = MallocT<char>(_bufsize * 4);
    00025   if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00026     usererror("waveOutPrepareHeader failed");
    00027 }
    00028
    00029 static void FillHeaders()
    00030 {
    00031   WAVEHDR *hdr;
    00032
    00033   for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
    00034     if (!(hdr->dwFlags & WHDR_INQUEUE)) {
    00035       MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
    00036       if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00037         usererror("waveOutWrite failed");
    00038     }
    00039   }
    00040 }
    00041
    00042 static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
    00043   DWORD dwParam1, DWORD dwParam2)
    00044 {
    00045   switch (uMsg) {
    00046     case WOM_DONE:
    00047       if (_waveout != NULL) FillHeaders();
    00048       break;
    00049     default: break;
    00050   }
    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", 11025);
    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) ? 2048 : 1024);
    00064
    00065   if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
    00066     return "waveOutOpen failed";
    00067
    00068   MxInitialize(wfex.nSamplesPerSec);
    00069
    00070   PrepareHeader(&_wave_hdr[0]);
    00071   PrepareHeader(&_wave_hdr[1]);
    00072   FillHeaders();
    00073   return NULL;
    00074 }
    00075
    00076 void SoundDriver_Win32::Stop()
    00077 {
    00078   HWAVEOUT waveout = _waveout;
    00079
    00080   _waveout = NULL;
    00081   waveOutReset(waveout);
    00082   waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
    00083   waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
    00084   waveOutClose(waveout);
    00085 }